Skip to content

Instantly share code, notes, and snippets.

@resengupta
Created January 30, 2018 05:59
Show Gist options
  • Save resengupta/123b3e92186431932689ac883d49fe1d to your computer and use it in GitHub Desktop.
Save resengupta/123b3e92186431932689ac883d49fe1d to your computer and use it in GitHub Desktop.
public final class DeveloperPreferenceUtils {
private DeveloperPreferenceUtils() {
// hide constructor
}
public static final String PREF_KEY_REMOTE_CONFIG_URL = "pref_key_remote_config_url";
public static final String PREF_KEY_ENABLE_WIP = "pref_key_enable_wip";
public static final String PREF_KEY_ENABLE_EDIT_TOKEN = "pref_key_enable_edit_token";
public static final String PREF_KEY_CLEAR_DATABASE = "pref_key_clear_database";
public static String getDebugConfigUrl(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(
PREF_KEY_REMOTE_CONFIG_URL, HttpConstants.Endpoints.getBaseUrl());
}
public static boolean isWIPFeaturesEnabled(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(PREF_KEY_ENABLE_WIP, false);
}
public static boolean isEditTokenEnabled(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(PREF_KEY_ENABLE_EDIT_TOKEN, false);
}
public static String getRefreshToken(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(PREF_KEY_REFRESH_TOKEN, null);
}
public static String getAccessToken(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(PREF_KEY_ACCESS_TOKEN, null);
}
}
/**
* Presents a set of application settings for use during development.
*/
@SuppressWarnings("squid:MaximumInheritanceDepth")
public class DeveloperSettingsActivity extends BaseActivity<NoViewModel> {
public static Intent startIntent(Context context) {
return new Intent(context, DeveloperSettingsActivity.class);
}
@Override
public NoViewModel initializeViewModel() {
return new NoViewModel();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (BuildConfig.IS_PRODUCTION) {
throw new IllegalStateException("Access denied. DeveloperSettingActivity not available for this build.");
}
if (savedInstanceState == null) {
final GeneralPreferenceFragment fragment = new GeneralPreferenceFragment();
getFragmentManager().beginTransaction()
.add(android.R.id.content, fragment)
.commit();
}
}
public static class GeneralPreferenceFragment extends PreferenceFragment implements
Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_developer_settings);
// setup Remote Config URL preference
final ListPreference remoteConfigUrlPreference = (ListPreference) findPreference(
DeveloperPreferenceUtils.PREF_KEY_REMOTE_CONFIG_URL);
remoteConfigUrlPreference.setOnPreferenceChangeListener(this);
initPreferenceSummary(
remoteConfigUrlPreference, HttpConstants.Endpoints.getBaseUrl());
final CheckBoxPreference wipFeaturePreference = (CheckBoxPreference)
findPreference(DeveloperPreferenceUtils.PREF_KEY_ENABLE_WIP);
wipFeaturePreference.setOnPreferenceChangeListener(this);
final Preference databaseClearPreference =
findPreference(DeveloperPreferenceUtils.PREF_KEY_CLEAR_DATABASE);
databaseClearPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
DatabaseManager.get().clearDB();
Snackbar.make(getView(), R.string.pref_data_base_clear,
Snackbar.LENGTH_SHORT).show();
return true;
}
});
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (DeveloperPreferenceUtils.PREF_KEY_REMOTE_CONFIG_URL.equals(
preference.getKey())) {
updatePreferenceSummary(preference, newValue);
Snackbar.make(getView(), R.string.pref_updated,
Snackbar.LENGTH_SHORT).show();
}
if (DeveloperPreferenceUtils.PREF_KEY_ENABLE_WIP.equals(preference.getKey())) {
Snackbar.make(getView(), R.string.pref_updated,
Snackbar.LENGTH_SHORT).show();
}
return true;
}
private static void initPreferenceSummary(Preference preference, String defValue) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
preference.getContext());
final String key = preference.getKey();
final String value = prefs.getString(key, defValue);
updatePreferenceSummary(preference, value);
}
private static void updatePreferenceSummary(Preference preference, Object value) {
final String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
final ListPreference listPreference = (ListPreference) preference;
final int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
}
}
}
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/pref_title_config">
<ListPreference
android:entries="@array/pref_entries_remote_config_url"
android:entryValues="@array/pref_entry_values_remote_config_url"
android:key="pref_key_remote_config_url"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_remote_config_url" />
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_key_enable_wip"
android:summary="Select to Enable WIP Features"
android:title="Enable WIP" />
<Preference
android:key="pref_key_clear_database"
android:summary="Hope you know what you're doing!"
android:title="Clear Database" />
</PreferenceCategory>
</PreferenceScreen>
<resources>
<string name="pref_title_config">GENERAL CONFIGURATION</string>
<string name="pref_title_network_config">NETWORK CONFIGURATION</string>
<string name="pref_title_remote_config_url">Remote Config Url</string>
<string name="pref_updated">It is highly recommended you restart the application before proceeding.</string>
<string name="pref_data_base_clear">Database Deleted</string>
<string-array name="pref_entry_values_remote_config_url">
<item>https://xom-cloud-int-mdta-common-acc.cloudhub.io/api/v2/exxonMobil/delivery/</item>
<item>https://xom-cloud-int-mdta-common-dev.cloudhub.io/api/v2/exxonMobil/delivery/</item>
<item>https://xom-cloud-int-mdta-common-prd.cloudhub.io/api/v2/exxonMobil/delivery/</item>
</string-array>
<string-array name="pref_entries_remote_config_url">
<item>Acceptance</item>
<item>Development</item>
<item>Production</item>
</string-array>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment