John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.content.Context; |
| 21 | import android.content.DialogInterface; |
John Reck | 0b3165d | 2011-06-22 10:44:33 -0700 | [diff] [blame] | 22 | import android.content.DialogInterface.OnCancelListener; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 23 | import android.content.DialogInterface.OnClickListener; |
| 24 | import android.content.Intent; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 25 | import android.content.SharedPreferences; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 26 | import android.os.Bundle; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 28 | import android.os.Message; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.util.Log; |
| 31 | |
| 32 | import java.io.ByteArrayOutputStream; |
John Reck | a481653 | 2011-06-29 14:41:59 -0700 | [diff] [blame] | 33 | import java.io.File; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 34 | import java.io.FileInputStream; |
| 35 | import java.io.FileNotFoundException; |
| 36 | import java.io.FileOutputStream; |
| 37 | |
| 38 | public class CrashRecoveryHandler { |
| 39 | |
| 40 | private static final String LOGTAG = "BrowserCrashRecovery"; |
| 41 | private static final String STATE_FILE = "browser_state.parcel"; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 42 | private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs"; |
| 43 | private static final String KEY_LAST_RECOVERED = "last_recovered"; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 44 | private static final int BUFFER_SIZE = 4096; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 45 | private static final long BACKUP_DELAY = 500; // 500ms between writes |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 46 | /* This is the duration for which we will prompt to restore |
| 47 | * instead of automatically restoring. The first time the browser crashes, |
| 48 | * we will automatically restore. If we then crash again within XX minutes, |
| 49 | * we will prompt instead of automatically restoring. |
| 50 | */ |
| 51 | private static final long PROMPT_INTERVAL = 30 * 60 * 1000; // 30 minutes |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 52 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 53 | private static final int MSG_WRITE_STATE = 1; |
| 54 | private static final int MSG_CLEAR_STATE = 2; |
| 55 | private static final int MSG_PRELOAD_STATE = 3; |
| 56 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 57 | private static CrashRecoveryHandler sInstance; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 58 | |
| 59 | private Controller mController; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 60 | private Context mContext; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 61 | private Handler mForegroundHandler; |
| 62 | private Handler mBackgroundHandler; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 63 | private boolean mIsPreloading = false; |
| 64 | private boolean mDidPreload = false; |
| 65 | private boolean mShouldPrompt = false; |
| 66 | private Bundle mRecoveryState = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 67 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 68 | public static CrashRecoveryHandler initialize(Controller controller) { |
| 69 | if (sInstance == null) { |
| 70 | sInstance = new CrashRecoveryHandler(controller); |
| 71 | } else { |
| 72 | sInstance.mController = controller; |
| 73 | } |
| 74 | return sInstance; |
| 75 | } |
| 76 | |
| 77 | public static CrashRecoveryHandler getInstance() { |
| 78 | return sInstance; |
| 79 | } |
| 80 | |
| 81 | private CrashRecoveryHandler(Controller controller) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 82 | mController = controller; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 83 | mContext = mController.getActivity().getApplicationContext(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 84 | mForegroundHandler = new Handler(); |
John Reck | cadae72 | 2011-07-25 11:36:17 -0700 | [diff] [blame^] | 85 | mBackgroundHandler = new Handler(BackgroundHandler.getLooper()) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | public void handleMessage(Message msg) { |
| 89 | switch (msg.what) { |
| 90 | case MSG_WRITE_STATE: |
| 91 | Parcel p = Parcel.obtain(); |
| 92 | try { |
| 93 | Bundle state = (Bundle) msg.obj; |
| 94 | state.writeToParcel(p, 0); |
| 95 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
| 96 | FileOutputStream fout = new FileOutputStream(stateFile); |
| 97 | fout.write(p.marshall()); |
| 98 | fout.close(); |
| 99 | } catch (Throwable e) { |
| 100 | Log.i(LOGTAG, "Failed to save persistent state", e); |
| 101 | } finally { |
| 102 | p.recycle(); |
| 103 | } |
| 104 | break; |
| 105 | case MSG_CLEAR_STATE: |
| 106 | File state = new File(mContext.getCacheDir(), STATE_FILE); |
| 107 | if (state.exists()) { |
| 108 | state.delete(); |
| 109 | } |
| 110 | break; |
| 111 | case MSG_PRELOAD_STATE: |
| 112 | mRecoveryState = loadCrashState(); |
| 113 | mShouldPrompt = shouldPrompt(); |
| 114 | synchronized (CrashRecoveryHandler.this) { |
| 115 | mIsPreloading = false; |
| 116 | mDidPreload = true; |
| 117 | CrashRecoveryHandler.this.notifyAll(); |
| 118 | } |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | }; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public void backupState() { |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 126 | mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 127 | } |
| 128 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 129 | private Runnable mCreateState = new Runnable() { |
| 130 | |
| 131 | @Override |
| 132 | public void run() { |
| 133 | try { |
| 134 | final Bundle state = new Bundle(); |
| 135 | mController.onSaveInstanceState(state, false); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 136 | Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state) |
| 137 | .sendToTarget(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 138 | // Remove any queued up saves |
| 139 | mForegroundHandler.removeCallbacks(mCreateState); |
| 140 | } catch (Throwable t) { |
| 141 | Log.w(LOGTAG, "Failed to save state", t); |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | }; |
| 147 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 148 | public void clearState() { |
| 149 | mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | public void promptToRecover(final Bundle state, final Intent intent) { |
| 153 | new AlertDialog.Builder(mController.getActivity()) |
| 154 | .setTitle(R.string.recover_title) |
| 155 | .setMessage(R.string.recover_prompt) |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 156 | .setIcon(R.mipmap.ic_launcher_browser) |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 157 | .setPositiveButton(R.string.recover_yes, new OnClickListener() { |
| 158 | @Override |
| 159 | public void onClick(DialogInterface dialog, int which) { |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 160 | updateLastRecovered(); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 161 | mController.doStart(state, intent); |
| 162 | } |
| 163 | }) |
| 164 | .setNegativeButton(R.string.recover_no, new OnClickListener() { |
| 165 | @Override |
| 166 | public void onClick(DialogInterface dialog, int which) { |
John Reck | 0b3165d | 2011-06-22 10:44:33 -0700 | [diff] [blame] | 167 | dialog.cancel(); |
| 168 | } |
| 169 | }) |
| 170 | .setOnCancelListener(new OnCancelListener() { |
| 171 | @Override |
| 172 | public void onCancel(DialogInterface dialog) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 173 | clearState(); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 174 | mController.doStart(null, intent); |
| 175 | } |
| 176 | }) |
| 177 | .show(); |
| 178 | } |
| 179 | |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 180 | private boolean shouldPrompt() { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 181 | SharedPreferences prefs = mContext.getSharedPreferences( |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 182 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 183 | long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 184 | long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered; |
| 185 | if (timeSinceLastRecover > PROMPT_INTERVAL) { |
| 186 | return false; |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | private void updateLastRecovered() { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 192 | SharedPreferences prefs = mContext.getSharedPreferences( |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 193 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
| 194 | prefs.edit() |
| 195 | .putLong(KEY_LAST_RECOVERED, System.currentTimeMillis()) |
| 196 | .commit(); |
| 197 | } |
| 198 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 199 | private Bundle loadCrashState() { |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 200 | Bundle state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 201 | Parcel parcel = Parcel.obtain(); |
| 202 | try { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 203 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
John Reck | a481653 | 2011-06-29 14:41:59 -0700 | [diff] [blame] | 204 | FileInputStream fin = new FileInputStream(stateFile); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 205 | ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); |
| 206 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 207 | int read; |
| 208 | while ((read = fin.read(buffer)) > 0) { |
| 209 | dataStream.write(buffer, 0, read); |
| 210 | } |
| 211 | byte[] data = dataStream.toByteArray(); |
| 212 | parcel.unmarshall(data, 0, data.length); |
| 213 | parcel.setDataPosition(0); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 214 | state = parcel.readBundle(); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 215 | } catch (FileNotFoundException e) { |
| 216 | // No state to recover |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 217 | state = null; |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 218 | } catch (Throwable e) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 219 | Log.w(LOGTAG, "Failed to recover state!", e); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 220 | state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 221 | } finally { |
| 222 | parcel.recycle(); |
| 223 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 224 | return state; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 225 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 226 | |
| 227 | public void startRecovery(Intent intent) { |
| 228 | synchronized (CrashRecoveryHandler.this) { |
| 229 | while (mIsPreloading) { |
| 230 | try { |
| 231 | CrashRecoveryHandler.this.wait(); |
| 232 | } catch (InterruptedException e) {} |
| 233 | } |
| 234 | } |
| 235 | if (!mDidPreload) { |
| 236 | mRecoveryState = loadCrashState(); |
| 237 | mShouldPrompt = shouldPrompt(); |
| 238 | } |
| 239 | if (mShouldPrompt) { |
| 240 | promptToRecover(mRecoveryState, intent); |
| 241 | return; |
| 242 | } else { |
| 243 | updateLastRecovered(); |
| 244 | } |
| 245 | mController.doStart(mRecoveryState, intent); |
| 246 | mRecoveryState = null; |
| 247 | } |
| 248 | |
| 249 | public void preloadCrashState() { |
| 250 | synchronized (CrashRecoveryHandler.this) { |
| 251 | if (mIsPreloading) { |
| 252 | return; |
| 253 | } |
| 254 | mIsPreloading = true; |
| 255 | } |
| 256 | mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE); |
| 257 | } |
| 258 | |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 259 | } |