blob: 60e39daa53299fc2e0d1143bb80da364f20dd98a [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;
22import android.content.DialogInterface.OnClickListener;
23import android.content.Intent;
24import android.os.Bundle;
John Reck847b5322011-04-14 17:02:18 -070025import android.os.Parcel;
26import android.util.Log;
27
28import java.io.ByteArrayOutputStream;
29import java.io.FileInputStream;
30import java.io.FileNotFoundException;
31import java.io.FileOutputStream;
32
33public class CrashRecoveryHandler {
34
35 private static final String LOGTAG = "BrowserCrashRecovery";
36 private static final String STATE_FILE = "browser_state.parcel";
37 private static final int BUFFER_SIZE = 4096;
38
39 private Controller mController;
40
41 public CrashRecoveryHandler(Controller controller) {
42 mController = controller;
43 }
44
45 public void backupState() {
46 final Bundle state = new Bundle();
John Reckaed9c542011-05-27 16:08:53 -070047 mController.onSaveInstanceState(state, false);
John Reck847b5322011-04-14 17:02:18 -070048 final Context context = mController.getActivity();
49 new Thread() {
50 @Override
51 public void run() {
52 Parcel p = Parcel.obtain();
53 try {
54 state.writeToParcel(p, 0);
55 FileOutputStream fout = context.openFileOutput(STATE_FILE,
56 Context.MODE_PRIVATE);
57 fout.write(p.marshall());
58 fout.close();
John Reckaed9c542011-05-27 16:08:53 -070059 } catch (Throwable e) {
John Reck847b5322011-04-14 17:02:18 -070060 Log.i(LOGTAG, "Failed to save persistent state", e);
61 } finally {
62 p.recycle();
63 }
64 }
65 }.start();
66 }
67
68 public void clearState() {
69 Context context = mController.getActivity();
70 context.deleteFile(STATE_FILE);
71 }
72
73 public void promptToRecover(final Bundle state, final Intent intent) {
74 new AlertDialog.Builder(mController.getActivity())
75 .setTitle(R.string.recover_title)
76 .setMessage(R.string.recover_prompt)
77 .setPositiveButton(R.string.recover_yes, new OnClickListener() {
78 @Override
79 public void onClick(DialogInterface dialog, int which) {
80 mController.doStart(state, intent);
81 }
82 })
83 .setNegativeButton(R.string.recover_no, new OnClickListener() {
84 @Override
85 public void onClick(DialogInterface dialog, int which) {
86 clearState();
87 mController.doStart(null, intent);
88 }
89 })
90 .show();
91 }
92
93 public void startRecovery(Intent intent) {
94 Parcel parcel = Parcel.obtain();
95 try {
96 Context context = mController.getActivity();
97 FileInputStream fin = context.openFileInput(STATE_FILE);
98 ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
99 byte[] buffer = new byte[BUFFER_SIZE];
100 int read;
101 while ((read = fin.read(buffer)) > 0) {
102 dataStream.write(buffer, 0, read);
103 }
104 byte[] data = dataStream.toByteArray();
105 parcel.unmarshall(data, 0, data.length);
106 parcel.setDataPosition(0);
107 Bundle state = parcel.readBundle();
108 promptToRecover(state, intent);
109 } catch (FileNotFoundException e) {
110 // No state to recover
111 mController.doStart(null, intent);
112 } catch (Exception e) {
113 Log.w(LOGTAG, "Failed to recover state!", e);
114 mController.doStart(null, intent);
115 } finally {
116 parcel.recycle();
117 }
118 }
119}