blob: 8d7f243b82b4eb1952dd9c2b15bd4a02694f8f9d [file] [log] [blame]
John Reck847b5322011-04-14 17:02:18 -07001/*
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
17package com.android.browser;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
John Reck0b3165d2011-06-22 10:44:33 -070022import android.content.DialogInterface.OnCancelListener;
John Reck847b5322011-04-14 17:02:18 -070023import android.content.DialogInterface.OnClickListener;
24import android.content.Intent;
John Reckcfeae6d2011-06-24 15:33:57 -070025import android.content.SharedPreferences;
John Reck847b5322011-04-14 17:02:18 -070026import android.os.Bundle;
John Reck378a4102011-06-09 16:23:01 -070027import android.os.Handler;
28import android.os.HandlerThread;
John Reck847b5322011-04-14 17:02:18 -070029import android.os.Parcel;
John Reck378a4102011-06-09 16:23:01 -070030import android.os.Process;
John Reck847b5322011-04-14 17:02:18 -070031import android.util.Log;
32
33import java.io.ByteArrayOutputStream;
John Recka4816532011-06-29 14:41:59 -070034import java.io.File;
John Reck847b5322011-04-14 17:02:18 -070035import java.io.FileInputStream;
36import java.io.FileNotFoundException;
37import java.io.FileOutputStream;
38
39public class CrashRecoveryHandler {
40
41 private static final String LOGTAG = "BrowserCrashRecovery";
42 private static final String STATE_FILE = "browser_state.parcel";
John Reckcfeae6d2011-06-24 15:33:57 -070043 private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs";
44 private static final String KEY_LAST_RECOVERED = "last_recovered";
John Reck847b5322011-04-14 17:02:18 -070045 private static final int BUFFER_SIZE = 4096;
John Reck378a4102011-06-09 16:23:01 -070046 private static final long BACKUP_DELAY = 500; // 500ms between writes
John Reckcfeae6d2011-06-24 15:33:57 -070047 /* 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 Reck378a4102011-06-09 16:23:01 -070053
54 private static CrashRecoveryHandler sInstance;
John Reck847b5322011-04-14 17:02:18 -070055
56 private Controller mController;
John Reck378a4102011-06-09 16:23:01 -070057 private Handler mForegroundHandler;
58 private Handler mBackgroundHandler;
John Reck847b5322011-04-14 17:02:18 -070059
John Reck378a4102011-06-09 16:23:01 -070060 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 Reck847b5322011-04-14 17:02:18 -070074 mController = controller;
John Reck378a4102011-06-09 16:23:01 -070075 mForegroundHandler = new Handler();
76 HandlerThread thread = new HandlerThread(LOGTAG,
77 Process.THREAD_PRIORITY_BACKGROUND);
78 thread.start();
79 mBackgroundHandler = new Handler(thread.getLooper());
John Reck847b5322011-04-14 17:02:18 -070080 }
81
82 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -070083 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -070084 }
85
John Reck378a4102011-06-09 16:23:01 -070086 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 Reck24f18262011-06-17 14:47:20 -0700117 if (mState.isEmpty()) {
118 clearState(mContext);
119 return;
120 }
John Reck378a4102011-06-09 16:23:01 -0700121 Parcel p = Parcel.obtain();
122 try {
123 mState.writeToParcel(p, 0);
John Recka4816532011-06-29 14:41:59 -0700124 File state = new File(mContext.getCacheDir(), STATE_FILE);
125 FileOutputStream fout = new FileOutputStream(state);
John Reck378a4102011-06-09 16:23:01 -0700126 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 Reckf630b552011-07-17 14:18:51 -0700137 public static void clearState(Context context) {
John Recka4816532011-06-29 14:41:59 -0700138 File state = new File(context.getCacheDir(), STATE_FILE);
139 if (state.exists()) {
140 state.delete();
141 }
John Reck847b5322011-04-14 17:02:18 -0700142 }
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 Reck24f18262011-06-17 14:47:20 -0700148 .setIcon(R.mipmap.ic_launcher_browser)
John Reck847b5322011-04-14 17:02:18 -0700149 .setPositiveButton(R.string.recover_yes, new OnClickListener() {
150 @Override
151 public void onClick(DialogInterface dialog, int which) {
John Reckcfeae6d2011-06-24 15:33:57 -0700152 updateLastRecovered();
John Reck847b5322011-04-14 17:02:18 -0700153 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 Reck0b3165d2011-06-22 10:44:33 -0700159 dialog.cancel();
160 }
161 })
162 .setOnCancelListener(new OnCancelListener() {
163 @Override
164 public void onCancel(DialogInterface dialog) {
John Reck24f18262011-06-17 14:47:20 -0700165 clearState(mController.getActivity());
John Reck847b5322011-04-14 17:02:18 -0700166 mController.doStart(null, intent);
167 }
168 })
169 .show();
170 }
171
John Reckcfeae6d2011-06-24 15:33:57 -0700172 private boolean shouldPrompt() {
173 Context context = mController.getActivity();
174 SharedPreferences prefs = context.getSharedPreferences(
175 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
176 long lastRecovered = prefs.getLong(KEY_LAST_RECOVERED,
177 System.currentTimeMillis());
178 long timeSinceLastRecover = System.currentTimeMillis() - lastRecovered;
179 if (timeSinceLastRecover > PROMPT_INTERVAL) {
180 return false;
181 }
182 return true;
183 }
184
185 private void updateLastRecovered() {
186 Context context = mController.getActivity();
187 SharedPreferences prefs = context.getSharedPreferences(
188 RECOVERY_PREFERENCES, Context.MODE_PRIVATE);
189 prefs.edit()
190 .putLong(KEY_LAST_RECOVERED, System.currentTimeMillis())
191 .commit();
192 }
193
John Reck847b5322011-04-14 17:02:18 -0700194 public void startRecovery(Intent intent) {
John Reckcfeae6d2011-06-24 15:33:57 -0700195 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700196 Parcel parcel = Parcel.obtain();
197 try {
198 Context context = mController.getActivity();
John Recka4816532011-06-29 14:41:59 -0700199 File stateFile = new File(context.getCacheDir(), STATE_FILE);
200 FileInputStream fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700201 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
202 byte[] buffer = new byte[BUFFER_SIZE];
203 int read;
204 while ((read = fin.read(buffer)) > 0) {
205 dataStream.write(buffer, 0, read);
206 }
207 byte[] data = dataStream.toByteArray();
208 parcel.unmarshall(data, 0, data.length);
209 parcel.setDataPosition(0);
John Reckcfeae6d2011-06-24 15:33:57 -0700210 state = parcel.readBundle();
211 if (shouldPrompt()) {
212 promptToRecover(state, intent);
213 return;
214 } else {
215 updateLastRecovered();
216 }
John Reck847b5322011-04-14 17:02:18 -0700217 } catch (FileNotFoundException e) {
218 // No state to recover
John Reckcfeae6d2011-06-24 15:33:57 -0700219 state = null;
John Reck847b5322011-04-14 17:02:18 -0700220 } catch (Exception e) {
221 Log.w(LOGTAG, "Failed to recover state!", e);
John Reckcfeae6d2011-06-24 15:33:57 -0700222 state = null;
John Reck847b5322011-04-14 17:02:18 -0700223 } finally {
224 parcel.recycle();
225 }
John Reckcfeae6d2011-06-24 15:33:57 -0700226 mController.doStart(state, intent);
John Reck847b5322011-04-14 17:02:18 -0700227 }
228}