Resolved conflict and added Android_JNI_RequestPermission()
authorSam Lantinga <slouken@libsdl.org>
Thu, 13 Feb 2020 10:19:05 -0800
changeset 135159d631a3485a5
parent 13514 529fc1a611e8
child 13516 dd33a1cfaae5
Resolved conflict and added Android_JNI_RequestPermission()
src/core/android/SDL_android.c
     1.1 --- a/src/core/android/SDL_android.c	Wed Feb 12 12:26:27 2020 -0800
     1.2 +++ b/src/core/android/SDL_android.c	Thu Feb 13 10:19:05 2020 -0800
     1.3 @@ -21,6 +21,7 @@
     1.4  #include "../../SDL_internal.h"
     1.5  #include "SDL_stdinc.h"
     1.6  #include "SDL_assert.h"
     1.7 +#include "SDL_atomic.h"
     1.8  #include "SDL_hints.h"
     1.9  #include "SDL_log.h"
    1.10  #include "SDL_main.h"
    1.11 @@ -155,6 +156,10 @@
    1.12          JNIEnv* env, jclass cls,
    1.13          jint touchId, jstring name);
    1.14  
    1.15 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePermissionResult)(
    1.16 +        JNIEnv* env, jclass cls,
    1.17 +        jint requestCode, jboolean result);
    1.18 +
    1.19  static JNINativeMethod SDLActivity_tab[] = {
    1.20      { "nativeSetupJNI",             "()I", SDL_JAVA_INTERFACE(nativeSetupJNI) },
    1.21      { "nativeRunMain",              "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)I", SDL_JAVA_INTERFACE(nativeRunMain) },
    1.22 @@ -181,7 +186,8 @@
    1.23      { "nativeGetHint",              "(Ljava/lang/String;)Ljava/lang/String;", SDL_JAVA_INTERFACE(nativeGetHint) },
    1.24      { "nativeSetenv",               "(Ljava/lang/String;Ljava/lang/String;)V", SDL_JAVA_INTERFACE(nativeSetenv) },
    1.25      { "onNativeOrientationChanged", "(I)V", SDL_JAVA_INTERFACE(onNativeOrientationChanged) },
    1.26 -    { "nativeAddTouch",             "(ILjava/lang/String;)V", SDL_JAVA_INTERFACE(nativeAddTouch) }
    1.27 +    { "nativeAddTouch",             "(ILjava/lang/String;)V", SDL_JAVA_INTERFACE(nativeAddTouch) },
    1.28 +    { "nativePermissionResult",     "(IZ)V", SDL_JAVA_INTERFACE(nativePermissionResult) }
    1.29  };
    1.30  
    1.31  /* Java class SDLInputConnection */
    1.32 @@ -283,34 +289,35 @@
    1.33  static jclass mActivityClass;
    1.34  
    1.35  /* method signatures */
    1.36 +static jmethodID midClipboardGetText;
    1.37 +static jmethodID midClipboardHasText;
    1.38 +static jmethodID midClipboardSetText;
    1.39 +static jmethodID midCreateCustomCursor;
    1.40 +static jmethodID midGetContext;
    1.41 +static jmethodID midGetDisplayDPI;
    1.42 +static jmethodID midGetManifestEnvironmentVariables;
    1.43  static jmethodID midGetNativeSurface;
    1.44 -static jmethodID midSetSurfaceViewFormat;
    1.45 -static jmethodID midSetActivityTitle;
    1.46 -static jmethodID midSetWindowStyle;
    1.47 -static jmethodID midSetOrientation;
    1.48 -static jmethodID midMinimizeWindow;
    1.49 -static jmethodID midShouldMinimizeOnFocusLoss;
    1.50 -static jmethodID midGetContext;
    1.51 -static jmethodID midIsTablet;
    1.52 +static jmethodID midInitTouch;
    1.53  static jmethodID midIsAndroidTV;
    1.54  static jmethodID midIsChromebook;
    1.55  static jmethodID midIsDeXMode;
    1.56 +static jmethodID midIsScreenKeyboardShown;
    1.57 +static jmethodID midIsTablet;
    1.58  static jmethodID midManualBackButton;
    1.59 -static jmethodID midInitTouch;
    1.60 +static jmethodID midMinimizeWindow;
    1.61 +static jmethodID midOpenAPKExpansionInputStream;
    1.62 +static jmethodID midRequestPermission;
    1.63  static jmethodID midSendMessage;
    1.64 +static jmethodID midSetActivityTitle;
    1.65 +static jmethodID midSetCustomCursor;
    1.66 +static jmethodID midSetOrientation;
    1.67 +static jmethodID midSetRelativeMouseEnabled;
    1.68 +static jmethodID midSetSurfaceViewFormat;
    1.69 +static jmethodID midSetSystemCursor;
    1.70 +static jmethodID midSetWindowStyle;
    1.71 +static jmethodID midShouldMinimizeOnFocusLoss;
    1.72  static jmethodID midShowTextInput;
    1.73 -static jmethodID midIsScreenKeyboardShown;
    1.74 -static jmethodID midClipboardSetText;
    1.75 -static jmethodID midClipboardGetText;
    1.76 -static jmethodID midClipboardHasText;
    1.77 -static jmethodID midOpenAPKExpansionInputStream;
    1.78 -static jmethodID midGetManifestEnvironmentVariables;
    1.79 -static jmethodID midGetDisplayDPI;
    1.80 -static jmethodID midCreateCustomCursor;
    1.81 -static jmethodID midSetCustomCursor;
    1.82 -static jmethodID midSetSystemCursor;
    1.83  static jmethodID midSupportsRelativeMouse;
    1.84 -static jmethodID midSetRelativeMouseEnabled;
    1.85  
    1.86  /* audio manager */
    1.87  static jclass mAudioManagerClass;
    1.88 @@ -342,7 +349,10 @@
    1.89  static float fLastAccelerometer[3];
    1.90  static SDL_bool bHasNewData;
    1.91  
    1.92 -static SDL_bool bHasEnvironmentVariables = SDL_FALSE;
    1.93 +static SDL_bool bHasEnvironmentVariables;
    1.94 +
    1.95 +static SDL_atomic_t bPermissionRequestPending;
    1.96 +static SDL_bool bPermissionRequestResult;
    1.97  
    1.98  /*******************************************************************************
    1.99                   Functions called by JNI
   1.100 @@ -552,68 +562,65 @@
   1.101  
   1.102      mActivityClass = (jclass)((*env)->NewGlobalRef(env, cls));
   1.103  
   1.104 -    midGetNativeSurface = (*env)->GetStaticMethodID(env, mActivityClass,
   1.105 -                                "getNativeSurface","()Landroid/view/Surface;");
   1.106 -    midSetSurfaceViewFormat = (*env)->GetStaticMethodID(env, mActivityClass,
   1.107 -                                "setSurfaceViewFormat","(I)V");
   1.108 -    midSetActivityTitle = (*env)->GetStaticMethodID(env, mActivityClass,
   1.109 -                                "setActivityTitle","(Ljava/lang/String;)Z");
   1.110 -    midSetWindowStyle = (*env)->GetStaticMethodID(env, mActivityClass,
   1.111 -                                "setWindowStyle","(Z)V");
   1.112 -    midSetOrientation = (*env)->GetStaticMethodID(env, mActivityClass,
   1.113 -                                "setOrientation","(IIZLjava/lang/String;)V");
   1.114 -    midMinimizeWindow = (*env)->GetStaticMethodID(env, mActivityClass,
   1.115 -                                "minimizeWindow","()V");
   1.116 -    midShouldMinimizeOnFocusLoss = (*env)->GetStaticMethodID(env, mActivityClass,
   1.117 -                                "shouldMinimizeOnFocusLoss","()Z");
   1.118 -    midGetContext = (*env)->GetStaticMethodID(env, mActivityClass,
   1.119 -                                "getContext","()Landroid/content/Context;");
   1.120 -    midIsTablet = (*env)->GetStaticMethodID(env, mActivityClass,
   1.121 -                                "isTablet", "()Z");
   1.122 -    midIsAndroidTV = (*env)->GetStaticMethodID(env, mActivityClass,
   1.123 -                                "isAndroidTV","()Z");
   1.124 -    midIsChromebook = (*env)->GetStaticMethodID(env, mActivityClass,
   1.125 -                                "isChromebook", "()Z");
   1.126 -    midIsDeXMode = (*env)->GetStaticMethodID(env, mActivityClass,
   1.127 -                                "isDeXMode", "()Z");
   1.128 -    midManualBackButton = (*env)->GetStaticMethodID(env, mActivityClass,
   1.129 -                                "manualBackButton", "()V");
   1.130 -    midInitTouch = (*env)->GetStaticMethodID(env, mActivityClass,
   1.131 -                                "initTouch", "()V");
   1.132 -    midSendMessage = (*env)->GetStaticMethodID(env, mActivityClass,
   1.133 -                                "sendMessage", "(II)Z");
   1.134 -    midShowTextInput =  (*env)->GetStaticMethodID(env, mActivityClass,
   1.135 -                                "showTextInput", "(IIII)Z");
   1.136 -    midIsScreenKeyboardShown = (*env)->GetStaticMethodID(env, mActivityClass,
   1.137 -                                "isScreenKeyboardShown","()Z");
   1.138 -    midClipboardSetText = (*env)->GetStaticMethodID(env, mActivityClass,
   1.139 -                                "clipboardSetText", "(Ljava/lang/String;)V");
   1.140 -    midClipboardGetText = (*env)->GetStaticMethodID(env, mActivityClass,
   1.141 -                                "clipboardGetText", "()Ljava/lang/String;");
   1.142 -    midClipboardHasText = (*env)->GetStaticMethodID(env, mActivityClass,
   1.143 -                                "clipboardHasText", "()Z");
   1.144 -    midOpenAPKExpansionInputStream = (*env)->GetStaticMethodID(env, mActivityClass,
   1.145 -                                "openAPKExpansionInputStream", "(Ljava/lang/String;)Ljava/io/InputStream;");
   1.146 -
   1.147 -    midGetManifestEnvironmentVariables = (*env)->GetStaticMethodID(env, mActivityClass,
   1.148 -                                "getManifestEnvironmentVariables", "()Z");
   1.149 -
   1.150 +    midClipboardGetText = (*env)->GetStaticMethodID(env, mActivityClass, "clipboardGetText", "()Ljava/lang/String;");
   1.151 +    midClipboardHasText = (*env)->GetStaticMethodID(env, mActivityClass, "clipboardHasText", "()Z");
   1.152 +    midClipboardSetText = (*env)->GetStaticMethodID(env, mActivityClass, "clipboardSetText", "(Ljava/lang/String;)V");
   1.153 +    midCreateCustomCursor = (*env)->GetStaticMethodID(env, mActivityClass, "createCustomCursor", "([IIIII)I");
   1.154 +    midGetContext = (*env)->GetStaticMethodID(env, mActivityClass, "getContext","()Landroid/content/Context;");
   1.155      midGetDisplayDPI = (*env)->GetStaticMethodID(env, mActivityClass, "getDisplayDPI", "()Landroid/util/DisplayMetrics;");
   1.156 -    midCreateCustomCursor = (*env)->GetStaticMethodID(env, mActivityClass, "createCustomCursor", "([IIIII)I");
   1.157 +    midGetManifestEnvironmentVariables = (*env)->GetStaticMethodID(env, mActivityClass, "getManifestEnvironmentVariables", "()Z");
   1.158 +    midGetNativeSurface = (*env)->GetStaticMethodID(env, mActivityClass, "getNativeSurface","()Landroid/view/Surface;");
   1.159 +    midInitTouch = (*env)->GetStaticMethodID(env, mActivityClass, "initTouch", "()V");
   1.160 +    midIsAndroidTV = (*env)->GetStaticMethodID(env, mActivityClass, "isAndroidTV","()Z");
   1.161 +    midIsChromebook = (*env)->GetStaticMethodID(env, mActivityClass, "isChromebook", "()Z");
   1.162 +    midIsDeXMode = (*env)->GetStaticMethodID(env, mActivityClass, "isDeXMode", "()Z");
   1.163 +    midIsScreenKeyboardShown = (*env)->GetStaticMethodID(env, mActivityClass, "isScreenKeyboardShown","()Z");
   1.164 +    midIsTablet = (*env)->GetStaticMethodID(env, mActivityClass, "isTablet", "()Z");
   1.165 +    midManualBackButton = (*env)->GetStaticMethodID(env, mActivityClass, "manualBackButton", "()V");
   1.166 +    midMinimizeWindow = (*env)->GetStaticMethodID(env, mActivityClass, "minimizeWindow","()V");
   1.167 +    midOpenAPKExpansionInputStream = (*env)->GetStaticMethodID(env, mActivityClass, "openAPKExpansionInputStream", "(Ljava/lang/String;)Ljava/io/InputStream;");
   1.168 +    midRequestPermission = (*env)->GetStaticMethodID(env, mActivityClass, "requestPermission", "(Ljava/lang/String;I)V");
   1.169 +    midSendMessage = (*env)->GetStaticMethodID(env, mActivityClass, "sendMessage", "(II)Z");
   1.170 +    midSetActivityTitle = (*env)->GetStaticMethodID(env, mActivityClass, "setActivityTitle","(Ljava/lang/String;)Z");
   1.171      midSetCustomCursor = (*env)->GetStaticMethodID(env, mActivityClass, "setCustomCursor", "(I)Z");
   1.172 +    midSetOrientation = (*env)->GetStaticMethodID(env, mActivityClass, "setOrientation","(IIZLjava/lang/String;)V");
   1.173 +    midSetRelativeMouseEnabled = (*env)->GetStaticMethodID(env, mActivityClass, "setRelativeMouseEnabled", "(Z)Z");
   1.174 +    midSetSurfaceViewFormat = (*env)->GetStaticMethodID(env, mActivityClass, "setSurfaceViewFormat","(I)V");
   1.175      midSetSystemCursor = (*env)->GetStaticMethodID(env, mActivityClass, "setSystemCursor", "(I)Z");
   1.176 -
   1.177 +    midSetWindowStyle = (*env)->GetStaticMethodID(env, mActivityClass, "setWindowStyle","(Z)V");
   1.178 +    midShouldMinimizeOnFocusLoss = (*env)->GetStaticMethodID(env, mActivityClass, "shouldMinimizeOnFocusLoss","()Z");
   1.179 +    midShowTextInput =  (*env)->GetStaticMethodID(env, mActivityClass, "showTextInput", "(IIII)Z");
   1.180      midSupportsRelativeMouse = (*env)->GetStaticMethodID(env, mActivityClass, "supportsRelativeMouse", "()Z");
   1.181 -    midSetRelativeMouseEnabled = (*env)->GetStaticMethodID(env, mActivityClass, "setRelativeMouseEnabled", "(Z)Z");
   1.182 -
   1.183 -
   1.184 -    if (!midGetNativeSurface || !midSetSurfaceViewFormat ||
   1.185 -       !midSetActivityTitle || !midSetWindowStyle || !midSetOrientation || !midMinimizeWindow || !midShouldMinimizeOnFocusLoss || !midGetContext || !midIsTablet || !midIsAndroidTV || !midInitTouch ||
   1.186 -       !midSendMessage || !midShowTextInput || !midIsScreenKeyboardShown ||
   1.187 -       !midClipboardSetText || !midClipboardGetText || !midClipboardHasText ||
   1.188 -       !midOpenAPKExpansionInputStream || !midGetManifestEnvironmentVariables || !midGetDisplayDPI ||
   1.189 -       !midCreateCustomCursor || !midSetCustomCursor || !midSetSystemCursor || !midSupportsRelativeMouse || !midSetRelativeMouseEnabled ||
   1.190 -       !midIsChromebook || !midIsDeXMode || !midManualBackButton) {
   1.191 +
   1.192 +    if (!midClipboardGetText ||
   1.193 +        !midClipboardHasText ||
   1.194 +        !midClipboardSetText ||
   1.195 +        !midCreateCustomCursor ||
   1.196 +        !midGetContext ||
   1.197 +        !midGetDisplayDPI ||
   1.198 +        !midGetManifestEnvironmentVariables ||
   1.199 +        !midGetNativeSurface ||
   1.200 +        !midInitTouch ||
   1.201 +        !midIsAndroidTV ||
   1.202 +        !midIsChromebook ||
   1.203 +        !midIsDeXMode ||
   1.204 +        !midIsScreenKeyboardShown ||
   1.205 +        !midIsTablet ||
   1.206 +        !midManualBackButton ||
   1.207 +        !midMinimizeWindow ||
   1.208 +        !midOpenAPKExpansionInputStream ||
   1.209 +        !midRequestPermission ||
   1.210 +        !midSendMessage ||
   1.211 +        !midSetActivityTitle ||
   1.212 +        !midSetCustomCursor ||
   1.213 +        !midSetOrientation ||
   1.214 +        !midSetRelativeMouseEnabled ||
   1.215 +        !midSetSurfaceViewFormat ||
   1.216 +        !midSetSystemCursor ||
   1.217 +        !midSetWindowStyle ||
   1.218 +        !midShouldMinimizeOnFocusLoss ||
   1.219 +        !midShowTextInput ||
   1.220 +        !midSupportsRelativeMouse) {
   1.221          __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLActivity.java?");
   1.222      }
   1.223  
   1.224 @@ -875,6 +882,14 @@
   1.225      (*env)->ReleaseStringUTFChars(env, name, utfname);
   1.226  }
   1.227  
   1.228 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePermissionResult)(
   1.229 +        JNIEnv* env, jclass cls,
   1.230 +        jint requestCode, jboolean result)
   1.231 +{
   1.232 +    bPermissionRequestResult = result;
   1.233 +    SDL_AtomicSet(&bPermissionRequestPending, SDL_FALSE);
   1.234 +}
   1.235 +
   1.236  /* Paddown */
   1.237  JNIEXPORT jint JNICALL SDL_JAVA_CONTROLLER_INTERFACE(onNativePadDown)(
   1.238                                      JNIEnv *env, jclass jcls,
   1.239 @@ -2759,6 +2774,27 @@
   1.240      return (*env)->CallStaticBooleanMethod(env, mActivityClass, midSetRelativeMouseEnabled, (enabled == 1));
   1.241  }
   1.242  
   1.243 +SDL_bool Android_JNI_RequestPermission(const char *permission)
   1.244 +{
   1.245 +    JNIEnv *env = Android_JNI_GetEnv();
   1.246 +	const int requestCode = 1;
   1.247 +
   1.248 +	/* Wait for any pending request on another thread */
   1.249 +	while (SDL_AtomicGet(&bPermissionRequestPending) == SDL_TRUE) {
   1.250 +		SDL_Delay(10);
   1.251 +	}
   1.252 +	SDL_AtomicSet(&bPermissionRequestPending, SDL_TRUE);
   1.253 +
   1.254 +    jstring jpermission = (*env)->NewStringUTF(env, permission);
   1.255 +    (*env)->CallStaticVoidMethod(env, mActivityClass, midRequestPermission, jpermission, requestCode);
   1.256 +    (*env)->DeleteLocalRef(env, jpermission);
   1.257 +
   1.258 +	/* Wait for the request to complete */
   1.259 +	while (SDL_AtomicGet(&bPermissionRequestPending) == SDL_TRUE) {
   1.260 +		SDL_Delay(10);
   1.261 +	}
   1.262 +	return bPermissionRequestResult;
   1.263 +}
   1.264  
   1.265  #endif /* __ANDROID__ */
   1.266