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-08-02 21:32:42 +02:00
|
|
|
import com.github.dfa.diaspora_android.R;
|
|
|
|
|
2016-03-20 22:49:11 +01:00
|
|
|
/**
|
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();
|
|
|
|
}
|
|
|
|
|
2016-08-02 21:32:42 +02:00
|
|
|
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
|
|
|
}
|
2016-03-26 01:14:17 +01:00
|
|
|
|
2016-08-02 21:32:42 +02: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
|
|
|
}
|
2016-03-26 01:14:17 +01:00
|
|
|
|
2016-08-02 21:32:42 +02:00
|
|
|
private void setBool(SharedPreferences pref, int keyRessourceId, boolean value) {
|
|
|
|
pref.edit().putBoolean(context.getString(keyRessourceId), value).apply();
|
2016-03-20 22:49:11 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 21:32:42 +02:00
|
|
|
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
|
|
|
}
|
2016-08-02 21:32:42 +02:00
|
|
|
setString(pref, keyRessourceId, sb.toString().replaceFirst("%%%", ""));
|
2016-06-05 13:57:34 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 21:32:42 +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("%%%");
|
|
|
|
}
|
|
|
|
|
2016-08-02 21:32:42 +02:00
|
|
|
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-06-04 03:48:57 +02:00
|
|
|
}
|
2016-03-25 00:12:00 +01:00
|
|
|
|
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
|
|
|
|
2016-03-20 22:49:11 +01:00
|
|
|
/*
|
|
|
|
// Setters & Getters
|
|
|
|
*/
|
2016-03-24 11:58:28 +01:00
|
|
|
public String getProfileId() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getString(prefPod, R.string.pref_key__podprofile_id, "");
|
2016-03-20 22:49:11 +01:00
|
|
|
}
|
2016-03-24 11:58:28 +01:00
|
|
|
|
|
|
|
public void setProfileId(String profileId) {
|
2016-08-02 21:32:42 +02:00
|
|
|
setString(prefPod, R.string.pref_key__podprofile_id, profileId);
|
2016-03-20 22:49:11 +01:00
|
|
|
}
|
2016-03-24 11:58:28 +01:00
|
|
|
|
|
|
|
public boolean isLoadImages() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getBoolean(prefApp, R.string.pref_key__load_images, true);
|
2016-03-24 11:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getMinimumFontSize() {
|
2016-08-02 21:32:42 +02:00
|
|
|
switch (getString(prefApp, R.string.pref_key__font_size, "")) {
|
2016-06-16 22:55:06 +02:00
|
|
|
case "huge":
|
|
|
|
return 20;
|
|
|
|
case "large":
|
|
|
|
return 16;
|
|
|
|
case "normal":
|
|
|
|
return 8;
|
|
|
|
default:
|
2016-08-02 21:32:42 +02:00
|
|
|
setString(prefApp, R.string.pref_key__font_size, "normal");
|
2016-06-16 22:55:06 +02:00
|
|
|
return 8;
|
|
|
|
}
|
2016-03-24 11:58:28 +01:00
|
|
|
}
|
2016-03-25 00:12:00 +01:00
|
|
|
|
|
|
|
public String getAvatarUrl() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getString(prefPod, R.string.pref_key__podprofile_avatar_url, "");
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setAvatarUrl(String avatarUrl) {
|
2016-08-02 21:32:42 +02:00
|
|
|
setString(prefPod, R.string.pref_key__podprofile_avatar_url, avatarUrl);
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:17 +01:00
|
|
|
public String getName() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getString(prefPod, R.string.pref_key__podprofile_name, "");
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:17 +01:00
|
|
|
public void setName(String name) {
|
2016-08-02 21:32:42 +02:00
|
|
|
setString(prefPod, R.string.pref_key__podprofile_name, name);
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:17 +01:00
|
|
|
public String getPodDomain() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getString(prefPod, R.string.pref_key__poddomain, "");
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 01:14:17 +01:00
|
|
|
public void setPodDomain(String podDomain) {
|
2016-08-02 21:32:42 +02:00
|
|
|
setString(prefPod, R.string.pref_key__poddomain, podDomain);
|
2016-03-25 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2016-06-05 17:25:11 +02:00
|
|
|
public boolean hasPodDomain() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return !getString(prefPod, R.string.pref_key__poddomain, "").equals("");
|
2016-06-04 03:48:57 +02:00
|
|
|
}
|
2016-06-05 13:57:34 +02:00
|
|
|
|
2016-06-05 17:25:11 +02:00
|
|
|
public String[] getPreviousPodlist() {
|
2016-08-02 21:32:42 +02:00
|
|
|
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) {
|
2016-08-02 21:32:42 +02:00
|
|
|
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) {
|
2016-08-02 21:32:42 +02:00
|
|
|
setStringArray(prefPod, R.string.pref_key__podprofile_aspects, aspects);
|
2016-06-05 17:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public PodAspect[] getPodAspects() {
|
2016-08-02 21:32:42 +02:00
|
|
|
String[] s = getStringArray(prefPod, R.string.pref_key__podprofile_aspects);
|
2016-06-05 17:25:11 +02:00
|
|
|
PodAspect[] aspects = new PodAspect[s.length];
|
2016-08-02 21:32:42 +02:00
|
|
|
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() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getStringArray(prefPod, R.string.pref_key__podprofile_followed_tags);
|
2016-07-18 14:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setFollowedTags(String[] tags) {
|
2016-08-02 21:32:42 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-08-02 23:58:36 +02:00
|
|
|
public boolean isAppendSharedViaApp() {
|
|
|
|
return getBoolean(prefApp, R.string.pref_key__append_shared_via_app, true);
|
|
|
|
}
|
|
|
|
|
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.
|
2016-08-02 21:32:42 +02:00
|
|
|
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-08-02 21:32:42 +02:00
|
|
|
*
|
2016-06-09 01:03:50 +02:00
|
|
|
* @return whether proxy is enabled or not
|
|
|
|
*/
|
|
|
|
public boolean isProxyEnabled() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getBoolean(prefApp, R.string.pref_key__proxy_enabled, false);
|
2016-06-09 01:03:50 +02:00
|
|
|
}
|
|
|
|
|
2016-06-16 23:36:52 +02:00
|
|
|
public boolean wasProxyEnabled() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getBoolean(prefApp, R.string.pref_key__proxy_was_enabled, false);
|
2016-06-16 23:36:52 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2016-08-02 21:32:42 +02:00
|
|
|
*
|
2016-06-17 19:43:28 +02:00
|
|
|
* @param b new value
|
|
|
|
*/
|
|
|
|
@SuppressLint("CommitPrefEdits")
|
2016-06-16 23:36:52 +02:00
|
|
|
public void setProxyWasEnabled(boolean b) {
|
2016-08-02 21:32:42 +02:00
|
|
|
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-08-02 21:32:42 +02:00
|
|
|
*
|
2016-06-09 01:03:50 +02:00
|
|
|
* @return proxy host
|
|
|
|
*/
|
|
|
|
public String getProxyHost() {
|
2016-08-02 21:32:42 +02:00
|
|
|
return getString(prefApp, R.string.pref_key__proxy_host, "");
|
2016-06-09 01:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default value: 0
|
2016-08-02 21:32:42 +02:00
|
|
|
*
|
2016-06-09 01:03:50 +02:00
|
|
|
* @return proxy port
|
|
|
|
*/
|
|
|
|
public int getProxyPort() {
|
2016-06-16 23:36:52 +02:00
|
|
|
try {
|
2016-08-02 21:32:42 +02:00
|
|
|
return Integer.parseInt(getString(prefApp, R.string.pref_key__proxy_port, "0"));
|
2016-06-16 23:36:52 +02:00
|
|
|
} catch (Exception e) {
|
2016-08-02 21:32:42 +02:00
|
|
|
setString(prefApp, R.string.pref_key__proxy_port, "0");
|
2016-06-16 23:36:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-09 01:03:50 +02:00
|
|
|
}
|
2016-07-31 15:16:22 +02:00
|
|
|
|
2016-08-02 21:32:42 +02:00
|
|
|
public boolean isIntellihideToolbars() {
|
|
|
|
return getBoolean(prefApp, R.string.pref_key__intellihide_toolbars, true);
|
2016-07-31 15:16:22 +02:00
|
|
|
}
|
2016-08-07 02:28:37 +02:00
|
|
|
|
|
|
|
public boolean isShowExitButtonInNavAlso(){
|
|
|
|
return getBoolean(prefApp, R.string.pref_key__show_exit_button_in_nav_also, false);
|
|
|
|
}
|
2016-03-20 22:49:11 +01:00
|
|
|
}
|