Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3cd562df authored by d34d's avatar d34d
Browse files

Only ask for storage perms when needed

Unless a user clicks on "My photos", there is no need to deny them
access to the entire wallpaper picker when they deny storage access
permissions.

Change-Id: I7028f373d4da65d2e8aa3939600681c92d03dc31
TICKET: CYNGNOS-3175
parent 4909ba22
Loading
Loading
Loading
Loading
+1 −38
Original line number Diff line number Diff line
@@ -60,8 +60,6 @@ import java.util.WeakHashMap;
public class WallpaperCropActivity extends BaseActivity implements Handler.Callback {
    private static final String LOGTAG = "CropActivity";

    private static final int REQUEST_CODE_STORAGE_PERMISSION_CHECK = 100;

    protected static final String WALLPAPER_WIDTH_KEY = WallpaperUtils.WALLPAPER_WIDTH_KEY;
    protected static final String WALLPAPER_HEIGHT_KEY = WallpaperUtils.WALLPAPER_HEIGHT_KEY;

@@ -94,33 +92,8 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!hasStoragePermissions()) {
            requestStoragePermissions();
        } else {
        load();
    }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
            int[] grantResults) {
        if (requestCode == REQUEST_CODE_STORAGE_PERMISSION_CHECK) {
            for (int i = 0; i < permissions.length; i++ ) {
                final String permission = permissions[i];
                final int grantResult = grantResults[i];
                if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    if (grantResult == PackageManager.PERMISSION_GRANTED) {
                       load();
                    } else {
                        Toast.makeText(this, getString(R.string.storage_permission_denied),
                                Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
            }
        }
    }

    private void load() {
        mLoaderThread = new HandlerThread("wallpaper_loader");
@@ -514,16 +487,6 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
                sp, getWindowManager(), WallpaperManager.getInstance(getContext()), true);
    }

    private boolean hasStoragePermissions() {
        return checkCallingOrSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) ==
                PackageManager.PERMISSION_GRANTED;
    }

    private void requestStoragePermissions() {
        requestPermissions(new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_CODE_STORAGE_PERMISSION_CHECK);
    }

    static class LoadRequest {
        BitmapSource src;
        boolean touchEnabled;
+47 −3
Original line number Diff line number Diff line
@@ -104,6 +104,8 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
    private static final String ACTION_SET_KEYGUARD_WALLPAPER =
            "android.intent.action.SET_KEYGUARD_WALLPAPER";

    private static final int REQUEST_CODE_STORAGE_PERMISSION_CHECK = 100;

    @Thunk
    View mSelectedTile;
    @Thunk boolean mIgnoreNextTap;
@@ -147,9 +149,11 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
    public static class PickImageInfo extends WallpaperTileInfo {
        @Override
        public void onClick(WallpaperPickerActivity a) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            a.startActivityForResultSafely(intent, IMAGE_PICK);
            if (a.hasStoragePermissions()) {
                a.startPickImageActivity();
            } else {
                a.requestStoragePermissions();
            }
        }
    }

@@ -511,6 +515,30 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
            int[] grantResults) {
        if (requestCode == REQUEST_CODE_STORAGE_PERMISSION_CHECK) {
            for (int i = 0; i < permissions.length; i++ ) {
                final String permission = permissions[i];
                final int grantResult = grantResults[i];
                if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    if (grantResult == PackageManager.PERMISSION_GRANTED) {
                        startPickImageActivity();
                    } else {
                        Toast.makeText(this, getString(R.string.storage_permission_denied),
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    }

    // called by onCreate; this is subclassed to overwrite WallpaperCropActivity
    protected void init() {
        setContentView(R.layout.wallpaper_picker);
@@ -1378,6 +1406,22 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
        Utilities.startActivityForResultSafely(getActivity(), intent, requestCode);
    }

    public void startPickImageActivity() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResultSafely(intent, IMAGE_PICK);
    }

    public boolean hasStoragePermissions() {
        return checkCallingOrSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) ==
                PackageManager.PERMISSION_GRANTED;
    }

    public void requestStoragePermissions() {
        requestPermissions(new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_CODE_STORAGE_PERMISSION_CHECK);
    }

    private static class ThemeWallpapersAdapter extends BaseAdapter implements ListAdapter {
        private LayoutInflater mLayoutInflater;
        private ArrayList<ThemeWallpaperInfo> mWallpapers;