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