blob: c63bf102f91d161c61c1d8771e4276624e26dc04 [file] [log] [blame]
John Reckd8c74522011-06-14 08:45:00 -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 */
Bijan Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
John Reckd8c74522011-06-14 08:45:00 -070017
18import android.content.ContentResolver;
19import android.content.ContentUris;
20import android.content.ContentValues;
John Reck2b71d6d2012-04-18 17:42:06 -070021import android.content.Context;
John Reckd8c74522011-06-14 08:45:00 -070022import android.database.Cursor;
Vivek Sekhar46c3ec02014-10-09 17:28:57 -070023import android.graphics.Bitmap;
John Reckd8c74522011-06-14 08:45:00 -070024import android.graphics.BitmapFactory;
John Reckd8c74522011-06-14 08:45:00 -070025import android.net.Uri;
26import android.os.AsyncTask;
John Reck1cf4b792011-07-26 10:22:22 -070027import android.os.Bundle;
John Reck2b71d6d2012-04-18 17:42:06 -070028import android.text.TextUtils;
John Reck8cc92352011-07-06 17:41:52 -070029import android.util.Log;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080030import org.codeaurora.swe.WebView;
John Reckd8c74522011-06-14 08:45:00 -070031
Bijan Amirzada41242f22014-03-21 12:12:18 -070032import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reckd8c74522011-06-14 08:45:00 -070033
34import java.io.ByteArrayInputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070035import java.io.FileNotFoundException;
36import java.io.InputStream;
John Reckef654f12011-07-12 16:42:08 -070037import java.util.Map;
John Reck8cc92352011-07-06 17:41:52 -070038import java.util.zip.GZIPInputStream;
John Reckd8c74522011-06-14 08:45:00 -070039
40
41public class SnapshotTab extends Tab {
42
John Reck8cc92352011-07-06 17:41:52 -070043 private static final String LOGTAG = "SnapshotTab";
44
John Reckd8c74522011-06-14 08:45:00 -070045 private long mSnapshotId;
46 private LoadData mLoadTask;
47 private WebViewFactory mWebViewFactory;
John Reckd8c74522011-06-14 08:45:00 -070048 private int mBackgroundColor;
John Reckef654f12011-07-12 16:42:08 -070049 private long mDateCreated;
50 private boolean mIsLive;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080051 private String mLiveUrl;
Axesh R. Ajmerae6b11aa2015-03-16 18:20:41 -070052
53 // Used for saving and restoring each Tab
54 static final String SNAPSHOT_ID = "snapshotId";
Axesh R. Ajmera4b000312015-07-23 10:00:16 -070055 static final String ID = "ID";
John Reckd8c74522011-06-14 08:45:00 -070056
Axesh R. Ajmera4b000312015-07-23 10:00:16 -070057
58 public SnapshotTab(WebViewController wvcontroller,
59 long snapshotId,
60 Bundle state) {
61 super(wvcontroller, null, state);
John Reckd8c74522011-06-14 08:45:00 -070062 mSnapshotId = snapshotId;
63 mWebViewFactory = mWebViewController.getWebViewFactory();
John Reckef654f12011-07-12 16:42:08 -070064 WebView web = mWebViewFactory.createWebView(false);
65 setWebView(web);
John Reckd8c74522011-06-14 08:45:00 -070066 loadData();
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -070067 mIsLive = false;
John Reckd8c74522011-06-14 08:45:00 -070068 }
69
70 @Override
71 void putInForeground() {
72 if (getWebView() == null) {
73 WebView web = mWebViewFactory.createWebView(false);
74 if (mBackgroundColor != 0) {
75 web.setBackgroundColor(mBackgroundColor);
76 }
77 setWebView(web);
78 loadData();
79 }
80 super.putInForeground();
81 }
82
83 @Override
84 void putInBackground() {
85 if (getWebView() == null) return;
86 super.putInBackground();
John Reckd8c74522011-06-14 08:45:00 -070087 }
88
89 void loadData() {
90 if (mLoadTask == null) {
John Reck2b71d6d2012-04-18 17:42:06 -070091 mLoadTask = new LoadData(this, mContext);
John Reckd8c74522011-06-14 08:45:00 -070092 mLoadTask.execute();
93 }
94 }
95
96 @Override
97 void addChildTab(Tab child) {
John Recke91c7482011-08-23 14:04:29 -070098 if (mIsLive) {
99 super.addChildTab(child);
100 } else {
101 throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
102 }
John Reckd8c74522011-06-14 08:45:00 -0700103 }
104
105 @Override
106 public boolean isSnapshot() {
John Reckef654f12011-07-12 16:42:08 -0700107 return !mIsLive;
John Reckd8c74522011-06-14 08:45:00 -0700108 }
109
110 public long getSnapshotId() {
111 return mSnapshotId;
112 }
113
114 @Override
Vivek Sekhar46c3ec02014-10-09 17:28:57 -0700115 public ContentValues createSnapshotValues(Bitmap bm) {
John Recke91c7482011-08-23 14:04:29 -0700116 if (mIsLive) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -0700117 return super.createSnapshotValues(bm);
John Recke91c7482011-08-23 14:04:29 -0700118 }
John Reckd8c74522011-06-14 08:45:00 -0700119 return null;
120 }
121
122 @Override
John Reck80a5fbb2011-07-27 15:05:16 -0700123 public Bundle saveState() {
John Recke91c7482011-08-23 14:04:29 -0700124 if (mIsLive) {
125 return super.saveState();
126 }
Axesh R. Ajmerae6b11aa2015-03-16 18:20:41 -0700127
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700128 Bundle savedState = new Bundle();
129 savedState.putLong(SNAPSHOT_ID, mSnapshotId);
130 savedState.putLong(ID, getId());
131
132 return savedState;
John Reckd8c74522011-06-14 08:45:00 -0700133 }
134
John Reckef654f12011-07-12 16:42:08 -0700135 public long getDateCreated() {
136 return mDateCreated;
137 }
138
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800139 public String getLiveUrl() {
140 return mLiveUrl;
141 }
142
John Reckef654f12011-07-12 16:42:08 -0700143 @Override
John Reckef654f12011-07-12 16:42:08 -0700144 public boolean canGoBack() {
145 return super.canGoBack() || mIsLive;
146 }
147
148 @Override
149 public boolean canGoForward() {
150 return mIsLive && super.canGoForward();
151 }
152
153 @Override
154 public void goBack() {
155 if (super.canGoBack()) {
156 super.goBack();
157 } else {
158 mIsLive = false;
159 getWebView().stopLoading();
160 loadData();
161 }
162 }
163
John Reckd8c74522011-06-14 08:45:00 -0700164 static class LoadData extends AsyncTask<Void, Void, Cursor> {
165
166 static final String[] PROJECTION = new String[] {
167 Snapshots._ID, // 0
John Reck2b71d6d2012-04-18 17:42:06 -0700168 Snapshots.URL, // 1
169 Snapshots.TITLE, // 2
John Reckd8c74522011-06-14 08:45:00 -0700170 Snapshots.FAVICON, // 3
171 Snapshots.VIEWSTATE, // 4
172 Snapshots.BACKGROUND, // 5
John Reckef654f12011-07-12 16:42:08 -0700173 Snapshots.DATE_CREATED, // 6
John Reck2b71d6d2012-04-18 17:42:06 -0700174 Snapshots.VIEWSTATE_PATH, // 7
John Reckd8c74522011-06-14 08:45:00 -0700175 };
John Reck2b71d6d2012-04-18 17:42:06 -0700176 static final int SNAPSHOT_ID = 0;
177 static final int SNAPSHOT_URL = 1;
178 static final int SNAPSHOT_TITLE = 2;
179 static final int SNAPSHOT_FAVICON = 3;
180 static final int SNAPSHOT_VIEWSTATE = 4;
181 static final int SNAPSHOT_BACKGROUND = 5;
182 static final int SNAPSHOT_DATE_CREATED = 6;
183 static final int SNAPSHOT_VIEWSTATE_PATH = 7;
John Reckd8c74522011-06-14 08:45:00 -0700184
185 private SnapshotTab mTab;
186 private ContentResolver mContentResolver;
John Reck2b71d6d2012-04-18 17:42:06 -0700187 private Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700188
John Reck2b71d6d2012-04-18 17:42:06 -0700189 public LoadData(SnapshotTab t, Context context) {
John Reckd8c74522011-06-14 08:45:00 -0700190 mTab = t;
John Reck2b71d6d2012-04-18 17:42:06 -0700191 mContentResolver = context.getContentResolver();
192 mContext = context;
John Reckd8c74522011-06-14 08:45:00 -0700193 }
194
195 @Override
196 protected Cursor doInBackground(Void... params) {
197 long id = mTab.mSnapshotId;
198 Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
199 return mContentResolver.query(uri, PROJECTION, null, null, null);
200 }
201
John Reck2b71d6d2012-04-18 17:42:06 -0700202 private InputStream getInputStream(Cursor c) throws FileNotFoundException {
John Reck2b71d6d2012-04-18 17:42:06 -0700203 byte[] data = c.getBlob(SNAPSHOT_VIEWSTATE);
204 ByteArrayInputStream bis = new ByteArrayInputStream(data);
205 return bis;
206 }
207
John Reckd8c74522011-06-14 08:45:00 -0700208 @Override
209 protected void onPostExecute(Cursor result) {
210 try {
211 if (result.moveToFirst()) {
John Reck2b71d6d2012-04-18 17:42:06 -0700212 mTab.mCurrentState.mTitle = result.getString(SNAPSHOT_TITLE);
213 mTab.mCurrentState.mUrl = result.getString(SNAPSHOT_URL);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800214 mTab.mLiveUrl = result.getString(SNAPSHOT_URL);
John Reck2b71d6d2012-04-18 17:42:06 -0700215 byte[] favicon = result.getBlob(SNAPSHOT_FAVICON);
John Reckd8c74522011-06-14 08:45:00 -0700216 if (favicon != null) {
217 mTab.mCurrentState.mFavicon = BitmapFactory
218 .decodeByteArray(favicon, 0, favicon.length);
219 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800220 WebView web = mTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700221 if (web != null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800222 String path = result.getString(SNAPSHOT_VIEWSTATE_PATH);
223 if (!TextUtils.isEmpty(path)) {
224 web.loadViewState(path);
225 } else {
226 InputStream ins = getInputStream(result);
227 GZIPInputStream stream = new GZIPInputStream(ins);
228 web.loadViewState(stream);
229 }
John Reckd8c74522011-06-14 08:45:00 -0700230 }
John Reck2b71d6d2012-04-18 17:42:06 -0700231 mTab.mBackgroundColor = result.getInt(SNAPSHOT_BACKGROUND);
232 mTab.mDateCreated = result.getLong(SNAPSHOT_DATE_CREATED);
John Reckd8c74522011-06-14 08:45:00 -0700233 mTab.mWebViewController.onPageFinished(mTab);
234 }
John Reck28263772011-10-11 17:13:42 -0700235 } catch (Exception e) {
236 Log.w(LOGTAG, "Failed to load view state, closing tab", e);
237 mTab.mWebViewController.closeTab(mTab);
John Reckd8c74522011-06-14 08:45:00 -0700238 } finally {
239 if (result != null) {
240 result.close();
241 }
242 mTab.mLoadTask = null;
243 }
244 }
245
246 }
John Reck1cf4b792011-07-26 10:22:22 -0700247
248 @Override
249 protected void persistThumbnail() {
John Recke91c7482011-08-23 14:04:29 -0700250 if (mIsLive) {
251 super.persistThumbnail();
252 }
John Reck1cf4b792011-07-26 10:22:22 -0700253 }
John Reckd8c74522011-06-14 08:45:00 -0700254}