From fe8ab96e8727c25c04cac473cafb264cf7e80156 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Mon, 17 Sep 2012 01:29:33 +1200 Subject: [PATCH] altosdroid: implement AltosPreferencesBackend, and initialize. Signed-off-by: Mike Beattie --- .../altusmetrum/AltosDroid/AltosDroid.java | 7 ++ .../AltosDroid/AltosDroidPreferences.java | 97 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 00689684..3396f77e 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -87,6 +87,9 @@ public class AltosDroid extends Activity { private Messenger mService = null; final Messenger mMessenger = new Messenger(new IncomingHandler(this)); + // Preferences + private AltosDroidPreferences prefs = null; + // TeleBT Config data private AltosConfigData mConfigData = null; // Local Bluetooth adapter @@ -228,6 +231,10 @@ public class AltosDroid extends Activity { return; } + // Initialise preferences + prefs = new AltosDroidPreferences(this); + AltosPreferences.init(prefs); + // Set up the window layout requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //setContentView(R.layout.main); diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java new file mode 100644 index 00000000..7ba328f4 --- /dev/null +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java @@ -0,0 +1,97 @@ +/* + * Copyright © 2012 Mike Beattie + * + * This program 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; version 2 of the License. + * + * This program 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 this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.AltosDroid; + +import java.util.Map; +import android.content.Context; +import android.content.SharedPreferences; +import org.altusmetrum.AltosLib.*; + +public class AltosDroidPreferences implements AltosPreferencesBackend { + public final static String NAME = "org.altusmetrum.AltosDroid"; + private Context context = null; + private SharedPreferences prefs = null; + private SharedPreferences.Editor editor = null; + + public AltosDroidPreferences(Context in_context) { + this(in_context, NAME); + } + + public AltosDroidPreferences(Context in_context, String in_prefs) { + context = in_context; + prefs = context.getSharedPreferences(in_prefs, 0); + editor = prefs.edit(); + } + + public AltosPreferencesBackend node(String key) { + return new AltosDroidPreferences(context, key); + } + + public void flush() { + editor.apply(); + } + + public String[] keys() { + Map data = prefs.getAll(); + String[] keys = new String[data.size()]; + data.keySet().toArray(keys); + return keys; + } + + public boolean nodeExists(String key) { + return prefs.contains(key); + } + + public boolean getBoolean(String key, boolean def) { + return prefs.getBoolean(key, def); + } + + public double getDouble(String key, double def) { + Float f = Float.valueOf(prefs.getFloat(key, (float)def)); + return f.doubleValue(); + } + + public int getInt(String key, int def) { + return prefs.getInt(key, def); + } + + public String getString(String key, String def) { + return prefs.getString(key, def); + } + + public void putBoolean(String key, boolean value) { + editor.putBoolean(key, value); + } + + public void putDouble(String key, double value) { + editor.putFloat(key, (float)value); + } + + public void putInt(String key, int value) { + editor.putInt(key, value); + } + + public void putString(String key, String value) { + editor.putString(key, value); + } + + public void remove(String key) { + editor.remove(key); + } + +} -- 2.30.2