1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-06-26 13:34:52 +02:00

Include String key as parameter in AppSettingsBase

This commit is contained in:
Gregor Santner 2017-08-29 14:44:43 +02:00
parent e9fa2082d9
commit 05d6efd107
No known key found for this signature in database
GPG key ID: 7E83A7834AECB009
4 changed files with 313 additions and 222 deletions

View file

@ -56,7 +56,7 @@ public class ThemedColorPickerPreference extends Preference implements Themeable
} else if ((appSettings.isKeyEqual(key, R.string.pref_key__accent_color_shade))) { } else if ((appSettings.isKeyEqual(key, R.string.pref_key__accent_color_shade))) {
color = appSettings.getAccentColor(); color = appSettings.getAccentColor();
} else { } else {
color = appSettings.getColor(getSharedPreferences(), key, color); color = appSettings.getColor(key, color, getSharedPreferences());
} }
circle.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); circle.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
} }

View file

@ -77,19 +77,19 @@ public class AppSettings extends AppSettingsBase {
//## Getter & Setter for settings //## Getter & Setter for settings
//################################# //#################################
public String getProfileId() { public String getProfileId() {
return getString(prefPod, R.string.pref_key__podprofile_id, ""); return getString(R.string.pref_key__podprofile_id, "");
} }
public void setProfileId(String profileId) { public void setProfileId(String profileId) {
setString(prefPod, R.string.pref_key__podprofile_id, profileId); setString(R.string.pref_key__podprofile_id, profileId,prefPod);
} }
public boolean isLoadImages() { public boolean isLoadImages() {
return getBool(_prefApp, R.string.pref_key__load_images, true); return getBool(R.string.pref_key__load_images, true);
} }
public int getMinimumFontSize() { public int getMinimumFontSize() {
switch (getString(_prefApp, R.string.pref_key__font_size, "")) { switch (getString(R.string.pref_key__font_size, "")) {
case "huge": case "huge":
return 20; return 20;
case "large": case "large":
@ -97,30 +97,30 @@ public class AppSettings extends AppSettingsBase {
case "normal": case "normal":
return 8; return 8;
default: default:
setString(_prefApp, R.string.pref_key__font_size, "normal"); setString(R.string.pref_key__font_size, "normal");
return 8; return 8;
} }
} }
public String getAvatarUrl() { public String getAvatarUrl() {
return getString(prefPod, R.string.pref_key__podprofile_avatar_url, ""); return getString(R.string.pref_key__podprofile_avatar_url, "",prefPod);
} }
public void setAvatarUrl(String avatarUrl) { public void setAvatarUrl(String avatarUrl) {
setString(prefPod, R.string.pref_key__podprofile_avatar_url, avatarUrl); setString(R.string.pref_key__podprofile_avatar_url, avatarUrl,prefPod);
} }
public String getName() { public String getName() {
return getString(prefPod, R.string.pref_key__podprofile_name, ""); return getString(R.string.pref_key__podprofile_name, "",prefPod);
} }
public void setName(String name) { public void setName(String name) {
setString(prefPod, R.string.pref_key__podprofile_name, name); setString(R.string.pref_key__podprofile_name, name,prefPod);
} }
public DiasporaPod getPod() { public DiasporaPod getPod() {
if (currentPod0Cached == null) { if (currentPod0Cached == null) {
String pref = getString(prefPod, R.string.pref_key__current_pod_0, ""); String pref = getString(R.string.pref_key__current_pod_0, "",prefPod);
try { try {
currentPod0Cached = new DiasporaPod().fromJson(new JSONObject(pref)); currentPod0Cached = new DiasporaPod().fromJson(new JSONObject(pref));
@ -133,23 +133,23 @@ public class AppSettings extends AppSettingsBase {
public void setPod(DiasporaPod pod) { public void setPod(DiasporaPod pod) {
try { try {
setString(prefPod, R.string.pref_key__current_pod_0, setString(R.string.pref_key__current_pod_0,
pod == null ? null : pod.toJson().toString()); pod == null ? null : pod.toJson().toString(),prefPod);
currentPod0Cached = pod; currentPod0Cached = pod;
} catch (JSONException ignored) { } catch (JSONException ignored) {
} }
} }
public boolean hasPod() { public boolean hasPod() {
return !getString(prefPod, R.string.pref_key__current_pod_0, "").equals(""); return !getString(R.string.pref_key__current_pod_0, "",prefPod).equals("");
} }
public void setPodAspects(DiasporaAspect[] aspects) { public void setPodAspects(DiasporaAspect[] aspects) {
setStringArray(prefPod, R.string.pref_key__podprofile_aspects, aspects); setStringArray(R.string.pref_key__podprofile_aspects, aspects,prefPod);
} }
public DiasporaAspect[] getAspects() { public DiasporaAspect[] getAspects() {
String[] s = getStringArray(prefPod, R.string.pref_key__podprofile_aspects); String[] s = getStringArray(R.string.pref_key__podprofile_aspects,prefPod);
DiasporaAspect[] aspects = new DiasporaAspect[s.length]; DiasporaAspect[] aspects = new DiasporaAspect[s.length];
for (int i = 0; i < aspects.length; i++) { for (int i = 0; i < aspects.length; i++) {
aspects[i] = new DiasporaAspect(s[i]); aspects[i] = new DiasporaAspect(s[i]);
@ -158,47 +158,47 @@ public class AppSettings extends AppSettingsBase {
} }
public String[] getFollowedTags() { public String[] getFollowedTags() {
return getStringArray(prefPod, R.string.pref_key__podprofile_followed_tags); return getStringArray(R.string.pref_key__podprofile_followed_tags,prefPod);
} }
public void setFollowedTags(String[] values) { public void setFollowedTags(String[] values) {
setStringArray(prefPod, R.string.pref_key__podprofile_followed_tags, values); setStringArray(R.string.pref_key__podprofile_followed_tags, values,prefPod);
} }
public String[] getFollowedTagsFavs() { public String[] getFollowedTagsFavs() {
return getStringArray(prefPod, R.string.pref_key__podprofile_followed_tags_favs); return getStringArray(R.string.pref_key__podprofile_followed_tags_favs,prefPod);
} }
public void setFollowedTagsFavs(List<String> values) { public void setFollowedTagsFavs(List<String> values) {
setStringList(prefPod, R.string.pref_key__podprofile_followed_tags_favs, values); setStringList(R.string.pref_key__podprofile_followed_tags_favs, values,prefPod);
} }
public String[] getAspectFavs() { public String[] getAspectFavs() {
return getStringArray(prefPod, R.string.pref_key__podprofile_aspects_favs); return getStringArray(R.string.pref_key__podprofile_aspects_favs,prefPod);
} }
public void setAspectFavs(List<String> values) { public void setAspectFavs(List<String> values) {
setStringList(prefPod, R.string.pref_key__podprofile_aspects_favs, values); setStringList(R.string.pref_key__podprofile_aspects_favs, values,prefPod);
} }
public int getUnreadMessageCount() { public int getUnreadMessageCount() {
return getInt(prefPod, R.string.pref_key__podprofile_unread_message_count, 0); return getInt(R.string.pref_key__podprofile_unread_message_count, 0,prefPod);
} }
public void setUnreadMessageCount(int unreadMessageCount) { public void setUnreadMessageCount(int unreadMessageCount) {
setInt(prefPod, R.string.pref_key__podprofile_unread_message_count, unreadMessageCount); setInt(R.string.pref_key__podprofile_unread_message_count, unreadMessageCount,prefPod);
} }
public int getNotificationCount() { public int getNotificationCount() {
return getInt(prefPod, R.string.pref_key__podprofile_notification_count, 0); return getInt(R.string.pref_key__podprofile_notification_count, 0,prefPod);
} }
public void setNotificationCount(int notificationCount) { public void setNotificationCount(int notificationCount) {
setInt(prefPod, R.string.pref_key__podprofile_notification_count, notificationCount); setInt(R.string.pref_key__podprofile_notification_count, notificationCount,prefPod);
} }
public boolean isAppendSharedViaApp() { public boolean isAppendSharedViaApp() {
return getBool(_prefApp, R.string.pref_key__append_shared_via_app, true); return getBool(R.string.pref_key__append_shared_via_app, true);
} }
@SuppressLint("CommitPrefEdits") @SuppressLint("CommitPrefEdits")
@ -214,7 +214,7 @@ public class AppSettings extends AppSettingsBase {
*/ */
public boolean isProxyHttpEnabled() { public boolean isProxyHttpEnabled() {
try { try {
return getBool(_prefApp, R.string.pref_key__http_proxy_enabled, false); return getBool(R.string.pref_key__http_proxy_enabled, false);
} catch (ClassCastException e) { } catch (ClassCastException e) {
setProxyHttpEnabled(false); setProxyHttpEnabled(false);
return false; return false;
@ -222,7 +222,7 @@ public class AppSettings extends AppSettingsBase {
} }
public boolean wasProxyEnabled() { public boolean wasProxyEnabled() {
return getBool(_prefApp, R.string.pref_key__proxy_was_enabled, false); return getBool(R.string.pref_key__proxy_was_enabled, false);
} }
/** /**
@ -242,11 +242,11 @@ public class AppSettings extends AppSettingsBase {
* @return proxy host * @return proxy host
*/ */
public String getProxyHttpHost() { public String getProxyHttpHost() {
return getString(_prefApp, R.string.pref_key__http_proxy_host, ""); return getString(R.string.pref_key__http_proxy_host, "");
} }
public void setProxyHttpHost(String value) { public void setProxyHttpHost(String value) {
setString(_prefApp, R.string.pref_key__http_proxy_host, value); setString(R.string.pref_key__http_proxy_host, value);
} }
/** /**
@ -256,17 +256,17 @@ public class AppSettings extends AppSettingsBase {
*/ */
public int getProxyHttpPort() { public int getProxyHttpPort() {
try { try {
String str = getString(_prefApp, R.string.pref_key__http_proxy_port, "0"); String str = getString(R.string.pref_key__http_proxy_port, "0");
return Integer.parseInt(str); return Integer.parseInt(str);
} catch (ClassCastException e) { } catch (ClassCastException e) {
int port = getInt(_prefApp, R.string.pref_key__http_proxy_port, 0); int port = getInt(R.string.pref_key__http_proxy_port, 0);
setProxyHttpPort(port); setProxyHttpPort(port);
return port; return port;
} }
} }
public void setProxyHttpPort(int value) { public void setProxyHttpPort(int value) {
setString(_prefApp, R.string.pref_key__http_proxy_port, Integer.toString(value)); setString(R.string.pref_key__http_proxy_port, Integer.toString(value));
} }
public ProxyHandler.ProxySettings getProxySettings() { public ProxyHandler.ProxySettings getProxySettings() {
@ -274,126 +274,126 @@ public class AppSettings extends AppSettingsBase {
} }
public boolean isIntellihideToolbars() { public boolean isIntellihideToolbars() {
return getBool(_prefApp, R.string.pref_key__intellihide_toolbars, true); return getBool(R.string.pref_key__intellihide_toolbars, true);
} }
public boolean isChromeCustomTabsEnabled() { public boolean isChromeCustomTabsEnabled() {
return getBool(_prefApp, R.string.pref_key__chrome_custom_tabs_enabled, true); return getBool(R.string.pref_key__chrome_custom_tabs_enabled, true);
} }
public boolean isLoggingEnabled() { public boolean isLoggingEnabled() {
return getBool(_prefApp, R.string.pref_key__logging_enabled, false); return getBool(R.string.pref_key__logging_enabled, false);
} }
public boolean isLoggingSpamEnabled() { public boolean isLoggingSpamEnabled() {
return getBool(_prefApp, R.string.pref_key__logging_spam_enabled, false); return getBool(R.string.pref_key__logging_spam_enabled, false);
} }
public boolean isVisibleInNavExit() { public boolean isVisibleInNavExit() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__exit, true); return getBool(R.string.pref_key__visibility_nav__exit, true);
} }
public boolean isVisibleInNavHelp_license() { public boolean isVisibleInNavHelp_license() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__help_license, true); return getBool(R.string.pref_key__visibility_nav__help_license, true);
} }
public boolean isVisibleInNavPublic_activities() { public boolean isVisibleInNavPublic_activities() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__public_activities, false); return getBool(R.string.pref_key__visibility_nav__public_activities, false);
} }
public boolean isVisibleInNavMentions() { public boolean isVisibleInNavMentions() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__mentions, false); return getBool(R.string.pref_key__visibility_nav__mentions, false);
} }
public boolean isVisibleInNavCommented() { public boolean isVisibleInNavCommented() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__commented, true); return getBool(R.string.pref_key__visibility_nav__commented, true);
} }
public boolean isVisibleInNavLiked() { public boolean isVisibleInNavLiked() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__liked, true); return getBool(R.string.pref_key__visibility_nav__liked, true);
} }
public boolean isVisibleInNavActivities() { public boolean isVisibleInNavActivities() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__activities, true); return getBool(R.string.pref_key__visibility_nav__activities, true);
} }
public boolean isVisibleInNavAspects() { public boolean isVisibleInNavAspects() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__aspects, true); return getBool(R.string.pref_key__visibility_nav__aspects, true);
} }
public boolean isVisibleInNavFollowed_tags() { public boolean isVisibleInNavFollowed_tags() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__followed_tags, true); return getBool(R.string.pref_key__visibility_nav__followed_tags, true);
} }
public boolean isVisibleInNavProfile() { public boolean isVisibleInNavProfile() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__profile, true); return getBool(R.string.pref_key__visibility_nav__profile, true);
} }
public boolean isVisibleInNavContacts() { public boolean isVisibleInNavContacts() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__contacts, false); return getBool(R.string.pref_key__visibility_nav__contacts, false);
} }
public boolean isVisibleInNavStatistics() { public boolean isVisibleInNavStatistics() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__statistics, false); return getBool(R.string.pref_key__visibility_nav__statistics, false);
} }
public boolean isVisibleInNavReports() { public boolean isVisibleInNavReports() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__reports, false); return getBool(R.string.pref_key__visibility_nav__reports, false);
} }
public boolean isVisibleInNavDandelionAccount() { public boolean isVisibleInNavDandelionAccount() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__dandelion_account, false); return getBool(R.string.pref_key__visibility_nav__dandelion_account, false);
} }
public boolean isVisibleInNavToggleMobileDesktop() { public boolean isVisibleInNavToggleMobileDesktop() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__toggle_mobile_desktop, false); return getBool(R.string.pref_key__visibility_nav__toggle_mobile_desktop, false);
} }
public boolean isTopbarStreamShortcutEnabled() { public boolean isTopbarStreamShortcutEnabled() {
return getBool(_prefApp, R.string.pref_key__topbar_stream_shortcut, false); return getBool(R.string.pref_key__topbar_stream_shortcut, false);
} }
public String getScreenRotation() { public String getScreenRotation() {
return getString(_prefApp, R.string.pref_key__screen_rotation, R.string.rotation_val_system); return getString(R.string.pref_key__screen_rotation, R.string.rotation_val_system);
} }
public boolean isAppFirstStart() { public boolean isAppFirstStart() {
boolean value = getBool(_prefApp, R.string.pref_key__app_first_start, true); boolean value = getBool(R.string.pref_key__app_first_start, true);
setBool(_prefApp, R.string.pref_key__app_first_start, false); setBool(R.string.pref_key__app_first_start, false);
return value; return value;
} }
public boolean isAppCurrentVersionFirstStart() { public boolean isAppCurrentVersionFirstStart() {
int value = getInt(_prefApp, R.string.pref_key__app_first_start_current_version, -1); int value = getInt(R.string.pref_key__app_first_start_current_version, -1);
setInt(_prefApp, R.string.pref_key__app_first_start_current_version, BuildConfig.VERSION_CODE); setInt(R.string.pref_key__app_first_start_current_version, BuildConfig.VERSION_CODE);
return value != BuildConfig.VERSION_CODE && !BuildConfig.IS_TEST_BUILD; return value != BuildConfig.VERSION_CODE && !BuildConfig.IS_TEST_BUILD;
} }
public long getLastVisitedPositionInStream() { public long getLastVisitedPositionInStream() {
return getLong(prefPod, R.string.pref_key__podprofile_last_stream_position, -1); return getLong(R.string.pref_key__podprofile_last_stream_position, -1,prefPod);
} }
public void setLastVisitedPositionInStream(long timestamp) { public void setLastVisitedPositionInStream(long timestamp) {
setLong(prefPod, R.string.pref_key__podprofile_last_stream_position, timestamp); setLong(R.string.pref_key__podprofile_last_stream_position, timestamp,prefPod);
} }
public void setLanguage(String value) { public void setLanguage(String value) {
setString(_prefApp, R.string.pref_key__language, value); setString(R.string.pref_key__language, value);
} }
public String getLanguage() { public String getLanguage() {
return getString(_prefApp, R.string.pref_key__language, ""); return getString(R.string.pref_key__language, "");
} }
public void setPrimaryColorSettings(int base, int shade) { public void setPrimaryColorSettings(int base, int shade) {
setInt(_prefApp, R.string.pref_key__primary_color_base, base); setInt(R.string.pref_key__primary_color_base, base);
setInt(_prefApp, R.string.pref_key__primary_color_shade, shade); setInt(R.string.pref_key__primary_color_shade, shade);
} }
public int[] getPrimaryColorSettings() { public int[] getPrimaryColorSettings() {
return new int[]{ return new int[]{
getInt(_prefApp, R.string.pref_key__primary_color_base, rcolor(R.color.md_blue_650)), getInt(R.string.pref_key__primary_color_base, rcolor(R.color.md_blue_650)),
getInt(_prefApp, R.string.pref_key__primary_color_shade, rcolor(R.color.primary)) getInt(R.string.pref_key__primary_color_shade, rcolor(R.color.primary))
}; };
} }
@ -402,36 +402,36 @@ public class AppSettings extends AppSettingsBase {
if (isAmoledColorMode()) { if (isAmoledColorMode()) {
return Color.BLACK; return Color.BLACK;
} else { } else {
return getInt(_prefApp, R.string.pref_key__primary_color_shade, rcolor( return getInt(R.string.pref_key__primary_color_shade, rcolor(
BuildConfig.IS_TEST_BUILD ? R.color.md_brown_800 : R.color.primary)); BuildConfig.IS_TEST_BUILD ? R.color.md_brown_800 : R.color.primary));
} }
} }
public void setAccentColorSettings(int base, int shade) { public void setAccentColorSettings(int base, int shade) {
setInt(_prefApp, R.string.pref_key__accent_color_base, base); setInt(R.string.pref_key__accent_color_base, base);
setInt(_prefApp, R.string.pref_key__accent_color_shade, shade); setInt(R.string.pref_key__accent_color_shade, shade);
} }
public int[] getAccentColorSettings() { public int[] getAccentColorSettings() {
return new int[]{ return new int[]{
getInt(_prefApp, R.string.pref_key__accent_color_base, rcolor(R.color.md_green_400)), getInt(R.string.pref_key__accent_color_base, rcolor(R.color.md_green_400)),
getInt(_prefApp, R.string.pref_key__accent_color_shade, rcolor(R.color.accent)) getInt(R.string.pref_key__accent_color_shade, rcolor(R.color.accent))
}; };
} }
public int getAccentColor() { public int getAccentColor() {
return getInt(_prefApp, R.string.pref_key__accent_color_shade, rcolor(R.color.accent)); return getInt(R.string.pref_key__accent_color_shade, rcolor(R.color.accent));
} }
public boolean isExtendedNotificationsActivated() { public boolean isExtendedNotificationsActivated() {
return getBool(_prefApp, R.string.pref_key__extended_notifications, false); return getBool(R.string.pref_key__extended_notifications, false);
} }
public boolean isAmoledColorMode() { public boolean isAmoledColorMode() {
return getBool(_prefApp, R.string.pref_key__primary_color__amoled_mode, false); return getBool(R.string.pref_key__primary_color__amoled_mode, false);
} }
public boolean isAdBlockEnabled() { public boolean isAdBlockEnabled() {
return getBool(_prefApp, R.string.pref_key__adblock_enable, true); return getBool(R.string.pref_key__adblock_enable, true);
} }
} }

View file

@ -53,6 +53,7 @@ import android.support.annotation.ColorRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -60,8 +61,8 @@ import java.util.List;
/** /**
* Wrapper for settings based on SharedPreferences * Wrapper for settings based on SharedPreferences with keys in resources
* with keys in resources * Default SharedPreference (_prefApp) will be taken if no SP is specified, else the first one
*/ */
@SuppressWarnings({"WeakerAccess", "unused", "SpellCheckingInspection", "SameParameterValue"}) @SuppressWarnings({"WeakerAccess", "unused", "SpellCheckingInspection", "SameParameterValue"})
public class AppSettingsBase { public class AppSettingsBase {
@ -72,8 +73,9 @@ public class AppSettingsBase {
//######################## //########################
//## Members, Constructors //## Members, Constructors
//######################## //########################
protected final SharedPreferences _prefApp;
protected final Context _context; protected final Context _context;
protected final SharedPreferences _prefApp;
protected final String _prefAppName;
public AppSettingsBase(final Context context) { public AppSettingsBase(final Context context) {
this(context, SHARED_PREF_APP); this(context, SHARED_PREF_APP);
@ -81,7 +83,9 @@ public class AppSettingsBase {
public AppSettingsBase(final Context context, final String prefAppName) { public AppSettingsBase(final Context context, final String prefAppName) {
_context = context.getApplicationContext(); _context = context.getApplicationContext();
_prefApp = _context.getSharedPreferences(prefAppName, Context.MODE_PRIVATE); _prefAppName = TextUtils.isEmpty(prefAppName) ?
_context.getPackageName() + "_preferences" : prefAppName;
_prefApp = _context.getSharedPreferences(_prefAppName, Context.MODE_PRIVATE);
} }
//##################### //#####################
@ -128,6 +132,23 @@ public class AppSettingsBase {
pref.unregisterOnSharedPreferenceChangeListener(value); pref.unregisterOnSharedPreferenceChangeListener(value);
} }
public SharedPreferences getDefaultPreferences() {
return _prefApp;
}
public SharedPreferences.Editor getDefaultPreferencesEditor() {
return _prefApp.edit();
}
public String getDefaultPreferencesName() {
return _prefAppName;
}
private SharedPreferences gp(final SharedPreferences... pref) {
return (pref != null && pref.length > 0 ? pref[0] : _prefApp);
}
//################################# //#################################
//## Getter for resources //## Getter for resources
//################################# //#################################
@ -139,189 +160,251 @@ public class AppSettingsBase {
return ContextCompat.getColor(_context, resColorId); return ContextCompat.getColor(_context, resColorId);
} }
//################################# //#################################
//## Getter & Setter for settings //## Getter & Setter for String
//################################# //#################################
public void setString(@StringRes int keyResourceId, String value) { public void setString(@StringRes int keyResourceId, String value, final SharedPreferences... pref) {
setString(_prefApp, keyResourceId, value); gp(pref).edit().putString(rstr(keyResourceId), value).apply();
} }
public void setString(final SharedPreferences pref, @StringRes int keyResourceId, String value) { public void setString(String key, String value, final SharedPreferences... pref) {
pref.edit().putString(rstr(keyResourceId), value).apply(); gp(pref).edit().putString(key, value).apply();
} }
public String getString(@StringRes int keyResourceId, String defaultValue) { public void setString(@StringRes int keyResourceId, @StringRes int defaultValueResourceId, final SharedPreferences... pref) {
return getString(_prefApp, keyResourceId, defaultValue); gp(pref).edit().putString(rstr(keyResourceId), rstr(defaultValueResourceId)).apply();
} }
public String getString(final SharedPreferences pref, @StringRes int keyResourceId, String defaultValue) { public String getString(@StringRes int keyResourceId, String defaultValue, final SharedPreferences... pref) {
return pref.getString(rstr(keyResourceId), defaultValue); return gp(pref).getString(rstr(keyResourceId), defaultValue);
} }
public String getString(@StringRes int keyResourceId, @StringRes int keyResourceIdDefaultValue) { public String getString(@StringRes int keyResourceId, @StringRes int defaultValueResourceId, final SharedPreferences... pref) {
return getString(_prefApp, keyResourceId, keyResourceIdDefaultValue); return gp(pref).getString(rstr(keyResourceId), rstr(defaultValueResourceId));
} }
public String getString(final SharedPreferences pref, @StringRes int keyResourceId, @StringRes int keyResourceIdDefaultValue) { public String getString(String key, String defaultValue, final SharedPreferences... pref) {
return pref.getString(rstr(keyResourceId), rstr(keyResourceIdDefaultValue)); return gp(pref).getString(key, defaultValue);
} }
public void setStringArray(@StringRes int keyResourceId, Object[] values) { public String getString(@StringRes int keyResourceId, String defaultValue, @StringRes int keyResourceIdDefaultValue, final SharedPreferences... pref) {
setStringArray(_prefApp, keyResourceId, values); return gp(pref).getString(rstr(keyResourceId), rstr(keyResourceIdDefaultValue));
} }
public void setStringArray(final SharedPreferences pref, @StringRes int keyResourceId, Object[] values) { public void setStringArray(@StringRes int keyResourceId, Object[] values, final SharedPreferences... pref) {
setStringArray(rstr(keyResourceId), values, gp(pref));
}
public void setStringArray(String key, Object[] values, final SharedPreferences... pref) {
setStringArray(key, values, gp(pref));
}
private void setStringArray(String key, Object[] values, final SharedPreferences pref) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Object value : values) { for (Object value : values) {
sb.append(ARRAY_SEPARATOR); sb.append(ARRAY_SEPARATOR);
sb.append(value.toString().replace(ARRAY_SEPARATOR, ARRAY_SEPARATOR_SUBSTITUTE)); sb.append(value.toString().replace(ARRAY_SEPARATOR, ARRAY_SEPARATOR_SUBSTITUTE));
} }
setString(pref, keyResourceId, sb.toString().replaceFirst(ARRAY_SEPARATOR, "")); setString(key, sb.toString().replaceFirst(ARRAY_SEPARATOR, ""), pref);
} }
@NonNull @NonNull
public String[] getStringArray(@StringRes int keyResourceId) { public String[] getStringArray(@StringRes int keyResourceId, final SharedPreferences... pref) {
return getStringArray(_prefApp, keyResourceId); return getStringArray(rstr(keyResourceId), gp(pref));
} }
@NonNull private String[] getStringArray(String key, final SharedPreferences... pref) {
public String[] getStringArray(final SharedPreferences pref, @StringRes int keyResourceId) { String value = gp(pref)
String value = pref.getString(rstr(keyResourceId), ARRAY_SEPARATOR).replace(ARRAY_SEPARATOR_SUBSTITUTE, ARRAY_SEPARATOR); .getString(key, ARRAY_SEPARATOR)
.replace(ARRAY_SEPARATOR_SUBSTITUTE, ARRAY_SEPARATOR);
if (value.equals(ARRAY_SEPARATOR)) { if (value.equals(ARRAY_SEPARATOR)) {
return new String[0]; return new String[0];
} }
return value.split(ARRAY_SEPARATOR); return value.split(ARRAY_SEPARATOR);
} }
public void setStringList(@StringRes int keyResourceId, List<String> values) { public void setStringList(@StringRes int keyResourceId, List<String> values, final SharedPreferences... pref) {
setStringList(_prefApp, keyResourceId, values); setStringArray(rstr(keyResourceId), values.toArray(new String[values.size()]), pref);
} }
public void setStringList(final SharedPreferences pref, @StringRes int keyResourceId, List<String> values) { public void setStringList(String key, List<String> values, final SharedPreferences... pref) {
setStringArray(pref, keyResourceId, values.toArray(new String[values.size()])); setStringArray(key, values.toArray(new String[values.size()]), pref);
} }
public ArrayList<String> getStringList(@StringRes int keyResourceId) { public ArrayList<String> getStringList(@StringRes int keyResourceId, final SharedPreferences... pref) {
return getStringList(_prefApp, keyResourceId); return new ArrayList<>(Arrays.asList(getStringArray(rstr(keyResourceId), gp(pref))));
} }
public ArrayList<String> getStringList(final SharedPreferences pref, @StringRes int keyResourceId) { public ArrayList<String> getStringList(String key, final SharedPreferences... pref) {
return new ArrayList<>(Arrays.asList(getStringArray(pref, keyResourceId))); return new ArrayList<>(Arrays.asList(getStringArray(key, gp(pref))));
} }
public void setLong(@StringRes int keyResourceId, long value) { //#################################
setLong(_prefApp, keyResourceId, value); //## Getter & Setter for integer
//#################################
public void setInt(@StringRes int keyResourceId, int value, final SharedPreferences... pref) {
gp(pref).edit().putInt(rstr(keyResourceId), value).apply();
} }
public void setLong(final SharedPreferences pref, @StringRes int keyResourceId, long value) { public void setInt(String key, int value, final SharedPreferences... pref) {
pref.edit().putLong(rstr(keyResourceId), value).apply(); gp(pref).edit().putInt(key, value).apply();
} }
public long getLong(@StringRes int keyResourceId, long defaultValue) { public int getInt(@StringRes int keyResourceId, int defaultValue, final SharedPreferences... pref) {
return getLong(_prefApp, keyResourceId, defaultValue); return gp(pref).getInt(rstr(keyResourceId), defaultValue);
} }
public long getLong(final SharedPreferences pref, @StringRes int keyResourceId, long defaultValue) { public int getInt(String key, int defaultValue, final SharedPreferences... pref) {
return pref.getLong(rstr(keyResourceId), defaultValue); return gp(pref).getInt(key, defaultValue);
} }
public void setBool(@StringRes int keyResourceId, boolean value) { public int getIntOfStringPref(@StringRes int keyResId, int defaultValue, final SharedPreferences... pref) {
setBool(_prefApp, keyResourceId, value); return getIntOfStringPref(rstr(keyResId), defaultValue, gp(pref));
} }
public void setBool(final SharedPreferences pref, @StringRes int keyResourceId, boolean value) { public int getIntOfStringPref(String key, int defaultValue, final SharedPreferences... pref) {
pref.edit().putBoolean(rstr(keyResourceId), value).apply(); String strNum = getString(key, Integer.toString(defaultValue), gp(pref));
}
public boolean getBool(@StringRes int keyResourceId, boolean defaultValue) {
return getBool(_prefApp, keyResourceId, defaultValue);
}
public boolean getBool(final SharedPreferences pref, @StringRes int keyResourceId, boolean defaultValue) {
return pref.getBoolean(rstr(keyResourceId), defaultValue);
}
public int getColor(String key, int defaultColor) {
return getColor(_prefApp, key, defaultColor);
}
public int getColor(final SharedPreferences pref, String key, int defaultColor) {
return pref.getInt(key, defaultColor);
}
public int getColor(@StringRes int keyResourceId, int defaultColor) {
return getColor(_prefApp, keyResourceId, defaultColor);
}
public int getColor(final SharedPreferences pref, @StringRes int keyResourceId, int defaultColor) {
return pref.getInt(rstr(keyResourceId), defaultColor);
}
public void setDouble(@StringRes int keyResId, double value) {
setDouble(_prefApp, keyResId, value);
}
public void setDouble(final SharedPreferences pref, @StringRes int keyResId, double value) {
_prefApp.edit().putLong(rstr(keyResId), Double.doubleToRawLongBits(value)).apply();
}
public double getDouble(@StringRes int keyResId, double defaultValue) {
return getDouble(_prefApp, keyResId, defaultValue);
}
public double getDouble(final SharedPreferences pref, @StringRes int keyResId, double defaultValue) {
return Double.longBitsToDouble(_prefApp.getLong(rstr(keyResId), Double.doubleToLongBits(defaultValue)));
}
public int getIntOfStringPref(@StringRes int keyResId, int defaultValue) {
String strNum = _prefApp.getString(_context.getString(keyResId), Integer.toString(defaultValue));
return Integer.valueOf(strNum); return Integer.valueOf(strNum);
} }
public void setInt(@StringRes int keyResourceId, int value) {
setInt(_prefApp, keyResourceId, value); public void setIntArray(@StringRes int keyResourceId, Object[] values, final SharedPreferences... pref) {
setIntArray(rstr(keyResourceId), values, gp(pref));
} }
public void setInt(final SharedPreferences pref, @StringRes int keyResourceId, int value) { public void setIntArray(String key, Object[] values, final SharedPreferences... pref) {
pref.edit().putInt(rstr(keyResourceId), value).apply(); setIntArray(key, values, gp(pref));
} }
public int getInt(@StringRes int keyResourceId, int defaultValue) { private void setIntArray(String key, Object[] values, final SharedPreferences pref) {
return getInt(_prefApp, keyResourceId, defaultValue);
}
public int getInt(final SharedPreferences pref, @StringRes int keyResourceId, int defaultValue) {
return pref.getInt(rstr(keyResourceId), defaultValue);
}
public void setIntList(@StringRes int keyResId, List<Integer> values) {
setIntList(_prefApp, keyResId, values);
}
public void setIntList(final SharedPreferences pref, @StringRes int keyResId, List<Integer> values) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int value : values) { for (Object value : values) {
sb.append(ARRAY_SEPARATOR); sb.append(ARRAY_SEPARATOR);
sb.append(Integer.toString(value)); sb.append(value.toString());
} }
setString(_prefApp, keyResId, sb.toString().replaceFirst(ARRAY_SEPARATOR, "")); setString(key, sb.toString().replaceFirst(ARRAY_SEPARATOR, ""), pref);
} }
@NonNull @NonNull
public ArrayList<Integer> getIntList(@StringRes int keyResId) { public Integer[] getIntArray(@StringRes int keyResourceId, final SharedPreferences... pref) {
return getIntList(_prefApp, keyResId); return getIntArray(rstr(keyResourceId), gp(pref));
} }
@NonNull private Integer[] getIntArray(String key, final SharedPreferences... pref) {
public ArrayList<Integer> getIntList(final SharedPreferences pref, @StringRes int keyResId) { String value = gp(pref).getString(key, ARRAY_SEPARATOR);
final ArrayList<Integer> ret = new ArrayList<>();
final String value = getString(_prefApp, keyResId, ARRAY_SEPARATOR);
if (value.equals(ARRAY_SEPARATOR)) { if (value.equals(ARRAY_SEPARATOR)) {
return ret; return new Integer[0];
} }
for (String intstr : value.split(ARRAY_SEPARATOR)) { String[] split = value.split(ARRAY_SEPARATOR);
ret.add(Integer.parseInt(intstr)); Integer[] ret = new Integer[split.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = Integer.parseInt(split[i]);
} }
return ret; return ret;
} }
public void setIntList(@StringRes int keyResourceId, List<Integer> values, final SharedPreferences... pref) {
setIntArray(rstr(keyResourceId), values.toArray(new Integer[values.size()]), pref);
}
public void setIntList(String key, List<Integer> values, final SharedPreferences... pref) {
setIntArray(key, values.toArray(new Integer[values.size()]), pref);
}
public ArrayList<Integer> getIntList(@StringRes int keyResourceId, final SharedPreferences... pref) {
return new ArrayList<>(Arrays.asList(getIntArray(rstr(keyResourceId), gp(pref))));
}
public ArrayList<Integer> getIntList(String key, final SharedPreferences... pref) {
return new ArrayList<>(Arrays.asList(getIntArray(key, gp(pref))));
}
//#################################
//## Getter & Setter for Long
//#################################
public void setLong(@StringRes int keyResourceId, long value, final SharedPreferences... pref) {
gp(pref).edit().putLong(rstr(keyResourceId), value).apply();
}
public void setLong(String key, long value, final SharedPreferences... pref) {
gp(pref).edit().putLong(key, value).apply();
}
public long getLong(@StringRes int keyResourceId, long defaultValue, final SharedPreferences... pref) {
return gp(pref).getLong(rstr(keyResourceId), defaultValue);
}
public long getLong(String key, long defaultValue, final SharedPreferences... pref) {
return gp(pref).getLong(key, defaultValue);
}
//#################################
//## Getter & Setter for Float
//#################################
public void setFloat(@StringRes int keyResourceId, float value, final SharedPreferences... pref) {
gp(pref).edit().putFloat(rstr(keyResourceId), value).apply();
}
public void setFloat(String key, float value, final SharedPreferences... pref) {
gp(pref).edit().putFloat(key, value).apply();
}
public float getFloat(@StringRes int keyResourceId, float defaultValue, final SharedPreferences... pref) {
return gp(pref).getFloat(rstr(keyResourceId), defaultValue);
}
public float getFloat(String key, float defaultValue, final SharedPreferences... pref) {
return gp(pref).getFloat(key, defaultValue);
}
//#################################
//## Getter & Setter for Double
//#################################
public void setDouble(@StringRes int keyResourceId, double value, final SharedPreferences... pref) {
setLong(rstr(keyResourceId), Double.doubleToRawLongBits(value));
}
public void setDouble(String key, double value, final SharedPreferences... pref) {
setLong(key, Double.doubleToRawLongBits(value));
}
public double getDouble(@StringRes int keyResourceId, double defaultValue, final SharedPreferences... pref) {
return getDouble(rstr(keyResourceId), defaultValue, gp(pref));
}
public double getDouble(String key, double defaultValue, final SharedPreferences... pref) {
return Double.longBitsToDouble(getLong(key, Double.doubleToRawLongBits(defaultValue), gp(pref)));
}
//#################################
//## Getter & Setter for boolean
//#################################
public void setBool(@StringRes int keyResourceId, boolean value, final SharedPreferences... pref) {
gp(pref).edit().putBoolean(rstr(keyResourceId), value).apply();
}
public void setBool(String key, boolean value, final SharedPreferences... pref) {
gp(pref).edit().putBoolean(key, value).apply();
}
public boolean getBool(@StringRes int keyResourceId, boolean defaultValue, final SharedPreferences... pref) {
return gp(pref).getBoolean(rstr(keyResourceId), defaultValue);
}
public boolean getBool(String key, boolean defaultValue, final SharedPreferences... pref) {
return gp(pref).getBoolean(key, defaultValue);
}
//#################################
//## Getter & Setter for Color
//#################################
public int getColor(String key, @ColorRes int defaultColor, final SharedPreferences... pref) {
return gp(pref).getInt(key, rcolor(defaultColor));
}
public int getColor(@StringRes int keyResourceId, @ColorRes int defaultColor, final SharedPreferences... pref) {
return gp(pref).getInt(rstr(keyResourceId), rcolor(defaultColor));
}
} }

View file

@ -59,13 +59,13 @@ public class Helpers {
_context = context; _context = context;
} }
//######################## public Context context() {
//## Methods return _context;
//########################
public String str(@StringRes int strResId) {
return _context.getString(strResId);
} }
//########################
//## Resources
//########################
static class ResType { static class ResType {
public static final String DRAWABLE = "drawable"; public static final String DRAWABLE = "drawable";
public static final String STRING = "string"; public static final String STRING = "string";
@ -78,6 +78,10 @@ public class Helpers {
public static final String RAW = "raw"; public static final String RAW = "raw";
} }
public String str(@StringRes int strResId) {
return _context.getString(strResId);
}
public Drawable drawable(@DrawableRes int resId) { public Drawable drawable(@DrawableRes int resId) {
return ContextCompat.getDrawable(_context, resId); return ContextCompat.getDrawable(_context, resId);
} }
@ -86,10 +90,23 @@ public class Helpers {
return ContextCompat.getColor(_context, resId); return ContextCompat.getColor(_context, resId);
} }
public Context context() { public int getResId(final String type, final String name) {
return _context; return _context.getResources().getIdentifier(name, type, _context.getPackageName());
} }
public boolean areResIdsAvailable(final String type, final String... names) {
for (String name : names) {
if (getResId(type, name) == 0) {
return false;
}
}
return true;
}
//########################
//## Methods
//########################
public String colorToHexString(int intColor) { public String colorToHexString(int intColor) {
return String.format("#%06X", 0xFFFFFF & intColor); return String.format("#%06X", 0xFFFFFF & intColor);
} }
@ -105,19 +122,6 @@ public class Helpers {
} }
} }
public int getResId(final String type, final String name) {
return _context.getResources().getIdentifier(name, type, _context.getPackageName());
}
public boolean areResIdsAvailable(final String type, final String... names) {
for (String name : names) {
if (getResId(type, name) == 0) {
return false;
}
}
return true;
}
public void openWebpageInExternalBrowser(final String url) { public void openWebpageInExternalBrowser(final String url) {
Uri uri = Uri.parse(url); Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri); Intent intent = new Intent(Intent.ACTION_VIEW, uri);
@ -250,14 +254,8 @@ public class Helpers {
} }
public void setHtmlToTextView(TextView textView, String html) { public void setHtmlToTextView(TextView textView, String html) {
Spanned spanned;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
spanned = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
spanned = Html.fromHtml(html);
}
textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(new SpannableString(spanned)); textView.setText(new SpannableString(htmlToSpanned(html)));
} }
public double getEstimatedScreenSizeInches() { public double getEstimatedScreenSizeInches() {
@ -301,6 +299,16 @@ public class Helpers {
+ (0.114 * Color.blue(colorOnBottomInt))))); + (0.114 * Color.blue(colorOnBottomInt)))));
} }
@SuppressWarnings("deprecation")
public Spanned htmlToSpanned(String html) {
Spanned result;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(html);
}
return result;
}
public float px2dp(final float px) { public float px2dp(final float px) {
return px / _context.getResources().getDisplayMetrics().density; return px / _context.getResources().getDisplayMetrics().density;