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; |
| 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 | f57c029 | 2011-07-21 18:15:39 -0700 | [diff] [blame] | 86 | Looper looper = BrowserSettings.getInstance().getBackgroundLooper(); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 87 | mBackgroundHandler = new Handler(looper) { |
| 88 | |
| 89 | @Override |
| 90 | public void handleMessage(Message msg) { |
| 91 | switch (msg.what) { |
| 92 | case MSG_WRITE_STATE: |
| 93 | Parcel p = Parcel.obtain(); |
| 94 | try { |
| 95 | Bundle state = (Bundle) msg.obj; |
| 96 | state.writeToParcel(p, 0); |
| 97 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
| 98 | FileOutputStream fout = new FileOutputStream(stateFile); |
| 99 | fout.write(p.marshall()); |
| 100 | fout.close(); |
| 101 | } catch (Throwable e) { |
| 102 | Log.i(LOGTAG, "Failed to save persistent state", e); |
| 103 | } finally { |
| 104 | p.recycle(); |
| 105 | } |
| 106 | break; |
| 107 | case MSG_CLEAR_STATE: |
| 108 | File state = new File(mContext.getCacheDir(), STATE_FILE); |
| 109 | if (state.exists()) { |
| 110 | state.delete(); |
| 111 | } |
| 112 | break; |
| 113 | case MSG_PRELOAD_STATE: |
| 114 | mRecoveryState = loadCrashState(); |
| 115 | mShouldPrompt = shouldPrompt(); |
| 116 | synchronized (CrashRecoveryHandler.this) { |
| 117 | mIsPreloading = false; |
| 118 | mDidPreload = true; |
| 119 | CrashRecoveryHandler.this.notifyAll(); |
| 120 | } |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | }; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public void backupState() { |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 128 | mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 129 | } |
| 130 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 131 | private Runnable mCreateState = new Runnable() { |
| 132 | |
| 133 | @Override |
| 134 | public void run() { |
| 135 | try { |
| 136 | final Bundle state = new Bundle(); |
| 137 | mController.onSaveInstanceState(state, false); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 138 | Message.obtain(mBackgroundHandler, MSG_WRITE_STATE, state) |
| 139 | .sendToTarget(); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 140 | // Remove any queued up saves |
| 141 | mForegroundHandler.removeCallbacks(mCreateState); |
| 142 | } catch (Throwable t) { |
| 143 | Log.w(LOGTAG, "Failed to save state", t); |
| 144 | return; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | }; |
| 149 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 150 | public void clearState() { |
| 151 | mBackgroundHandler.sendEmptyMessage(MSG_CLEAR_STATE); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | public void promptToRecover(final Bundle state, final Intent intent) { |
| 155 | new AlertDialog.Builder(mController.getActivity()) |
| 156 | .setTitle(R.string.recover_title) |
| 157 | .setMessage(R.string.recover_prompt) |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 158 | .setIcon(R.mipmap.ic_launcher_browser) |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 159 | .setPositiveButton(R.string.recover_yes, new OnClickListener() { |
| 160 | @Override |
| 161 | public void onClick(DialogInterface dialog, int which) { |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 162 | updateLastRecovered(); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 163 | mController.doStart(state, intent); |
| 164 | } |
| 165 | }) |
| 166 | .setNegativeButton(R.string.recover_no, new OnClickListener() { |
| 167 | @Override |
| 168 | public void onClick(DialogInterface dialog, int which) { |
John Reck | 0b3165d | 2011-06-22 10:44:33 -0700 | [diff] [blame] | 169 | dialog.cancel(); |
| 170 | } |
| 171 | }) |
| 172 | .setOnCancelListener(new OnCancelListener() { |
| 173 | @Override |
| 174 | public void onCancel(DialogInterface dialog) { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 175 | clearState(); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 176 | mController.doStart(null, intent); |
| 177 | } |
| 178 | }) |
| 179 | .show(); |
| 180 | } |
| 181 | |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 182 | private boolean shouldPrompt() { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 183 | SharedPreferences prefs = mContext.getSharedPreferences( |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 184 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 185 | long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, 0); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 186 | long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered; |
| 187 | if (timeSinceLastRecover > PROMPT_INTERVAL) { |
| 188 | return false; |
| 189 | } |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | private void updateLastRecovered() { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 194 | SharedPreferences prefs = mContext.getSharedPreferences( |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 195 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
| 196 | prefs.edit() |
| 197 | .putLong(KEY_LAST_RECOVERED, System.currentTimeMillis()) |
Ben Murdoch | fb72a9a | 2011-07-25 18:18:32 +0100 | [diff] [blame^] | 198 | .apply(); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 199 | } |
| 200 | |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 201 | private Bundle loadCrashState() { |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 202 | Bundle state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 203 | Parcel parcel = Parcel.obtain(); |
| 204 | try { |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 205 | File stateFile = new File(mContext.getCacheDir(), STATE_FILE); |
John Reck | a481653 | 2011-06-29 14:41:59 -0700 | [diff] [blame] | 206 | FileInputStream 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(); |
| 225 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 226 | return state; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 227 | } |
John Reck | bc490d2 | 2011-07-22 15:04:59 -0700 | [diff] [blame] | 228 | |
| 229 | public void startRecovery(Intent intent) { |
| 230 | synchronized (CrashRecoveryHandler.this) { |
| 231 | while (mIsPreloading) { |
| 232 | try { |
| 233 | CrashRecoveryHandler.this.wait(); |
| 234 | } catch (InterruptedException e) {} |
| 235 | } |
| 236 | } |
| 237 | if (!mDidPreload) { |
| 238 | mRecoveryState = loadCrashState(); |
| 239 | mShouldPrompt = shouldPrompt(); |
| 240 | } |
| 241 | if (mShouldPrompt) { |
| 242 | promptToRecover(mRecoveryState, intent); |
| 243 | return; |
| 244 | } else { |
| 245 | updateLastRecovered(); |
| 246 | } |
| 247 | mController.doStart(mRecoveryState, intent); |
| 248 | mRecoveryState = null; |
| 249 | } |
| 250 | |
| 251 | public void preloadCrashState() { |
| 252 | synchronized (CrashRecoveryHandler.this) { |
| 253 | if (mIsPreloading) { |
| 254 | return; |
| 255 | } |
| 256 | mIsPreloading = true; |
| 257 | } |
| 258 | mBackgroundHandler.sendEmptyMessage(MSG_PRELOAD_STATE); |
| 259 | } |
| 260 | |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 261 | } |