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

262 lines
8.4 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;
import com.github.dfa.diaspora_android.R;
/**
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, int keyRessourceId, String value) {
pref.edit().putString(context.getString(keyRessourceId), value).apply();
2016-03-24 11:58:28 +01:00
}
private void setInt(SharedPreferences pref, int keyRessourceId, int value) {
pref.edit().putInt(context.getString(keyRessourceId), value).apply();
2016-03-24 11:58:28 +01:00
}
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) {
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
}
setString(pref, keyRessourceId, sb.toString().replaceFirst("%%%", ""));
2016-06-05 13:57:34 +02:00
}
private String[] getStringArray(SharedPreferences pref, int keyRessourceId) {
String value = pref.getString(context.getString(keyRessourceId), "%%%");
2016-06-05 17:25:11 +02:00
if (value.equals("%%%")) {
2016-06-05 13:57:34 +02:00
return new String[0];
}
return value.split("%%%");
}
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);
}
2016-08-07 11:39:20 +02:00
private int getInt(SharedPreferences pref, int ressourceId, int defaultValue) {
return pref.getInt(context.getString(ressourceId), defaultValue);
}
2016-03-24 11:58:28 +01:00
/*
// Setters & Getters
*/
2016-03-24 11:58:28 +01:00
public String getProfileId() {
return getString(prefPod, R.string.pref_key__podprofile_id, "");
}
2016-03-24 11:58:28 +01:00
public void setProfileId(String profileId) {
setString(prefPod, R.string.pref_key__podprofile_id, profileId);
}
2016-03-24 11:58:28 +01:00
public boolean isLoadImages() {
return getBoolean(prefApp, R.string.pref_key__load_images, true);
2016-03-24 11:58:28 +01:00
}
public int getMinimumFontSize() {
switch (getString(prefApp, R.string.pref_key__font_size, "")) {
case "huge":
return 20;
case "large":
return 16;
case "normal":
return 8;
default:
setString(prefApp, R.string.pref_key__font_size, "normal");
return 8;
}
2016-03-24 11:58:28 +01:00
}
public String getAvatarUrl() {
return getString(prefPod, R.string.pref_key__podprofile_avatar_url, "");
}
public void setAvatarUrl(String avatarUrl) {
setString(prefPod, R.string.pref_key__podprofile_avatar_url, avatarUrl);
}
public String getName() {
return getString(prefPod, R.string.pref_key__podprofile_name, "");
}
public void setName(String name) {
setString(prefPod, R.string.pref_key__podprofile_name, name);
}
public String getPodDomain() {
return getString(prefPod, R.string.pref_key__poddomain, "");
}
public void setPodDomain(String podDomain) {
setString(prefPod, R.string.pref_key__poddomain, podDomain);
}
2016-06-05 17:25:11 +02:00
public boolean hasPodDomain() {
return !getString(prefPod, R.string.pref_key__poddomain, "").equals("");
}
2016-06-05 13:57:34 +02:00
2016-06-05 17:25:11 +02:00
public String[] getPreviousPodlist() {
return getStringArray(prefApp, R.string.pref_key__previous_podlist);
2016-06-05 13:57:34 +02:00
}
2016-06-05 17:25:11 +02:00
public void setPreviousPodlist(String[] pods) {
setStringArray(prefApp, R.string.pref_key__previous_podlist, pods);
2016-06-05 13:57:34 +02:00
}
2016-06-05 17:25:11 +02:00
public void setPodAspects(PodAspect[] aspects) {
setStringArray(prefPod, R.string.pref_key__podprofile_aspects, aspects);
2016-06-05 17:25:11 +02:00
}
public PodAspect[] getPodAspects() {
String[] s = getStringArray(prefPod, R.string.pref_key__podprofile_aspects);
2016-06-05 17:25:11 +02:00
PodAspect[] aspects = new PodAspect[s.length];
for (int i = 0; i < aspects.length; i++) {
2016-06-05 17:25:11 +02:00
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, R.string.pref_key__podprofile_followed_tags);
2016-07-18 14:02:18 +02:00
}
public void setFollowedTags(String[] tags) {
setStringArray(prefPod, R.string.pref_key__podprofile_followed_tags, tags);
2016-07-18 14:02:18 +02:00
}
2016-08-07 11:39:20 +02:00
public int getUnreadMessageCount(){
return getInt(prefPod, R.string.pref_key__podprofile_unread_message_count, 0);
}
public void setUnreadMessageCount(int unreadMessageCount) {
setInt(prefPod, R.string.pref_key__podprofile_unread_message_count, unreadMessageCount);
}
public int getNotificationCount(){
return getInt(prefPod, R.string.pref_key__podprofile_notification_count, 0);
}
public void setNotificationCount(int notificationCount) {
setInt(prefPod, R.string.pref_key__podprofile_notification_count, notificationCount);
}
public boolean isAppendSharedViaApp() {
return getBoolean(prefApp, R.string.pref_key__append_shared_via_app, true);
}
@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(context.getString(R.string.pref_key__proxy_enabled), enabled).commit();
2016-06-09 01:03:50 +02:00
}
/**
* Default return value: false
*
2016-06-09 01:03:50 +02:00
* @return whether proxy is enabled or not
*/
public boolean isProxyEnabled() {
return getBoolean(prefApp, R.string.pref_key__proxy_enabled, false);
2016-06-09 01:03:50 +02:00
}
public boolean wasProxyEnabled() {
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(context.getString(R.string.pref_key__proxy_was_enabled), b).commit();
2016-06-09 01:03:50 +02:00
}
/**
* Default value: ""
*
2016-06-09 01:03:50 +02:00
* @return proxy host
*/
public String getProxyHost() {
return getString(prefApp, R.string.pref_key__proxy_host, "");
2016-06-09 01:03:50 +02:00
}
/**
* Default value: 0
*
2016-06-09 01:03:50 +02:00
* @return proxy port
*/
public int getProxyPort() {
try {
return Integer.parseInt(getString(prefApp, R.string.pref_key__proxy_port, "0"));
} catch (Exception e) {
setString(prefApp, R.string.pref_key__proxy_port, "0");
return 0;
}
2016-06-09 01:03:50 +02:00
}
2016-07-31 15:16:22 +02:00
public boolean isIntellihideToolbars() {
return getBoolean(prefApp, R.string.pref_key__intellihide_toolbars, true);
2016-07-31 15:16:22 +02:00
}
public boolean isShowExitButtonInNavAlso(){
return getBoolean(prefApp, R.string.pref_key__show_exit_button_in_nav_also, false);
}
}