2 * Copyright © 2012 Mike Beattie <mike@ethernal.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosDroid;
22 import android.content.Context;
23 import android.content.SharedPreferences;
24 import android.os.Environment;
25 import android.util.*;
27 import org.altusmetrum.altoslib_10.*;
29 public class AltosDroidPreferencesBackend extends AltosPreferencesBackend {
30 public final static String NAME = "org.altusmetrum.AltosDroid";
31 private Context context = null;
32 private SharedPreferences prefs = null;
33 private SharedPreferences.Editor editor = null;
35 public AltosDroidPreferencesBackend(Context in_context) {
36 this(in_context, NAME);
39 public AltosDroidPreferencesBackend(Context in_context, String in_prefs) {
41 prefs = context.getSharedPreferences(in_prefs, 0);
42 editor = prefs.edit();
45 public String[] keys() {
46 Map<String, ?> all = prefs.getAll();
47 Object[] ao = all.keySet().toArray();
49 String[] as = new String[ao.length];
50 for (int i = 0; i < ao.length; i++)
51 as[i] = (String) ao[i];
55 public AltosPreferencesBackend node(String key) {
57 putBoolean(key, true);
58 return new AltosDroidPreferencesBackend(context, key);
61 public boolean nodeExists(String key) {
62 return prefs.contains(key);
65 public boolean getBoolean(String key, boolean def) {
66 return prefs.getBoolean(key, def);
69 public double getDouble(String key, double def) {
70 Float f = Float.valueOf(prefs.getFloat(key, (float)def));
71 return f.doubleValue();
74 public int getInt(String key, int def) {
75 return prefs.getInt(key, def);
78 public String getString(String key, String def) {
79 return prefs.getString(key, def);
82 public byte[] getBytes(String key, byte[] def) {
83 String save = prefs.getString(key, null);
88 byte[] bytes = Base64.decode(save, Base64.DEFAULT);
92 public void putBoolean(String key, boolean value) {
93 editor.putBoolean(key, value);
96 public void putDouble(String key, double value) {
97 editor.putFloat(key, (float)value);
100 public void putInt(String key, int value) {
101 editor.putInt(key, value);
104 public void putString(String key, String value) {
105 editor.putString(key, value);
108 public void putBytes(String key, byte[] bytes) {
109 String save = Base64.encodeToString(bytes, Base64.DEFAULT);
110 editor.putString(key, save);
113 public void remove(String key) {
114 AltosDebug.debug("remove preference %s\n", key);
118 public void flush() {
122 public File homeDirectory() {
123 return Environment.getExternalStorageDirectory();
126 public void debug(String format, Object ... arguments) {
127 AltosDebug.debug(format, arguments);