diff --git a/leakcanary-android/src/main/java/com/squareup/leakcanary/internal/RequestStoragePermissionActivity.java b/leakcanary-android/src/main/java/com/squareup/leakcanary/internal/RequestStoragePermissionActivity.java
index 2d94130223..3d8a23f19a 100644
--- a/leakcanary-android/src/main/java/com/squareup/leakcanary/internal/RequestStoragePermissionActivity.java
+++ b/leakcanary-android/src/main/java/com/squareup/leakcanary/internal/RequestStoragePermissionActivity.java
@@ -20,6 +20,9 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
+import android.os.Bundle;
+import android.widget.Toast;
+import com.squareup.leakcanary.R;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
@@ -27,6 +30,7 @@
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.Build.VERSION_CODES.M;
+import static android.widget.Toast.LENGTH_LONG;
import static com.squareup.leakcanary.internal.LeakCanaryInternals.setEnabledBlocking;
@TargetApi(M) //
@@ -39,18 +43,31 @@ public static PendingIntent createPendingIntent(Context context) {
return PendingIntent.getActivity(context, 1, intent, FLAG_UPDATE_CURRENT);
}
- @Override protected void onResume() {
- super.onResume();
- // This won't work well if the user doesn't enable the permission.
- // Seems ok for a dev tool, especially since you have to click a notification
- // to get here.
- if (checkSelfPermission(WRITE_EXTERNAL_STORAGE) == PERMISSION_GRANTED) {
- finish();
- } else {
+ @Override protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ if (savedInstanceState == null) {
+ if (hasStoragePermission()) {
+ finish();
+ return;
+ }
String[] permissions = {
WRITE_EXTERNAL_STORAGE
};
requestPermissions(permissions, 42);
}
}
+
+ @Override public void onRequestPermissionsResult(int requestCode, String[] permissions,
+ int[] grantResults) {
+ if (!hasStoragePermission()) {
+ Toast.makeText(getApplication(), R.string.leak_canary_permission_not_granted, LENGTH_LONG)
+ .show();
+ }
+ finish();
+ }
+
+ private boolean hasStoragePermission() {
+ return checkSelfPermission(WRITE_EXTERNAL_STORAGE) == PERMISSION_GRANTED;
+ }
}
diff --git a/leakcanary-android/src/main/res/values/leak_canary_strings.xml b/leakcanary-android/src/main/res/values/leak_canary_strings.xml
index b53e29ce16..d9aaee1355 100644
--- a/leakcanary-android/src/main/res/values/leak_canary_strings.xml
+++ b/leakcanary-android/src/main/res/values/leak_canary_strings.xml
@@ -34,6 +34,7 @@
No leak found
The GC was being lazy.
[Excluded] %s
+ Please grant external storage permission, otherwise memory leaks will not be detected.
Leak detected, need permission
Click to enable storage permission for %s.