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; |
| 28 | import android.os.HandlerThread; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 29 | import android.os.Parcel; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 30 | import android.os.Process; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 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 | |
| 54 | private static CrashRecoveryHandler sInstance; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 55 | |
| 56 | private Controller mController; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 57 | private Handler mForegroundHandler; |
| 58 | private Handler mBackgroundHandler; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 59 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 60 | public static CrashRecoveryHandler initialize(Controller controller) { |
| 61 | if (sInstance == null) { |
| 62 | sInstance = new CrashRecoveryHandler(controller); |
| 63 | } else { |
| 64 | sInstance.mController = controller; |
| 65 | } |
| 66 | return sInstance; |
| 67 | } |
| 68 | |
| 69 | public static CrashRecoveryHandler getInstance() { |
| 70 | return sInstance; |
| 71 | } |
| 72 | |
| 73 | private CrashRecoveryHandler(Controller controller) { |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 74 | mController = controller; |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 75 | mForegroundHandler = new Handler(); |
| 76 | HandlerThread thread = new HandlerThread(LOGTAG, |
| 77 | Process.THREAD_PRIORITY_BACKGROUND); |
| 78 | thread.start(); |
| 79 | mBackgroundHandler = new Handler(thread.getLooper()); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public void backupState() { |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 83 | mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 86 | private Runnable mCreateState = new Runnable() { |
| 87 | |
| 88 | @Override |
| 89 | public void run() { |
| 90 | try { |
| 91 | final Bundle state = new Bundle(); |
| 92 | mController.onSaveInstanceState(state, false); |
| 93 | Context context = mController.getActivity() |
| 94 | .getApplicationContext(); |
| 95 | mBackgroundHandler.post(new WriteState(context, state)); |
| 96 | // Remove any queued up saves |
| 97 | mForegroundHandler.removeCallbacks(mCreateState); |
| 98 | } catch (Throwable t) { |
| 99 | Log.w(LOGTAG, "Failed to save state", t); |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | }; |
| 105 | |
| 106 | static class WriteState implements Runnable { |
| 107 | private Context mContext; |
| 108 | private Bundle mState; |
| 109 | |
| 110 | WriteState(Context context, Bundle state) { |
| 111 | mContext = context; |
| 112 | mState = state; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void run() { |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 117 | if (mState.isEmpty()) { |
| 118 | clearState(mContext); |
| 119 | return; |
| 120 | } |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 121 | Parcel p = Parcel.obtain(); |
| 122 | try { |
| 123 | mState.writeToParcel(p, 0); |
John Reck | a481653 | 2011-06-29 14:41:59 -0700 | [diff] [blame] | 124 | File state = new File(mContext.getCacheDir(), STATE_FILE); |
| 125 | FileOutputStream fout = new FileOutputStream(state); |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 126 | fout.write(p.marshall()); |
| 127 | fout.close(); |
| 128 | } catch (Throwable e) { |
| 129 | Log.i(LOGTAG, "Failed to save persistent state", e); |
| 130 | } finally { |
| 131 | p.recycle(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 137 | private static void clearState(Context context) { |
John Reck | a481653 | 2011-06-29 14:41:59 -0700 | [diff] [blame] | 138 | File state = new File(context.getCacheDir(), STATE_FILE); |
| 139 | if (state.exists()) { |
| 140 | state.delete(); |
| 141 | } |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public void promptToRecover(final Bundle state, final Intent intent) { |
| 145 | new AlertDialog.Builder(mController.getActivity()) |
| 146 | .setTitle(R.string.recover_title) |
| 147 | .setMessage(R.string.recover_prompt) |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 148 | .setIcon(R.mipmap.ic_launcher_browser) |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 149 | .setPositiveButton(R.string.recover_yes, new OnClickListener() { |
| 150 | @Override |
| 151 | public void onClick(DialogInterface dialog, int which) { |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 152 | updateLastRecovered(); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 153 | mController.doStart(state, intent); |
| 154 | } |
| 155 | }) |
| 156 | .setNegativeButton(R.string.recover_no, new OnClickListener() { |
| 157 | @Override |
| 158 | public void onClick(DialogInterface dialog, int which) { |
John Reck | 0b3165d | 2011-06-22 10:44:33 -0700 | [diff] [blame] | 159 | dialog.cancel(); |
| 160 | } |
| 161 | }) |
| 162 | .setOnCancelListener(new OnCancelListener() { |
| 163 | @Override |
| 164 | public void onCancel(DialogInterface dialog) { |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 165 | clearState(mController.getActivity()); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 166 | mController.doStart(null, intent); |
| 167 | } |
| 168 | }) |
| 169 | .show(); |
| 170 | } |
| 171 | |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 172 | private boolean shouldPrompt() { |
John Reck | a481653 | 2011-06-29 14:41:59 -0700 | [diff] [blame] | 173 | // STOPSHIP TODO: Remove once b/4971724 is fixed |
| 174 | if (true) return false; |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 175 | Context context = mController.getActivity(); |
| 176 | SharedPreferences prefs = context.getSharedPreferences( |
| 177 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
| 178 | long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED, |
| 179 | System.currentTimeMillis()); |
| 180 | long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered; |
| 181 | if (timeSinceLastRecover > PROMPT_INTERVAL) { |
| 182 | return false; |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | private void updateLastRecovered() { |
| 188 | Context context = mController.getActivity(); |
| 189 | SharedPreferences prefs = context.getSharedPreferences( |
| 190 | RECOVERY_PREFERENCES, Context.MODE_PRIVATE); |
| 191 | prefs.edit() |
| 192 | .putLong(KEY_LAST_RECOVERED, System.currentTimeMillis()) |
| 193 | .commit(); |
| 194 | } |
| 195 | |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 196 | public void startRecovery(Intent intent) { |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 197 | Bundle state = null; |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 198 | Parcel parcel = Parcel.obtain(); |
| 199 | try { |
| 200 | Context context = mController.getActivity(); |
John Reck | a481653 | 2011-06-29 14:41:59 -0700 | [diff] [blame] | 201 | File stateFile = new File(context.getCacheDir(), STATE_FILE); |
| 202 | FileInputStream fin = new FileInputStream(stateFile); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 203 | ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); |
| 204 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 205 | int read; |
| 206 | while ((read = fin.read(buffer)) > 0) { |
| 207 | dataStream.write(buffer, 0, read); |
| 208 | } |
| 209 | byte[] data = dataStream.toByteArray(); |
| 210 | parcel.unmarshall(data, 0, data.length); |
| 211 | parcel.setDataPosition(0); |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 212 | state = parcel.readBundle(); |
| 213 | if (shouldPrompt()) { |
| 214 | promptToRecover(state, intent); |
| 215 | return; |
| 216 | } else { |
| 217 | updateLastRecovered(); |
| 218 | } |
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 | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 222 | } catch (Exception e) { |
| 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(); |
| 227 | } |
John Reck | cfeae6d | 2011-06-24 15:33:57 -0700 | [diff] [blame] | 228 | mController.doStart(state, intent); |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 229 | } |
| 230 | } |