From b1a8b7a0d50b3ca3bdd8126af9eaa43bdf45490c Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Mon, 9 May 2016 21:52:18 -0400 Subject: [PATCH] Add debug settings so that you can toggle LeakCanary (and other settings in the future) in debug --- .../lightning/activity/SettingsActivity.java | 19 ++++++- .../browser/lightning/app/AppComponent.java | 5 ++ .../acr/browser/lightning/app/BrowserApp.java | 18 +++++- .../fragment/DebugSettingsFragment.java | 57 +++++++++++++++++++ .../preference/PreferenceManager.java | 10 ++++ app/src/main/res/values/strings.xml | 5 ++ app/src/main/res/xml/preference_debug.xml | 9 +++ app/src/main/res/xml/preferences_headers.xml | 3 + 8 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/acr/browser/lightning/fragment/DebugSettingsFragment.java create mode 100644 app/src/main/res/xml/preference_debug.xml diff --git a/app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java b/app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java index f9c82ce..6fcdb29 100644 --- a/app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java @@ -15,13 +15,15 @@ import android.widget.LinearLayout; import com.anthonycr.grant.PermissionsManager; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import acr.browser.lightning.R; +import acr.browser.lightning.app.BrowserApp; public class SettingsActivity extends ThemableSettingsActivity { - private static final List mFragments = new ArrayList<>(6); + private static final List mFragments = new ArrayList<>(7); @Override protected void onCreate(Bundle savedInstanceState) { @@ -45,12 +47,23 @@ public class SettingsActivity extends ThemableSettingsActivity { public void onBuildHeaders(List
target) { loadHeadersFromResource(R.xml.preferences_headers, target); mFragments.clear(); - for (Header header : target) { + Iterator
headerIterator = target.iterator(); + while (headerIterator.hasNext()) { + Header header = headerIterator.next(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // Workaround for bug in the AppCompat support library header.iconRes = R.drawable.empty; } - mFragments.add(header.fragment); + + if (header.titleRes == R.string.debug_title) { + if (BrowserApp.isRelease()) { + headerIterator.remove(); + } else { + mFragments.add(header.fragment); + } + } else { + mFragments.add(header.fragment); + } } } diff --git a/app/src/main/java/acr/browser/lightning/app/AppComponent.java b/app/src/main/java/acr/browser/lightning/app/AppComponent.java index 0f8187e..2522ce1 100644 --- a/app/src/main/java/acr/browser/lightning/app/AppComponent.java +++ b/app/src/main/java/acr/browser/lightning/app/AppComponent.java @@ -1,5 +1,7 @@ package acr.browser.lightning.app; +import android.preference.PreferenceFragment; + import javax.inject.Singleton; import acr.browser.lightning.activity.BrowserActivity; @@ -13,6 +15,7 @@ import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.download.LightningDownloadListener; import acr.browser.lightning.fragment.BookmarkSettingsFragment; import acr.browser.lightning.fragment.BookmarksFragment; +import acr.browser.lightning.fragment.DebugSettingsFragment; import acr.browser.lightning.fragment.LightningPreferenceFragment; import acr.browser.lightning.fragment.PrivacySettingsFragment; import acr.browser.lightning.fragment.TabsFragment; @@ -67,4 +70,6 @@ public interface AppComponent { void inject(TabsManager manager); + void inject(DebugSettingsFragment fragment); + } diff --git a/app/src/main/java/acr/browser/lightning/app/BrowserApp.java b/app/src/main/java/acr/browser/lightning/app/BrowserApp.java index f0689d6..110f4aa 100644 --- a/app/src/main/java/acr/browser/lightning/app/BrowserApp.java +++ b/app/src/main/java/acr/browser/lightning/app/BrowserApp.java @@ -15,6 +15,7 @@ import java.util.concurrent.Executors; import javax.inject.Inject; import acr.browser.lightning.BuildConfig; +import acr.browser.lightning.preference.PreferenceManager; public class BrowserApp extends Application { @@ -23,14 +24,18 @@ public class BrowserApp extends Application { private static final Executor mTaskThread = Executors.newCachedThreadPool(); @Inject Bus mBus; + @Inject PreferenceManager mPreferenceManager; @Override public void onCreate() { super.onCreate(); mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build(); mAppComponent.inject(this); - LeakCanary.install(this); - if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + + if (mPreferenceManager.getUseLeakCanary() && !isRelease()) { + LeakCanary.install(this); + } + if (!isRelease() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true); } } @@ -58,4 +63,13 @@ public class BrowserApp extends Application { return get(context).mBus; } + /** + * Determines whether this is a release build. + * + * @return true if this is a release build, false otherwise. + */ + public static boolean isRelease() { + return !BuildConfig.DEBUG || BuildConfig.BUILD_TYPE.toLowerCase().equals("release"); + } + } diff --git a/app/src/main/java/acr/browser/lightning/fragment/DebugSettingsFragment.java b/app/src/main/java/acr/browser/lightning/fragment/DebugSettingsFragment.java new file mode 100644 index 0000000..dba40b7 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/fragment/DebugSettingsFragment.java @@ -0,0 +1,57 @@ +package acr.browser.lightning.fragment; + +import android.app.Activity; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import android.preference.SwitchPreference; +import android.support.annotation.NonNull; + +import javax.inject.Inject; + +import acr.browser.lightning.R; +import acr.browser.lightning.app.BrowserApp; +import acr.browser.lightning.preference.PreferenceManager; +import acr.browser.lightning.utils.Utils; + +public class DebugSettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { + + private static final String LEAK_CANARY = "leak_canary_enabled"; + + @Inject PreferenceManager mPreferenceManager; + + private SwitchPreference mSwitchLeakCanary; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + BrowserApp.getAppComponent().inject(this); + addPreferencesFromResource(R.xml.preference_debug); + + mSwitchLeakCanary = (SwitchPreference) findPreference(LEAK_CANARY); + mSwitchLeakCanary.setChecked(mPreferenceManager.getUseLeakCanary()); + mSwitchLeakCanary.setOnPreferenceChangeListener(this); + } + + + @Override + public boolean onPreferenceClick(@NonNull Preference preference) { + return false; + } + + @Override + public boolean onPreferenceChange(@NonNull Preference preference, @NonNull Object newValue) { + switch (preference.getKey()) { + case LEAK_CANARY: + boolean value = Boolean.TRUE.equals(newValue); + mPreferenceManager.setUseLeakCanary(value); + Activity activity = getActivity(); + if (activity != null) { + Utils.showSnackbar(activity, R.string.app_restart); + } + mSwitchLeakCanary.setChecked(value); + return true; + } + return false; + } +} diff --git a/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java b/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java index b0e9af0..e5a418c 100644 --- a/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java +++ b/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java @@ -61,6 +61,8 @@ public class PreferenceManager { public static final String USE_PROXY_PORT = "useProxyPort"; public static final String INITIAL_CHECK_FOR_TOR = "checkForTor"; public static final String INITIAL_CHECK_FOR_I2P = "checkForI2P"; + + public static final String LEAK_CANARY = "leakCanary"; } @NonNull private final SharedPreferences mPrefs; @@ -423,6 +425,14 @@ public class PreferenceManager { putInt(Name.THEME, theme); } + public void setUseLeakCanary(boolean useLeakCanary) { + putBoolean(Name.LEAK_CANARY, useLeakCanary); + } + + public boolean getUseLeakCanary() { + return mPrefs.getBoolean(Name.LEAK_CANARY, false); + } + /** * Valid choices: *
    diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 63199d1..7b4b403 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -237,4 +237,9 @@ FAQ Frequently Asked Questions + + + Debug Settings + LeakCanary + Please restart the app for the change to take effect. diff --git a/app/src/main/res/xml/preference_debug.xml b/app/src/main/res/xml/preference_debug.xml new file mode 100644 index 0000000..b6b49f6 --- /dev/null +++ b/app/src/main/res/xml/preference_debug.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/preferences_headers.xml b/app/src/main/res/xml/preferences_headers.xml index 6070001..ebbae41 100644 --- a/app/src/main/res/xml/preferences_headers.xml +++ b/app/src/main/res/xml/preferences_headers.xml @@ -30,4 +30,7 @@ android:value="SELF"/>
+
\ No newline at end of file