Changed settings to always use Default Shared Preferences. Settings is now only an interface with the definition of the keys stored in Shered Preferences

This commit is contained in:
Synced Synapse 2015-03-11 18:38:59 +00:00
parent 551583aa9d
commit 5193afd4b6
3 changed files with 23 additions and 79 deletions

View File

@ -15,33 +15,19 @@
*/
package org.xbmc.kore;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.format.DateUtils;
import org.xbmc.kore.utils.LogUtils;
/**
* Singleton that holds the settings of the app, that are not stored in the default shared preferences
*
* Interfaces with {@link android.content.SharedPreferences} to load/store these preferences.
* Interface that contains various constants and the keys for settings stored in shared preferences
*/
public class Settings {
private static final String TAG = LogUtils.makeLogTag(Settings.class);
public interface Settings {
/**
* The update interval for the records in the DB. If the last update is older than this value
* a refresh will be triggered. Aplicable to TV Shows and Movies.
* a refresh will be triggered. Applicable to TV Shows and Movies.
*/
// public static final long DB_UPDATE_INTERVAL = 12 * DateUtils.HOUR_IN_MILLIS;
public static final long DB_UPDATE_INTERVAL = 5 * DateUtils.MINUTE_IN_MILLIS;
// Constants for Shared Preferences
private static final String SETTINGS_KEY = "SETTINGS_SHARED_PREFS";
// Tags to save the values
private static final String CURRENT_HOST_ID = "CURRENT_HOST_ID";
// Maximum pictures to show on cast list (-1 to show all)
public static final int DEFAULT_MAX_CAST_PICTURES = 12;
@ -51,7 +37,7 @@ public class Settings {
/**
* Preferences keys.
* These settings are automatically managed by the Preferences mechanism.
* Some of these settings are automatically managed by the Preferences mechanism.
* Make sure these are the same as in preferences.xml
*/
@ -82,7 +68,7 @@ public class Settings {
public static final String KEY_PREF_MOVIES_IGNORE_PREFIXES = "movies_ignore_prefixes";
public static final boolean DEFAULT_PREF_MOVIES_IGNORE_PREFIXES = false;
// Filter watched tv shows on tvshow list
// Filter watched tv shows on tvshows list
public static final String KEY_PREF_TVSHOWS_FILTER_HIDE_WATCHED = "tvshows_filter_hide_watched";
public static final boolean DEFAULT_PREF_TVSHOWS_FILTER_HIDE_WATCHED = false;
@ -102,47 +88,7 @@ public class Settings {
public static final String KEY_PREF_USE_HARDWARE_VOLUME_KEYS = "pref_use_hardware_volume_keys";
public static final boolean DEFAULT_PREF_USE_HARDWARE_VOLUME_KEYS = true;
// Singleton instance
private static Settings instance = null;
private Context context;
/**
* Current saved host id
*/
public int currentHostId;
/**
* Protected singleton constructor. Loads all the preferences
* @param context App context
*/
protected Settings(Context context) {
this.context = context.getApplicationContext();
SharedPreferences preferences = context.getSharedPreferences(SETTINGS_KEY, Context.MODE_PRIVATE);
currentHostId = preferences.getInt(CURRENT_HOST_ID, -1);
}
/**
* Returns the singleton {@link Settings} object.
* @param context App context
* @return Singleton instance
*/
public static Settings getInstance(Context context) {
if (instance == null)
instance = new Settings(context);
return instance;
}
/**
* Save the current values in {@link android.content.SharedPreferences}
*/
public void save() {
SharedPreferences preferences = context.getSharedPreferences(SETTINGS_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(CURRENT_HOST_ID, currentHostId);
editor.apply();
}
// Current host id
public static final String KEY_PREF_CURRENT_HOST_ID = "current_host_id";
public static final int DEFAULT_PREF_CURRENT_HOST_ID = -1;
}

View File

@ -17,8 +17,10 @@ package org.xbmc.kore.host;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import com.squareup.picasso.Picasso;
import org.xbmc.kore.Settings;
@ -143,19 +145,23 @@ public class HostManager {
*/
public HostInfo getHostInfo() {
if (currentHostInfo == null) {
Settings settings = Settings.getInstance(context);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int currentHostId = prefs.getInt(Settings.KEY_PREF_CURRENT_HOST_ID, Settings.DEFAULT_PREF_CURRENT_HOST_ID);
ArrayList<HostInfo> hosts = getHosts();
// No host selected. Check if there are hosts configured and default to the first one
if (settings.currentHostId == -1) {
if (currentHostId == -1) {
if (hosts.size() > 0) {
currentHostInfo = hosts.get(0);
settings.currentHostId = currentHostInfo.getId();
settings.save();
currentHostId = currentHostInfo.getId();
prefs.edit()
.putInt(Settings.KEY_PREF_CURRENT_HOST_ID, currentHostId)
.apply();
}
} else {
for (HostInfo host : hosts) {
if (host.getId() == settings.currentHostId) {
if (host.getId() == currentHostId) {
currentHostInfo = host;
break;
}
@ -221,9 +227,10 @@ public class HostManager {
currentHostInfo = hostInfo;
if (currentHostInfo != null) {
Settings settings = Settings.getInstance(context);
settings.currentHostId = currentHostInfo.getId();
settings.save();
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putInt(Settings.KEY_PREF_CURRENT_HOST_ID, currentHostInfo.getId())
.apply();
}
}

View File

@ -36,11 +36,6 @@ public class SettingsFragment extends PreferenceFragment
private static final String TAG = LogUtils.makeLogTag(SettingsFragment.class);
public static final String COFFEE_SKU = "coffee";
public static final int COFFEE_RC = 1001;
private Settings mSettings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -48,8 +43,6 @@ public class SettingsFragment extends PreferenceFragment
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
mSettings = Settings.getInstance(getActivity());
setupPreferences();
}
@ -88,8 +81,6 @@ public class SettingsFragment extends PreferenceFragment
* Sets up the preferences state and summaries
*/
private void setupPreferences() {
final Settings settings = Settings.getInstance(getActivity());
// Theme preferences
ListPreference themePref = (ListPreference)findPreference(Settings.KEY_PREF_THEME);
themePref.setSummary(themePref.getEntry());