Skip to content

Commit

Permalink
Fix Strict mode violations
Browse files Browse the repository at this point in the history
* Enable Strict Mode thread policy in the sample app.
* Fixed violation when cleaning up files on startup.
* Fixed violation when enabling components, the other side of the binder does IO operations.

Fixes #15
  • Loading branch information
pyricau committed May 12, 2015
1 parent 131cc3f commit 2fe93c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.os.Debug;
import android.util.Log;
import com.squareup.leakcanary.internal.LeakCanaryInternals;
import java.io.File;
import java.io.IOException;

Expand Down Expand Up @@ -57,13 +58,17 @@ private File getHeapDumpFile() {
* the app process was killed.
*/
public void cleanup() {
if (isExternalStorageWritable()) {
Log.d(TAG, "Could not attempt cleanup, external storage not mounted.");
}
File heapDumpFile = getHeapDumpFile();
if (heapDumpFile.exists()) {
Log.d(TAG, "Previous analysis did not complete correctly, cleaning: " + heapDumpFile);
heapDumpFile.delete();
}
LeakCanaryInternals.executeOnFileIoThread(new Runnable() {
@Override public void run() {
if (isExternalStorageWritable()) {
Log.d(TAG, "Could not attempt cleanup, external storage not mounted.");
}
File heapDumpFile = getHeapDumpFile();
if (heapDumpFile.exists()) {
Log.d(TAG, "Previous analysis did not complete correctly, cleaning: " + heapDumpFile);
heapDumpFile.delete();
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Expand All @@ -34,6 +36,12 @@

public final class LeakCanaryInternals {

private static final Executor fileIoExecutor = Executors.newSingleThreadExecutor();

public static void executeOnFileIoThread(Runnable runnable) {
fileIoExecutor.execute(runnable);
}

public static File storageDirectory() {
File downloadsDirectory = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
File leakCanaryDirectory = new File(downloadsDirectory, "leakcanary");
Expand Down Expand Up @@ -78,12 +86,18 @@ public static String classSimpleName(String className) {
}
}

public static void setEnabled(Context context, Class<?> componentClass, boolean enabled) {
ComponentName component = new ComponentName(context, componentClass);
PackageManager packageManager = context.getPackageManager();
int newState = enabled ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED;
// Blocks on IPC.
packageManager.setComponentEnabledSetting(component, newState, DONT_KILL_APP);
public static void setEnabled(Context context, final Class<?> componentClass,
final boolean enabled) {
final Context appContext = context.getApplicationContext();
executeOnFileIoThread(new Runnable() {
@Override public void run() {
ComponentName component = new ComponentName(appContext, componentClass);
PackageManager packageManager = appContext.getPackageManager();
int newState = enabled ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED;
// Blocks on IPC.
packageManager.setComponentEnabledSetting(component, newState, DONT_KILL_APP);
}
});
}

public static boolean isInServiceProcess(Context context, Class<? extends Service> serviceClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@
package com.example.leakcanary;

import android.app.Application;
import android.content.Context;
import android.os.StrictMode;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

public class ExampleApplication extends Application {

public static RefWatcher getRefWatcher(Context context) {
ExampleApplication application = (ExampleApplication) context.getApplicationContext();
return application.refWatcher;
}

private RefWatcher refWatcher;

@Override public void onCreate() {
super.onCreate();
refWatcher = LeakCanary.install(this);
enabledStrictMode();
LeakCanary.install(this);
}

private void enabledStrictMode() {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() //
.detectAll() //
.penaltyLog() //
.penaltyDeath() //
.build());
}
}

0 comments on commit 2fe93c4

Please sign in to comment.