altoslib: Add getBytes/putBytes interface to AltosPreferencesBackend
authorKeith Packard <keithp@keithp.com>
Sun, 16 Nov 2014 06:48:15 +0000 (22:48 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 16 Nov 2014 06:48:15 +0000 (22:48 -0800)
This lets us store arbitrary binary data in the preferences database

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidPreferencesBackend.java
altoslib/AltosPreferencesBackend.java
altosuilib/AltosUIPreferencesBackend.java

index be41ae7cf35b345b157d11127730c73971618bdc..09a2a7a2d9fb70dc93bec13a82025e5993b7c0dc 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Map;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Environment;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Environment;
+import android.util.*;
 
 import org.altusmetrum.altoslib_5.*;
 
 
 import org.altusmetrum.altoslib_5.*;
 
@@ -71,6 +72,16 @@ public class AltosDroidPreferencesBackend implements AltosPreferencesBackend {
                return prefs.getString(key, def);
        }
 
                return prefs.getString(key, def);
        }
 
+       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 putBoolean(String key, boolean value) {
                editor.putBoolean(key, value);
        }
@@ -87,6 +98,11 @@ public class AltosDroidPreferencesBackend implements AltosPreferencesBackend {
                editor.putString(key, value);
        }
 
                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) {
                editor.remove(key);
        }
        public void remove(String key) {
                editor.remove(key);
        }
index 76a99acbb1ef82187d034d34885e457c37649a5f..9bb4c99bf8a17a3dde622ec6494205fbc104551a 100644 (file)
@@ -33,6 +33,9 @@ public interface AltosPreferencesBackend {
        public boolean getBoolean(String key, boolean def);
        public void    putBoolean(String key, boolean value);
 
        public boolean getBoolean(String key, boolean def);
        public void    putBoolean(String key, boolean value);
 
+       public byte[]  getBytes(String key, byte[] def);
+       public void    putBytes(String key, byte[] value);
+
        public boolean nodeExists(String key);
        public AltosPreferencesBackend node(String key);
 
        public boolean nodeExists(String key);
        public AltosPreferencesBackend node(String key);
 
index 4048fd83b99f4a52b0018c829cb69e8df48d0140..8d4431f12beca5a207eccd09f0f1f7868f143f05 100644 (file)
@@ -62,6 +62,14 @@ public class AltosUIPreferencesBackend implements AltosPreferencesBackend {
                _preferences.putBoolean(key, value);
        }
 
                _preferences.putBoolean(key, value);
        }
 
+       public byte[] getBytes(String key, byte[] def) {
+               return _preferences.getByteArray(key, def);
+       }
+
+       public void putBytes(String key, byte[] value) {
+               _preferences.putByteArray(key, value);
+       }
+
        public boolean nodeExists(String key) {
                try {
                        return _preferences.nodeExists(key);
        public boolean nodeExists(String key) {
                try {
                        return _preferences.nodeExists(key);