Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroidPreferences.java
index fd4b0768ac33f8d60e55946be7f0d6bb1ad39d09..a4e27006ee37ab0d2d19e745877f305760ec524b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2012 Mike Beattie <mike@ethernal.org>
+ * Copyright © 2014 Keith Packard <keithp@keithp.com>
  *
  * 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
  * 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.io.File;
-import java.util.Map;
 import android.content.Context;
-import android.content.SharedPreferences;
-import android.os.Environment;
+import org.altusmetrum.altoslib_8.*;
 
-import org.altusmetrum.altoslib_1.*;
+public class AltosDroidPreferences extends AltosPreferences {
 
-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;
+       /* Active device preference name */
+       final static String activeDeviceAddressPreference = "ACTIVE-DEVICE-ADDRESS";
+       final static String activeDeviceNamePreference = "ACTIVE-DEVICE-NAME";
 
-       public AltosDroidPreferences(Context in_context) {
-               this(in_context, NAME);
-       }
+       static DeviceAddress    active_device_address;
 
-       public AltosDroidPreferences(Context in_context, String in_prefs) {
-               context = in_context;
-               prefs   = context.getSharedPreferences(in_prefs, 0);
-               editor  = prefs.edit();
-       }
+       /* Map source preference name */
+       final static String mapSourcePreference = "MAP-SOURCE";
 
-       public String[] keys() {
-               Map<String, ?> all = prefs.getAll();
-               return (String[])all.keySet().toArray();
-       }
+       static final int        MAP_SOURCE_OFFLINE = 0;
+       static final int        MAP_SOURCE_ONLINE = 1;
 
-       public AltosPreferencesBackend node(String key) {
-               return new AltosDroidPreferences(context, key);
-       }
+       static int      map_source;
 
-       public boolean nodeExists(String key) {
-               return prefs.contains(key);
-       }
+       public static void init(Context context) {
+               if (backend != null)
+                       return;
 
-       public boolean getBoolean(String key, boolean def) {
-               return prefs.getBoolean(key, def);
-       }
+               AltosPreferences.init(new AltosDroidPreferencesBackend(context));
 
-       public double getDouble(String key, double def) {
-               Float f = Float.valueOf(prefs.getFloat(key, (float)def));
-               return f.doubleValue();
-       }
+               String address = backend.getString(activeDeviceAddressPreference, null);
+               String name = backend.getString(activeDeviceNamePreference, null);
 
-       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);
-       }
+               if (address != null && name != null)
+                       active_device_address = new DeviceAddress (address, name);
 
-       public void putInt(String key, int value) {
-               editor.putInt(key, value);
+               map_source = backend.getInt(mapSourcePreference, MAP_SOURCE_ONLINE);
        }
 
-       public void putString(String key, String value) {
-               editor.putString(key, value);
+       public static void set_active_device(DeviceAddress address) {
+               synchronized(backend) {
+                       active_device_address = address;
+                       backend.putString(activeDeviceAddressPreference, active_device_address.address);
+                       backend.putString(activeDeviceNamePreference, active_device_address.name);
+                       flush_preferences();
+               }
        }
 
-       public void remove(String key) {
-               editor.remove(key);
+       public static DeviceAddress active_device() {
+               synchronized(backend) {
+                       return active_device_address;
+               }
        }
 
-       public void flush() {
-               editor.apply();
+       public static void set_map_source(int map_source) {
+               synchronized(backend) {
+                       AltosDroidPreferences.map_source = map_source;
+                       backend.putInt(mapSourcePreference, map_source);
+                       flush_preferences();
+               }
        }
 
-       public File homeDirectory() {
-               return Environment.getExternalStorageDirectory();
+       public static int map_source() {
+               synchronized(backend) {
+                       return map_source;
+               }
        }
 }