blob: 6e40ffdb6d14b1adc84a7a61668e2c1f6bdc9a8d [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;
John Reckf57c0292011-07-21 18:15:39 -070028import android.os.Looper;
John Reck847b5322011-04-14 17:02:18 -070029import android.os.Parcel;
30import android.util.Log;
31
32import java.io.ByteArrayOutputStream;
John Recka4816532011-06-29 14:41:59 -070033import java.io.File;
John Reck847b5322011-04-14 17:02:18 -070034import java.io.FileInputStream;
35import java.io.FileNotFoundException;
36import java.io.FileOutputStream;
37
38public class CrashRecoveryHandler {
39
40 private static final String LOGTAG = "BrowserCrashRecovery";
41 private static final String STATE_FILE = "browser_state.parcel";
John Reckcfeae6d2011-06-24 15:33:57 -070042 private static final String RECOVERY_PREFERENCES = "browser_recovery_prefs";
43 private static final String KEY_LAST_RECOVERED = "last_recovered";
John Reck847b5322011-04-14 17:02:18 -070044 private static final int BUFFER_SIZE = 4096;
John Reck378a4102011-06-09 16:23:01 -070045 private static final long BACKUP_DELAY = 500; // 500ms between writes
John Reckcfeae6d2011-06-24 15:33:57 -070046 /* 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 Reck378a4102011-06-09 16:23:01 -070052
53 private static CrashRecoveryHandler sInstance;
John Reck847b5322011-04-14 17:02:18 -070054
55 private Controller mController;
John Reck378a4102011-06-09 16:23:01 -070056 private Handler mForegroundHandler;
57 private Handler mBackgroundHandler;
John Reck847b5322011-04-14 17:02:18 -070058
John Reck378a4102011-06-09 16:23:01 -070059 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 Reck847b5322011-04-14 17:02:18 -070073 mController = controller;
John Reck378a4102011-06-09 16:23:01 -070074 mForegroundHandler = new Handler();
John Reckf57c0292011-07-21 18:15:39 -070075 Looper looper = BrowserSettings.getInstance().getBackgroundLooper();
76 mBackgroundHandler = new Handler(looper);
John Reck847b5322011-04-14 17:02:18 -070077 }
78
79 public void backupState() {
John Reck378a4102011-06-09 16:23:01 -070080 mForegroundHandler.postDelayed(mCreateState, BACKUP_DELAY);
John Reck847b5322011-04-14 17:02:18 -070081 }
82
John Reck378a4102011-06-09 16:23:01 -070083 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 Reck24f18262011-06-17 14:47:20 -0700114 if (mState.isEmpty()) {
115 clearState(mContext);
116 return;
117 }
John Reck378a4102011-06-09 16:23:01 -0700118 Parcel p = Parcel.obtain();
119 try {
120 mState.writeToParcel(p, 0);
John Recka4816532011-06-29 14:41:59 -0700121 File state = new File(mContext.getCacheDir(), STATE_FILE);
122 FileOutputStream fout = new FileOutputStream(state);
John Reck378a4102011-06-09 16:23:01 -0700123 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 Reckf630b552011-07-17 14:18:51 -0700134 public static void clearState(Context context) {
John Recka4816532011-06-29 14:41:59 -0700135 File state = new File(context.getCacheDir(), STATE_FILE);
136 if (state.exists()) {
137 state.delete();
138 }
John Reck847b5322011-04-14 17:02:18 -0700139 }
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 Reck24f18262011-06-17 14:47:20 -0700145 .setIcon(R.mipmap.ic_launcher_browser)
John Reck847b5322011-04-14 17:02:18 -0700146 .setPositiveButton(R.string.recover_yes, new OnClickListener() {
147 @Override
148 public void onClick(DialogInterface dialog, int which) {
John Reckcfeae6d2011-06-24 15:33:57 -0700149 updateLastRecovered();
John Reck847b5322011-04-14 17:02:18 -0700150 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 Reck0b3165d2011-06-22 10:44:33 -0700156 dialog.cancel();
157 }
158 })
159 .setOnCancelListener(new OnCancelListener() {
160 @Override
161 public void onCancel(DialogInterface dialog) {
John Reck24f18262011-06-17 14:47:20 -0700162 clearState(mController.getActivity());
John Reck847b5322011-04-14 17:02:18 -0700163 mController.doStart(null, intent);
164 }
165 })
166 .show();
167 }
168
John Reckcfeae6d2011-06-24 15:33:57 -0700169 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 Reck847b5322011-04-14 17:02:18 -0700191 public void startRecovery(Intent intent) {
John Reckcfeae6d2011-06-24 15:33:57 -0700192 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700193 Parcel parcel = Parcel.obtain();
194 try {
195 Context context = mController.getActivity();
John Recka4816532011-06-29 14:41:59 -0700196 File stateFile = new File(context.getCacheDir(), STATE_FILE);
197 FileInputStream fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700198 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 Reckcfeae6d2011-06-24 15:33:57 -0700207 state = parcel.readBundle();
208 if (shouldPrompt()) {
209 promptToRecover(state, intent);
210 return;
211 } else {
212 updateLastRecovered();
213 }
John Reck847b5322011-04-14 17:02:18 -0700214 } catch (FileNotFoundException e) {
215 // No state to recover
John Reckcfeae6d2011-06-24 15:33:57 -0700216 state = null;
John Reck847b5322011-04-14 17:02:18 -0700217 } catch (Exception e) {
218 Log.w(LOGTAG, "Failed to recover state!", e);
John Reckcfeae6d2011-06-24 15:33:57 -0700219 state = null;
John Reck847b5322011-04-14 17:02:18 -0700220 } finally {
221 parcel.recycle();
222 }
John Reckcfeae6d2011-06-24 15:33:57 -0700223 mController.doStart(state, intent);
John Reck847b5322011-04-14 17:02:18 -0700224 }
225}