1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-12-22 19:08:03 +01: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))) {
color = appSettings.getAccentColor();
} else {
color = appSettings.getColor(getSharedPreferences(), key, color);
color = appSettings.getColor(key, color, getSharedPreferences());
}
circle.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}

View file

@ -77,19 +77,19 @@ public class AppSettings extends AppSettingsBase {
//## Getter & Setter for settings
//#################################
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) {
setString(prefPod, R.string.pref_key__podprofile_id, profileId);
setString(R.string.pref_key__podprofile_id, profileId,prefPod);
}
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() {
switch (getString(_prefApp, R.string.pref_key__font_size, "")) {
switch (getString(R.string.pref_key__font_size, "")) {
case "huge":
return 20;
case "large":
@ -97,30 +97,30 @@ public class AppSettings extends AppSettingsBase {
case "normal":
return 8;
default:
setString(_prefApp, R.string.pref_key__font_size, "normal");
setString(R.string.pref_key__font_size, "normal");
return 8;
}
}
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) {
setString(prefPod, R.string.pref_key__podprofile_avatar_url, avatarUrl);
setString(R.string.pref_key__podprofile_avatar_url, avatarUrl,prefPod);
}
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) {
setString(prefPod, R.string.pref_key__podprofile_name, name);
setString(R.string.pref_key__podprofile_name, name,prefPod);
}
public DiasporaPod getPod() {
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 {
currentPod0Cached = new DiasporaPod().fromJson(new JSONObject(pref));
@ -133,23 +133,23 @@ public class AppSettings extends AppSettingsBase {
public void setPod(DiasporaPod pod) {
try {
setString(prefPod, R.string.pref_key__current_pod_0,
pod == null ? null : pod.toJson().toString());
setString(R.string.pref_key__current_pod_0,
pod == null ? null : pod.toJson().toString(),prefPod);
currentPod0Cached = pod;
} catch (JSONException ignored) {
}
}
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) {
setStringArray(prefPod, R.string.pref_key__podprofile_aspects, aspects);
setStringArray(R.string.pref_key__podprofile_aspects, aspects,prefPod);
}
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];
for (int i = 0; i < aspects.length; i++) {
aspects[i] = new DiasporaAspect(s[i]);
@ -158,47 +158,47 @@ public class AppSettings extends AppSettingsBase {
}
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) {
setStringArray(prefPod, R.string.pref_key__podprofile_followed_tags, values);
setStringArray(R.string.pref_key__podprofile_followed_tags, values,prefPod);
}
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) {
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() {
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) {
setStringList(prefPod, R.string.pref_key__podprofile_aspects_favs, values);
setStringList(R.string.pref_key__podprofile_aspects_favs, values,prefPod);
}
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) {
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() {
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) {
setInt(prefPod, R.string.pref_key__podprofile_notification_count, notificationCount);
setInt(R.string.pref_key__podprofile_notification_count, notificationCount,prefPod);
}
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")
@ -214,7 +214,7 @@ public class AppSettings extends AppSettingsBase {
*/
public boolean isProxyHttpEnabled() {
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) {
setProxyHttpEnabled(false);
return false;
@ -222,7 +222,7 @@ public class AppSettings extends AppSettingsBase {
}
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
*/
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) {
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() {
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);
} 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);
return port;
}
}
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() {
@ -274,126 +274,126 @@ public class AppSettings extends AppSettingsBase {
}
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() {
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() {
return getBool(_prefApp, R.string.pref_key__logging_enabled, false);
return getBool(R.string.pref_key__logging_enabled, false);
}
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() {
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() {
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() {
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() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__mentions, false);
return getBool(R.string.pref_key__visibility_nav__mentions, false);
}
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() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__liked, true);
return getBool(R.string.pref_key__visibility_nav__liked, true);
}
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() {
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() {
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() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__profile, true);
return getBool(R.string.pref_key__visibility_nav__profile, true);
}
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() {
return getBool(_prefApp, R.string.pref_key__visibility_nav__statistics, false);
return getBool(R.string.pref_key__visibility_nav__statistics, false);
}
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() {
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() {
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() {
return getBool(_prefApp, R.string.pref_key__topbar_stream_shortcut, false);
return getBool(R.string.pref_key__topbar_stream_shortcut, false);
}
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() {
boolean value = getBool(_prefApp, R.string.pref_key__app_first_start, true);
setBool(_prefApp, R.string.pref_key__app_first_start, false);
boolean value = getBool(R.string.pref_key__app_first_start, true);
setBool(R.string.pref_key__app_first_start, false);
return value;
}
public boolean isAppCurrentVersionFirstStart() {
int value = getInt(_prefApp, R.string.pref_key__app_first_start_current_version, -1);
setInt(_prefApp, R.string.pref_key__app_first_start_current_version, BuildConfig.VERSION_CODE);
int value = getInt(R.string.pref_key__app_first_start_current_version, -1);
setInt(R.string.pref_key__app_first_start_current_version, BuildConfig.VERSION_CODE);
return value != BuildConfig.VERSION_CODE && !BuildConfig.IS_TEST_BUILD;
}
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) {
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) {
setString(_prefApp, R.string.pref_key__language, value);
setString(R.string.pref_key__language, value);
}
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) {
setInt(_prefApp, 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_base, base);
setInt(R.string.pref_key__primary_color_shade, shade);
}
public int[] getPrimaryColorSettings() {
return new int[]{
getInt(_prefApp, 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_base, rcolor(R.color.md_blue_650)),
getInt(R.string.pref_key__primary_color_shade, rcolor(R.color.primary))
};
}
@ -402,36 +402,36 @@ public class AppSettings extends AppSettingsBase {
if (isAmoledColorMode()) {
return Color.BLACK;
} 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));
}
}
public void setAccentColorSettings(int base, int shade) {
setInt(_prefApp, 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_base, base);
setInt(R.string.pref_key__accent_color_shade, shade);
}
public int[] getAccentColorSettings() {
return new int[]{
getInt(_prefApp, 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_base, rcolor(R.color.md_green_400)),
getInt(R.string.pref_key__accent_color_shade, rcolor(R.color.accent))
};
}
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() {
return getBool(_prefApp, R.string.pref_key__extended_notifications, false);
return getBool(R.string.pref_key__extended_notifications, false);
}
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() {
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.StringRes;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Arrays;
@ -60,8 +61,8 @@ import java.util.List;
/**
* Wrapper for settings based on SharedPreferences
* with keys in resources
* Wrapper for settings based on SharedPreferences with keys in resources
* Default SharedPreference (_prefApp) will be taken if no SP is specified, else the first one
*/
@SuppressWarnings({"WeakerAccess", "unused", "SpellCheckingInspection", "SameParameterValue"})
public class AppSettingsBase {
@ -72,8 +73,9 @@ public class AppSettingsBase {
//########################
//## Members, Constructors
//########################
protected final SharedPreferences _prefApp;
protected final Context _context;
protected final SharedPreferences _prefApp;
protected final String _prefAppName;
public AppSettingsBase(final Context context) {
this(context, SHARED_PREF_APP);
@ -81,7 +83,9 @@ public class AppSettingsBase {
public AppSettingsBase(final Context context, final String prefAppName) {
_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);
}
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
//#################################
@ -139,189 +160,251 @@ public class AppSettingsBase {
return ContextCompat.getColor(_context, resColorId);
}
//#################################
//## Getter & Setter for settings
//## Getter & Setter for String
//#################################
public void setString(@StringRes int keyResourceId, String value) {
setString(_prefApp, keyResourceId, value);
public void setString(@StringRes int keyResourceId, String value, final SharedPreferences... pref) {
gp(pref).edit().putString(rstr(keyResourceId), value).apply();
}
public void setString(final SharedPreferences pref, @StringRes int keyResourceId, String value) {
pref.edit().putString(rstr(keyResourceId), value).apply();
public void setString(String key, String value, final SharedPreferences... pref) {
gp(pref).edit().putString(key, value).apply();
}
public String getString(@StringRes int keyResourceId, String defaultValue) {
return getString(_prefApp, keyResourceId, defaultValue);
public void setString(@StringRes int keyResourceId, @StringRes int defaultValueResourceId, final SharedPreferences... pref) {
gp(pref).edit().putString(rstr(keyResourceId), rstr(defaultValueResourceId)).apply();
}
public String getString(final SharedPreferences pref, @StringRes int keyResourceId, String defaultValue) {
return pref.getString(rstr(keyResourceId), defaultValue);
public String getString(@StringRes int keyResourceId, String defaultValue, final SharedPreferences... pref) {
return gp(pref).getString(rstr(keyResourceId), defaultValue);
}
public String getString(@StringRes int keyResourceId, @StringRes int keyResourceIdDefaultValue) {
return getString(_prefApp, keyResourceId, keyResourceIdDefaultValue);
public String getString(@StringRes int keyResourceId, @StringRes int defaultValueResourceId, final SharedPreferences... pref) {
return gp(pref).getString(rstr(keyResourceId), rstr(defaultValueResourceId));
}
public String getString(final SharedPreferences pref, @StringRes int keyResourceId, @StringRes int keyResourceIdDefaultValue) {
return pref.getString(rstr(keyResourceId), rstr(keyResourceIdDefaultValue));
public String getString(String key, String defaultValue, final SharedPreferences... pref) {
return gp(pref).getString(key, defaultValue);
}
public void setStringArray(@StringRes int keyResourceId, Object[] values) {
setStringArray(_prefApp, keyResourceId, values);
public String getString(@StringRes int keyResourceId, String defaultValue, @StringRes int keyResourceIdDefaultValue, final SharedPreferences... pref) {
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();
for (Object value : values) {
sb.append(ARRAY_SEPARATOR);
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
public String[] getStringArray(@StringRes int keyResourceId) {
return getStringArray(_prefApp, keyResourceId);
public String[] getStringArray(@StringRes int keyResourceId, final SharedPreferences... pref) {
return getStringArray(rstr(keyResourceId), gp(pref));
}
@NonNull
public String[] getStringArray(final SharedPreferences pref, @StringRes int keyResourceId) {
String value = pref.getString(rstr(keyResourceId), ARRAY_SEPARATOR).replace(ARRAY_SEPARATOR_SUBSTITUTE, ARRAY_SEPARATOR);
private String[] getStringArray(String key, final SharedPreferences... pref) {
String value = gp(pref)
.getString(key, ARRAY_SEPARATOR)
.replace(ARRAY_SEPARATOR_SUBSTITUTE, ARRAY_SEPARATOR);
if (value.equals(ARRAY_SEPARATOR)) {
return new String[0];
}
return value.split(ARRAY_SEPARATOR);
}
public void setStringList(@StringRes int keyResourceId, List<String> values) {
setStringList(_prefApp, keyResourceId, values);
public void setStringList(@StringRes int keyResourceId, List<String> values, final SharedPreferences... pref) {
setStringArray(rstr(keyResourceId), values.toArray(new String[values.size()]), pref);
}
public void setStringList(final SharedPreferences pref, @StringRes int keyResourceId, List<String> values) {
setStringArray(pref, keyResourceId, values.toArray(new String[values.size()]));
public void setStringList(String key, List<String> values, final SharedPreferences... pref) {
setStringArray(key, values.toArray(new String[values.size()]), pref);
}
public ArrayList<String> getStringList(@StringRes int keyResourceId) {
return getStringList(_prefApp, keyResourceId);
public ArrayList<String> getStringList(@StringRes int keyResourceId, final SharedPreferences... pref) {
return new ArrayList<>(Arrays.asList(getStringArray(rstr(keyResourceId), gp(pref))));
}
public ArrayList<String> getStringList(final SharedPreferences pref, @StringRes int keyResourceId) {
return new ArrayList<>(Arrays.asList(getStringArray(pref, keyResourceId)));
public ArrayList<String> getStringList(String key, final SharedPreferences... pref) {
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) {
pref.edit().putLong(rstr(keyResourceId), value).apply();
public void setInt(String key, int value, final SharedPreferences... pref) {
gp(pref).edit().putInt(key, value).apply();
}
public long getLong(@StringRes int keyResourceId, long defaultValue) {
return getLong(_prefApp, keyResourceId, defaultValue);
public int getInt(@StringRes int keyResourceId, int defaultValue, final SharedPreferences... pref) {
return gp(pref).getInt(rstr(keyResourceId), defaultValue);
}
public long getLong(final SharedPreferences pref, @StringRes int keyResourceId, long defaultValue) {
return pref.getLong(rstr(keyResourceId), defaultValue);
public int getInt(String key, int defaultValue, final SharedPreferences... pref) {
return gp(pref).getInt(key, defaultValue);
}
public void setBool(@StringRes int keyResourceId, boolean value) {
setBool(_prefApp, keyResourceId, value);
public int getIntOfStringPref(@StringRes int keyResId, int defaultValue, final SharedPreferences... pref) {
return getIntOfStringPref(rstr(keyResId), defaultValue, gp(pref));
}
public void setBool(final SharedPreferences pref, @StringRes int keyResourceId, boolean value) {
pref.edit().putBoolean(rstr(keyResourceId), value).apply();
}
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));
public int getIntOfStringPref(String key, int defaultValue, final SharedPreferences... pref) {
String strNum = getString(key, Integer.toString(defaultValue), gp(pref));
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) {
pref.edit().putInt(rstr(keyResourceId), value).apply();
public void setIntArray(String key, Object[] values, final SharedPreferences... pref) {
setIntArray(key, values, gp(pref));
}
public int getInt(@StringRes int keyResourceId, int defaultValue) {
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) {
private void setIntArray(String key, Object[] values, final SharedPreferences pref) {
StringBuilder sb = new StringBuilder();
for (int value : values) {
for (Object value : values) {
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
public ArrayList<Integer> getIntList(@StringRes int keyResId) {
return getIntList(_prefApp, keyResId);
public Integer[] getIntArray(@StringRes int keyResourceId, final SharedPreferences... pref) {
return getIntArray(rstr(keyResourceId), gp(pref));
}
@NonNull
public ArrayList<Integer> getIntList(final SharedPreferences pref, @StringRes int keyResId) {
final ArrayList<Integer> ret = new ArrayList<>();
final String value = getString(_prefApp, keyResId, ARRAY_SEPARATOR);
private Integer[] getIntArray(String key, final SharedPreferences... pref) {
String value = gp(pref).getString(key, ARRAY_SEPARATOR);
if (value.equals(ARRAY_SEPARATOR)) {
return ret;
return new Integer[0];
}
for (String intstr : value.split(ARRAY_SEPARATOR)) {
ret.add(Integer.parseInt(intstr));
String[] split = value.split(ARRAY_SEPARATOR);
Integer[] ret = new Integer[split.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = Integer.parseInt(split[i]);
}
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;
}
//########################
//## Methods
//########################
public String str(@StringRes int strResId) {
return _context.getString(strResId);
public Context context() {
return _context;
}
//########################
//## Resources
//########################
static class ResType {
public static final String DRAWABLE = "drawable";
public static final String STRING = "string";
@ -78,6 +78,10 @@ public class Helpers {
public static final String RAW = "raw";
}
public String str(@StringRes int strResId) {
return _context.getString(strResId);
}
public Drawable drawable(@DrawableRes int resId) {
return ContextCompat.getDrawable(_context, resId);
}
@ -86,10 +90,23 @@ public class Helpers {
return ContextCompat.getColor(_context, resId);
}
public Context context() {
return _context;
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;
}
//########################
//## Methods
//########################
public String colorToHexString(int 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) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
@ -250,14 +254,8 @@ public class Helpers {
}
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.setText(new SpannableString(spanned));
textView.setText(new SpannableString(htmlToSpanned(html)));
}
public double getEstimatedScreenSizeInches() {
@ -301,6 +299,16 @@ public class Helpers {
+ (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) {
return px / _context.getResources().getDisplayMetrics().density;