Crash recover no longer uses icicle.

 Bug 5508252
 Changed load state to use the crash recovery state every
 time instead of using icicle. When the state is saved,
 the value is written synchronously.
 Moved settings from CrashRecoveryHandler to BrowserSettings

Change-Id: I1a241d4c488fe3246c7d7f1ee0ce26c42ba29429
diff --git a/src/com/android/browser/CrashRecoveryHandler.java b/src/com/android/browser/CrashRecoveryHandler.java
index 3202016..822e82a 100644
--- a/src/com/android/browser/CrashRecoveryHandler.java
+++ b/src/com/android/browser/CrashRecoveryHandler.java
@@ -37,8 +37,6 @@
     private static final boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
     private static final String LOGTAG = "BrowserCrashRecovery";
     private static final String STATE_FILE = "browser_state.parcel";
-    private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs";
-    private static final String KEY_LAST_RECOVERED = "last_recovered";
     private static final int BUFFER_SIZE = 4096;
     private static final long BACKUP_DELAY = 500; // 500ms between writes
     /* This is the duration for which we will prompt to restore
@@ -85,31 +83,8 @@
             public void handleMessage(Message msg) {
                 switch (msg.what) {
                 case MSG_WRITE_STATE:
-                    if (LOGV_ENABLED) {
-                        Log.v(LOGTAG, "Saving crash recovery state");
-                    }
-                    Parcel p = Parcel.obtain();
-                    try {
-                        Bundle state = (Bundle) msg.obj;
-                        state.writeToParcel(p, 0);
-                        File stateJournal = new File(mContext.getCacheDir(),
-                                STATE_FILE + ".journal");
-                        FileOutputStream fout = new FileOutputStream(stateJournal);
-                        fout.write(p.marshall());
-                        fout.close();
-                        File stateFile = new File(mContext.getCacheDir(),
-                                STATE_FILE);
-                        if (!stateJournal.renameTo(stateFile)) {
-                            // Failed to rename, try deleting the existing
-                            // file and try again
-                            stateFile.delete();
-                            stateJournal.renameTo(stateFile);
-                        }
-                    } catch (Throwable e) {
-                        Log.i(LOGTAG, "Failed to save persistent state", e);
-                    } finally {
-                        p.recycle();
-                    }
+                    Bundle saveState = (Bundle) msg.obj;
+                    writeState(saveState);
                     break;
                 case MSG_CLEAR_STATE:
                     if (LOGV_ENABLED) {
@@ -142,8 +117,7 @@
         @Override
         public void run() {
             try {
-                final Bundle state = new Bundle();
-                mController.onSaveInstanceState(state);
+                final Bundle state = mController.createSaveState();
                 Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state)
                         .sendToTarget();
                 // Remove any queued up saves
@@ -162,28 +136,24 @@
     }
 
     private boolean shouldRestore() {
-        SharedPreferences prefs = mContext.getSharedPreferences(
-                RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
-        long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0);
+        BrowserSettings browserSettings = BrowserSettings.getInstance();
+        long lastRecovered = browserSettings.getLastRecovered();
         long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
-        if (timeSinceLastRecover > PROMPT_INTERVAL) {
-            return true;
-        }
-        return false;
+        return (timeSinceLastRecover > PROMPT_INTERVAL)
+                || browserSettings.wasLastRunPaused();
     }
 
     private void updateLastRecovered(long time) {
-        SharedPreferences prefs = mContext.getSharedPreferences(
-                RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
-        prefs.edit()
-            .putLong(KEY_LAST_RECOVERED, time)
-            .apply();
+        BrowserSettings browserSettings = BrowserSettings.getInstance();
+        browserSettings.setLastRecovered(time);
     }
 
-    private Bundle loadCrashState() {
+    synchronized private Bundle loadCrashState() {
         if (!shouldRestore()) {
             return null;
         }
+        BrowserSettings browserSettings = BrowserSettings.getInstance();
+        browserSettings.setLastRunPaused(false);
         Bundle state = null;
         Parcel parcel = Parcel.obtain();
         FileInputStream fin = null;
@@ -231,7 +201,7 @@
         }
         updateLastRecovered(mRecoveryState != null
                 ? System.currentTimeMillis() : 0);
-        mController.doStart(mRecoveryState, intent, true);
+        mController.doStart(mRecoveryState, intent);
         mRecoveryState = null;
     }
 
@@ -245,4 +215,35 @@
         mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE);
     }
 
-}
+    /**
+     * Writes the crash recovery state to a file synchronously.
+     * Errors are swallowed, but logged.
+     * @param state The state to write out
+     */
+    synchronized void writeState(Bundle state) {
+        if (LOGV_ENABLED) {
+            Log.v(LOGTAG, "Saving crash recovery state");
+        }
+        Parcel p = Parcel.obtain();
+        try {
+            state.writeToParcel(p, 0);
+            File stateJournal = new File(mContext.getCacheDir(),
+                    STATE_FILE + ".journal");
+            FileOutputStream fout = new FileOutputStream(stateJournal);
+            fout.write(p.marshall());
+            fout.close();
+            File stateFile = new File(mContext.getCacheDir(),
+                    STATE_FILE);
+            if (!stateJournal.renameTo(stateFile)) {
+                // Failed to rename, try deleting the existing
+                // file and try again
+                stateFile.delete();
+                stateJournal.renameTo(stateFile);
+            }
+        } catch (Throwable e) {
+            Log.i(LOGTAG, "Failed to save persistent state", e);
+        } finally {
+            p.recycle();
+        }
+    }
+}
\ No newline at end of file