170353ab4838098035f6e1dc37768bb0ee41f9f4
[fw/altos] / altosdroid / app / src / main / java / 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_14.*;
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                 String  ret;
81                 if (key.equals(AltosPreferences.logdirPreference))
82                         ret = null;
83                 else
84                         ret = prefs.getString(key, def);
85                 AltosDebug.debug("AltosDroidPreferencesBackend get string %s:\n", key);
86                 if (ret == null)
87                         AltosDebug.debug("      (null)\n");
88                 else {
89                         String[] lines = ret.split("\n");
90                         for (String l : lines)
91                                 AltosDebug.debug("        %s\n", l);
92                 }
93                 return ret;
94         }
95
96         public byte[] getBytes(String key, byte[] def) {
97                 String save = prefs.getString(key, null);
98
99                 if (save == null)
100                         return def;
101
102                 byte[] bytes = Base64.decode(save, Base64.DEFAULT);
103                 return bytes;
104         }
105
106         public void putBoolean(String key, boolean value) {
107                 editor.putBoolean(key, value);
108         }
109
110         public void putDouble(String key, double value) {
111                 editor.putFloat(key, (float)value);
112         }
113
114         public void putInt(String key, int value) {
115                 editor.putInt(key, value);
116         }
117
118         public void putString(String key, String value) {
119 //              AltosDebug.debug("AltosDroidPreferencesBackend put string %s:\n", key);
120 //              String[] lines = value.split("\n");
121 //              for (String l : lines)
122 //                      AltosDebug.debug("        %s\n", l);
123                 editor.putString(key, value);
124         }
125
126         public void putBytes(String key, byte[] bytes) {
127                 String save = Base64.encodeToString(bytes, Base64.DEFAULT);
128                 editor.putString(key, save);
129         }
130
131         public void remove(String key) {
132                 AltosDebug.debug("remove preference %s\n", key);
133                 editor.remove(key);
134         }
135
136         public void flush() {
137                 editor.apply();
138         }
139
140         public File homeDirectory() {
141                 return context.getExternalMediaDirs()[0];
142         }
143
144         public void debug(String format, Object ... arguments) {
145                 AltosDebug.debug(format, arguments);
146         }
147 }