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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.AltosDroid;
23 import android.content.Context;
24 import android.content.SharedPreferences;
25 import android.os.Environment;
26 import android.util.*;
28 import org.altusmetrum.altoslib_14.*;
30 public class AltosDroidPreferencesBackend extends AltosPreferencesBackend {
31 public final static String NAME = "org.altusmetrum.AltosDroid";
32 private Context context = null;
33 private SharedPreferences prefs = null;
34 private SharedPreferences.Editor editor = null;
36 public AltosDroidPreferencesBackend(Context in_context) {
37 this(in_context, NAME);
40 public AltosDroidPreferencesBackend(Context in_context, String in_prefs) {
42 prefs = context.getSharedPreferences(in_prefs, 0);
43 editor = prefs.edit();
46 public String[] keys() {
47 Map<String, ?> all = prefs.getAll();
48 Object[] ao = all.keySet().toArray();
50 String[] as = new String[ao.length];
51 for (int i = 0; i < ao.length; i++)
52 as[i] = (String) ao[i];
56 public AltosPreferencesBackend node(String key) {
58 putBoolean(key, true);
59 return new AltosDroidPreferencesBackend(context, key);
62 public boolean nodeExists(String key) {
63 return prefs.contains(key);
66 public boolean getBoolean(String key, boolean def) {
67 return prefs.getBoolean(key, def);
70 public double getDouble(String key, double def) {
71 Float f = Float.valueOf(prefs.getFloat(key, (float)def));
72 return f.doubleValue();
75 public int getInt(String key, int def) {
76 return prefs.getInt(key, def);
79 public String getString(String key, String def) {
81 ret = prefs.getString(key, def);
82 // AltosDebug.debug("AltosDroidPreferencesBackend get string %s:\n", key);
84 // AltosDebug.debug(" (null)\n");
86 // String[] lines = ret.split("\n");
87 // for (String l : lines)
88 // AltosDebug.debug(" %s\n", l);
93 public byte[] getBytes(String key, byte[] def) {
94 String save = prefs.getString(key, null);
99 byte[] bytes = Base64.decode(save, Base64.DEFAULT);
103 public void putBoolean(String key, boolean value) {
104 editor.putBoolean(key, value);
107 public void putDouble(String key, double value) {
108 editor.putFloat(key, (float)value);
111 public void putInt(String key, int value) {
112 editor.putInt(key, value);
115 public void putString(String key, String value) {
116 // AltosDebug.debug("AltosDroidPreferencesBackend put string %s:\n", key);
117 // String[] lines = value.split("\n");
118 // for (String l : lines)
119 // AltosDebug.debug(" %s\n", l);
120 editor.putString(key, value);
123 public void putBytes(String key, byte[] bytes) {
124 String save = Base64.encodeToString(bytes, Base64.DEFAULT);
125 editor.putString(key, save);
128 public void remove(String key) {
129 AltosDebug.debug("remove preference %s\n", key);
133 public void flush() {
137 public File homeDirectory() {
138 return Environment.getExternalStorageDirectory();
141 public void debug(String format, Object ... arguments) {
142 AltosDebug.debug(format, arguments);