SDL: request Marshmallow SD card runtime permission using SDL_ANDROID_RequestExternalStorageRuntimePermission()

This commit is contained in:
pelya
2016-06-27 01:11:18 +03:00
parent 58558aa892
commit 7e032dd40a
8 changed files with 52 additions and 2 deletions

View File

@@ -53,6 +53,9 @@ import android.text.SpannedString;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.Manifest;
import android.content.pm.PackageManager;
class CountingInputStream extends BufferedInputStream
{
@@ -328,6 +331,19 @@ class DataDownloader extends Thread
Status.setText( downloadCount + "/" + downloadTotal + ": " + res.getString(R.string.connecting_to, url) );
if( url.indexOf("obb:") == 0 ) // APK expansion file provided by Google Play
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
int permissionCheck = Parent.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED && !Parent.writeExternalStoragePermissionDialogAnswered)
{
Parent.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
while( !Parent.writeExternalStoragePermissionDialogAnswered )
{
try{ Thread.sleep(300); } catch (InterruptedException e) {}
}
}
}
url = getObbFilePath(url);
InputStream stream1 = null;
try {

View File

@@ -1580,6 +1580,11 @@ public class MainActivity extends Activity
{
Log.i("SDL", "libSDL: Record audio permission: " + (grantResults[0] == PackageManager.PERMISSION_GRANTED ? "GRANTED" : "DENIED"));
}
if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[0]))
{
Log.i("SDL", "libSDL: Write external storage permission: " + (grantResults[0] == PackageManager.PERMISSION_GRANTED ? "GRANTED" : "DENIED"));
writeExternalStoragePermissionDialogAnswered = true;
}
}
public FrameLayout getVideoLayout() { return _videoLayout; }
@@ -1620,6 +1625,7 @@ public class MainActivity extends Activity
public LinkedList<Integer> textInput = new LinkedList<Integer> ();
public static MainActivity instance = null;
public boolean writeExternalStoragePermissionDialogAnswered = false;
}
// *** HONEYCOMB / ICS FIX FOR FULLSCREEN MODE, by lmak ***

View File

@@ -63,6 +63,8 @@ import android.content.Intent;
import android.view.View;
import android.view.Display;
import android.net.Uri;
import android.Manifest;
import android.content.pm.PackageManager;
class Mouse
@@ -947,6 +949,18 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer
Settings.setConfigOptionFromSDL(option, value);
}
public void requestExternalStorageRuntimePermissionFromSDL()
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
int permissionCheck = context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED && !context.writeExternalStoragePermissionDialogAnswered)
{
context.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
}
}
private int PowerOf2(int i)
{
int value = 1;

View File

@@ -170,7 +170,7 @@ AppUsesMultitouch=y
AppRecordsAudio=n
# Application needs to access SD card. If your data files are bigger than 5 Mb, enable it. (y) / (n)
AccessSdCard=
AccessSdCard=y
# Application needs Internet access. If you disable it, you'll have to bundle all your data files inside .apk (y) / (n)
AccessInternet=

View File

@@ -18,6 +18,7 @@
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_screenkeyboard.h>
#include <SDL/SDL_android.h>
#define fprintf(X, ...) __android_log_print(ANDROID_LOG_INFO, "Ballfield", __VA_ARGS__)
#define printf(...) __android_log_print(ANDROID_LOG_INFO, "Ballfield", __VA_ARGS__)
@@ -686,7 +687,10 @@ int main(int argc, char* argv[])
if(evt.key.keysym.sym == SDLK_0)
SDL_ANDROID_SetScreenKeyboardButtonShown(SDL_ANDROID_SCREENKEYBOARD_BUTTON_2, 1);
if(evt.key.keysym.sym == SDLK_1)
{
SDL_ANDROID_SetScreenKeyboardButtonShown(SDL_ANDROID_SCREENKEYBOARD_BUTTON_2, 0);
SDL_ANDROID_RequestExternalStorageRuntimePermission();
}
if(evt.key.keysym.sym == SDLK_2)
{
SDL_ANDROID_SetScreenKeyboardButtonShown(SDL_ANDROID_SCREENKEYBOARD_BUTTON_DPAD, 1);

View File

@@ -64,7 +64,7 @@ SdlVideoResize=y
SdlVideoResizeKeepAspect=n
# Do not allow device to sleep when the application is in foreground, set this for video players or apps which use accelerometer
InhibitSuspend=n
InhibitSuspend=y
# Create Android service, so the app is less likely to be killed while in background
CreateService=

View File

@@ -134,6 +134,9 @@ enum {
/* Set SDL Android-specifc option, such as video depth or mouse emulation mode. Most options require restarting the app. */
extern DECLSPEC void SDLCALL SDL_ANDROID_SetConfigOption(int option, int value);
/* Show runtime permission dialog for accessing SD card on Android 6.0 and above */
extern DECLSPEC void SDLCALL SDL_ANDROID_RequestExternalStorageRuntimePermission();
#ifdef __cplusplus
}
#endif

View File

@@ -88,6 +88,7 @@ static jmethodID JavaRequestCloudLoad = NULL;
static jmethodID JavaRequestOpenExternalApp = NULL;
static jmethodID JavaRequestRestartMyself = NULL;
static jmethodID JavaRequestSetConfigOption = NULL;
static jmethodID JavaRequestExternalStorageRuntimePermission = NULL;
static int glContextLost = 0;
static int showScreenKeyboardDeferred = 0;
static const char * showScreenKeyboardOldText = "";
@@ -361,6 +362,7 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeInitJavaCallbacks) ( JNIEnv* env, jobject t
JavaRequestOpenExternalApp = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "openExternalApp", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
JavaRequestRestartMyself = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "restartMyself", "(Ljava/lang/String;)V");
JavaRequestSetConfigOption = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setConfigOptionFromSDL", "(II)V");
JavaRequestExternalStorageRuntimePermission = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "requestExternalStorageRuntimePermissionFromSDL", "()V");
ANDROID_InitOSKeymap();
}
@@ -607,6 +609,11 @@ void SDLCALL SDL_ANDROID_OpenExternalWebBrowser(const char *url)
SDL_ANDROID_OpenExternalApp(NULL, NULL, url);
}
void SDLCALL SDL_ANDROID_RequestExternalStorageRuntimePermission()
{
(*JavaEnv)->CallVoidMethod( JavaEnv, JavaRenderer, JavaRequestExternalStorageRuntimePermission );
}
// Dummy callback for SDL2 to satisfy linker
extern void SDL_Android_Init(JNIEnv* env, jclass cls);
void SDL_Android_Init(JNIEnv* env, jclass cls)