2017-08-07 06:02:30 -07:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-02-10 11:07:17 -08:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.support.annotation.NonNull;
|
2017-08-07 06:02:30 -07:00
|
|
|
import android.support.multidex.MultiDex;
|
|
|
|
|
|
|
|
import com.facebook.stetho.Stetho;
|
2018-02-20 16:24:43 +01:00
|
|
|
import com.facebook.stetho.okhttp3.StethoInterceptor;
|
2018-02-10 11:07:17 -08:00
|
|
|
import com.squareup.leakcanary.AndroidHeapDumper;
|
|
|
|
import com.squareup.leakcanary.DefaultLeakDirectoryProvider;
|
|
|
|
import com.squareup.leakcanary.HeapDumper;
|
2018-02-08 12:46:54 -08:00
|
|
|
import com.squareup.leakcanary.LeakCanary;
|
2018-02-10 11:07:17 -08:00
|
|
|
import com.squareup.leakcanary.LeakDirectoryProvider;
|
2018-02-08 12:46:54 -08:00
|
|
|
import com.squareup.leakcanary.RefWatcher;
|
|
|
|
|
2018-02-10 11:07:17 -08:00
|
|
|
import java.io.File;
|
2018-02-08 12:46:54 -08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-08-07 06:02:30 -07:00
|
|
|
|
2018-02-20 16:24:43 +01:00
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
2017-08-07 06:02:30 -07:00
|
|
|
public class DebugApp extends App {
|
|
|
|
private static final String TAG = DebugApp.class.toString();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void attachBaseContext(Context base) {
|
|
|
|
super.attachBaseContext(base);
|
|
|
|
MultiDex.install(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
initStetho();
|
2018-02-20 16:24:43 +01:00
|
|
|
Downloader.client = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).readTimeout(30, TimeUnit.SECONDS).build();
|
2017-08-07 06:02:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initStetho() {
|
|
|
|
// Create an InitializerBuilder
|
|
|
|
Stetho.InitializerBuilder initializerBuilder =
|
|
|
|
Stetho.newInitializerBuilder(this);
|
|
|
|
|
|
|
|
// Enable Chrome DevTools
|
|
|
|
initializerBuilder.enableWebKitInspector(
|
|
|
|
Stetho.defaultInspectorModulesProvider(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Enable command line interface
|
|
|
|
initializerBuilder.enableDumpapp(
|
|
|
|
Stetho.defaultDumperPluginsProvider(getApplicationContext())
|
|
|
|
);
|
|
|
|
|
|
|
|
// Use the InitializerBuilder to generate an Initializer
|
|
|
|
Stetho.Initializer initializer = initializerBuilder.build();
|
|
|
|
|
|
|
|
// Initialize Stetho with the Initializer
|
|
|
|
Stetho.initialize(initializer);
|
|
|
|
}
|
2018-02-08 12:46:54 -08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected RefWatcher installLeakCanary() {
|
|
|
|
return LeakCanary.refWatcher(this)
|
2018-02-10 11:07:17 -08:00
|
|
|
.heapDumper(new ToggleableHeapDumper(this))
|
|
|
|
// give each object 10 seconds to be gc'ed, before leak canary gets nosy on it
|
|
|
|
.watchDelay(10, TimeUnit.SECONDS)
|
2018-02-08 12:46:54 -08:00
|
|
|
.buildAndInstall();
|
|
|
|
}
|
2018-02-10 11:07:17 -08:00
|
|
|
|
|
|
|
public static class ToggleableHeapDumper implements HeapDumper {
|
|
|
|
private final HeapDumper dumper;
|
|
|
|
private final SharedPreferences preferences;
|
|
|
|
private final String dumpingAllowanceKey;
|
|
|
|
|
|
|
|
ToggleableHeapDumper(@NonNull final Context context) {
|
|
|
|
LeakDirectoryProvider leakDirectoryProvider = new DefaultLeakDirectoryProvider(context);
|
|
|
|
this.dumper = new AndroidHeapDumper(context, leakDirectoryProvider);
|
|
|
|
this.preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
this.dumpingAllowanceKey = context.getString(R.string.allow_heap_dumping_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isDumpingAllowed() {
|
|
|
|
return preferences.getBoolean(dumpingAllowanceKey, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public File dumpHeap() {
|
|
|
|
return isDumpingAllowed() ? dumper.dumpHeap() : HeapDumper.RETRY_LATER;
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 06:02:30 -07:00
|
|
|
}
|