X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosdroid%2Fsrc%2Forg%2Faltusmetrum%2FAltosDroid%2FAltosDroidPreferences.java;h=8bb78c00dce6fd915551aca270bcbe99e062b648;hp=fd4b0768ac33f8d60e55946be7f0d6bb1ad39d09;hb=4a257455b2dc57069c41e1845daf66239c5cbe1d;hpb=bf88c5f829ea5d32043431945e862a9f6c96740a diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java index fd4b0768..8bb78c00 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferences.java @@ -1,9 +1,10 @@ /* - * Copyright © 2012 Mike Beattie + * Copyright © 2014 Keith Packard * * 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. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,88 +15,98 @@ * 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 java.io.*; +import java.util.*; +import java.text.*; + import android.content.Context; -import android.content.SharedPreferences; -import android.os.Environment; +import org.altusmetrum.altoslib_13.*; -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 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); - } + if (address != null && name != null) + active_device_address = new DeviceAddress (address, name); - public String getString(String key, String def) { - return prefs.getString(key, def); + map_source = backend.getInt(mapSourcePreference, MAP_SOURCE_ONLINE); } - public void putBoolean(String key, boolean value) { - editor.putBoolean(key, value); + public static void set_active_device(DeviceAddress address) { + synchronized(backend) { + active_device_address = address; + if (active_device_address != null) { + backend.putString(activeDeviceAddressPreference, active_device_address.address); + backend.putString(activeDeviceNamePreference, active_device_address.name); + } else { + backend.remove(activeDeviceAddressPreference); + backend.remove(activeDeviceNamePreference); + } + flush_preferences(); + } } - public void putDouble(String key, double value) { - editor.putFloat(key, (float)value); + public static DeviceAddress active_device() { + synchronized(backend) { + return active_device_address; + } } - public void putInt(String key, int value) { - editor.putInt(key, value); - } + static LinkedList map_source_listeners; - public void putString(String key, String value) { - editor.putString(key, value); + public static void set_map_source(int map_source) { + synchronized(backend) { + AltosDroidPreferences.map_source = map_source; + backend.putInt(mapSourcePreference, map_source); + flush_preferences(); + } + if (map_source_listeners != null) { + for (AltosDroidMapSourceListener l : map_source_listeners) { + l.map_source_changed(map_source); + } + } } - public void remove(String key) { - editor.remove(key); + public static int map_source() { + synchronized(backend) { + return map_source; + } } - public void flush() { - editor.apply(); + public static void register_map_source_listener(AltosDroidMapSourceListener l) { + synchronized(backend) { + if (map_source_listeners == null) + map_source_listeners = new LinkedList(); + map_source_listeners.add(l); + } } - public File homeDirectory() { - return Environment.getExternalStorageDirectory(); + public static void unregister_map_source_listener(AltosDroidMapSourceListener l) { + synchronized(backend) { + map_source_listeners.remove(l); + } } }