altosdroid: simplify keys() method
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroidPreferences.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.util.Map;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import org.altusmetrum.AltosLib.*;
24
25 public class AltosDroidPreferences implements AltosPreferencesBackend {
26         public final static String        NAME    = "org.altusmetrum.AltosDroid";
27         private Context                   context = null;
28         private SharedPreferences         prefs   = null;
29         private SharedPreferences.Editor  editor  = null;
30
31         public AltosDroidPreferences(Context in_context) {
32                 this(in_context, NAME);
33         }
34
35         public AltosDroidPreferences(Context in_context, String in_prefs) {
36                 context = in_context;
37                 prefs   = context.getSharedPreferences(in_prefs, 0);
38                 editor  = prefs.edit();
39         }
40
41         public AltosPreferencesBackend node(String key) {
42                 return new AltosDroidPreferences(context, key);
43         }
44
45         public void flush() {
46                 editor.apply();
47         }
48
49         public String[] keys() {
50                 Map<String, ?> all = prefs.getAll();
51                 return (String[])all.keySet().toArray();
52         }
53
54         public boolean nodeExists(String key) {
55                 return prefs.contains(key);
56         }
57
58         public boolean getBoolean(String key, boolean def) {
59                 return prefs.getBoolean(key, def);
60         }
61
62         public double getDouble(String key, double def) {
63                 Float f = Float.valueOf(prefs.getFloat(key, (float)def));
64                 return f.doubleValue();
65         }
66
67         public int getInt(String key, int def) {
68                 return prefs.getInt(key, def);
69         }
70
71         public String getString(String key, String def) {
72                 return prefs.getString(key, def);
73         }
74
75         public void putBoolean(String key, boolean value) {
76                 editor.putBoolean(key, value);
77         }
78
79         public void putDouble(String key, double value) {
80                 editor.putFloat(key, (float)value);
81         }
82
83         public void putInt(String key, int value) {
84                 editor.putInt(key, value);
85         }
86
87         public void putString(String key, String value) {
88                 editor.putString(key, value);
89         }
90
91         public void remove(String key) {
92                 editor.remove(key);
93         }
94
95 }