From 97cf314526ad2a390e11acf5231a720659a82a67 Mon Sep 17 00:00:00 2001 From: Sylvain Becker Date: Tue, 27 Oct 2020 21:14:49 +0100 Subject: [PATCH] Android: apply code simplications found with lint / Android Studio --- .../app/src/main/java/org/libsdl/app/SDL.java | 15 +- .../main/java/org/libsdl/app/SDLActivity.java | 317 +++++++++--------- .../java/org/libsdl/app/SDLAudioManager.java | 9 +- .../org/libsdl/app/SDLControllerManager.java | 93 +++-- 4 files changed, 217 insertions(+), 217 deletions(-) diff --git a/android-project/app/src/main/java/org/libsdl/app/SDL.java b/android-project/app/src/main/java/org/libsdl/app/SDL.java index fb7f7319a..dafc0cb87 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDL.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDL.java @@ -2,7 +2,8 @@ package org.libsdl.app; import android.content.Context; -import java.lang.reflect.*; +import java.lang.Class; +import java.lang.reflect.Method; /** SDL library initialization @@ -51,16 +52,16 @@ public class SDL { // To use ReLinker, just add it as a dependency. For more information, see // https://github.com/KeepSafe/ReLinker for ReLinker's repository. // - Class relinkClass = mContext.getClassLoader().loadClass("com.getkeepsafe.relinker.ReLinker"); - Class relinkListenerClass = mContext.getClassLoader().loadClass("com.getkeepsafe.relinker.ReLinker$LoadListener"); - Class contextClass = mContext.getClassLoader().loadClass("android.content.Context"); - Class stringClass = mContext.getClassLoader().loadClass("java.lang.String"); + Class relinkClass = mContext.getClassLoader().loadClass("com.getkeepsafe.relinker.ReLinker"); + Class relinkListenerClass = mContext.getClassLoader().loadClass("com.getkeepsafe.relinker.ReLinker$LoadListener"); + Class contextClass = mContext.getClassLoader().loadClass("android.content.Context"); + Class stringClass = mContext.getClassLoader().loadClass("java.lang.String"); // Get a 'force' instance of the ReLinker, so we can ensure libraries are reinstalled if // they've changed during updates. Method forceMethod = relinkClass.getDeclaredMethod("force"); Object relinkInstance = forceMethod.invoke(null); - Class relinkInstanceClass = relinkInstance.getClass(); + Class relinkInstanceClass = relinkInstance.getClass(); // Actually load the library! Method loadMethod = relinkInstanceClass.getDeclaredMethod("loadLibrary", contextClass, stringClass, stringClass, relinkListenerClass); @@ -77,7 +78,7 @@ public class SDL { catch (final SecurityException se) { throw se; } - } + } } protected static Context mContext; diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java index 408d9c6a6..3e7af8c5b 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java @@ -1,34 +1,61 @@ package org.libsdl.app; -import java.util.Hashtable; -import java.util.Locale; -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.net.Uri; + +import java.util.Hashtable; +import java.util.Locale; + /** SDL Activity @@ -40,7 +67,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; @@ -93,8 +120,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(); @@ -172,7 +198,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh mTextEdit = null; mLayout = null; mClipboardHandler = null; - mCursors = new Hashtable(); + mCursors = new Hashtable<>(); mLastCursorID = 0; mSDLThread = null; mIsResumedCalled = false; @@ -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); @@ -564,11 +590,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; } @@ -620,34 +645,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: @@ -670,7 +693,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); @@ -723,53 +746,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. + 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; + 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 ); + 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())); + 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 (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); + 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; } - 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(); + } } } } @@ -863,9 +886,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) { @@ -1001,7 +1024,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); } /** @@ -1025,13 +1048,10 @@ 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"); } - public static double getDiagonal() + public static double getDiagonal() { DisplayMetrics metrics = new DisplayMetrics(); Activity activity = (Activity)getContext(); @@ -1043,9 +1063,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh double dWidthInches = metrics.widthPixels / (double)metrics.xdpi; double dHeightInches = metrics.heightPixels / (double)metrics.ydpi; - double dDiagonal = Math.sqrt((dWidthInches * dWidthInches) + (dHeightInches * dHeightInches)); - - return dDiagonal; + return Math.sqrt((dWidthInches * dWidthInches) + (dHeightInches * dHeightInches)); } /** @@ -1075,7 +1093,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) { @@ -1124,7 +1142,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 { @@ -1209,7 +1227,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh */ public static void setSurfaceViewFormat(int format) { mSingleton.sendCommand(COMMAND_CHANGE_SURFACEVIEW_FORMAT, format); - return; } // Input @@ -1220,8 +1237,8 @@ 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()); } @@ -1352,7 +1369,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh int[] buttonIds = args.getIntArray("buttonIds"); String[] buttonTexts = args.getStringArray("buttonTexts"); - final SparseArray