c2db3774bed213172ee1c0003344ea8e5eebd196
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroidPreferencesBackend.java
1 /*
2  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosDroid;
19
20 import java.io.File;
21 import java.util.Map;
22 import android.content.Context;
23 import android.content.SharedPreferences;
24 import android.os.Environment;
25 import android.util.*;
26
27 import org.altusmetrum.altoslib_10.*;
28
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;
34
35         public AltosDroidPreferencesBackend(Context in_context) {
36                 this(in_context, NAME);
37         }
38
39         public AltosDroidPreferencesBackend(Context in_context, String in_prefs) {
40                 context = in_context;
41                 prefs   = context.getSharedPreferences(in_prefs, 0);
42                 editor  = prefs.edit();
43         }
44
45         public String[] keys() {
46                 Map<String, ?> all = prefs.getAll();
47                 Object[] ao = all.keySet().toArray();
48
49                 String[] as = new String[ao.length];
50                 for (int i = 0; i < ao.length; i++)
51                         as[i] = (String) ao[i];
52                 return as;
53         }
54
55         public AltosPreferencesBackend node(String key) {
56                 if (!nodeExists(key))
57                         putBoolean(key, true);
58                 return new AltosDroidPreferencesBackend(context, key);
59         }
60
61         public boolean nodeExists(String key) {
62                 return prefs.contains(key);
63         }
64
65         public boolean getBoolean(String key, boolean def) {
66                 return prefs.getBoolean(key, def);
67         }
68
69         public double getDouble(String key, double def) {
70                 Float f = Float.valueOf(prefs.getFloat(key, (float)def));
71                 return f.doubleValue();
72         }
73
74         public int getInt(String key, int def) {
75                 return prefs.getInt(key, def);
76         }
77
78         public String getString(String key, String def) {
79                 return prefs.getString(key, def);
80         }
81
82         public byte[] getBytes(String key, byte[] def) {
83                 String save = prefs.getString(key, null);
84
85                 if (save == null)
86                         return def;
87
88                 byte[] bytes = Base64.decode(save, Base64.DEFAULT);
89                 return bytes;
90         }
91
92         public void putBoolean(String key, boolean value) {
93                 editor.putBoolean(key, value);
94         }
95
96         public void putDouble(String key, double value) {
97                 editor.putFloat(key, (float)value);
98         }
99
100         public void putInt(String key, int value) {
101                 editor.putInt(key, value);
102         }
103
104         public void putString(String key, String value) {
105                 editor.putString(key, value);
106         }
107
108         public void putBytes(String key, byte[] bytes) {
109                 String save = Base64.encodeToString(bytes, Base64.DEFAULT);
110                 editor.putString(key, save);
111         }
112
113         public void remove(String key) {
114                 AltosDebug.debug("remove preference %s\n", key);
115                 editor.remove(key);
116         }
117
118         public void flush() {
119                 editor.apply();
120         }
121
122         public File homeDirectory() {
123                 return Environment.getExternalStorageDirectory();
124         }
125
126         public void debug(String format, Object ... arguments) {
127                 AltosDebug.debug(format, arguments);
128         }
129 }