X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=build-projects%2Fandroid%2Fapp%2Fsrc%2Fmain%2Fjava%2Forg%2Flibsdl%2Fapp%2FSDLActivity.java;fp=build-projects%2Fandroid%2Fapp%2Fsrc%2Fmain%2Fjava%2Forg%2Flibsdl%2Fapp%2FSDLActivity.java;h=9144949b619417e235d250b212d23fc3dd3b85c4;hp=a61dd6db54dbd578f7a4304034cd9c12d8f4e774;hb=42ff780b1f0470fc5b2cb5ee02e9d8e1150eaf67;hpb=be985cbe05a1e28b8db94564380bbfb9b908656c diff --git a/build-projects/android/app/src/main/java/org/libsdl/app/SDLActivity.java b/build-projects/android/app/src/main/java/org/libsdl/app/SDLActivity.java index a61dd6db..9144949b 100644 --- a/build-projects/android/app/src/main/java/org/libsdl/app/SDLActivity.java +++ b/build-projects/android/app/src/main/java/org/libsdl/app/SDLActivity.java @@ -1,35 +1,62 @@ package org.libsdl.app; -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.Hashtable; -import java.lang.reflect.Method; -import java.lang.Math; - -import android.app.*; -import android.content.*; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.UiModeManager; +import android.content.ClipboardManager; +import android.content.ClipData; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; import android.content.res.Configuration; +import android.graphics.Bitmap; +import android.graphics.Color; +import android.graphics.PixelFormat; +import android.graphics.PorterDuff; +import android.graphics.drawable.Drawable; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.text.InputType; -import android.view.*; +import android.util.DisplayMetrics; +import android.util.Log; +import android.util.SparseArray; +import android.view.Display; +import android.view.Gravity; +import android.view.InputDevice; +import android.view.KeyEvent; +import android.view.MotionEvent; +import android.view.PointerIcon; +import android.view.Surface; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import android.view.inputmethod.BaseInputConnection; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; -import android.widget.RelativeLayout; import android.widget.Button; import android.widget.LinearLayout; +import android.widget.RelativeLayout; import android.widget.TextView; -import android.os.*; -import android.util.DisplayMetrics; -import android.util.Log; -import android.util.SparseArray; -import android.graphics.*; -import android.graphics.drawable.Drawable; -import android.hardware.*; -import android.content.pm.ActivityInfo; -import android.content.pm.PackageManager; -import android.content.pm.ApplicationInfo; +import android.widget.Toast; + +import java.util.Hashtable; +import java.util.Locale; + /** SDL Activity @@ -41,7 +68,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh public static final boolean mHasMultiWindow = (Build.VERSION.SDK_INT >= 24); // Cursor types - private static final int SDL_SYSTEM_CURSOR_NONE = -1; + // private static final int SDL_SYSTEM_CURSOR_NONE = -1; private static final int SDL_SYSTEM_CURSOR_ARROW = 0; private static final int SDL_SYSTEM_CURSOR_IBEAM = 1; private static final int SDL_SYSTEM_CURSOR_WAIT = 2; @@ -62,6 +89,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh protected static final int SDL_ORIENTATION_PORTRAIT_FLIPPED = 4; protected static int mCurrentOrientation; + protected static Locale mCurrentLocale; // Handle the state of the native layer public enum NativeState { @@ -72,7 +100,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh public static NativeState mCurrentNativeState; /** If shared libraries (e.g. SDL or the native application) could not be loaded. */ - public static boolean mBrokenLibraries; + public static boolean mBrokenLibraries = true; // Main components protected static SDLActivity mSingleton; @@ -93,8 +121,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh if (mMotionListener == null) { if (Build.VERSION.SDK_INT >= 26) { mMotionListener = new SDLGenericMotionListener_API26(); - } else - if (Build.VERSION.SDK_INT >= 24) { + } else if (Build.VERSION.SDK_INT >= 24) { mMotionListener = new SDLGenericMotionListener_API24(); } else { mMotionListener = new SDLGenericMotionListener_API12(); @@ -137,7 +164,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh */ protected String[] getLibraries() { return new String[] { - "hidapi", "SDL2", "SDL2_image", "SDL2_mixer", @@ -175,7 +201,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh mCursors = new Hashtable(); mLastCursorID = 0; mSDLThread = null; - mBrokenLibraries = false; mIsResumedCalled = false; mHasFocus = true; mNextNativeState = NativeState.INIT; @@ -200,6 +225,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh String errorMsgBrokenLib = ""; try { loadLibraries(); + mBrokenLibraries = false; /* success */ } catch(UnsatisfiedLinkError e) { System.err.println(e.getMessage()); mBrokenLibraries = true; @@ -243,7 +269,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh mSingleton = this; SDL.setContext(this); - mClipboardHandler = new SDLClipboardHandler_API11(); + mClipboardHandler = new SDLClipboardHandler(); mHIDDeviceManager = HIDDeviceManager.acquire(this); @@ -258,6 +284,15 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh // Only record current orientation SDLActivity.onNativeOrientationChanged(mCurrentOrientation); + try { + if (Build.VERSION.SDK_INT < 24) { + mCurrentLocale = getContext().getResources().getConfiguration().locale; + } else { + mCurrentLocale = getContext().getResources().getConfiguration().getLocales().get(0); + } + } catch(Exception ignored) { + } + setContentView(mLayout); setWindowStyle(false); @@ -343,11 +378,14 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh } public static int getCurrentOrientation() { - final Context context = SDLActivity.getContext(); - final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); - int result = SDL_ORIENTATION_UNKNOWN; + Activity activity = (Activity)getContext(); + if (activity == null) { + return result; + } + Display display = activity.getWindowManager().getDefaultDisplay(); + switch (display.getRotation()) { case Surface.ROTATION_0: result = SDL_ORIENTATION_PORTRAIT; @@ -407,6 +445,21 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh SDLActivity.nativeLowMemory(); } + @Override + public void onConfigurationChanged(Configuration newConfig) { + Log.v(TAG, "onConfigurationChanged()"); + super.onConfigurationChanged(newConfig); + + if (SDLActivity.mBrokenLibraries) { + return; + } + + if (mCurrentLocale == null || !mCurrentLocale.equals(newConfig.locale)) { + mCurrentLocale = newConfig.locale; + SDLActivity.onNativeLocaleChanged(); + } + } + @Override protected void onDestroy() { Log.v(TAG, "onDestroy()"); @@ -446,8 +499,8 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh // If we do, the normal hardware back button will no longer work and people have to use home, // but the mouse right click will work. // - String trapBack = SDLActivity.nativeGetHint("SDL_ANDROID_TRAP_BACK_BUTTON"); - if ((trapBack != null) && trapBack.equals("1")) { + boolean trapBack = SDLActivity.nativeGetHintBoolean("SDL_ANDROID_TRAP_BACK_BUTTON", false); + if (trapBack) { // Exit and let the mouse handler handle this button (if appropriate) return; } @@ -540,11 +593,10 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh mSDLThread.start(); // No nativeResume(), don't signal Android_ResumeSem - mSurface.handleResume(); } else { nativeResume(); - mSurface.handleResume(); } + mSurface.handleResume(); mCurrentNativeState = mNextNativeState; } @@ -555,7 +607,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh static final int COMMAND_CHANGE_TITLE = 1; static final int COMMAND_CHANGE_WINDOW_STYLE = 2; static final int COMMAND_TEXTEDIT_HIDE = 3; - static final int COMMAND_CHANGE_SURFACEVIEW_FORMAT = 4; static final int COMMAND_SET_KEEP_SCREEN_ON = 5; protected static final int COMMAND_USER = 0x8000; @@ -596,34 +647,32 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh } break; case COMMAND_CHANGE_WINDOW_STYLE: - if (Build.VERSION.SDK_INT < 19) { - // This version of Android doesn't support the immersive fullscreen mode - break; - } - if (context instanceof Activity) { - Window window = ((Activity) context).getWindow(); - if (window != null) { - if ((msg.obj instanceof Integer) && (((Integer) msg.obj).intValue() != 0)) { - int flags = View.SYSTEM_UI_FLAG_FULLSCREEN | + if (Build.VERSION.SDK_INT >= 19) { + if (context instanceof Activity) { + Window window = ((Activity) context).getWindow(); + if (window != null) { + if ((msg.obj instanceof Integer) && ((Integer) msg.obj != 0)) { + int flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.INVISIBLE; - window.getDecorView().setSystemUiVisibility(flags); - window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); - window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); - SDLActivity.mFullscreenModeActive = true; - } else { - int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_VISIBLE; - window.getDecorView().setSystemUiVisibility(flags); - window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); - window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); - SDLActivity.mFullscreenModeActive = false; + window.getDecorView().setSystemUiVisibility(flags); + window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); + window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); + SDLActivity.mFullscreenModeActive = true; + } else { + int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_VISIBLE; + window.getDecorView().setSystemUiVisibility(flags); + window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); + window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); + SDLActivity.mFullscreenModeActive = false; + } } + } else { + Log.e(TAG, "error handling message, getContext() returned no Activity"); } - } else { - Log.e(TAG, "error handling message, getContext() returned no Activity"); } break; case COMMAND_TEXTEDIT_HIDE: @@ -646,7 +695,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh if (context instanceof Activity) { Window window = ((Activity) context).getWindow(); if (window != null) { - if ((msg.obj instanceof Integer) && (((Integer) msg.obj).intValue() != 0)) { + if ((msg.obj instanceof Integer) && ((Integer) msg.obj != 0)) { window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } else { window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); @@ -655,32 +704,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh } break; } - case COMMAND_CHANGE_SURFACEVIEW_FORMAT: - { - int format = (Integer) msg.obj; - int pf; - - if (SDLActivity.mSurface == null) { - return; - } - - SurfaceHolder holder = SDLActivity.mSurface.getHolder(); - if (holder == null) { - return; - } - - if (format == 1) { - pf = PixelFormat.RGBA_8888; - } else if (format == 2) { - pf = PixelFormat.RGBX_8888; - } else { - pf = PixelFormat.RGB_565; - } - - holder.setFormat(pf); - - break; - } default: if ((context instanceof SDLActivity) && !((SDLActivity) context).onUnhandledMessage(msg.arg1, msg.obj)) { Log.e(TAG, "error handling message, command is " + msg.arg1); @@ -699,53 +722,53 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh msg.obj = data; boolean result = commandHandler.sendMessage(msg); - if ((Build.VERSION.SDK_INT >= 19) && (command == COMMAND_CHANGE_WINDOW_STYLE)) { - // Ensure we don't return until the resize has actually happened, - // or 500ms have passed. - - boolean bShouldWait = false; - - if (data instanceof Integer) { - // Let's figure out if we're already laid out fullscreen or not. - Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); - android.util.DisplayMetrics realMetrics = new android.util.DisplayMetrics(); - display.getRealMetrics( realMetrics ); - - boolean bFullscreenLayout = ((realMetrics.widthPixels == mSurface.getWidth()) && - (realMetrics.heightPixels == mSurface.getHeight())); - - if (((Integer)data).intValue() == 1) { - // If we aren't laid out fullscreen or actively in fullscreen mode already, we're going - // to change size and should wait for surfaceChanged() before we return, so the size - // is right back in native code. If we're already laid out fullscreen, though, we're - // not going to change size even if we change decor modes, so we shouldn't wait for - // surfaceChanged() -- which may not even happen -- and should return immediately. - bShouldWait = !bFullscreenLayout; - } - else { - // If we're laid out fullscreen (even if the status bar and nav bar are present), - // or are actively in fullscreen, we're going to change size and should wait for - // surfaceChanged before we return, so the size is right back in native code. - bShouldWait = bFullscreenLayout; + if (Build.VERSION.SDK_INT >= 19) { + if (command == COMMAND_CHANGE_WINDOW_STYLE) { + // Ensure we don't return until the resize has actually happened, + // or 500ms have passed. + + boolean bShouldWait = false; + + if (data instanceof Integer) { + // Let's figure out if we're already laid out fullscreen or not. + Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + DisplayMetrics realMetrics = new DisplayMetrics(); + display.getRealMetrics(realMetrics); + + boolean bFullscreenLayout = ((realMetrics.widthPixels == mSurface.getWidth()) && + (realMetrics.heightPixels == mSurface.getHeight())); + + if ((Integer) data == 1) { + // If we aren't laid out fullscreen or actively in fullscreen mode already, we're going + // to change size and should wait for surfaceChanged() before we return, so the size + // is right back in native code. If we're already laid out fullscreen, though, we're + // not going to change size even if we change decor modes, so we shouldn't wait for + // surfaceChanged() -- which may not even happen -- and should return immediately. + bShouldWait = !bFullscreenLayout; + } else { + // If we're laid out fullscreen (even if the status bar and nav bar are present), + // or are actively in fullscreen, we're going to change size and should wait for + // surfaceChanged before we return, so the size is right back in native code. + bShouldWait = bFullscreenLayout; + } } - } - if (bShouldWait && (SDLActivity.getContext() != null)) { - // We'll wait for the surfaceChanged() method, which will notify us - // when called. That way, we know our current size is really the - // size we need, instead of grabbing a size that's still got - // the navigation and/or status bars before they're hidden. - // - // We'll wait for up to half a second, because some devices - // take a surprisingly long time for the surface resize, but - // then we'll just give up and return. - // - synchronized(SDLActivity.getContext()) { - try { - SDLActivity.getContext().wait(500); - } - catch (InterruptedException ie) { - ie.printStackTrace(); + if (bShouldWait && (SDLActivity.getContext() != null)) { + // We'll wait for the surfaceChanged() method, which will notify us + // when called. That way, we know our current size is really the + // size we need, instead of grabbing a size that's still got + // the navigation and/or status bars before they're hidden. + // + // We'll wait for up to half a second, because some devices + // take a surprisingly long time for the surface resize, but + // then we'll just give up and return. + // + synchronized (SDLActivity.getContext()) { + try { + SDLActivity.getContext().wait(500); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } } } } @@ -764,7 +787,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh public static native void nativeResume(); public static native void nativeFocusChanged(boolean hasFocus); public static native void onNativeDropFile(String filename); - public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, int format, float rate); + public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float rate); public static native void onNativeResize(); public static native void onNativeKeyDown(int keycode); public static native void onNativeKeyUp(int keycode); @@ -780,10 +803,12 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh public static native void onNativeSurfaceChanged(); public static native void onNativeSurfaceDestroyed(); public static native String nativeGetHint(String name); + public static native boolean nativeGetHintBoolean(String name, boolean default_value); public static native void nativeSetenv(String name, String value); public static native void onNativeOrientationChanged(int orientation); public static native void nativeAddTouch(int touchId, String name); public static native void nativePermissionResult(int requestCode, boolean result); + public static native void onNativeLocaleChanged(); /** * This method is called by SDL using JNI. @@ -838,9 +863,9 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh orientation_portrait = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } - boolean is_landscape_allowed = (orientation_landscape == -1 ? false : true); - boolean is_portrait_allowed = (orientation_portrait == -1 ? false : true); - int req = -1; /* Requested orientation */ + boolean is_landscape_allowed = (orientation_landscape != -1); + boolean is_portrait_allowed = (orientation_portrait != -1); + int req; /* Requested orientation */ /* No valid hint, nothing is explicitly allowed */ if (!is_portrait_allowed && !is_landscape_allowed) { @@ -872,7 +897,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh } } - Log.v("SDL", "setOrientation() requestedOrientation=" + req + " width=" + w +" height="+ h +" resizable=" + resizable + " hint=" + hint); + Log.v(TAG, "setOrientation() requestedOrientation=" + req + " width=" + w +" height="+ h +" resizable=" + resizable + " hint=" + hint); mSingleton.setRequestedOrientation(req); } @@ -938,11 +963,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh */ public static boolean supportsRelativeMouse() { - // ChromeOS doesn't provide relative mouse motion via the Android 7 APIs - if (isChromebook()) { - return false; - } - // DeX mode in Samsung Experience 9.0 and earlier doesn't support relative mice properly under // Android 7 APIs, and simply returns no data under Android 8 APIs. // @@ -976,7 +996,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh if (mSingleton == null) { return false; } - return mSingleton.sendCommand(command, Integer.valueOf(param)); + return mSingleton.sendCommand(command, param); } /** @@ -1000,30 +1020,30 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh if (Build.MANUFACTURER.equals("Amlogic") && Build.MODEL.equals("X96-W")) { return true; } - if (Build.MANUFACTURER.equals("Amlogic") && Build.MODEL.startsWith("TV")) { - return true; - } - return false; + return Build.MANUFACTURER.equals("Amlogic") && Build.MODEL.startsWith("TV"); } - /** - * This method is called by SDL using JNI. - */ - public static boolean isTablet() { + public static double getDiagonal() + { DisplayMetrics metrics = new DisplayMetrics(); Activity activity = (Activity)getContext(); if (activity == null) { - return false; + return 0.0; } activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); double dWidthInches = metrics.widthPixels / (double)metrics.xdpi; double dHeightInches = metrics.heightPixels / (double)metrics.ydpi; - double dDiagonal = Math.sqrt((dWidthInches * dWidthInches) + (dHeightInches * dHeightInches)); + return Math.sqrt((dWidthInches * dWidthInches) + (dHeightInches * dHeightInches)); + } + /** + * This method is called by SDL using JNI. + */ + public static boolean isTablet() { // If our diagonal size is seven inches or greater, we consider ourselves a tablet. - return (dDiagonal >= 7.0); + return (getDiagonal() >= 7.0); } /** @@ -1045,7 +1065,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh } try { final Configuration config = getContext().getResources().getConfiguration(); - final Class configClass = config.getClass(); + final Class configClass = config.getClass(); return configClass.getField("SEM_DESKTOP_MODE_ENABLED").getInt(configClass) == configClass.getField("semDesktopModeEnabled").getInt(config); } catch(Exception ignored) { @@ -1065,6 +1085,10 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh */ public static boolean getManifestEnvironmentVariables() { try { + if (getContext() == null) { + return false; + } + ApplicationInfo applicationInfo = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA); Bundle bundle = applicationInfo.metaData; if (bundle == null) { @@ -1082,7 +1106,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh /* environment variables set! */ return true; } catch (Exception e) { - Log.v("SDL", "exception " + e.toString()); + Log.v(TAG, "exception " + e.toString()); } return false; } @@ -1090,7 +1114,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh // This method is called by SDLControllerManager's API 26 Generic Motion Handler. public static View getContentView() { - return mSingleton.mLayout; + return mLayout; } static class ShowTextInputTask implements Runnable { @@ -1170,14 +1194,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh return SDLActivity.mSurface.getNativeSurface(); } - /** - * This method is called by SDL using JNI. - */ - public static void setSurfaceViewFormat(int format) { - mSingleton.sendCommand(COMMAND_CHANGE_SURFACEVIEW_FORMAT, format); - return; - } - // Input /** @@ -1186,92 +1202,19 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh public static void initTouch() { int[] ids = InputDevice.getDeviceIds(); - for (int i = 0; i < ids.length; ++i) { - InputDevice device = InputDevice.getDevice(ids[i]); + for (int id : ids) { + InputDevice device = InputDevice.getDevice(id); if (device != null && (device.getSources() & InputDevice.SOURCE_TOUCHSCREEN) != 0) { nativeAddTouch(device.getId(), device.getName()); } } } - // APK expansion files support - - /** com.android.vending.expansion.zipfile.ZipResourceFile object or null. */ - private static Object expansionFile; - - /** com.android.vending.expansion.zipfile.ZipResourceFile's getInputStream() or null. */ - private static Method expansionFileMethod; - - /** - * This method is called by SDL using JNI. - * @return an InputStream on success or null if no expansion file was used. - * @throws IOException on errors. Message is set for the SDL error message. - */ - public static InputStream openAPKExpansionInputStream(String fileName) throws IOException { - // Get a ZipResourceFile representing a merger of both the main and patch files - if (expansionFile == null) { - String mainHint = nativeGetHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION"); - if (mainHint == null) { - return null; // no expansion use if no main version was set - } - String patchHint = nativeGetHint("SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION"); - if (patchHint == null) { - return null; // no expansion use if no patch version was set - } - - Integer mainVersion; - Integer patchVersion; - try { - mainVersion = Integer.valueOf(mainHint); - patchVersion = Integer.valueOf(patchHint); - } catch (NumberFormatException ex) { - ex.printStackTrace(); - throw new IOException("No valid file versions set for APK expansion files", ex); - } - - try { - // To avoid direct dependency on Google APK expansion library that is - // not a part of Android SDK we access it using reflection - expansionFile = Class.forName("com.android.vending.expansion.zipfile.APKExpansionSupport") - .getMethod("getAPKExpansionZipFile", Context.class, int.class, int.class) - .invoke(null, SDL.getContext(), mainVersion, patchVersion); - - expansionFileMethod = expansionFile.getClass() - .getMethod("getInputStream", String.class); - } catch (Exception ex) { - ex.printStackTrace(); - expansionFile = null; - expansionFileMethod = null; - throw new IOException("Could not access APK expansion support library", ex); - } - } - - // Get an input stream for a known file inside the expansion file ZIPs - InputStream fileStream; - try { - fileStream = (InputStream)expansionFileMethod.invoke(expansionFile, fileName); - } catch (Exception ex) { - // calling "getInputStream" failed - ex.printStackTrace(); - throw new IOException("Could not open stream from APK expansion file", ex); - } - - if (fileStream == null) { - // calling "getInputStream" was successful but null was returned - throw new IOException("Could not find path in APK expansion file"); - } - - return fileStream; - } - // Messagebox /** Result of current messagebox. Also used for blocking the calling thread. */ protected final int[] messageboxSelection = new int[1]; - /** Id of current dialog. */ - protected int dialogs = 0; - /** * This method is called by SDL using JNI. * Shows the messagebox from UI thread and block calling thread. @@ -1315,7 +1258,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh runOnUiThread(new Runnable() { @Override public void run() { - showDialog(dialogs++, args); + messageboxCreateAndShow(args); } }); @@ -1335,8 +1278,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh return messageboxSelection[0]; } - @Override - protected Dialog onCreateDialog(int ignore, Bundle args) { + protected void messageboxCreateAndShow(Bundle args) { // TODO set values from "flags" to messagebox dialog @@ -1365,7 +1307,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh // create dialog with title and a listener to wake up calling thread - final Dialog dialog = new Dialog(this); + final AlertDialog dialog = new AlertDialog.Builder(this).create(); dialog.setTitle(args.getString("title")); dialog.setCancelable(false); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @@ -1451,7 +1393,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh // add content to dialog and return - dialog.setContentView(content); + dialog.setView(content); dialog.setOnKeyListener(new Dialog.OnKeyListener() { @Override public boolean onKey(DialogInterface d, int keyCode, KeyEvent event) { @@ -1466,20 +1408,22 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh } }); - return dialog; + dialog.show(); } private final Runnable rehideSystemUi = new Runnable() { @Override public void run() { - int flags = View.SYSTEM_UI_FLAG_FULLSCREEN | + if (Build.VERSION.SDK_INT >= 19) { + int flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.INVISIBLE; - SDLActivity.this.getWindow().getDecorView().setSystemUiVisibility(flags); + SDLActivity.this.getWindow().getDecorView().setSystemUiVisibility(flags); + } } }; @@ -1535,6 +1479,19 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh return mLastCursorID; } + /** + * This method is called by SDL using JNI. + */ + public static void destroyCustomCursor(int cursorID) { + if (Build.VERSION.SDK_INT >= 24) { + try { + mCursors.remove(cursorID); + } catch (Exception e) { + } + } + return; + } + /** * This method is called by SDL using JNI. */ @@ -1624,11 +1581,78 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { - if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - nativePermissionResult(requestCode, true); - } else { - nativePermissionResult(requestCode, false); + boolean result = (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED); + nativePermissionResult(requestCode, result); + } + + /** + * This method is called by SDL using JNI. + */ + public static int openURL(String url) + { + try { + Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(url)); + + int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK; + if (Build.VERSION.SDK_INT >= 21) { + flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT; + } else { + flags |= Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET; + } + i.addFlags(flags); + + mSingleton.startActivity(i); + } catch (Exception ex) { + return -1; + } + return 0; + } + + /** + * This method is called by SDL using JNI. + */ + public static int showToast(String message, int duration, int gravity, int xOffset, int yOffset) + { + if(null == mSingleton) { + return - 1; + } + + try + { + class OneShotTask implements Runnable { + String mMessage; + int mDuration; + int mGravity; + int mXOffset; + int mYOffset; + + OneShotTask(String message, int duration, int gravity, int xOffset, int yOffset) { + mMessage = message; + mDuration = duration; + mGravity = gravity; + mXOffset = xOffset; + mYOffset = yOffset; + } + + public void run() { + try + { + Toast toast = Toast.makeText(mSingleton, mMessage, mDuration); + if (mGravity >= 0) { + toast.setGravity(mGravity, mXOffset, mYOffset); + } + toast.show(); + } catch(Exception ex) { + Log.e(TAG, ex.getMessage()); + } + } + } + mSingleton.runOnUiThread(new OneShotTask(message, duration, gravity, xOffset, yOffset)); + } catch(Exception ex) { + return -1; } + return 0; } } @@ -1655,13 +1679,12 @@ class SDLMain implements Runnable { Log.v("SDL", "Finished main function"); - if (SDLActivity.mSingleton == null || SDLActivity.mSingleton.isFinishing()) { - // Activity is already being destroyed - } else { + if (SDLActivity.mSingleton != null && !SDLActivity.mSingleton.isFinishing()) { // Let's finish the Activity SDLActivity.mSDLThread = null; SDLActivity.mSingleton.finish(); - } + } // else: Activity is already being destroyed + } } @@ -1755,30 +1778,6 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, return; } - int sdlFormat = 0x15151002; // SDL_PIXELFORMAT_RGB565 by default - switch (format) { - case PixelFormat.RGBA_8888: - Log.v("SDL", "pixel format RGBA_8888"); - sdlFormat = 0x16462004; // SDL_PIXELFORMAT_RGBA8888 - break; - case PixelFormat.RGBX_8888: - Log.v("SDL", "pixel format RGBX_8888"); - sdlFormat = 0x16261804; // SDL_PIXELFORMAT_RGBX8888 - break; - case PixelFormat.RGB_565: - Log.v("SDL", "pixel format RGB_565"); - sdlFormat = 0x15151002; // SDL_PIXELFORMAT_RGB565 - break; - case PixelFormat.RGB_888: - Log.v("SDL", "pixel format RGB_888"); - // Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead? - sdlFormat = 0x16161804; // SDL_PIXELFORMAT_RGB888 - break; - default: - Log.v("SDL", "pixel format unknown " + format); - break; - } - mWidth = width; mHeight = height; int nDeviceWidth = width; @@ -1786,13 +1785,13 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, try { if (Build.VERSION.SDK_INT >= 17) { - android.util.DisplayMetrics realMetrics = new android.util.DisplayMetrics(); + DisplayMetrics realMetrics = new DisplayMetrics(); mDisplay.getRealMetrics( realMetrics ); nDeviceWidth = realMetrics.widthPixels; nDeviceHeight = realMetrics.heightPixels; } + } catch(Exception ignored) { } - catch ( java.lang.Throwable throwable ) {} synchronized(SDLActivity.getContext()) { // In case we're waiting on a size change after going fullscreen, send a notification. @@ -1801,7 +1800,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, Log.v("SDL", "Window size: " + width + "x" + height); Log.v("SDL", "Device size: " + nDeviceWidth + "x" + nDeviceHeight); - SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, sdlFormat, mDisplay.getRefreshRate()); + SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, mDisplay.getRefreshRate()); SDLActivity.onNativeResize(); // Prevent a screen distortion glitch, @@ -1809,12 +1808,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, boolean skip = false; int requestedOrientation = SDLActivity.mSingleton.getRequestedOrientation(); - if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) - { - // Accept any - } - else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) - { + if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) { if (mWidth > mHeight) { skip = true; } @@ -1868,6 +1862,19 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, int deviceId = event.getDeviceId(); int source = event.getSource(); + if (source == InputDevice.SOURCE_UNKNOWN) { + InputDevice device = InputDevice.getDevice(deviceId); + if (device != null) { + source = device.getSources(); + } + } + +// if (event.getAction() == KeyEvent.ACTION_DOWN) { +// Log.v("SDL", "key down: " + keyCode + ", deviceId = " + deviceId + ", source = " + source); +// } else if (event.getAction() == KeyEvent.ACTION_UP) { +// Log.v("SDL", "key up: " + keyCode + ", deviceId = " + deviceId + ", source = " + source); +// } + // Dispatch the different events depending on where they come from // Some SOURCE_JOYSTICK, SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD // So, we try to process them as JOYSTICK/DPAD/GAMEPAD events first, if that fails we try them as KEYBOARD @@ -1888,24 +1895,14 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, } } - if (source == InputDevice.SOURCE_UNKNOWN) { - InputDevice device = InputDevice.getDevice(deviceId); - if (device != null) { - source = device.getSources(); - } - } - if ((source & InputDevice.SOURCE_KEYBOARD) != 0) { if (event.getAction() == KeyEvent.ACTION_DOWN) { - //Log.v("SDL", "key down: " + keyCode); if (SDLActivity.isTextInputEvent(event)) { SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1); } SDLActivity.onNativeKeyDown(keyCode); return true; - } - else if (event.getAction() == KeyEvent.ACTION_UP) { - //Log.v("SDL", "key up: " + keyCode); + } else if (event.getAction() == KeyEvent.ACTION_UP) { SDLActivity.onNativeKeyUp(keyCode); return true; } @@ -1932,22 +1929,34 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, @Override public boolean onTouch(View v, MotionEvent event) { /* Ref: http://developer.android.com/training/gestures/multi.html */ - final int touchDevId = event.getDeviceId(); + int touchDevId = event.getDeviceId(); final int pointerCount = event.getPointerCount(); int action = event.getActionMasked(); int pointerFingerId; - int mouseButton; int i = -1; float x,y,p; + /* + * Prevent id to be -1, since it's used in SDL internal for synthetic events + * Appears when using Android emulator, eg: + * adb shell input mouse tap 100 100 + * adb shell input touchscreen tap 100 100 + */ + if (touchDevId < 0) { + touchDevId -= 1; + } + // 12290 = Samsung DeX mode desktop mouse // 12290 = 0x3002 = 0x2002 | 0x1002 = SOURCE_MOUSE | SOURCE_TOUCHSCREEN // 0x2 = SOURCE_CLASS_POINTER if (event.getSource() == InputDevice.SOURCE_MOUSE || event.getSource() == (InputDevice.SOURCE_MOUSE | InputDevice.SOURCE_TOUCHSCREEN)) { + int mouseButton = 1; try { - mouseButton = (Integer) event.getClass().getMethod("getButtonState").invoke(event); - } catch(Exception e) { - mouseButton = 1; // oh well. + Object object = event.getClass().getMethod("getButtonState").invoke(event); + if (object != null) { + mouseButton = (Integer) object; + } + } catch(Exception ignored) { } // We need to check if we're in relative mouse mode and get the axis offset rather than the x/y values @@ -1978,6 +1987,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, case MotionEvent.ACTION_DOWN: // Primary pointer up/down, the index is always zero i = 0; + /* fallthrough */ case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_POINTER_DOWN: // Non primary pointer up/down @@ -2044,7 +2054,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, // Since we may have an orientation set, we won't receive onConfigurationChanged events. // We thus should check here. - int newOrientation = SDLActivity.SDL_ORIENTATION_UNKNOWN; + int newOrientation; float x, y; switch (mDisplay.getRotation()) { @@ -2063,6 +2073,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, y = -event.values[1]; newOrientation = SDLActivity.SDL_ORIENTATION_PORTRAIT_FLIPPED; break; + case Surface.ROTATION_0: default: x = event.values[0]; y = event.values[1]; @@ -2109,8 +2120,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, // Change our action value to what SDL's code expects. if (action == MotionEvent.ACTION_BUTTON_PRESS) { action = MotionEvent.ACTION_DOWN; - } - else if (action == MotionEvent.ACTION_BUTTON_RELEASE) { + } else { /* MotionEvent.ACTION_BUTTON_RELEASE */ action = MotionEvent.ACTION_UP; } @@ -2275,45 +2285,38 @@ class SDLInputConnection extends BaseInputConnection { } } -interface SDLClipboardHandler { - - public boolean clipboardHasText(); - public String clipboardGetText(); - public void clipboardSetText(String string); - -} - - -class SDLClipboardHandler_API11 implements - SDLClipboardHandler, - android.content.ClipboardManager.OnPrimaryClipChangedListener { +class SDLClipboardHandler implements + ClipboardManager.OnPrimaryClipChangedListener { - protected android.content.ClipboardManager mClipMgr; + protected ClipboardManager mClipMgr; - SDLClipboardHandler_API11() { - mClipMgr = (android.content.ClipboardManager) SDL.getContext().getSystemService(Context.CLIPBOARD_SERVICE); + SDLClipboardHandler() { + mClipMgr = (ClipboardManager) SDL.getContext().getSystemService(Context.CLIPBOARD_SERVICE); mClipMgr.addPrimaryClipChangedListener(this); } - @Override public boolean clipboardHasText() { - return mClipMgr.hasText(); + return mClipMgr.hasPrimaryClip(); } - @Override public String clipboardGetText() { - CharSequence text; - text = mClipMgr.getText(); - if (text != null) { - return text.toString(); + ClipData clip = mClipMgr.getPrimaryClip(); + if (clip != null) { + ClipData.Item item = clip.getItemAt(0); + if (item != null) { + CharSequence text = item.getText(); + if (text != null) { + return text.toString(); + } + } } return null; } - @Override public void clipboardSetText(String string) { mClipMgr.removePrimaryClipChangedListener(this); - mClipMgr.setText(string); + ClipData clip = ClipData.newPlainText(null, string); + mClipMgr.setPrimaryClip(clip); mClipMgr.addPrimaryClipChangedListener(this); } @@ -2321,6 +2324,5 @@ class SDLClipboardHandler_API11 implements public void onPrimaryClipChanged() { SDLActivity.onNativeClipboardChanged(); } - }