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