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