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;
|
2016-03-20 22:49:11 +01:00
|
|
|
|
2016-06-17 19:43:28 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2016-03-20 22:49:11 +01:00
|
|
|
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.
|
2016-03-20 22:49:11 +01:00
|
|
|
*/
|
2016-03-26 01:14:17 +01:00
|
|
|
public class AppSettings {
|
2016-06-04 03:48:57 +02:00
|
|
|
private final SharedPreferences prefApp;
|
|
|
|
private final SharedPreferences prefPod;
|
2016-03-25 00:12:00 +01:00
|
|
|
private final Context context;
|
2016-03-20 22:49:11 +01:00
|
|
|
|
2016-03-24 11:58:28 +01:00
|
|
|
public AppSettings(Context context) {
|
2016-03-25 00:12:00 +01:00
|
|
|
this.context = context.getApplicationContext();
|
2016-06-04 03:48:57 +02:00
|
|
|
prefApp = this.context.getSharedPreferences("app", Context.MODE_PRIVATE);
|
|
|
|
prefPod = this.context.getSharedPreferences("pod0", Context.MODE_PRIVATE);
|
2016-03-20 22:49:11 +01:00
|
|
|
}
|
|
|
|
|
2016-06-05 17:25:11 +02:00
|
|
|
public Context getApplicationContext() {
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
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();
|
|
|
|
}
|
2016-03-26 01:14:17 +01:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
private void setInt(SharedPreferences pref, String key, int value) {
|
2016-03-24 11:58:28 +01:00
|
|
|
pref.edit().putInt(key, value).apply();
|
|
|
|
}
|
2016-03-26 01:14:17 +01:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
private void setBool(SharedPreferences pref, String key, boolean value) {
|
2016-03-24 11:58:28 +01:00
|
|
|
pref.edit().putBoolean(key, value).apply();
|
2016-03-20 22:49:11 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2016-06-04 03:48:57 +02:00
|
|
|
public static class PREF {
|
2016-06-11 00:28:26 +02:00
|
|
|
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";
|
2016-06-16 23:36:52 +02:00
|
|
|
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-07-31 15:16:22 +02:00
|
|
|
public static final String UI_INTELLIHIDE_TOOLBARS ="pref_key_intellihide_toolbars";
|
2016-06-04 03:48:57 +02:00
|
|
|
}
|
2016-03-25 00:12:00 +01:00
|
|
|
|
2016-03-24 11:58:28 +01:00
|
|
|
|
2016-03-20 22:49:11 +01:00
|
|
|
/*
|
|
|
|
// Setters & Getters
|
|
|
|
*/
|
2016-03-24 11:58:28 +01:00
|
|
|
public String getProfileId() {
|
2016-06-04 03:48:57 +02:00
|
|
|
return prefPod.getString(PREF.PODUSERPROFILE_ID, "");
|
2016-03-20 22:49:11 +01:00
|
|
|
}
|
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-20 22:49:11 +01:00
|
|
|
}
|
2016-03-24 11:58:28 +01:00
|
|
|
|
|
|
|
public boolean isLoadImages() {
|
2016-06-04 03:48:57 +02:00
|
|
|
return prefApp.getBoolean(PREF.IS_LOAD_IMAGES, true);
|
2016-03-24 11:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getMinimumFontSize() {
|
2016-06-16 22:55:06 +02:00
|
|
|
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
|
|
|
}
|
2016-03-25 00:12:00 +01:00
|
|
|
|
|
|
|
public String getAvatarUrl() {
|
2016-06-04 03:48:57 +02:00
|
|
|
return prefPod.getString(PREF.PODUSERPROFILE_AVATAR_URL, "");
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setAvatarUrl(String avatarUrl) {
|
2016-06-04 03:48:57 +02:00
|
|
|
setString(prefPod, PREF.PODUSERPROFILE_AVATAR_URL, avatarUrl);
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:17 +01:00
|
|
|
public String getName() {
|
2016-06-04 03:48:57 +02:00
|
|
|
return prefPod.getString(PREF.PODUSERPROFILE_NAME, "");
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:17 +01:00
|
|
|
public void setName(String name) {
|
2016-06-04 03:48:57 +02:00
|
|
|
setString(prefPod, PREF.PODUSERPROFILE_NAME, name);
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:17 +01:00
|
|
|
public String getPodDomain() {
|
2016-06-04 03:48:57 +02:00
|
|
|
return prefPod.getString(PREF.PODDOMAIN, "");
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:17 +01:00
|
|
|
public void setPodDomain(String podDomain) {
|
2016-06-04 03:48:57 +02:00
|
|
|
setString(prefPod, PREF.PODDOMAIN, podDomain);
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-06-05 17:25:11 +02:00
|
|
|
public boolean hasPodDomain() {
|
2016-06-04 03:48:57 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-06-17 19:43:28 +02:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:36:52 +02:00
|
|
|
public boolean wasProxyEnabled() {
|
|
|
|
return prefApp.getBoolean(PREF.PROXY_WAS_ENABLED, false);
|
|
|
|
}
|
|
|
|
|
2016-06-17 19:43:28 +02:00
|
|
|
/**
|
|
|
|
* 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")
|
2016-06-16 23:36:52 +02:00
|
|
|
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() {
|
2016-06-16 23:36:52 +02:00
|
|
|
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
|
|
|
}
|
2016-07-31 15:16:22 +02:00
|
|
|
|
|
|
|
public boolean isIntellihideToolbars(){
|
|
|
|
return prefApp.getBoolean(PREF.UI_INTELLIHIDE_TOOLBARS, true);
|
|
|
|
}
|
2016-03-20 22:49:11 +01:00
|
|
|
}
|