Skip to content

Commit

Permalink
Upgrade LeakCanary
Browse files Browse the repository at this point in the history
  • Loading branch information
wb9688 committed May 25, 2020
1 parent 7231150 commit 0cae58c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 138 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ext {
icepickVersion = '3.2.0'
checkstyleVersion = '8.32'
stethoVersion = '1.5.1'
leakCanaryVersion = '1.5.4'
leakCanaryVersion = '2.2'
exoPlayerVersion = '2.11.4'
androidxLifecycleVersion = '2.2.0'
androidxRoomVersion = '2.2.5'
Expand Down Expand Up @@ -150,7 +150,7 @@ dependencies {
debugImplementation "com.facebook.stetho:stetho-okhttp3:${stethoVersion}"

debugImplementation "com.squareup.leakcanary:leakcanary-android:${leakCanaryVersion}"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:${leakCanaryVersion}"
implementation "com.squareup.leakcanary:leakcanary-object-watcher-android:${leakCanaryVersion}"

debugImplementation "androidx.multidex:multidex:2.0.1"

Expand Down
107 changes: 0 additions & 107 deletions app/src/debug/java/org/schabi/newpipe/DebugApp.java

This file was deleted.

59 changes: 59 additions & 0 deletions app/src/debug/java/org/schabi/newpipe/DebugApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.schabi.newpipe

import android.content.Context
import androidx.multidex.MultiDex
import androidx.preference.PreferenceManager
import com.facebook.stetho.Stetho
import com.facebook.stetho.okhttp3.StethoInterceptor
import leakcanary.AppWatcher
import leakcanary.LeakCanary
import okhttp3.OkHttpClient
import org.schabi.newpipe.extractor.downloader.Downloader

class DebugApp : App() {
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}

override fun onCreate() {
super.onCreate()
initStetho()

// Give each object 10 seconds to be GC'ed, before LeakCanary gets nosy on it
AppWatcher.config = AppWatcher.config.copy(watchDurationMillis = 10000)
LeakCanary.config = LeakCanary.config.copy(dumpHeap = PreferenceManager
.getDefaultSharedPreferences(this).getBoolean(getString(
R.string.allow_heap_dumping_key), false))
}

override fun getDownloader(): Downloader {
val downloader = DownloaderImpl.init(OkHttpClient.Builder()
.addNetworkInterceptor(StethoInterceptor()))
setCookiesToDownloader(downloader)
return downloader
}

private fun initStetho() {
// Create an InitializerBuilder
val initializerBuilder = Stetho.newInitializerBuilder(this)

// Enable Chrome DevTools
initializerBuilder.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))

// Enable command line interface
initializerBuilder.enableDumpapp(
Stetho.defaultDumperPluginsProvider(applicationContext))

// Use the InitializerBuilder to generate an Initializer
val initializer = initializerBuilder.build()

// Initialize Stetho with the Initializer
Stetho.initialize(initializer)
}

override fun isDisposedRxExceptionsReported(): Boolean {
return PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(getString(R.string.allow_disposed_exceptions_key), false)
}
}
21 changes: 0 additions & 21 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import android.os.Build;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;

import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

import org.acra.ACRA;
import org.acra.config.ACRAConfiguration;
Expand Down Expand Up @@ -72,13 +69,6 @@ public class App extends Application {
private static final Class<? extends ReportSenderFactory>[]
REPORT_SENDER_FACTORY_CLASSES = new Class[]{AcraReportSenderFactory.class};
private static App app;
private RefWatcher refWatcher;

@Nullable
public static RefWatcher getRefWatcher(final Context context) {
final App application = (App) context.getApplicationContext();
return application.refWatcher;
}

public static App getApp() {
return app;
Expand All @@ -95,13 +85,6 @@ protected void attachBaseContext(final Context base) {
public void onCreate() {
super.onCreate();

if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
refWatcher = installLeakCanary();

app = this;

// Initialize settings first because others inits can use its values
Expand Down Expand Up @@ -280,10 +263,6 @@ private void setUpUpdateNotificationChannel(final int importance) {
appUpdateNotificationManager.createNotificationChannel(appUpdateChannel);
}

protected RefWatcher installLeakCanary() {
return RefWatcher.DISABLED;
}

protected boolean isDisposedRxExceptionsReported() {
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/org/schabi/newpipe/BaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import androidx.fragment.app.FragmentManager;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.squareup.leakcanary.RefWatcher;

import icepick.Icepick;
import icepick.State;
import leakcanary.AppWatcher;

public abstract class BaseFragment extends Fragment {
public static final ImageLoader IMAGE_LOADER = ImageLoader.getInstance();
Expand Down Expand Up @@ -78,16 +78,14 @@ public void onSaveInstanceState(final Bundle outState) {
Icepick.saveInstanceState(this, outState);
}

protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) { }
protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
}

@Override
public void onDestroy() {
super.onDestroy();

RefWatcher refWatcher = App.getRefWatcher(getActivity());
if (refWatcher != null) {
refWatcher.watch(this);
}
AppWatcher.INSTANCE.getObjectWatcher().watch(this);
}

@Override
Expand All @@ -100,9 +98,11 @@ public void setUserVisibleHint(final boolean isVisibleToUser) {
// Init
//////////////////////////////////////////////////////////////////////////*/

protected void initViews(final View rootView, final Bundle savedInstanceState) { }
protected void initViews(final View rootView, final Bundle savedInstanceState) {
}

protected void initListeners() { }
protected void initListeners() {
}

/*//////////////////////////////////////////////////////////////////////////
// Utils
Expand Down

0 comments on commit 0cae58c

Please sign in to comment.