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_7.*;
29 public class AltosDroidPreferencesBackend implements 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) {
56 return new AltosDroidPreferencesBackend(context, key);
59 public boolean nodeExists(String key) {
60 return prefs.contains(key);
63 public boolean getBoolean(String key, boolean def) {
64 return prefs.getBoolean(key, def);
67 public double getDouble(String key, double def) {
68 Float f = Float.valueOf(prefs.getFloat(key, (float)def));
69 return f.doubleValue();
72 public int getInt(String key, int def) {
73 return prefs.getInt(key, def);
76 public String getString(String key, String def) {
77 return prefs.getString(key, def);
80 public byte[] getBytes(String key, byte[] def) {
81 String save = prefs.getString(key, null);
86 byte[] bytes = Base64.decode(save, Base64.DEFAULT);
90 public void putBoolean(String key, boolean value) {
91 editor.putBoolean(key, value);
94 public void putDouble(String key, double value) {
95 editor.putFloat(key, (float)value);
98 public void putInt(String key, int value) {
99 editor.putInt(key, value);
102 public void putString(String key, String value) {
103 editor.putString(key, value);
106 public void putBytes(String key, byte[] bytes) {
107 String save = Base64.encodeToString(bytes, Base64.DEFAULT);
108 editor.putString(key, save);
111 public void remove(String key) {
112 AltosDebug.debug("remove preference %s\n", key);
116 public void flush() {
120 public File homeDirectory() {
121 return Environment.getExternalStorageDirectory();