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 return (String[])all.keySet().toArray();
50 public AltosPreferencesBackend node(String key) {
51 return new AltosDroidPreferencesBackend(context, key);
54 public boolean nodeExists(String key) {
55 return prefs.contains(key);
58 public boolean getBoolean(String key, boolean def) {
59 return prefs.getBoolean(key, def);
62 public double getDouble(String key, double def) {
63 Float f = Float.valueOf(prefs.getFloat(key, (float)def));
64 return f.doubleValue();
67 public int getInt(String key, int def) {
68 return prefs.getInt(key, def);
71 public String getString(String key, String def) {
72 return prefs.getString(key, def);
75 public byte[] getBytes(String key, byte[] def) {
76 String save = prefs.getString(key, null);
81 byte[] bytes = Base64.decode(save, Base64.DEFAULT);
85 public void putBoolean(String key, boolean value) {
86 editor.putBoolean(key, value);
89 public void putDouble(String key, double value) {
90 editor.putFloat(key, (float)value);
93 public void putInt(String key, int value) {
94 editor.putInt(key, value);
97 public void putString(String key, String value) {
98 editor.putString(key, value);
101 public void putBytes(String key, byte[] bytes) {
102 String save = Base64.encodeToString(bytes, Base64.DEFAULT);
103 editor.putString(key, save);
106 public void remove(String key) {
110 public void flush() {
114 public File homeDirectory() {
115 return Environment.getExternalStorageDirectory();