Skip to content

Commit

Permalink
fix init error, change method modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
markzhai committed Apr 21, 2016
1 parent 28c7184 commit 2c05ff0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
package com.github.moduth.blockcanary;

import android.content.Context;
import android.util.Log;

/**
* No-op implementation.
*/
public final class BlockCanary {

private static final String TAG = "BlockCanary-no-op";
private static BlockCanary sInstance = null;

private BlockCanary() {
Expand All @@ -42,22 +44,22 @@ public static BlockCanary get() {
}

public void start() {
throw new UnsupportedOperationException();
Log.i(TAG, "start");
}

public void stop() {
throw new UnsupportedOperationException();
Log.i(TAG, "stop");
}

public void upload() {
throw new UnsupportedOperationException();
Log.i(TAG, "upload");
}

public void recordStartTime() {
throw new UnsupportedOperationException();
Log.i(TAG, "recordStartTime");
}

public boolean isMonitorDurationEnd() {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,6 @@ public final class BlockCanary {
private BlockCanaryCore mBlockCanaryCore;
private boolean mLooperLoggingStarted = false;

// these lines are originally copied from LeakCanary: Copyright (C) 2015 Square, Inc.
private static final Executor fileIoExecutor = newSingleThreadExecutor("File-IO");

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

public static Executor newSingleThreadExecutor(String threadName) {
return Executors.newSingleThreadExecutor(new LeakCanarySingleThreadFactory(threadName));
}

public static void enableDisplayBlockActivity(Context context) {
setEnabled(context, DisplayBlockActivity.class, true);
}

public static void setEnabled(Context context, final Class<?> componentClass,
final boolean enabled) {
final Context appContext = context.getApplicationContext();
executeOnFileIoThread(new Runnable() {
@Override
public void run() {
setEnabledBlocking(appContext, componentClass, enabled);
}
});
}

public static void setEnabledBlocking(Context appContext, Class<?> componentClass,
boolean enabled) {
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);
}
// end of lines copied from LeakCanary

private BlockCanary() {
BlockCanaryCore.setIBlockCanaryContext(BlockCanaryContext.get());
mBlockCanaryCore = BlockCanaryCore.get();
Expand All @@ -91,8 +55,8 @@ private BlockCanary() {
* @return {@link BlockCanary}
*/
public static BlockCanary install(Context context, BlockCanaryContext blockCanaryContext) {
enableDisplayBlockActivity(context);
BlockCanaryContext.init(context, blockCanaryContext);
setEnabled(context, DisplayBlockActivity.class, BlockCanaryContext.get().isNeedDisplay());
return get();
}

Expand Down Expand Up @@ -182,4 +146,36 @@ private void initNotification() {
Log.e(TAG, "initNotification: ", e);
}
}

// these lines are originally copied from LeakCanary: Copyright (C) 2015 Square, Inc.
private static final Executor fileIoExecutor = newSingleThreadExecutor("File-IO");

private static void setEnabledBlocking(Context appContext, Class<?> componentClass,
boolean enabled) {
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);
}
// end of lines copied from LeakCanary

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

private static Executor newSingleThreadExecutor(String threadName) {
return Executors.newSingleThreadExecutor(new LeakCanarySingleThreadFactory(threadName));
}

private static void setEnabled(Context context, final Class<?> componentClass,
final boolean enabled) {
final Context appContext = context.getApplicationContext();
executeOnFileIoThread(new Runnable() {
@Override
public void run() {
setEnabledBlocking(appContext, componentClass, enabled);
}
});
}
}

0 comments on commit 2c05ff0

Please sign in to comment.