Browse Source

Add debug settings so that you can toggle LeakCanary (and other settings in the future) in debug

master
Anthony Restaino 9 years ago
parent
commit
b1a8b7a0d5
  1. 17
      app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java
  2. 5
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  3. 16
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  4. 57
      app/src/main/java/acr/browser/lightning/fragment/DebugSettingsFragment.java
  5. 10
      app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java
  6. 5
      app/src/main/res/values/strings.xml
  7. 9
      app/src/main/res/xml/preference_debug.xml
  8. 3
      app/src/main/res/xml/preferences_headers.xml

17
app/src/main/java/acr/browser/lightning/activity/SettingsActivity.java

@ -15,13 +15,15 @@ import android.widget.LinearLayout; @@ -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<String> mFragments = new ArrayList<>(6);
private static final List<String> mFragments = new ArrayList<>(7);
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -45,14 +47,25 @@ public class SettingsActivity extends ThemableSettingsActivity { @@ -45,14 +47,25 @@ public class SettingsActivity extends ThemableSettingsActivity {
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preferences_headers, target);
mFragments.clear();
for (Header header : target) {
Iterator<Header> 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;
}
if (header.titleRes == R.string.debug_title) {
if (BrowserApp.isRelease()) {
headerIterator.remove();
} else {
mFragments.add(header.fragment);
}
} else {
mFragments.add(header.fragment);
}
}
}
@Override
protected boolean isValidFragment(String fragmentName) {

5
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -1,5 +1,7 @@ @@ -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; @@ -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 { @@ -67,4 +70,6 @@ public interface AppComponent {
void inject(TabsManager manager);
void inject(DebugSettingsFragment fragment);
}

16
app/src/main/java/acr/browser/lightning/app/BrowserApp.java

@ -15,6 +15,7 @@ import java.util.concurrent.Executors; @@ -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 { @@ -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);
if (mPreferenceManager.getUseLeakCanary() && !isRelease()) {
LeakCanary.install(this);
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
}
if (!isRelease() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
}
@ -58,4 +63,13 @@ public class BrowserApp extends Application { @@ -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");
}
}

57
app/src/main/java/acr/browser/lightning/fragment/DebugSettingsFragment.java

@ -0,0 +1,57 @@ @@ -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;
}
}

10
app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java

@ -61,6 +61,8 @@ public class PreferenceManager { @@ -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 { @@ -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:
* <ul>

5
app/src/main/res/values/strings.xml

@ -237,4 +237,9 @@ @@ -237,4 +237,9 @@
<string name="faq">FAQ</string>
<string name="faq_description">Frequently Asked Questions</string>
<!-- debug strings -->
<string name="debug_title">Debug Settings</string>
<string name="debug_leak_canary">LeakCanary</string>
<string name="app_restart">Please restart the app for the change to take effect.</string>
</resources>

9
app/src/main/res/xml/preference_debug.xml

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/debug_title">
<SwitchPreference
android:defaultValue="false"
android:key="leak_canary_enabled"
android:title="@string/debug_leak_canary"/>
</PreferenceCategory>
</PreferenceScreen>

3
app/src/main/res/xml/preferences_headers.xml

@ -30,4 +30,7 @@ @@ -30,4 +30,7 @@
android:value="SELF"/>
</intent>
</header>
<header
android:fragment="acr.browser.lightning.fragment.DebugSettingsFragment"
android:title="@string/debug_title"/>
</preference-headers>
Loading…
Cancel
Save