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