1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/*
* Copyright © 2012 Mike Beattie <mike@ethernal.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package org.altusmetrum.AltosDroid;
import java.io.File;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.*;
import org.altusmetrum.altoslib_12.*;
public class AltosDroidPreferencesBackend extends AltosPreferencesBackend {
public final static String NAME = "org.altusmetrum.AltosDroid";
private Context context = null;
private SharedPreferences prefs = null;
private SharedPreferences.Editor editor = null;
public AltosDroidPreferencesBackend(Context in_context) {
this(in_context, NAME);
}
public AltosDroidPreferencesBackend(Context in_context, String in_prefs) {
context = in_context;
prefs = context.getSharedPreferences(in_prefs, 0);
editor = prefs.edit();
}
public String[] keys() {
Map<String, ?> all = prefs.getAll();
Object[] ao = all.keySet().toArray();
String[] as = new String[ao.length];
for (int i = 0; i < ao.length; i++)
as[i] = (String) ao[i];
return as;
}
public AltosPreferencesBackend node(String key) {
if (!nodeExists(key))
putBoolean(key, true);
return new AltosDroidPreferencesBackend(context, key);
}
public boolean nodeExists(String key) {
return prefs.contains(key);
}
public boolean getBoolean(String key, boolean def) {
return prefs.getBoolean(key, def);
}
public double getDouble(String key, double def) {
Float f = Float.valueOf(prefs.getFloat(key, (float)def));
return f.doubleValue();
}
public int getInt(String key, int def) {
return prefs.getInt(key, def);
}
public String getString(String key, String def) {
String ret;
ret = prefs.getString(key, def);
// AltosDebug.debug("AltosDroidPreferencesBackend get string %s:\n", key);
// if (ret == null)
// AltosDebug.debug(" (null)\n");
// else {
// String[] lines = ret.split("\n");
// for (String l : lines)
// AltosDebug.debug(" %s\n", l);
// }
return ret;
}
public byte[] getBytes(String key, byte[] def) {
String save = prefs.getString(key, null);
if (save == null)
return def;
byte[] bytes = Base64.decode(save, Base64.DEFAULT);
return bytes;
}
public void putBoolean(String key, boolean value) {
editor.putBoolean(key, value);
}
public void putDouble(String key, double value) {
editor.putFloat(key, (float)value);
}
public void putInt(String key, int value) {
editor.putInt(key, value);
}
public void putString(String key, String value) {
// AltosDebug.debug("AltosDroidPreferencesBackend put string %s:\n", key);
// String[] lines = value.split("\n");
// for (String l : lines)
// AltosDebug.debug(" %s\n", l);
editor.putString(key, value);
}
public void putBytes(String key, byte[] bytes) {
String save = Base64.encodeToString(bytes, Base64.DEFAULT);
editor.putString(key, save);
}
public void remove(String key) {
AltosDebug.debug("remove preference %s\n", key);
editor.remove(key);
}
public void flush() {
editor.apply();
}
public File homeDirectory() {
return Environment.getExternalStorageDirectory();
}
public void debug(String format, Object ... arguments) {
AltosDebug.debug(format, arguments);
}
}
|