Skip to content

Commit

Permalink
Don't loop when storage permission denied
Browse files Browse the repository at this point in the history
If the storage permission is denied, don't retry, just show a toast.

Fixes square#422
  • Loading branch information
pyricau committed Mar 30, 2016
1 parent 2aa6742 commit 37b6ab7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
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;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
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) //
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/res/values-de/leak_canary_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="leak_canary_no_leak_title">Kein Leak gefunden</string>
<string name="leak_canary_no_leak_text">Der GC war untätig.</string>
<string name="leak_canary_excluded_row">[Ausgeschlossen] %s</string>
<string name="leak_canary_permission_not_granted">Please grant external storage permission, otherwise memory leaks will not be detected.</string>
<string name="leak_canary_permission_notification_title">Leak detektiert, benötige Berechtigung</string>
<string name="leak_canary_permission_notification_text">Hier klicken um Storage Berechtigung für %s zu aktivieren.</string>
</resources>
1 change: 1 addition & 0 deletions src/main/res/values/leak_canary_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<string name="leak_canary_no_leak_title">No leak found</string>
<string name="leak_canary_no_leak_text">The GC was being lazy.</string>
<string name="leak_canary_excluded_row">[Excluded] %s</string>
<string name="leak_canary_permission_not_granted">Please grant external storage permission, otherwise memory leaks will not be detected.</string>
<string name="leak_canary_permission_notification_title">Leak detected, need permission</string>
<string name="leak_canary_permission_notification_text">Click to enable storage permission for %s.</string>
</resources>

0 comments on commit 37b6ab7

Please sign in to comment.