Update java library versions
[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_9.*;
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                 return new AltosDroidPreferencesBackend(context, key);
57         }
58
59         public boolean nodeExists(String key) {
60                 return prefs.contains(key);
61         }
62
63         public boolean getBoolean(String key, boolean def) {
64                 return prefs.getBoolean(key, def);
65         }
66
67         public double getDouble(String key, double def) {
68                 Float f = Float.valueOf(prefs.getFloat(key, (float)def));
69                 return f.doubleValue();
70         }
71
72         public int getInt(String key, int def) {
73                 return prefs.getInt(key, def);
74         }
75
76         public String getString(String key, String def) {
77                 return prefs.getString(key, def);
78         }
79
80         public byte[] getBytes(String key, byte[] def) {
81                 String save = prefs.getString(key, null);
82
83                 if (save == null)
84                         return def;
85
86                 byte[] bytes = Base64.decode(save, Base64.DEFAULT);
87                 return bytes;
88         }
89
90         public void putBoolean(String key, boolean value) {
91                 editor.putBoolean(key, value);
92         }
93
94         public void putDouble(String key, double value) {
95                 editor.putFloat(key, (float)value);
96         }
97
98         public void putInt(String key, int value) {
99                 editor.putInt(key, value);
100         }
101
102         public void putString(String key, String value) {
103                 editor.putString(key, value);
104         }
105
106         public void putBytes(String key, byte[] bytes) {
107                 String save = Base64.encodeToString(bytes, Base64.DEFAULT);
108                 editor.putString(key, save);
109         }
110
111         public void remove(String key) {
112                 AltosDebug.debug("remove preference %s\n", key);
113                 editor.remove(key);
114         }
115
116         public void flush() {
117                 editor.apply();
118         }
119
120         public File homeDirectory() {
121                 return Environment.getExternalStorageDirectory();
122         }
123 }