+ 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);
+ backend.putInt(statePreferenceLatest, serial);
+ flush_preferences();
+ }
+ } catch (IOException ie) {
+ }
+ }
+
+ public static ArrayList<Integer> list_states() {
+ String[] keys = backend.keys();
+ ArrayList<Integer> states = new ArrayList<Integer>();
+
+ for (String key : keys) {
+ if (key.startsWith(statePreferenceHead)) {
+ try {
+ int serial = AltosParse.parse_int(key.substring(statePreferenceHead.length()));
+ states.add(serial);
+ } catch (ParseException pe) {
+ }
+ }
+ }
+ return states;
+ }
+
+ public static void remove_state(int serial) {
+ synchronized(backend) {
+ backend.remove(String.format(statePreferenceFormat, serial));
+ }
+ }
+
+ public static int latest_state() {
+ int latest = 0;
+ synchronized (backend) {
+ latest = backend.getInt(statePreferenceLatest, 0);
+ }
+ return latest;
+ }
+
+ 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;
+ }
+