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