Skip to content

Commit

Permalink
Trebuchet: Add runtime storage perm check for wallpaper picker
Browse files Browse the repository at this point in the history
Issue-id: FEIJ-441

Change-Id: Iac9a2c249de595568f68313335d337410928d19e
  • Loading branch information
cretin45 committed May 5, 2016
1 parent dd6f2a2 commit 71e0922
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Expand Up @@ -16,13 +16,15 @@

package com.android.launcher3;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -58,6 +60,8 @@
public class WallpaperCropActivity extends BaseActivity implements Handler.Callback {
private static final String LOGTAG = "Launcher3.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;

Expand Down Expand Up @@ -90,6 +94,34 @@ public class WallpaperCropActivity extends BaseActivity implements Handler.Callb
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");
mLoaderThread.start();
mLoaderHandler = new Handler(mLoaderThread.getLooper(), this);
Expand Down Expand Up @@ -451,6 +483,16 @@ protected void updateWallpaperDimensions(int width, int height) {
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;
Expand Down
Expand Up @@ -721,7 +721,7 @@ protected Bitmap getThumbnailOfLastPhoto() {
public void onStop() {
super.onStop();
mWallpaperStrip = findViewById(R.id.wallpaper_strip);
if (mWallpaperStrip.getAlpha() < 1f) {
if (mWallpaperStrip != null && mWallpaperStrip.getAlpha() < 1f) {
mWallpaperStrip.setAlpha(1f);
mWallpaperStrip.setVisibility(View.VISIBLE);
}
Expand Down
2 changes: 2 additions & 0 deletions res/values/cm_strings.xml
Expand Up @@ -77,4 +77,6 @@

<!-- App not available toast text -->
<string name="app_not_available">App not available</string>

<string name="storage_permission_denied">Can\'t access storage</string>
</resources>

0 comments on commit 71e0922

Please sign in to comment.