+ } else if (wallpaperButtonClicked) {
+ startActivity(new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER));
+ }
+ }
+
+ private void wallpaperButtonClicked() {
+ wallpaperButtonClicked = true;
+ checkPermission();
+ }
+
+ private void daydreamButtonClicked() {
+ daydreamButtonClicked = true;
+ checkPermission();
+ }
+
+ void checkPermission() {
+ // RES introduced in API 16
+ String permission = Manifest.permission.READ_EXTERNAL_STORAGE;
+ if (havePermission(permission)) {
+ withProceed();
+ } else {
+ noPermission(permission);
+ }
+ }
+
+ private void noPermission(String permission) {
+ int myRequestCode;
+ myRequestCode = MY_REQ_READ_EXTERNAL_STORAGE;
+
+ if (permissionsDeniedRationale(permission)) {
+ showDeniedRationale();
+ } else {
+ requestPermission(permission, myRequestCode);
+ }
+ }
+
+ private boolean permissionsDeniedRationale(String permission) {
+ boolean rationale = ActivityCompat.shouldShowRequestPermissionRationale(this,
+ permission);
+ return rationale;
+ }
+
+ private void requestPermission(String permission, int myRequestCode) {
+ ActivityCompat.requestPermissions(this,
+ new String[]{permission},
+ myRequestCode);
+
+ // myRequestCode is an app-defined int constant.
+ // The callback method gets the result of the request.
+ }
+
+ // TODO: This method should be asynchronous, and not block the thread
+ private void showDeniedRationale() {
+ withProceed();
+ }
+
+ boolean havePermission(String permission) {
+
+ if (Build.VERSION.SDK_INT < 16) {
+ return true;
+ }
+
+ if (permissionGranted(permission)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ private boolean permissionGranted(String permission) {
+ boolean check = ContextCompat.checkSelfPermission(this, permission) ==
+ PackageManager.PERMISSION_GRANTED;
+ return check;
+ }
+
+ public void proceedIfPermissionGranted(int[] grantResults) {
+
+ // If request is cancelled, the result arrays are empty.
+ if (grantResults.length > 0
+ && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
+ withProceed();
+ } else if (grantResults.length > 0) {
+ withProceed();
+ }
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int requestCode,
+ String permissions[], int[] grantResults) {
+ switch (requestCode) {
+ case MY_REQ_READ_EXTERNAL_STORAGE:
+ proceedIfPermissionGranted(grantResults);