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