From 6a4f804228c5ce93ccc48a2e2725d32d83a5bd8c Mon Sep 17 00:00:00 2001 From: Roman Birg Date: Mon, 11 Jan 2016 11:16:36 -0800 Subject: PhotoPhase: ask for runtime permissions Ref: CYNGNOS-1555 Change-Id: I7cc351fca1085ec29823b12d3959b83142eac4cb Signed-off-by: Roman Birg --- .../wallpapers/photophase/PhotoPhaseWallpaper.java | 10 ++++ .../photophase/RequestPermissionsActivity.java | 55 ++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/org/cyanogenmod/wallpapers/photophase/RequestPermissionsActivity.java (limited to 'src/org/cyanogenmod/wallpapers') diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java index ecb0872..2c53709 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java @@ -19,6 +19,7 @@ package org.cyanogenmod.wallpapers.photophase; import android.app.ActivityManager; import android.app.WallpaperManager; import android.content.Context; +import android.content.Intent; import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView.Renderer; import android.os.Bundle; @@ -94,10 +95,19 @@ public class PhotoPhaseWallpaper */ @Override public Engine onCreateEngine() { + checkPermission(); mEngine = new PhotoPhaseWallpaperEngine(this); return mEngine; } + private void checkPermission() { + if (!RequestPermissionsActivity.hasRequestedPermissions(this)) { + startActivity(new Intent(Intent.ACTION_MAIN) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .setClass(this, RequestPermissionsActivity.class)); + } + } + /** * A wallpaper engine implementation using GLES. */ diff --git a/src/org/cyanogenmod/wallpapers/photophase/RequestPermissionsActivity.java b/src/org/cyanogenmod/wallpapers/photophase/RequestPermissionsActivity.java new file mode 100644 index 0000000..5999d10 --- /dev/null +++ b/src/org/cyanogenmod/wallpapers/photophase/RequestPermissionsActivity.java @@ -0,0 +1,55 @@ +package org.cyanogenmod.wallpapers.photophase; + +import android.Manifest; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.app.Activity; +import android.content.Context; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.widget.Toast; + +public class RequestPermissionsActivity extends Activity { + + public static final int REQUEST_STORAGE_PERMISSION = 1; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (!hasRequestedPermissions(this)) { + requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, + REQUEST_STORAGE_PERMISSION); + } else { + setResult(Activity.RESULT_OK); + finish(); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, + @NonNull int[] grantResults) { + switch (requestCode) { + case REQUEST_STORAGE_PERMISSION: { + // If request is cancelled, the result arrays are empty. + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + setResult(Activity.RESULT_OK); + } else { + Toast.makeText(this, R.string.runtime_permission_warning, Toast.LENGTH_SHORT) + .show(); + setResult(Activity.RESULT_CANCELED); + } + finish(); + return; + } + } + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + + public static boolean hasRequestedPermissions(Context context) { + return context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == + PackageManager.PERMISSION_GRANTED; + } + +} -- cgit v1.2.3