diff options
author | Keith Packard <keithp@keithp.com> | 2014-11-15 22:50:31 -0800 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2014-11-15 22:50:31 -0800 |
commit | 52ce23327bd81e2d40a1817442e75bd6b60ffe95 (patch) | |
tree | e5e15872339d133ab8e74782244dd19fa2130420 /altoslib/AltosPreferences.java | |
parent | a488da3ca72fe0778b2d79a8cac935621d1d789d (diff) |
altoslib: add AltosPreferences state save/restore interfaces
This serializes an entire AltosState object and stores it in the
preferences database for later retrieval. AltosDroid uses this to
recover the old state data when restarting.
Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'altoslib/AltosPreferences.java')
-rw-r--r-- | altoslib/AltosPreferences.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/altoslib/AltosPreferences.java b/altoslib/AltosPreferences.java index dba57dcb..5fe810d7 100644 --- a/altoslib/AltosPreferences.java +++ b/altoslib/AltosPreferences.java @@ -41,6 +41,9 @@ public class AltosPreferences { /* log file format preference name */ public final static String logfilePreferenceFormat = "LOGFILE-%d"; + /* state preference name */ + public final static String statePreferenceFormat = "STATE-%d"; + /* voice preference name */ public final static String voicePreference = "VOICE"; @@ -333,6 +336,48 @@ public class AltosPreferences { } } + public static void set_state(int serial, AltosState state, AltosListenerState listener_state) { + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + try { + ObjectOutputStream oos = new ObjectOutputStream(baos); + + AltosSavedState saved_state = new AltosSavedState(state, listener_state); + oos.writeObject(saved_state); + + byte[] bytes = baos.toByteArray(); + + synchronized(backend) { + backend.putBytes(String.format(statePreferenceFormat, serial), bytes); + flush_preferences(); + } + } catch (IOException ie) { + } + } + + public static AltosSavedState state(int serial) { + byte[] bytes = null; + + synchronized(backend) { + bytes = backend.getBytes(String.format(statePreferenceFormat, serial), null); + } + + if (bytes == null) + return null; + + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + + try { + ObjectInputStream ois = new ObjectInputStream(bais); + AltosSavedState saved_state = (AltosSavedState) ois.readObject(); + return saved_state; + } catch (IOException ie) { + } catch (ClassNotFoundException ce) { + } + return null; + } + public static void set_scanning_telemetry(int new_scanning_telemetry) { synchronized (backend) { scanning_telemetry = new_scanning_telemetry; |