blob: 7f87cf238b1c65e993017fd252860c1147995c4d [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;
John Reckd8c74522011-06-14 08:45:00 -070052
53 public SnapshotTab(WebViewController wvcontroller, long snapshotId) {
John Reck1cf4b792011-07-26 10:22:22 -070054 super(wvcontroller, null, null);
John Reckd8c74522011-06-14 08:45:00 -070055 mSnapshotId = snapshotId;
56 mWebViewFactory = mWebViewController.getWebViewFactory();
John Reckef654f12011-07-12 16:42:08 -070057 WebView web = mWebViewFactory.createWebView(false);
58 setWebView(web);
John Reckd8c74522011-06-14 08:45:00 -070059 loadData();
60 }
61
62 @Override
63 void putInForeground() {
64 if (getWebView() == null) {
65 WebView web = mWebViewFactory.createWebView(false);
66 if (mBackgroundColor != 0) {
67 web.setBackgroundColor(mBackgroundColor);
68 }
69 setWebView(web);
70 loadData();
71 }
72 super.putInForeground();
73 }
74
75 @Override
76 void putInBackground() {
77 if (getWebView() == null) return;
78 super.putInBackground();
John Reckd8c74522011-06-14 08:45:00 -070079 }
80
81 void loadData() {
82 if (mLoadTask == null) {
John Reck2b71d6d2012-04-18 17:42:06 -070083 mLoadTask = new LoadData(this, mContext);
John Reckd8c74522011-06-14 08:45:00 -070084 mLoadTask.execute();
85 }
86 }
87
88 @Override
89 void addChildTab(Tab child) {
John Recke91c7482011-08-23 14:04:29 -070090 if (mIsLive) {
91 super.addChildTab(child);
92 } else {
93 throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
94 }
John Reckd8c74522011-06-14 08:45:00 -070095 }
96
97 @Override
98 public boolean isSnapshot() {
John Reckef654f12011-07-12 16:42:08 -070099 return !mIsLive;
John Reckd8c74522011-06-14 08:45:00 -0700100 }
101
102 public long getSnapshotId() {
103 return mSnapshotId;
104 }
105
106 @Override
Vivek Sekhar46c3ec02014-10-09 17:28:57 -0700107 public ContentValues createSnapshotValues(Bitmap bm) {
John Recke91c7482011-08-23 14:04:29 -0700108 if (mIsLive) {
Vivek Sekhar46c3ec02014-10-09 17:28:57 -0700109 return super.createSnapshotValues(bm);
John Recke91c7482011-08-23 14:04:29 -0700110 }
John Reckd8c74522011-06-14 08:45:00 -0700111 return null;
112 }
113
114 @Override
John Reck80a5fbb2011-07-27 15:05:16 -0700115 public Bundle saveState() {
John Recke91c7482011-08-23 14:04:29 -0700116 if (mIsLive) {
117 return super.saveState();
118 }
John Reck1cf4b792011-07-26 10:22:22 -0700119 return null;
John Reckd8c74522011-06-14 08:45:00 -0700120 }
121
John Reckef654f12011-07-12 16:42:08 -0700122 public long getDateCreated() {
123 return mDateCreated;
124 }
125
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800126 public String getLiveUrl() {
127 return mLiveUrl;
128 }
129
John Reckef654f12011-07-12 16:42:08 -0700130 @Override
131 public void loadUrl(String url, Map<String, String> headers) {
132 if (!mIsLive) {
133 mIsLive = true;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800134 getWebView().clearViewState();
John Reckef654f12011-07-12 16:42:08 -0700135 }
136 super.loadUrl(url, headers);
137 }
138
139 @Override
140 public boolean canGoBack() {
141 return super.canGoBack() || mIsLive;
142 }
143
144 @Override
145 public boolean canGoForward() {
146 return mIsLive && super.canGoForward();
147 }
148
149 @Override
150 public void goBack() {
151 if (super.canGoBack()) {
152 super.goBack();
153 } else {
154 mIsLive = false;
155 getWebView().stopLoading();
156 loadData();
157 }
158 }
159
John Reckd8c74522011-06-14 08:45:00 -0700160 static class LoadData extends AsyncTask<Void, Void, Cursor> {
161
162 static final String[] PROJECTION = new String[] {
163 Snapshots._ID, // 0
John Reck2b71d6d2012-04-18 17:42:06 -0700164 Snapshots.URL, // 1
165 Snapshots.TITLE, // 2
John Reckd8c74522011-06-14 08:45:00 -0700166 Snapshots.FAVICON, // 3
167 Snapshots.VIEWSTATE, // 4
168 Snapshots.BACKGROUND, // 5
John Reckef654f12011-07-12 16:42:08 -0700169 Snapshots.DATE_CREATED, // 6
John Reck2b71d6d2012-04-18 17:42:06 -0700170 Snapshots.VIEWSTATE_PATH, // 7
John Reckd8c74522011-06-14 08:45:00 -0700171 };
John Reck2b71d6d2012-04-18 17:42:06 -0700172 static final int SNAPSHOT_ID = 0;
173 static final int SNAPSHOT_URL = 1;
174 static final int SNAPSHOT_TITLE = 2;
175 static final int SNAPSHOT_FAVICON = 3;
176 static final int SNAPSHOT_VIEWSTATE = 4;
177 static final int SNAPSHOT_BACKGROUND = 5;
178 static final int SNAPSHOT_DATE_CREATED = 6;
179 static final int SNAPSHOT_VIEWSTATE_PATH = 7;
John Reckd8c74522011-06-14 08:45:00 -0700180
181 private SnapshotTab mTab;
182 private ContentResolver mContentResolver;
John Reck2b71d6d2012-04-18 17:42:06 -0700183 private Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700184
John Reck2b71d6d2012-04-18 17:42:06 -0700185 public LoadData(SnapshotTab t, Context context) {
John Reckd8c74522011-06-14 08:45:00 -0700186 mTab = t;
John Reck2b71d6d2012-04-18 17:42:06 -0700187 mContentResolver = context.getContentResolver();
188 mContext = context;
John Reckd8c74522011-06-14 08:45:00 -0700189 }
190
191 @Override
192 protected Cursor doInBackground(Void... params) {
193 long id = mTab.mSnapshotId;
194 Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
195 return mContentResolver.query(uri, PROJECTION, null, null, null);
196 }
197
John Reck2b71d6d2012-04-18 17:42:06 -0700198 private InputStream getInputStream(Cursor c) throws FileNotFoundException {
John Reck2b71d6d2012-04-18 17:42:06 -0700199 byte[] data = c.getBlob(SNAPSHOT_VIEWSTATE);
200 ByteArrayInputStream bis = new ByteArrayInputStream(data);
201 return bis;
202 }
203
John Reckd8c74522011-06-14 08:45:00 -0700204 @Override
205 protected void onPostExecute(Cursor result) {
206 try {
207 if (result.moveToFirst()) {
John Reck2b71d6d2012-04-18 17:42:06 -0700208 mTab.mCurrentState.mTitle = result.getString(SNAPSHOT_TITLE);
209 mTab.mCurrentState.mUrl = result.getString(SNAPSHOT_URL);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800210 mTab.mLiveUrl = result.getString(SNAPSHOT_URL);
John Reck2b71d6d2012-04-18 17:42:06 -0700211 byte[] favicon = result.getBlob(SNAPSHOT_FAVICON);
John Reckd8c74522011-06-14 08:45:00 -0700212 if (favicon != null) {
213 mTab.mCurrentState.mFavicon = BitmapFactory
214 .decodeByteArray(favicon, 0, favicon.length);
215 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800216 WebView web = mTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700217 if (web != null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800218 String path = result.getString(SNAPSHOT_VIEWSTATE_PATH);
219 if (!TextUtils.isEmpty(path)) {
220 web.loadViewState(path);
221 } else {
222 InputStream ins = getInputStream(result);
223 GZIPInputStream stream = new GZIPInputStream(ins);
224 web.loadViewState(stream);
225 }
John Reckd8c74522011-06-14 08:45:00 -0700226 }
John Reck2b71d6d2012-04-18 17:42:06 -0700227 mTab.mBackgroundColor = result.getInt(SNAPSHOT_BACKGROUND);
228 mTab.mDateCreated = result.getLong(SNAPSHOT_DATE_CREATED);
John Reckd8c74522011-06-14 08:45:00 -0700229 mTab.mWebViewController.onPageFinished(mTab);
230 }
John Reck28263772011-10-11 17:13:42 -0700231 } catch (Exception e) {
232 Log.w(LOGTAG, "Failed to load view state, closing tab", e);
233 mTab.mWebViewController.closeTab(mTab);
John Reckd8c74522011-06-14 08:45:00 -0700234 } finally {
235 if (result != null) {
236 result.close();
237 }
238 mTab.mLoadTask = null;
239 }
240 }
241
242 }
John Reck1cf4b792011-07-26 10:22:22 -0700243
244 @Override
245 protected void persistThumbnail() {
John Recke91c7482011-08-23 14:04:29 -0700246 if (mIsLive) {
247 super.persistThumbnail();
248 }
John Reck1cf4b792011-07-26 10:22:22 -0700249 }
John Reckd8c74522011-06-14 08:45:00 -0700250}