Fixed bug 4319 - Android remove reflection for PointerIcon
authorSam Lantinga <slouken@libsdl.org>
Fri, 02 Nov 2018 17:22:15 -0700
changeset 1238869af2eac84e9
parent 12387 2f02c753a5dc
child 12389 5817dbd75619
Fixed bug 4319 - Android remove reflection for PointerIcon

Sylvain

Since SDL2 min requirement is Android SDK 26, and PointerIcon is 24. We don't need reflection to access it.
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Fri Nov 02 17:18:03 2018 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Fri Nov 02 17:22:15 2018 -0700
     1.3 @@ -86,7 +86,7 @@
     1.4      protected static boolean mScreenKeyboardShown;
     1.5      protected static ViewGroup mLayout;
     1.6      protected static SDLClipboardHandler mClipboardHandler;
     1.7 -    protected static Hashtable<Integer, Object> mCursors;
     1.8 +    protected static Hashtable<Integer, PointerIcon> mCursors;
     1.9      protected static int mLastCursorID;
    1.10      protected static SDLGenericMotionListener_API12 mMotionListener;
    1.11      protected static HIDDeviceManager mHIDDeviceManager;
    1.12 @@ -176,7 +176,7 @@
    1.13          mTextEdit = null;
    1.14          mLayout = null;
    1.15          mClipboardHandler = null;
    1.16 -        mCursors = new Hashtable<Integer, Object>();
    1.17 +        mCursors = new Hashtable<Integer, PointerIcon>();
    1.18          mLastCursorID = 0;
    1.19          mSDLThread = null;
    1.20          mExitCalledFromJava = false;
    1.21 @@ -1379,13 +1379,14 @@
    1.22      public static int createCustomCursor(int[] colors, int width, int height, int hotSpotX, int hotSpotY) {
    1.23          Bitmap bitmap = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
    1.24          ++mLastCursorID;
    1.25 -        // This requires API 24, so use reflection to implement this
    1.26 -        try {
    1.27 -            Class PointerIconClass = Class.forName("android.view.PointerIcon");
    1.28 -            Class[] arg_types = new Class[] { Bitmap.class, float.class, float.class };
    1.29 -            Method create = PointerIconClass.getMethod("create", arg_types);
    1.30 -            mCursors.put(mLastCursorID, create.invoke(null, bitmap, hotSpotX, hotSpotY));
    1.31 -        } catch (Exception e) {
    1.32 +
    1.33 +        if (Build.VERSION.SDK_INT >= 24) {
    1.34 +            try {
    1.35 +                mCursors.put(mLastCursorID, PointerIcon.create(bitmap, hotSpotX, hotSpotY));
    1.36 +            } catch (Exception e) {
    1.37 +                return 0;
    1.38 +            }
    1.39 +        } else {
    1.40              return 0;
    1.41          }
    1.42          return mLastCursorID;
    1.43 @@ -1395,12 +1396,14 @@
    1.44       * This method is called by SDL using JNI.
    1.45       */
    1.46      public static boolean setCustomCursor(int cursorID) {
    1.47 -        // This requires API 24, so use reflection to implement this
    1.48 -        try {
    1.49 -            Class PointerIconClass = Class.forName("android.view.PointerIcon");
    1.50 -            Method setPointerIcon = SDLSurface.class.getMethod("setPointerIcon", PointerIconClass);
    1.51 -            setPointerIcon.invoke(mSurface, mCursors.get(cursorID));
    1.52 -        } catch (Exception e) {
    1.53 +
    1.54 +        if (Build.VERSION.SDK_INT >= 24) {
    1.55 +            try {
    1.56 +                mSurface.setPointerIcon(mCursors.get(cursorID));
    1.57 +            } catch (Exception e) {
    1.58 +                return false;
    1.59 +            }
    1.60 +        } else {
    1.61              return false;
    1.62          }
    1.63          return true;
    1.64 @@ -1449,15 +1452,12 @@
    1.65              cursor_type = 1002; //PointerIcon.TYPE_HAND;
    1.66              break;
    1.67          }
    1.68 -        // This requires API 24, so use reflection to implement this
    1.69 -        try {
    1.70 -            Class PointerIconClass = Class.forName("android.view.PointerIcon");
    1.71 -            Class[] arg_types = new Class[] { Context.class, int.class };
    1.72 -            Method getSystemIcon = PointerIconClass.getMethod("getSystemIcon", arg_types);
    1.73 -            Method setPointerIcon = SDLSurface.class.getMethod("setPointerIcon", PointerIconClass);
    1.74 -            setPointerIcon.invoke(mSurface, getSystemIcon.invoke(null, SDL.getContext(), cursor_type));
    1.75 -        } catch (Exception e) {
    1.76 -            return false;
    1.77 +        if (Build.VERSION.SDK_INT >= 24) {
    1.78 +            try {
    1.79 +                mSurface.setPointerIcon(PointerIcon.getSystemIcon(SDL.getContext(), cursor_type));
    1.80 +            } catch (Exception e) {
    1.81 +                return false;
    1.82 +            }
    1.83          }
    1.84          return true;
    1.85      }