Skip to content

Commit

Permalink
Fix permission crash for DisplayLeakActivity on M
Browse files Browse the repository at this point in the history
  • Loading branch information
pyricau committed Mar 30, 2016
1 parent 37cb2b7 commit 131a8ae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public AndroidHeapDumper(Context context, LeakDirectoryProvider leakDirectoryPro
@Override public File dumpHeap() {
if (!leakDirectoryProvider.isLeakStorageWritable()) {
CanaryLog.d("Could not write to leak storage to dump heap.");
leakDirectoryProvider.requestWritePermission();
leakDirectoryProvider.requestWritePermissionNotification();
return NO_DUMP;
}
File heapDumpFile = getHeapDumpFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.squareup.leakcanary;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Environment;
Expand Down Expand Up @@ -48,7 +49,7 @@ public DefaultLeakDirectoryProvider(Context context) {
return directory;
}

@Override public void requestWritePermission() {
@Override public void requestWritePermissionNotification() {
if (hasStoragePermission()) {
return;
}
Expand All @@ -60,6 +61,16 @@ public DefaultLeakDirectoryProvider(Context context) {
showNotification(context, contentTitle, contentText, pendingIntent);
}

@TargetApi(M) @Override public void requestPermission(Activity activity) {
if (hasStoragePermission()) {
return;
}
String[] permissions = {
WRITE_EXTERNAL_STORAGE
};
activity.requestPermissions(permissions, 42);
}

@Override public boolean isLeakStorageWritable() {
if (!hasStoragePermission()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.squareup.leakcanary;

import android.app.Activity;
import java.io.File;

/**
Expand All @@ -27,7 +28,9 @@ public interface LeakDirectoryProvider {
/** Returns a path to an existing directory were leaks can be stored. */
File leakDirectory();

void requestWritePermission();
void requestWritePermissionNotification();

void requestPermission(Activity activity);

/** True if we can currently write to the leak directory. */
boolean isLeakStorageWritable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ public static void setLeakDirectoryProvider(LeakDirectoryProvider leakDirectoryP
DisplayLeakActivity.leakDirectoryProvider = leakDirectoryProvider;
}

static File getLeakDirectory(Context context) {
private static LeakDirectoryProvider leakDirectoryProvider(Context context) {
LeakDirectoryProvider leakDirectoryProvider = DisplayLeakActivity.leakDirectoryProvider;
if (leakDirectoryProvider != null) {
return leakDirectoryProvider.leakDirectory();
} else {
return new DefaultLeakDirectoryProvider(context).leakDirectory();
if (leakDirectoryProvider == null) {
leakDirectoryProvider = new DefaultLeakDirectoryProvider(context);
}
return leakDirectoryProvider;
}

// null until it's been first loaded.
Expand Down Expand Up @@ -141,7 +140,13 @@ static File getLeakDirectory(Context context) {

@Override protected void onResume() {
super.onResume();
LoadLeaks.load(this);
LeakDirectoryProvider leakDirectoryProvider = leakDirectoryProvider(this);
if (leakDirectoryProvider.isLeakStorageWritable()) {
File leakDirectory = leakDirectoryProvider.leakDirectory();
LoadLeaks.load(this, leakDirectory);
} else {
leakDirectoryProvider.requestPermission(this);
}
}

@Override public void setTheme(int resid) {
Expand Down Expand Up @@ -237,7 +242,7 @@ void deleteVisibleLeak() {
}

void deleteAllLeaks() {
File leakDirectory = getLeakDirectory(DisplayLeakActivity.this);
File leakDirectory = leakDirectoryProvider(DisplayLeakActivity.this).leakDirectory();
File[] files = leakDirectory.listFiles();
if (files != null) {
for (File file : files) {
Expand Down Expand Up @@ -428,8 +433,8 @@ static class LoadLeaks implements Runnable {

static final Executor backgroundExecutor = newSingleThreadExecutor("LoadLeaks");

static void load(DisplayLeakActivity activity) {
LoadLeaks loadLeaks = new LoadLeaks(activity);
static void load(DisplayLeakActivity activity, File leakDirectory) {
LoadLeaks loadLeaks = new LoadLeaks(activity, leakDirectory);
inFlight.add(loadLeaks);
backgroundExecutor.execute(loadLeaks);
}
Expand All @@ -445,9 +450,9 @@ static void forgetActivity() {
private final File leakDirectory;
private final Handler mainHandler;

LoadLeaks(DisplayLeakActivity activity) {
LoadLeaks(DisplayLeakActivity activity, File leakDirectory) {
this.activityOrNull = activity;
leakDirectory = getLeakDirectory(activity);
this.leakDirectory = leakDirectory;
mainHandler = new Handler(Looper.getMainLooper());
}

Expand Down

0 comments on commit 131a8ae

Please sign in to comment.