1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-22 04:12:08 +01:00

Use opoc/AppSettingsBase as base of AppSettings

This commit is contained in:
Gregor Santner 2017-05-20 02:08:04 +02:00
parent 1ff93b6959
commit e443a8ed6f
No known key found for this signature in database
GPG key ID: 7E83A7834AECB009
20 changed files with 460 additions and 204 deletions

View file

@ -35,17 +35,22 @@ import com.github.dfa.diaspora_android.util.AppSettings;
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
public class App extends Application {
private volatile static App app;
private AppSettings appSettings;
private AvatarImageLoader avatarImageLoader;
private CookieManager cookieManager;
private DiasporaUserProfile diasporaUserProfile;
public static App get() {
return app;
}
@Override
public void onCreate() {
super.onCreate();
app = this;
final Context c = getApplicationContext();
appSettings = new AppSettings(c);
appSettings = AppSettings.get();
// Init app log
AppLog.setLoggingEnabled(appSettings.isLoggingEnabled());
@ -79,9 +84,9 @@ public class App extends Application {
new AvatarImageLoader(this).clearAvatarImage();
// Clear preferences__master
appSettings.clearPodSettings();
appSettings.resetPodSettings();
// Clear User profile - reload empty AppSettings data
// Clear User profile - reload empty AppSettingsBase data
diasporaUserProfile.loadFromAppSettings();
// Clear cookies

View file

@ -77,7 +77,7 @@ import com.github.dfa.diaspora_android.util.AppLog;
import com.github.dfa.diaspora_android.util.AppSettings;
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
import com.github.dfa.diaspora_android.util.Helpers;
import com.github.dfa.diaspora_android.util.SimpleMarkdownParser;
import io.github.gsantner.opoc.util.SimpleMarkdownParser;
import com.github.dfa.diaspora_android.web.BrowserFragment;
import com.github.dfa.diaspora_android.web.ContextMenuWebView;
import com.github.dfa.diaspora_android.web.ProxyHandler;

View file

@ -68,7 +68,7 @@ public class SettingsActivity extends ThemedActivity implements SharedPreference
SettingsActivity.this.onBackPressed();
}
});
getAppSettings().registerPrefAppPreferenceChangedListener(this);
getAppSettings().registerPreferenceChangedListener(this);
oldProxySettings = getAppSettings().getProxySettings();
showFragment(SettingsFragmentMaster.TAG, false);
}
@ -126,7 +126,7 @@ public class SettingsActivity extends ThemedActivity implements SharedPreference
ProxyHandler.getInstance().updateProxySettings(this);
}
}
getAppSettings().unregisterPrefAppPreferenceChangedListener(this);
getAppSettings().unregisterPreferenceChangedListener(this);
super.onStop();
}
@ -223,7 +223,7 @@ public class SettingsActivity extends ThemedActivity implements SharedPreference
getActivity().finish();
return true;
} else if (settings.isKeyEqual(key, R.string.pref_key__change_account)) {
new ThemedAlertDialogBuilder(getActivity(), new AppSettings(getActivity().getApplication()))
new ThemedAlertDialogBuilder(getActivity(), AppSettings.get())
.setTitle(getString(R.string.confirmation))
.setMessage(getString(R.string.pref_warning__change_account))
.setNegativeButton(android.R.string.no, null)
@ -398,8 +398,8 @@ public class SettingsActivity extends ThemedActivity implements SharedPreference
public void updateSummaries() {
if (isAdded()) {
AppSettings settings = ((App) getActivity().getApplication()).getSettings();
findPreference(settings.getKey(R.string.pref_key__http_proxy_host)).setSummary(settings.getProxyHttpHost());
findPreference(settings.getKey(R.string.pref_key__http_proxy_port)).setSummary(Integer.toString(settings.getProxyHttpPort()));
findPreference(settings.rstr(R.string.pref_key__http_proxy_host)).setSummary(settings.getProxyHttpHost());
findPreference(settings.rstr(R.string.pref_key__http_proxy_port)).setSummary(Integer.toString(settings.getProxyHttpPort()));
}
}
@ -472,7 +472,7 @@ public class SettingsActivity extends ThemedActivity implements SharedPreference
}
private void showWipeSettingsDialog() {
final AppSettings appSettings = new AppSettings(this.getActivity().getApplication());
final AppSettings appSettings = AppSettings.get();
ThemedAlertDialogBuilder builder = new ThemedAlertDialogBuilder(getActivity(), appSettings);
builder.setTitle(R.string.confirmation)
@ -480,8 +480,8 @@ public class SettingsActivity extends ThemedActivity implements SharedPreference
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
appSettings.clearAppSettings();
appSettings.clearPodSettings();
appSettings.resetAppSettings();
appSettings.resetPodSettings();
Intent restartActivity = new Intent(getActivity(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 12374, restartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

View file

@ -48,7 +48,7 @@ public class OpenExternalLinkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent receiveIntent) {
AppSettings appSettings = new AppSettings(c);
AppSettings appSettings = AppSettings.get();
ThemeHelper.getInstance(appSettings);
AppLog.v(this, "OpenExternalLinkReceiver.onReceive(): url");

View file

@ -48,7 +48,7 @@ public class BadgeDrawable extends Drawable {
public BadgeDrawable(Context context) {
float textSize = context.getResources().getDimension(R.dimen.textsize_badge_count);
AppSettings settings = new AppSettings(context);
AppSettings settings = AppSettings.get();
badgeBackground = new Paint();
badgeBackground.setColor(settings.getAccentColor());
badgeBackground.setAntiAlias(true);

View file

@ -159,7 +159,7 @@ public class PodSelectionDialog extends ThemedAppCompatDialogFragment {
if (isAdded()) {
return ((App) getActivity().getApplication()).getSettings();
} else {
return new AppSettings(getContext().getApplicationContext());
return AppSettings.get();
}
}

View file

@ -64,7 +64,7 @@ public class ThemeHelper {
public static ThemeHelper getInstance() {
if (instance == null)
throw new IllegalStateException("ThemeHelper must be initialized using getInstance(AppSettings) before it can be used!");
throw new IllegalStateException("ThemeHelper must be initialized using getInstance(AppSettingsBase) before it can be used!");
return instance;
}
@ -154,7 +154,7 @@ public class ThemeHelper {
}
public static int getNeutralGreyColor() {
return ContextCompat.getColor(getInstance().appSettings.getApplicationContext(), R.color.md_grey_800);
return ContextCompat.getColor(getInstance().appSettings.getContext(), R.color.md_grey_800);
}
public static void updateAlertDialogColor(AlertDialog alertDialog) {

View file

@ -100,8 +100,8 @@ public abstract class ThemedActivity extends AppCompatActivity {
public void updateLanguage() {
AppSettings appSettings = getAppSettings();
Locale locale = Helpers.getLocaleByAndroidCode(appSettings.getLanguage());
Configuration config = appSettings.getApplicationContext().getResources().getConfiguration();
Configuration config = appSettings.getContext().getResources().getConfiguration();
config.locale = locale != null ? locale : Locale.getDefault();
appSettings.getApplicationContext().getResources().updateConfiguration(config, null);
appSettings.getContext().getResources().updateConfiguration(config, null);
}
}

View file

@ -42,7 +42,7 @@ public class ThemedCheckBoxPreference extends CheckBoxPreference implements Them
@Override
public void setColors() {
CheckBox checkBox = (CheckBox) rootLayout.findViewById(android.R.id.checkbox);
ThemeHelper.getInstance(new AppSettings(getContext()));
ThemeHelper.getInstance(AppSettings.get());
ThemeHelper.updateCheckBoxColor(checkBox);
}
}

View file

@ -47,7 +47,7 @@ public class ThemedColorPickerPreference extends Preference implements Themeable
Drawable circle;
if (colorPreview != null && (circle = colorPreview.getDrawable()) != null) {
Context c = getContext();
AppSettings appSettings = new AppSettings(getContext());
AppSettings appSettings = AppSettings.get();
String key = getKey();
int color = Helpers.getColorFromRessource(c, R.color.primary);

View file

@ -44,7 +44,7 @@ public class ThemedPreferenceCategory extends PreferenceCategory implements Them
@Override
public void setColors() {
if (titleTextView != null) {
ThemeHelper.getInstance(new AppSettings(getContext()));
ThemeHelper.getInstance(AppSettings.get());
ThemeHelper.updateTextViewTextColor(titleTextView);
}
}

View file

@ -29,7 +29,7 @@ public class ThemedVisibilityPreference extends ThemedCheckBoxPreference {
public void setColors() {
CheckBox checkBox = (CheckBox) rootLayout.findViewById(android.R.id.checkbox);
checkBox.setButtonDrawable(R.drawable.ic_visibility_selector);
ThemeHelper.getInstance(new AppSettings(getContext()));
ThemeHelper.getInstance(AppSettings.get());
ThemeHelper.updateCheckBoxColor(checkBox);
}
}

View file

@ -17,9 +17,8 @@ package com.github.dfa.diaspora_android.util;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.ColorRes;
import android.support.v4.content.ContextCompat;
import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.BuildConfig;
import com.github.dfa.diaspora_android.R;
import com.github.dfa.diaspora_android.data.DiasporaAspect;
@ -31,24 +30,24 @@ import org.json.JSONObject;
import java.util.List;
import io.github.gsantner.opoc.util.AppSettingsBase;
/**
* Settings
* Created by gsantner (https://gsantner.github.io/) on 20.03.16. Part of dandelion*.
*/
public class AppSettings {
private final SharedPreferences prefApp;
@SuppressWarnings("ConstantConditions")
public class AppSettings extends AppSettingsBase {
private final SharedPreferences prefPod;
private final Context context;
private DiasporaPod currentPod0Cached;
public AppSettings(Context context) {
this.context = context.getApplicationContext();
prefApp = this.context.getSharedPreferences("app", Context.MODE_PRIVATE);
prefPod = this.context.getSharedPreferences("pod0", Context.MODE_PRIVATE);
public static AppSettings get() {
return new AppSettings(App.get());
}
public Context getApplicationContext() {
return context;
private AppSettings(Context context) {
super(context);
prefPod = this.context.getSharedPreferences("pod0", Context.MODE_PRIVATE);
}
/**
@ -58,8 +57,8 @@ public class AppSettings {
* kills the app after the calling this, so we have to block until we are finished.
*/
@SuppressLint("CommitPrefEdits")
public void clearPodSettings() {
prefPod.edit().clear().commit();
public void resetPodSettings() {
super.resetSettings(prefPod);
}
/**
@ -69,92 +68,13 @@ public class AppSettings {
* kills the app after the calling this, so we have to block until we are finished.
*/
@SuppressLint("CommitPrefEdits")
public void clearAppSettings() {
prefApp.edit().clear().commit();
public void resetAppSettings() {
super.resetSettings(prefApp);
}
public String getKey(int stringKeyResourceId) {
return context.getString(stringKeyResourceId);
}
public boolean isKeyEqual(String key, int stringKeyRessourceId) {
return key.equals(getKey(stringKeyRessourceId));
}
private void setString(SharedPreferences pref, int keyRessourceId, String value) {
pref.edit().putString(context.getString(keyRessourceId), value).apply();
}
private void setInt(SharedPreferences pref, int keyRessourceId, int value) {
pref.edit().putInt(context.getString(keyRessourceId), value).apply();
}
private void setLong(SharedPreferences pref, int keyRessourceId, long value) {
pref.edit().putLong(context.getString(keyRessourceId), value).apply();
}
private void setBool(SharedPreferences pref, int keyRessourceId, boolean value) {
pref.edit().putBoolean(context.getString(keyRessourceId), value).apply();
}
private void setStringArray(SharedPreferences pref, int keyRessourceId, Object[] values) {
StringBuilder sb = new StringBuilder();
for (Object value : values) {
sb.append("%%%");
sb.append(value.toString());
}
setString(pref, keyRessourceId, sb.toString().replaceFirst("%%%", ""));
}
private String[] getStringArray(SharedPreferences pref, int keyRessourceId) {
String value = pref.getString(context.getString(keyRessourceId), "%%%");
if (value.equals("%%%")) {
return new String[0];
}
return value.split("%%%");
}
private String getString(SharedPreferences pref, int ressourceId, String defaultValue) {
return pref.getString(context.getString(ressourceId), defaultValue);
}
private String getString(SharedPreferences pref, int ressourceId, int ressourceIdDefaultValue) {
return pref.getString(context.getString(ressourceId), context.getString(ressourceIdDefaultValue));
}
private boolean getBool(SharedPreferences pref, int ressourceId, boolean defaultValue) {
return pref.getBoolean(context.getString(ressourceId), defaultValue);
}
private int getInt(SharedPreferences pref, int ressourceId, int defaultValue) {
return pref.getInt(context.getString(ressourceId), defaultValue);
}
private long getLong(SharedPreferences pref, int ressourceId, long defaultValue) {
return pref.getLong(context.getString(ressourceId), defaultValue);
}
public int getColor(SharedPreferences pref, String key, int defaultColor) {
return pref.getInt(key, defaultColor);
}
public int getColorRes(@ColorRes int resColorId){
return ContextCompat.getColor(context, resColorId);
}
public void registerPrefAppPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
prefApp.registerOnSharedPreferenceChangeListener(listener);
}
public void unregisterPrefAppPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
prefApp.unregisterOnSharedPreferenceChangeListener(listener);
}
/*
// Setters & Getters
*/
//#################################
//## Getter & Setter for settings
//#################################
public String getProfileId() {
return getString(prefPod, R.string.pref_key__podprofile_id, "");
}
@ -249,7 +169,7 @@ public class AppSettings {
}
public void setFollowedTagsFavs(List<String> values) {
setStringArray(prefPod, R.string.pref_key__podprofile_followed_tags_favs, values.toArray(new String[values.size()]));
setStringList(prefPod, R.string.pref_key__podprofile_followed_tags_favs, values);
}
public String[] getAspectFavs() {
@ -257,7 +177,7 @@ public class AppSettings {
}
public void setAspectFavs(List<String> values) {
setStringArray(prefPod, R.string.pref_key__podprofile_aspects_favs, values.toArray(new String[values.size()]));
setStringList(prefPod, R.string.pref_key__podprofile_aspects_favs, values);
}
public int getUnreadMessageCount() {
@ -467,14 +387,14 @@ public class AppSettings {
public int[] getPrimaryColorSettings() {
return new int[]{
getInt(prefApp, R.string.pref_key__primary_color_base, getColorRes(R.color.md_blue_650)),
getInt(prefApp, R.string.pref_key__primary_color_shade, getColorRes(R.color.primary))
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))
};
}
@SuppressWarnings("ConstantConditions")
public int getPrimaryColor() {
return getInt(prefApp, R.string.pref_key__primary_color_shade, getColorRes(
return getInt(prefApp, R.string.pref_key__primary_color_shade, rcolor(
BuildConfig.IS_TEST_BUILD ? R.color.md_brown_800 : R.color.primary));
}
@ -485,13 +405,13 @@ public class AppSettings {
public int[] getAccentColorSettings() {
return new int[]{
getInt(prefApp, R.string.pref_key__accent_color_base, getColorRes(R.color.md_green_400)),
getInt(prefApp, R.string.pref_key__accent_color_shade, getColorRes(R.color.accent))
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))
};
}
public int getAccentColor() {
return getInt(prefApp, R.string.pref_key__accent_color_shade, getColorRes(R.color.accent));
return getInt(prefApp, R.string.pref_key__accent_color_shade, rcolor(R.color.accent));
}
public boolean isExtendedNotificationsActivated() {

View file

@ -68,7 +68,7 @@ public class DiasporaUrlHelper {
}
/**
* Return a url of the pod set in AppSettings.
* Return a url of the pod set in AppSettingsBase.
* Eg. https://pod.geraspora.de
*
* @return https://(pod-domain.tld)

View file

@ -52,6 +52,8 @@ import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
import io.github.gsantner.opoc.util.SimpleMarkdownParser;
public class Helpers {
public static int getColorFromRessource(Context context, int ressourceId) {
Resources res = context.getResources();

View file

@ -62,7 +62,7 @@ public class DiasporaStreamWebChromeClient extends FileUploadWebChromeClient {
@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
ThemedAlertDialogBuilder builder = new ThemedAlertDialogBuilder(view.getContext(), new AppSettings(view.getContext()));
ThemedAlertDialogBuilder builder = new ThemedAlertDialogBuilder(view.getContext(), AppSettings.get());
builder.setTitle(view.getContext().getString(R.string.confirmation))
.setMessage(message)
.setPositiveButton(android.R.string.ok,

View file

@ -55,7 +55,7 @@ public class ProxyHandler {
public void updateProxySettings(Context context) {
AppLog.d(this, "UpdateProxySettings()");
AppSettings appSettings = new AppSettings(context);
AppSettings appSettings = AppSettings.get();
StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
StrictMode.ThreadPolicy tmp = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(tmp);
@ -86,7 +86,7 @@ public class ProxyHandler {
private void updateWebViewProxySettings(WebView wv, Context context) {
AppLog.d(this, "UpdateWebViewProxySettings()");
AppSettings appSettings = new AppSettings(context);
AppSettings appSettings = AppSettings.get();
StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
StrictMode.ThreadPolicy tmp = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(tmp);

View file

@ -0,0 +1,323 @@
/*
* ----------------------------------------------------------------------------
* "THE COKE-WARE LIBRARY LICENSE" (Revision 255):
* Gregor Santner <gsantner.github.io> wrote this file. You can do whatever
* you want with this stuff. If we meet some day, and you think this stuff is
* worth it, you can buy me a coke in return. Provided as is without any kind
* of warranty. No attribution required. - Gregor Santner
* ----------------------------------------------------------------------------
*/
/*
* Get updates:
* https://github.com/gsantner/onePieceOfCode/blob/master/java/AppSettingsBase.java
* This is a wrapper for settings based on SharedPreferences
* with keys in resources. Extend from this class and add
* getters/setters for the app's settings.
* Example:
public boolean isAppFirstStart() {
return getBool(prefApp, R.string.pref_key__app_first_start, true);
}
public void setAppFirstStart(boolean value) {
setBool(prefApp, R.string.pref_key__app_first_start, value);
}
public boolean isAppFirstStartCurrentVersion() {
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);
return value != BuildConfig.VERSION_CODE && !BuildConfig.IS_TEST_BUILD;
}
* Maybe add a singleton for this:
* Whereas App.get() is returning ApplicationContext
private AppSettings(Context context) {
super(context);
}
public static AppSettings get() {
return new AppSettings(App.get());
}
*/
package io.github.gsantner.opoc.util;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Wrapper for settings based on SharedPreferences
* with keys in resources
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class AppSettingsBase {
protected static final String ARRAY_SEPARATOR = "%%%";
protected static final String ARRAY_SEPARATOR_SUBSTITUTE = "§§§";
public static final String SHARED_PREF_APP = "app";
//#####################
//## Members
//#####################
protected final SharedPreferences prefApp;
protected final Context context;
//#####################
//## Methods
//#####################
public AppSettingsBase(Context context) {
this(context, SHARED_PREF_APP);
}
public AppSettingsBase(Context context, String prefAppName) {
this.context = context.getApplicationContext();
prefApp = this.context.getSharedPreferences(prefAppName, Context.MODE_PRIVATE);
}
public Context getContext() {
return context;
}
public boolean isKeyEqual(String key, int stringKeyResourceId) {
return key.equals(rstr(stringKeyResourceId));
}
public void resetSettings() {
resetSettings(prefApp);
}
@SuppressLint("ApplySharedPref")
public void resetSettings(SharedPreferences pref) {
pref.edit().clear().commit();
}
public boolean isPrefSet(@StringRes int stringKeyResourceId) {
return isPrefSet(prefApp, stringKeyResourceId);
}
public boolean isPrefSet(SharedPreferences pref, @StringRes int stringKeyResourceId) {
return pref.contains(rstr(stringKeyResourceId));
}
public void registerPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener value) {
registerPreferenceChangedListener(prefApp, value);
}
public void registerPreferenceChangedListener(SharedPreferences pref, SharedPreferences.OnSharedPreferenceChangeListener value) {
pref.registerOnSharedPreferenceChangeListener(value);
}
public void unregisterPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener value) {
unregisterPreferenceChangedListener(prefApp, value);
}
public void unregisterPreferenceChangedListener(SharedPreferences pref, SharedPreferences.OnSharedPreferenceChangeListener value) {
pref.unregisterOnSharedPreferenceChangeListener(value);
}
//#################################
//## Getter for resources
//#################################
public String rstr(@StringRes int stringKeyResourceId) {
return context.getString(stringKeyResourceId);
}
public int rcolor(@ColorRes int resColorId) {
return ContextCompat.getColor(context, resColorId);
}
//#################################
//## Getter & Setter for settings
//#################################
public void setString(@StringRes int keyResourceId, String value) {
setString(prefApp, keyResourceId, value);
}
public void setString(SharedPreferences pref, @StringRes int keyResourceId, String value) {
pref.edit().putString(rstr(keyResourceId), value).apply();
}
public String getString(@StringRes int keyResourceId, String defaultValue) {
return getString(prefApp, keyResourceId, defaultValue);
}
public String getString(SharedPreferences pref, @StringRes int keyResourceId, String defaultValue) {
return pref.getString(rstr(keyResourceId), defaultValue);
}
public String getString(@StringRes int keyResourceId, @StringRes int keyResourceIdDefaultValue) {
return getString(prefApp, keyResourceId, keyResourceIdDefaultValue);
}
public String getString(SharedPreferences pref, @StringRes int keyResourceId, @StringRes int keyResourceIdDefaultValue) {
return pref.getString(rstr(keyResourceId), rstr(keyResourceIdDefaultValue));
}
public void setStringArray(@StringRes int keyResourceId, Object[] values) {
setStringArray(prefApp, keyResourceId, values);
}
public void setStringArray(SharedPreferences pref, @StringRes int keyResourceId, Object[] values) {
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, ""));
}
@NonNull
public String[] getStringArray(@StringRes int keyResourceId) {
return getStringArray(prefApp, keyResourceId);
}
@NonNull
public String[] getStringArray(SharedPreferences pref, @StringRes int keyResourceId) {
String value = pref.getString(rstr(keyResourceId), 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(SharedPreferences pref, @StringRes int keyResourceId, List<String> values) {
setStringArray(pref, keyResourceId, values.toArray(new String[values.size()]));
}
public ArrayList<String> getStringList(@StringRes int keyResourceId) {
return getStringList(prefApp, keyResourceId);
}
public ArrayList<String> getStringList(SharedPreferences pref, @StringRes int keyResourceId) {
return new ArrayList<>(Arrays.asList(getStringArray(pref, keyResourceId)));
}
public void setLong(@StringRes int keyResourceId, long value) {
setLong(prefApp, keyResourceId, value);
}
public void setLong(SharedPreferences pref, @StringRes int keyResourceId, long value) {
pref.edit().putLong(rstr(keyResourceId), value).apply();
}
public long getLong(@StringRes int keyResourceId, long defaultValue) {
return getLong(prefApp, keyResourceId, defaultValue);
}
public long getLong(SharedPreferences pref, @StringRes int keyResourceId, long defaultValue) {
return pref.getLong(rstr(keyResourceId), defaultValue);
}
public void setBool(@StringRes int keyResourceId, boolean value) {
setBool(prefApp, keyResourceId, value);
}
public void setBool(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(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(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(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(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(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);
}
public void setInt(@StringRes int keyResourceId, int value) {
setInt(prefApp, keyResourceId, value);
}
public void setInt(SharedPreferences pref, @StringRes int keyResourceId, int value) {
pref.edit().putInt(rstr(keyResourceId), value).apply();
}
public int getInt(@StringRes int keyResourceId, int defaultValue) {
return getInt(prefApp, keyResourceId, defaultValue);
}
public int getInt(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(SharedPreferences pref, @StringRes int keyResId, List<Integer> values) {
StringBuilder sb = new StringBuilder();
for (int value : values) {
sb.append(ARRAY_SEPARATOR);
sb.append(Integer.toString(value));
}
setString(prefApp, keyResId, sb.toString().replaceFirst(ARRAY_SEPARATOR, ""));
}
@NonNull
public ArrayList<Integer> getIntList(@StringRes int keyResId) {
return getIntList(prefApp, keyResId);
}
@NonNull
public ArrayList<Integer> getIntList(SharedPreferences pref, @StringRes int keyResId) {
ArrayList<Integer> ret = new ArrayList<>();
String value = getString(prefApp, keyResId, ARRAY_SEPARATOR);
if (value.equals(ARRAY_SEPARATOR)) {
return ret;
}
for (String s : value.split(ARRAY_SEPARATOR)) {
ret.add(Integer.parseInt(s));
}
return ret;
}
}

View file

@ -7,7 +7,69 @@
* of warranty. No attribution required. - Gregor Santner
* ----------------------------------------------------------------------------
*/
package com.github.dfa.diaspora_android.util;
/*
* Get updates:
* https://github.com/gsantner/onePieceOfCode/blob/master/java/SimpleMarkdownParser.java
* Parses most common markdown tags. Only inline tags are supported, multiline/block syntax
* is not supported (citation, multiline code, ..). This is intended to stay as easy as possible.
*
* You can e.g. apply a accent color by replacing #000001 with your accentColor string.
*
* FILTER_ANDROID_TEXTVIEW output is intended to be used at simple Android TextViews,
* were a limited set of html tags is supported. This allow to still display e.g. a simple
* CHANGELOG.md file without inlcuding a WebView for showing HTML, or other additional UI-libraries.
*
* FILTER_HTMLPART is intended to be used at engines understanding most common HTML tags.
*
* You can use this anywhere you want, no backlink/attribution required, but I would appreciate it.
*/
/*
// Apply to Android TextView:
textView.setText(new SpannableString(Html.fromHtml(htmlFromParser)));
// As wrapper method, includes applying accent color
public static String loadMarkdownFromRawForTextView(Context context, @RawRes int rawMdFile, String prepend) {
try {
return new SimpleMarkdownParser()
.parse(context.getResources().openRawResource(rawMdFile),
SimpleMarkdownParser.FILTER_ANDROID_TEXTVIEW, prepend)
.replaceColor("#000001", ContextCompat.getColor(context, R.color.accent))
.removeMultiNewlines().replaceBulletCharacter("*").getHtml();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
// Show HTML a TextView in a scrollable Dialog
public static void showDialogWithHtmlTextView(Context context, String html, @StringRes int resTitleId) {
LinearLayout layout = new LinearLayout(context);
TextView textView = new TextView(context);
textView.setMovementMethod(LinkMovementMethod.getInstance());
ScrollView root = new ScrollView(context);
int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,
context.getResources().getDisplayMetrics());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(margin, 0, margin, 0);
layout.setLayoutParams(layoutParams);
layout.addView(textView);
root.addView(layout);
textView.setText(new SpannableString(Html.fromHtml(html)));
AlertDialog.Builder dialog = new AlertDialog.Builder(context)
.setPositiveButton(android.R.string.ok, null)
.setTitle(resTitleId)
.setView(root);
dialog.show();
}
*/
package io.github.gsantner.opoc.util;
import java.io.BufferedReader;
import java.io.FileInputStream;
@ -17,19 +79,6 @@ import java.io.InputStreamReader;
/**
* Simple Markdown Parser
* <p>
* Parses most common markdown tags. Only inline tags are supported, multiline/block syntax
* is not supported (citation, multiline code, ..). This is intended to stay as easy as possible.
* <p>
* You can e.g. apply a accent color by replacing #000001 with your accentColor string.
* <p>
* FILTER_ANDROID_TEXTVIEW output is intended to be used at simple Android TextViews,
* were a limited set of html tags is supported. This allow to still display e.g. a simple
* CHANGELOG.md file without inlcuding a WebView for showing HTML, or other additional UI-libraries.
* <p>
* FILTER_HTMLPART is intended to be used at engines understanding most common HTML tags.
* <p>
* You can use this anywhere you want, no backlink/attribution required, but I would appreciate it.
*/
@SuppressWarnings({"WeakerAccess", "CaughtExceptionImmediatelyRethrown"})
public class SimpleMarkdownParser {
@ -157,46 +206,3 @@ public class SimpleMarkdownParser {
return html != null ? html : "";
}
}
/*
// Apply to Android TextView:
textView.setText(new SpannableString(Html.fromHtml(htmlFromParser)));
// As wrapper method, includes applying accent color
public static String loadMarkdownFromRawForTextView(Context context, @RawRes int rawMdFile, String prepend) {
try {
return new SimpleMarkdownParser()
.parse(context.getResources().openRawResource(rawMdFile),
SimpleMarkdownParser.FILTER_ANDROID_TEXTVIEW, prepend)
.replaceColor("#000001", ContextCompat.getColor(context, R.color.accent))
.removeMultiNewlines().replaceBulletCharacter("*").getHtml();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
// Show HTML a TextView in a scrollable Dialog
public static void showDialogWithHtmlTextView(Context context, String html, @StringRes int resTitleId) {
LinearLayout layout = new LinearLayout(context);
TextView textView = new TextView(context);
textView.setMovementMethod(LinkMovementMethod.getInstance());
ScrollView root = new ScrollView(context);
int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,
context.getResources().getDisplayMetrics());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(margin, 0, margin, 0);
layout.setLayoutParams(layoutParams);
layout.addView(textView);
root.addView(layout);
textView.setText(new SpannableString(Html.fromHtml(html)));
AlertDialog.Builder dialog = new AlertDialog.Builder(context)
.setPositiveButton(android.R.string.ok, null)
.setTitle(resTitleId)
.setView(root);
dialog.show();
}
*/

View file

@ -54,16 +54,16 @@
<string name="pref_key__visibility_nav__toggle_mobile_desktop" translatable="false">pref_key__visibility_nav__toggle_mobile_desktop</string>
<!-- PodProfile -->
<string name="pref_key__podprofile_avatar_url" translatable="false">podUserProfile_avatar</string>
<string name="pref_key__podprofile_name" translatable="false">podUserProfile_name</string>
<string name="pref_key__podprofile_id" translatable="false">podUserProfile_guid</string>
<string name="pref_key__podprofile_aspects" translatable="false">podUserProfile_aspects</string>
<string name="pref_key__podprofile_aspects_favs" translatable="false">podUserProfile_aspects_favs</string>
<string name="pref_key__podprofile_followed_tags" translatable="false">podUserProfile_followedTags</string>
<string name="pref_key__podprofile_followed_tags_favs" translatable="false">podUserProfile_followedTags_favs</string>
<string name="pref_key__podprofile_unread_message_count" translatable="false">podUserProfile_unreadMessageCount</string>
<string name="pref_key__podprofile_notification_count" translatable="false">podUserProfile_NotificationCount</string>
<string name="pref_key__podprofile_last_stream_position" translatable="false">podUserProfile_LastStreamPosition</string>
<string name="pref_key__podprofile_avatar_url" translatable="false">pref_key__podprofile_avatar_url</string>
<string name="pref_key__podprofile_name" translatable="false">pref_key__podprofile_name</string>
<string name="pref_key__podprofile_id" translatable="false">pref_key__podprofile_id</string>
<string name="pref_key__podprofile_aspects" translatable="false">pref_key__podprofile_aspects</string>
<string name="pref_key__podprofile_aspects_favs" translatable="false">pref_key__podprofile_aspects_favs</string>
<string name="pref_key__podprofile_followed_tags" translatable="false">pref_key__podprofile_followed_tags</string>
<string name="pref_key__podprofile_followed_tags_favs" translatable="false">pref_key__podprofile_followed_tags_favs</string>
<string name="pref_key__podprofile_unread_message_count" translatable="false">pref_key__podprofile_unread_message_count</string>
<string name="pref_key__podprofile_notification_count" translatable="false">pref_key__podprofile_notification_count</string>
<string name="pref_key__podprofile_last_stream_position" translatable="false">pref_key__podprofile_last_stream_position</string>
<!-- More -->
<string name="pref_key__logging_spam_enabled" translatable="false">pref_key__logging_spam_enabled</string>