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