Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable ui component by default, enable it at BlockCanary.install() #45

Merged
merged 1 commit into from
Apr 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions blockcanary-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// compile project(':blockcanary-core')
compile 'com.github.moduth:blockcanary-core:1.2.0'
compile project(':blockcanary-core')
}
19 changes: 16 additions & 3 deletions blockcanary-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<manifest package="com.github.moduth.blockcanary.android">

<application/>
<manifest package="com.github.moduth.blockcanary"
xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<activity
android:name=".ui.DisplayBlockActivity"
android:icon="@drawable/block_canary_icon"
android:enabled="false"
android:label="@string/display_activity_label"
android:taskAffinity="com.github.moduth.blockcanary"
android:theme="@style/block_canary_BlockCanary.Base">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@
*/
package com.github.moduth.blockcanary;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.util.Log;

import com.github.moduth.blockcanary.ui.DisplayBlockActivity;
import java.lang.reflect.Constructor;
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;
import static android.content.pm.PackageManager.DONT_KILL_APP;

/**
* Looper thread monitor.
Expand All @@ -33,6 +41,42 @@ 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 @@ -42,11 +86,12 @@ private BlockCanary() {
/**
* Install {@link BlockCanary}
*
* @param context application context
* @param context application context
* @param blockCanaryContext implementation for {@link BlockCanaryContext}
* @return {@link BlockCanary}
*/
public static BlockCanary install(Context context, BlockCanaryContext blockCanaryContext) {
enableDisplayBlockActivity(context);
BlockCanaryContext.init(context, blockCanaryContext);
return get();
}
Expand Down Expand Up @@ -97,11 +142,14 @@ public void upload() {
}

/**
* Record monitor start time to preference, you may use it when after push which tells start BlockCanary.
* Record monitor start time to preference, you may use it when after push which tells start
* BlockCanary.
*/
public void recordStartTime() {
PreferenceManager.getDefaultSharedPreferences(BlockCanaryContext.get().getContext())
.edit().putLong("BlockCanary_StartTime", System.currentTimeMillis()).commit();
.edit()
.putLong("BlockCanary_StartTime", System.currentTimeMillis())
.commit();
}

/**
Expand All @@ -110,11 +158,11 @@ public void recordStartTime() {
* @return true if ended
*/
public boolean isMonitorDurationEnd() {
long startTime = PreferenceManager.getDefaultSharedPreferences(
BlockCanaryContext.get().getContext()).getLong("BlockCanary_StartTime", 0);
return startTime != 0 &&
System.currentTimeMillis() - startTime >
BlockCanaryContext.get().getConfigDuration() * 3600 * 1000;
long startTime =
PreferenceManager.getDefaultSharedPreferences(BlockCanaryContext.get().getContext())
.getLong("BlockCanary_StartTime", 0);
return startTime != 0 && System.currentTimeMillis() - startTime >
BlockCanaryContext.get().getConfigDuration() * 3600 * 1000;
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.moduth.blockcanary;

import java.util.concurrent.ThreadFactory;

/**
* This is intended to only be used with a single thread executor.
*/
final class LeakCanarySingleThreadFactory implements ThreadFactory {

private final String threadName;

LeakCanarySingleThreadFactory(String threadName) {
this.threadName = "LeakCanary-" + threadName;
}

@Override public Thread newThread(Runnable runnable) {
return new Thread(runnable, threadName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.github.moduth.blockcanary.BlockCanaryCore;
import com.github.moduth.blockcanary.ui.R;
import com.github.moduth.blockcanary.R;
import com.github.moduth.blockcanary.log.Block;
import com.github.moduth.blockcanary.log.ProcessUtils;

import java.util.Arrays;

final class BlockDetailAdapter extends BaseAdapter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.github.moduth.blockcanary.LogWriter;
import com.github.moduth.blockcanary.R;
import com.github.moduth.blockcanary.log.Block;
import com.github.moduth.blockcanary.log.BlockCanaryInternals;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.github.moduth.blockcanary.OnBlockEventInterceptor;
import com.github.moduth.blockcanary.ui.R;
import com.github.moduth.blockcanary.R;

import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.os.Build.VERSION.SDK_INT;
Expand Down
1 change: 0 additions & 1 deletion blockcanary-ui/.gitignore

This file was deleted.

26 changes: 0 additions & 26 deletions blockcanary-ui/build.gradle

This file was deleted.

115 changes: 0 additions & 115 deletions blockcanary-ui/gradle-mvn-push.gradle

This file was deleted.

18 changes: 0 additions & 18 deletions blockcanary-ui/gradle.properties

This file was deleted.

Loading