diff options
Diffstat (limited to 'src/com/android/browser/CrashRecoveryHandler.java')
-rw-r--r-- | src/com/android/browser/CrashRecoveryHandler.java | 89 |
1 files changed, 45 insertions, 44 deletions
diff --git a/src/com/android/browser/CrashRecoveryHandler.java b/src/com/android/browser/CrashRecoveryHandler.java index 3202016e..822e82a8 100644 --- a/src/com/android/browser/CrashRecoveryHandler.java +++ b/src/com/android/browser/CrashRecoveryHandler.java @@ -37,8 +37,6 @@ public class CrashRecoveryHandler { 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 class CrashRecoveryHandler { 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 @@ public class CrashRecoveryHandler { @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 @@ public class CrashRecoveryHandler { } 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 @@ public class CrashRecoveryHandler { } updateLastRecovered(mRecoveryState != null ? System.currentTimeMillis() : 0); - mController.doStart(mRecoveryState, intent, true); + mController.doStart(mRecoveryState, intent); mRecoveryState = null; } @@ -245,4 +215,35 @@ public class CrashRecoveryHandler { 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 |