1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2025-12-15 00:31:11 +01:00

Merge preferences from AppSettings and SettingsActivy/preferences.xml

This commit is contained in:
Gregor Santner 2016-08-02 21:32:42 +02:00
parent 3c6558df9c
commit ad2cb4c615
10 changed files with 226 additions and 190 deletions

View file

@ -538,15 +538,15 @@ public class MainActivity extends AppCompatActivity
@Override
public void onReceive(Context context, Intent intent) {
String url = intent.getStringExtra(EXTRA_URL);
Log.d(App.TAG, "BroadcastReceiver: Received setTitleIntent: "+url);
if (url != null && url.startsWith("https://"+podDomain)) {
String subUrl = url.substring(("https://"+podDomain).length());
Log.d(App.TAG, "LocalBroadcastReceiver: SubUrl: "+subUrl);
// Log.d(App.TAG, "BroadcastReceiver: Received setTitleIntent: "+url);
if (url != null && url.startsWith("https://" + podDomain)) {
String subUrl = url.substring(("https://" + podDomain).length());
//Log.d(App.TAG, "LocalBroadcastReceiver: SubUrl: "+subUrl); // Spams!
if (subUrl.startsWith("/stream")) {
setTitle(R.string.title_stream);
} else if (subUrl.startsWith("/posts/")) {
setTitle(R.string.diaspora); //TODO: Extract posts title somehow?
} else if(subUrl.startsWith("/notifications")) {
} else if (subUrl.startsWith("/notifications")) {
setTitle(R.string.title_notifications);
} else if (subUrl.startsWith("/conversations")) {
setTitle(R.string.title_conversations);

View file

@ -31,7 +31,6 @@ import android.preference.PreferenceScreen;
import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.R;
import com.github.dfa.diaspora_android.data.AppSettings;
/**
* @author vanitas
@ -48,13 +47,15 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
sharedPreferences = getPreferenceScreen().getSharedPreferences();
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
setPreferenceSummaries();
sharedPreferences.edit().putBoolean(AppSettings.PREF.PROXY_WAS_ENABLED,
sharedPreferences.getBoolean(AppSettings.PREF.PROXY_ENABLED, false)).apply();
sharedPreferences.edit().putBoolean(getString(R.string.pref_key__proxy_was_enabled),
sharedPreferences.getBoolean(getString(R.string.pref_key__proxy_enabled), false)).apply();
}
private void setPreferenceSummaries() {
String[] editTextKeys = new String[]{AppSettings.PREF.PROXY_HOST, AppSettings.PREF.PROXY_PORT};
for(String key : editTextKeys) {
String[] editTextKeys = new String[]{
getString(R.string.pref_key__proxy_host), getString(R.string.pref_key__proxy_port)
};
for (String key : editTextKeys) {
EditTextPreference p = (EditTextPreference) findPreference(key);
p.setSummary(p.getText());
}
@ -66,13 +67,15 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
}
private void updatePreference(Preference preference, String key) {
if (preference == null) return;
if (preference == null) {
return;
}
if (preference instanceof EditTextPreference) {
EditTextPreference textPref = (EditTextPreference) preference;
textPref.setSummary(textPref.getText());
return;
}
if(preference instanceof ListPreference) {
if (preference instanceof ListPreference) {
ListPreference listPref = (ListPreference) preference;
listPref.setSummary(listPref.getEntry());
return;
@ -82,24 +85,28 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
@Override
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
Intent intent = new Intent(this, MainActivity.class);
String podDomain = ((App)getApplication()).getSettings().getPodDomain();
switch(preference.getKey()) {
case "pref_key_personal_settings":
String podDomain = ((App) getApplication()).getSettings().getPodDomain();
switch (preference.getTitleRes()) {
case R.string.pref_title__personal_settings: {
intent.setAction(MainActivity.ACTION_OPEN_URL);
intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/user/edit");
break;
case "pref_key_manage_tags":
}
case R.string.pref_title__manage_tags: {
intent.setAction(MainActivity.ACTION_OPEN_URL);
intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/tag_followings/manage");
break;
case "pref_key_manage_contacts":
}
case R.string.pref_title__manage_contacts: {
intent.setAction(MainActivity.ACTION_OPEN_URL);
intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/contacts");
break;
case "pref_key_change_account":
}
case R.string.pref_title__change_account: {
new AlertDialog.Builder(SettingsActivity.this)
.setTitle(getString(R.string.confirmation))
.setMessage(getString(R.string.pref_warning_change_account))
.setMessage(getString(R.string.pref_warning__change_account))
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
@ -112,14 +119,17 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
})
.show();
return true;
case "pref_key_clear_cache":
}
case R.string.pref_title__clear_cache: {
intent.setAction(MainActivity.ACTION_CLEAR_CACHE);
break;
default:
}
default: {
intent = null;
break;
}
}
if(intent != null) {
if (intent != null) {
startActivity(intent);
finish();
return true;

View file

@ -22,6 +22,8 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import com.github.dfa.diaspora_android.R;
/**
* Created by gsantner (https://gsantner.github.io/) on 20.03.16. Part of Diaspora for Android.
*/
@ -48,53 +50,41 @@ public class AppSettings {
prefApp.edit().clear().apply();
}
private void setString(SharedPreferences pref, String key, String value) {
pref.edit().putString(key, value).apply();
private void setString(SharedPreferences pref, int keyRessourceId, String value) {
pref.edit().putString(context.getString(keyRessourceId), value).apply();
}
private void setInt(SharedPreferences pref, String key, int value) {
pref.edit().putInt(key, value).apply();
private void setInt(SharedPreferences pref, int keyRessourceId, int value) {
pref.edit().putInt(context.getString(keyRessourceId), value).apply();
}
private void setBool(SharedPreferences pref, String key, boolean value) {
pref.edit().putBoolean(key, value).apply();
private void setBool(SharedPreferences pref, int keyRessourceId, boolean value) {
pref.edit().putBoolean(context.getString(keyRessourceId), value).apply();
}
private void setStringArray(SharedPreferences pref, String key, Object[] values) {
private void setStringArray(SharedPreferences pref, int keyRessourceId, Object[] values) {
StringBuffer sb = new StringBuffer();
for (Object value : values) {
sb.append("%%%");
sb.append(value.toString());
}
setString(pref, key, sb.toString().replaceFirst("%%%", ""));
setString(pref, keyRessourceId, sb.toString().replaceFirst("%%%", ""));
}
private String[] getStringArray(SharedPreferences pref, String key) {
String value = pref.getString(key, "%%%");
private String[] getStringArray(SharedPreferences pref, int keyRessourceId) {
String value = pref.getString(context.getString(keyRessourceId), "%%%");
if (value.equals("%%%")) {
return new String[0];
}
return value.split("%%%");
}
/*
// Preferences
*/
public static class PREF {
public static final String PREVIOUS_PODLIST = "previousPodlist";
public static final String IS_LOAD_IMAGES = "pref_key_load_images";
public static final String MINIMUM_FONT_SIZE = "pref_key_font_size";
public static final String PODUSERPROFILE_AVATAR_URL = "podUserProfile_avatar";
public static final String PODUSERPROFILE_NAME = "podUserProfile_name";
public static final String PODUSERPROFILE_ID = "podUserProfile_guid";
public static final String PODDOMAIN = "podDomain";
public static final String PODUSERPROFILE_ASPECTS = "podUserProfile_aspects";
public static final String PODUSERPROFILE_FOLLOWED_TAGS = "podUserProfile_followedTags";
public static final String PROXY_ENABLED = "pref_key_proxy_enabled";
public static final String PROXY_WAS_ENABLED = "wasProxyEnabled";
public static final String PROXY_HOST = "pref_key_proxy_host";
public static final String PROXY_PORT = "pref_key_proxy_port";
public static final String UI_INTELLIHIDE_TOOLBARS ="pref_key_intellihide_toolbars";
private String getString(SharedPreferences pref, int ressourceId, String defaultValue) {
return pref.getString(context.getString(ressourceId), defaultValue);
}
private boolean getBoolean(SharedPreferences pref, int ressourceId, boolean defaultValue) {
return pref.getBoolean(context.getString(ressourceId), defaultValue);
}
@ -102,19 +92,19 @@ public class AppSettings {
// Setters & Getters
*/
public String getProfileId() {
return prefPod.getString(PREF.PODUSERPROFILE_ID, "");
return getString(prefPod, R.string.pref_key__podprofile_id, "");
}
public void setProfileId(String profileId) {
setString(prefPod, PREF.PODUSERPROFILE_ID, profileId);
setString(prefPod, R.string.pref_key__podprofile_id, profileId);
}
public boolean isLoadImages() {
return prefApp.getBoolean(PREF.IS_LOAD_IMAGES, true);
return getBoolean(prefApp, R.string.pref_key__load_images, true);
}
public int getMinimumFontSize() {
switch (prefApp.getString(PREF.MINIMUM_FONT_SIZE, "")) {
switch (getString(prefApp, R.string.pref_key__font_size, "")) {
case "huge":
return 20;
case "large":
@ -122,118 +112,122 @@ public class AppSettings {
case "normal":
return 8;
default:
prefApp.edit().putString(PREF.MINIMUM_FONT_SIZE, "normal").apply();
setString(prefApp, R.string.pref_key__font_size, "normal");
return 8;
}
}
public String getAvatarUrl() {
return prefPod.getString(PREF.PODUSERPROFILE_AVATAR_URL, "");
return getString(prefPod, R.string.pref_key__podprofile_avatar_url, "");
}
public void setAvatarUrl(String avatarUrl) {
setString(prefPod, PREF.PODUSERPROFILE_AVATAR_URL, avatarUrl);
setString(prefPod, R.string.pref_key__podprofile_avatar_url, avatarUrl);
}
public String getName() {
return prefPod.getString(PREF.PODUSERPROFILE_NAME, "");
return getString(prefPod, R.string.pref_key__podprofile_name, "");
}
public void setName(String name) {
setString(prefPod, PREF.PODUSERPROFILE_NAME, name);
setString(prefPod, R.string.pref_key__podprofile_name, name);
}
public String getPodDomain() {
return prefPod.getString(PREF.PODDOMAIN, "");
return getString(prefPod, R.string.pref_key__poddomain, "");
}
public void setPodDomain(String podDomain) {
setString(prefPod, PREF.PODDOMAIN, podDomain);
setString(prefPod, R.string.pref_key__poddomain, podDomain);
}
public boolean hasPodDomain() {
return !prefPod.getString(PREF.PODDOMAIN, "").equals("");
return !getString(prefPod, R.string.pref_key__poddomain, "").equals("");
}
public String[] getPreviousPodlist() {
return getStringArray(prefApp, PREF.PREVIOUS_PODLIST);
return getStringArray(prefApp, R.string.pref_key__previous_podlist);
}
public void setPreviousPodlist(String[] pods) {
setStringArray(prefApp, PREF.PREVIOUS_PODLIST, pods);
setStringArray(prefApp, R.string.pref_key__previous_podlist, pods);
}
public void setPodAspects(PodAspect[] aspects) {
setStringArray(prefPod, PREF.PODUSERPROFILE_ASPECTS, aspects);
setStringArray(prefPod, R.string.pref_key__podprofile_aspects, aspects);
}
public PodAspect[] getPodAspects() {
String[] s= getStringArray(prefPod, PREF.PODUSERPROFILE_ASPECTS);
String[] s = getStringArray(prefPod, R.string.pref_key__podprofile_aspects);
PodAspect[] aspects = new PodAspect[s.length];
for(int i=0; i < aspects.length; i++){
for (int i = 0; i < aspects.length; i++) {
aspects[i] = new PodAspect(s[i]);
}
return aspects;
}
public String[] getFollowedTags() {
return getStringArray(prefPod, PREF.PODUSERPROFILE_FOLLOWED_TAGS);
return getStringArray(prefPod, R.string.pref_key__podprofile_followed_tags);
}
public void setFollowedTags(String[] tags) {
setStringArray(prefPod, PREF.PODUSERPROFILE_FOLLOWED_TAGS, tags);
setStringArray(prefPod, R.string.pref_key__podprofile_followed_tags, tags);
}
@SuppressLint("CommitPrefEdits")
public void setProxyEnabled(boolean enabled) {
//commit instead of apply because the app is likely to be killed before apply is called.
prefApp.edit().putBoolean(PREF.PROXY_ENABLED, enabled).commit();
prefApp.edit().putBoolean(context.getString(R.string.pref_key__proxy_enabled), enabled).commit();
}
/**
* Default return value: false
*
* @return whether proxy is enabled or not
*/
public boolean isProxyEnabled() {
return prefApp.getBoolean(PREF.PROXY_ENABLED, false);
return getBoolean(prefApp, R.string.pref_key__proxy_enabled, false);
}
public boolean wasProxyEnabled() {
return prefApp.getBoolean(PREF.PROXY_WAS_ENABLED, false);
return getBoolean(prefApp, R.string.pref_key__proxy_was_enabled, false);
}
/**
* Needed in order to determine, whether the proxy has just been disabled (trigger app restart)
* or if proxy was disabled before (do not restart app)
*
* @param b new value
*/
@SuppressLint("CommitPrefEdits")
public void setProxyWasEnabled(boolean b) {
prefApp.edit().putBoolean(PREF.PROXY_WAS_ENABLED, b).commit();
prefApp.edit().putBoolean(context.getString(R.string.pref_key__proxy_was_enabled), b).commit();
}
/**
* Default value: ""
*
* @return proxy host
*/
public String getProxyHost() {
return prefApp.getString(PREF.PROXY_HOST, "");
return getString(prefApp, R.string.pref_key__proxy_host, "");
}
/**
* Default value: 0
*
* @return proxy port
*/
public int getProxyPort() {
try {
return Integer.parseInt(prefApp.getString(PREF.PROXY_PORT, "0"));
return Integer.parseInt(getString(prefApp, R.string.pref_key__proxy_port, "0"));
} catch (Exception e) {
prefApp.edit().putString(PREF.PROXY_PORT, "0").apply();
setString(prefApp, R.string.pref_key__proxy_port, "0");
return 0;
}
}
public boolean isIntellihideToolbars(){
return prefApp.getBoolean(PREF.UI_INTELLIHIDE_TOOLBARS, true);
public boolean isIntellihideToolbars() {
return getBoolean(prefApp, R.string.pref_key__intellihide_toolbars, true);
}
}