-Added toggle for enabling leak canary heap dump.
This commit is contained in:
parent
622d698ff8
commit
829059ea01
|
@ -1,12 +1,20 @@
|
||||||
package org.schabi.newpipe;
|
package org.schabi.newpipe;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.multidex.MultiDex;
|
import android.support.multidex.MultiDex;
|
||||||
|
|
||||||
import com.facebook.stetho.Stetho;
|
import com.facebook.stetho.Stetho;
|
||||||
|
import com.squareup.leakcanary.AndroidHeapDumper;
|
||||||
|
import com.squareup.leakcanary.DefaultLeakDirectoryProvider;
|
||||||
|
import com.squareup.leakcanary.HeapDumper;
|
||||||
import com.squareup.leakcanary.LeakCanary;
|
import com.squareup.leakcanary.LeakCanary;
|
||||||
|
import com.squareup.leakcanary.LeakDirectoryProvider;
|
||||||
import com.squareup.leakcanary.RefWatcher;
|
import com.squareup.leakcanary.RefWatcher;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class DebugApp extends App {
|
public class DebugApp extends App {
|
||||||
|
@ -49,7 +57,31 @@ public class DebugApp extends App {
|
||||||
@Override
|
@Override
|
||||||
protected RefWatcher installLeakCanary() {
|
protected RefWatcher installLeakCanary() {
|
||||||
return LeakCanary.refWatcher(this)
|
return LeakCanary.refWatcher(this)
|
||||||
.watchDelay(5, TimeUnit.SECONDS)
|
.heapDumper(new ToggleableHeapDumper(this))
|
||||||
|
// give each object 10 seconds to be gc'ed, before leak canary gets nosy on it
|
||||||
|
.watchDelay(10, TimeUnit.SECONDS)
|
||||||
.buildAndInstall();
|
.buildAndInstall();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import android.app.NotificationChannel;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
|
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
|
||||||
|
@ -167,6 +168,7 @@ public class App extends Application {
|
||||||
mNotificationManager.createNotificationChannel(mChannel);
|
mNotificationManager.createNotificationChannel(mChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
public static RefWatcher getRefWatcher(Context context) {
|
public static RefWatcher getRefWatcher(Context context) {
|
||||||
final App application = (App) context.getApplicationContext();
|
final App application = (App) context.getApplicationContext();
|
||||||
return application.refWatcher;
|
return application.refWatcher;
|
||||||
|
|
|
@ -71,8 +71,9 @@ public abstract class BaseFragment extends Fragment {
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|
||||||
RefWatcher refWatcher = App.getRefWatcher(getActivity());
|
RefWatcher refWatcher = App.getRefWatcher(getActivity());
|
||||||
refWatcher.watch(this);
|
if (refWatcher != null) refWatcher.watch(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -211,6 +211,12 @@ public class MainActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onHeapDumpToggled(@NonNull MenuItem item) {
|
||||||
|
final boolean newToggleState = !item.isChecked();
|
||||||
|
sharedPreferences.edit().putBoolean(getString(R.string.allow_heap_dumping_key),
|
||||||
|
newToggleState).apply();
|
||||||
|
item.setChecked(newToggleState);
|
||||||
|
}
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
// Menu
|
// Menu
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
@ -232,6 +238,10 @@ public class MainActivity extends AppCompatActivity {
|
||||||
inflater.inflate(R.menu.main_menu, menu);
|
inflater.inflate(R.menu.main_menu, menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
getMenuInflater().inflate(R.menu.debug_menu, menu);
|
||||||
|
}
|
||||||
|
|
||||||
ActionBar actionBar = getSupportActionBar();
|
ActionBar actionBar = getSupportActionBar();
|
||||||
if (actionBar != null) {
|
if (actionBar != null) {
|
||||||
actionBar.setDisplayHomeAsUpEnabled(false);
|
actionBar.setDisplayHomeAsUpEnabled(false);
|
||||||
|
@ -242,6 +252,17 @@ public class MainActivity extends AppCompatActivity {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||||
|
MenuItem heapDumpToggle = menu.findItem(R.id.action_toggle_heap_dump);
|
||||||
|
if (heapDumpToggle != null) {
|
||||||
|
final boolean isToggled = sharedPreferences.getBoolean(
|
||||||
|
getString(R.string.allow_heap_dumping_key), false);
|
||||||
|
heapDumpToggle.setChecked(isToggled);
|
||||||
|
}
|
||||||
|
return super.onPrepareOptionsMenu(menu);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
if (DEBUG) Log.d(TAG, "onOptionsItemSelected() called with: item = [" + item + "]");
|
if (DEBUG) Log.d(TAG, "onOptionsItemSelected() called with: item = [" + item + "]");
|
||||||
|
@ -262,6 +283,9 @@ public class MainActivity extends AppCompatActivity {
|
||||||
case R.id.action_history:
|
case R.id.action_history:
|
||||||
NavigationHelper.openHistory(this);
|
NavigationHelper.openHistory(this);
|
||||||
return true;
|
return true;
|
||||||
|
case R.id.action_toggle_heap_dump:
|
||||||
|
onHeapDumpToggled(item);
|
||||||
|
return true;
|
||||||
default:
|
default:
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
<item android:id="@+id/action_toggle_heap_dump"
|
||||||
|
android:orderInCategory="9999"
|
||||||
|
android:checkable="true"
|
||||||
|
android:title="@string/toggle_leak_canary"
|
||||||
|
android:visible="true"
|
||||||
|
app:showAsAction="never"/>
|
||||||
|
|
||||||
|
</menu>
|
|
@ -83,6 +83,9 @@
|
||||||
|
|
||||||
<string name="last_orientation_landscape_key" translatable="false">last_orientation_landscape_key</string>
|
<string name="last_orientation_landscape_key" translatable="false">last_orientation_landscape_key</string>
|
||||||
|
|
||||||
|
<!-- DEBUG ONLY -->
|
||||||
|
<string name="allow_heap_dumping_key" translatable="false">allow_heap_dumping_key</string>
|
||||||
|
|
||||||
<!-- THEMES -->
|
<!-- THEMES -->
|
||||||
<string name="theme_key" translatable="false">theme</string>
|
<string name="theme_key" translatable="false">theme</string>
|
||||||
<string name="light_theme_key" translatable="false">light_theme</string>
|
<string name="light_theme_key" translatable="false">light_theme</string>
|
||||||
|
|
|
@ -395,4 +395,7 @@
|
||||||
<string name="playlist_add_stream_success">Added to playlist</string>
|
<string name="playlist_add_stream_success">Added to playlist</string>
|
||||||
<string name="playlist_thumbnail_change_success">Playlist thumbnail changed</string>
|
<string name="playlist_thumbnail_change_success">Playlist thumbnail changed</string>
|
||||||
<string name="playlist_delete_failure">Failed to delete playlist</string>
|
<string name="playlist_delete_failure">Failed to delete playlist</string>
|
||||||
|
|
||||||
|
<!-- Debug Only -->
|
||||||
|
<string name="toggle_leak_canary">Leak Canary</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in New Issue