blob: ca538bd42e82ae22017000cc92ba00e560101cc4 [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 Reck24f18262011-06-17 14:47:20 -0700137 private 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() {
John Recka4816532011-06-29 14:41:59 -0700173 // STOPSHIP TODO: Remove once b/4971724 is fixed
174 if (true) return false;
John Reckcfeae6d2011-06-24 15:33:57 -0700175 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 Reck847b5322011-04-14 17:02:18 -0700196 public void startRecovery(Intent intent) {
John Reckcfeae6d2011-06-24 15:33:57 -0700197 Bundle state = null;
John Reck847b5322011-04-14 17:02:18 -0700198 Parcel parcel = Parcel.obtain();
199 try {
200 Context context = mController.getActivity();
John Recka4816532011-06-29 14:41:59 -0700201 File stateFile = new File(context.getCacheDir(), STATE_FILE);
202 FileInputStream fin = new FileInputStream(stateFile);
John Reck847b5322011-04-14 17:02:18 -0700203 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 Reckcfeae6d2011-06-24 15:33:57 -0700212 state = parcel.readBundle();
213 if (shouldPrompt()) {
214 promptToRecover(state, intent);
215 return;
216 } else {
217 updateLastRecovered();
218 }
John Reck847b5322011-04-14 17:02:18 -0700219 } catch (FileNotFoundException e) {
220 // No state to recover
John Reckcfeae6d2011-06-24 15:33:57 -0700221 state = null;
John Reck847b5322011-04-14 17:02:18 -0700222 } catch (Exception e) {
223 Log.w(LOGTAG, "Failed to recover state!", e);
John Reckcfeae6d2011-06-24 15:33:57 -0700224 state = null;
John Reck847b5322011-04-14 17:02:18 -0700225 } finally {
226 parcel.recycle();
227 }
John Reckcfeae6d2011-06-24 15:33:57 -0700228 mController.doStart(state, intent);
John Reck847b5322011-04-14 17:02:18 -0700229 }
230}