dandelion/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java

235 lines
7.5 KiB
Java
Raw Normal View History

2016-07-29 14:00:28 +02:00
/*
This file is part of the Diaspora for Android.
Diaspora for Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Diaspora for Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the Diaspora for Android.
If not, see <http://www.gnu.org/licenses/>.
*/
2016-03-29 19:38:50 +02:00
package com.github.dfa.diaspora_android.data;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
/**
2016-07-29 14:00:28 +02:00
* Created by gsantner (https://gsantner.github.io/) on 20.03.16. Part of Diaspora for Android.
*/
public class AppSettings {
private final SharedPreferences prefApp;
private final SharedPreferences prefPod;
private final Context context;
2016-03-24 11:58:28 +01:00
public AppSettings(Context context) {
this.context = context.getApplicationContext();
prefApp = this.context.getSharedPreferences("app", Context.MODE_PRIVATE);
prefPod = this.context.getSharedPreferences("pod0", Context.MODE_PRIVATE);
}
2016-06-05 17:25:11 +02:00
public Context getApplicationContext() {
return context;
}
public void clearPodSettings() {
prefPod.edit().clear().apply();
}
public void clearAppSettings() {
prefApp.edit().clear().apply();
}
private void setString(SharedPreferences pref, String key, String value) {
2016-03-24 11:58:28 +01:00
pref.edit().putString(key, value).apply();
}
private void setInt(SharedPreferences pref, String key, int value) {
2016-03-24 11:58:28 +01:00
pref.edit().putInt(key, value).apply();
}
private void setBool(SharedPreferences pref, String key, boolean value) {
2016-03-24 11:58:28 +01:00
pref.edit().putBoolean(key, value).apply();
}
2016-06-05 17:25:11 +02:00
private void setStringArray(SharedPreferences pref, String key, Object[] values) {
2016-06-05 13:57:34 +02:00
StringBuffer sb = new StringBuffer();
2016-06-05 17:25:11 +02:00
for (Object value : values) {
2016-06-05 13:57:34 +02:00
sb.append("%%%");
2016-06-05 17:25:11 +02:00
sb.append(value.toString());
2016-06-05 13:57:34 +02:00
}
2016-06-05 17:25:11 +02:00
setString(pref, key, sb.toString().replaceFirst("%%%", ""));
2016-06-05 13:57:34 +02:00
}
2016-06-05 17:25:11 +02:00
private String[] getStringArray(SharedPreferences pref, String key) {
String value = pref.getString(key, "%%%");
if (value.equals("%%%")) {
2016-06-05 13:57:34 +02:00
return new String[0];
}
return value.split("%%%");
}
2016-03-24 11:58:28 +01:00
/*
// 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";
2016-07-18 14:02:18 +02:00
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";
}
2016-03-24 11:58:28 +01:00
/*
// Setters & Getters
*/
2016-03-24 11:58:28 +01:00
public String getProfileId() {
return prefPod.getString(PREF.PODUSERPROFILE_ID, "");
}
2016-03-24 11:58:28 +01:00
public void setProfileId(String profileId) {
2016-06-05 17:25:11 +02:00
setString(prefPod, PREF.PODUSERPROFILE_ID, profileId);
}
2016-03-24 11:58:28 +01:00
public boolean isLoadImages() {
return prefApp.getBoolean(PREF.IS_LOAD_IMAGES, true);
2016-03-24 11:58:28 +01:00
}
public int getMinimumFontSize() {
switch (prefApp.getString(PREF.MINIMUM_FONT_SIZE, "")) {
case "huge":
return 20;
case "large":
return 16;
case "normal":
return 8;
default:
prefApp.edit().putString(PREF.MINIMUM_FONT_SIZE, "normal").apply();
return 8;
}
2016-03-24 11:58:28 +01:00
}
public String getAvatarUrl() {
return prefPod.getString(PREF.PODUSERPROFILE_AVATAR_URL, "");
}
public void setAvatarUrl(String avatarUrl) {
setString(prefPod, PREF.PODUSERPROFILE_AVATAR_URL, avatarUrl);
}
public String getName() {
return prefPod.getString(PREF.PODUSERPROFILE_NAME, "");
}
public void setName(String name) {
setString(prefPod, PREF.PODUSERPROFILE_NAME, name);
}
public String getPodDomain() {
return prefPod.getString(PREF.PODDOMAIN, "");
}
public void setPodDomain(String podDomain) {
setString(prefPod, PREF.PODDOMAIN, podDomain);
}
2016-06-05 17:25:11 +02:00
public boolean hasPodDomain() {
return !prefPod.getString(PREF.PODDOMAIN, "").equals("");
}
2016-06-05 13:57:34 +02:00
2016-06-05 17:25:11 +02:00
public String[] getPreviousPodlist() {
2016-06-05 13:57:34 +02:00
return getStringArray(prefApp, PREF.PREVIOUS_PODLIST);
}
2016-06-05 17:25:11 +02:00
public void setPreviousPodlist(String[] pods) {
2016-06-05 13:57:34 +02:00
setStringArray(prefApp, PREF.PREVIOUS_PODLIST, pods);
}
2016-06-05 17:25:11 +02:00
public void setPodAspects(PodAspect[] aspects) {
setStringArray(prefPod, PREF.PODUSERPROFILE_ASPECTS, aspects);
}
public PodAspect[] getPodAspects() {
String[] s= getStringArray(prefPod, PREF.PODUSERPROFILE_ASPECTS);
PodAspect[] aspects = new PodAspect[s.length];
for(int i=0; i < aspects.length; i++){
aspects[i] = new PodAspect(s[i]);
}
return aspects;
}
2016-06-09 01:03:50 +02:00
2016-07-18 14:02:18 +02:00
public String[] getFollowedTags() {
return getStringArray(prefPod, PREF.PODUSERPROFILE_FOLLOWED_TAGS);
}
public void setFollowedTags(String[] tags) {
setStringArray(prefPod, PREF.PODUSERPROFILE_FOLLOWED_TAGS, tags);
}
@SuppressLint("CommitPrefEdits")
2016-06-09 01:03:50 +02:00
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();
}
/**
* Default return value: false
* @return whether proxy is enabled or not
*/
public boolean isProxyEnabled() {
return prefApp.getBoolean(PREF.PROXY_ENABLED, false);
}
public boolean wasProxyEnabled() {
return prefApp.getBoolean(PREF.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();
2016-06-09 01:03:50 +02:00
}
/**
* Default value: ""
* @return proxy host
*/
public String getProxyHost() {
return prefApp.getString(PREF.PROXY_HOST, "");
}
/**
* Default value: 0
* @return proxy port
*/
public int getProxyPort() {
try {
return Integer.parseInt(prefApp.getString(PREF.PROXY_PORT, "0"));
} catch (Exception e) {
prefApp.edit().putString(PREF.PROXY_PORT, "0").apply();
return 0;
}
2016-06-09 01:03:50 +02:00
}
}