blob: cc6eeb77233d7f7c3ff8355cf5c1a266c6d62598 [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 */
16package com.android.browser;
17
18import android.content.ContentResolver;
19import android.content.ContentUris;
20import android.content.ContentValues;
21import android.database.Cursor;
22import android.graphics.BitmapFactory;
John Reckd8c74522011-06-14 08:45:00 -070023import android.net.Uri;
24import android.os.AsyncTask;
John Reck1cf4b792011-07-26 10:22:22 -070025import android.os.Bundle;
John Reck8cc92352011-07-06 17:41:52 -070026import android.util.Log;
John Reckd8c74522011-06-14 08:45:00 -070027import android.webkit.WebView;
28
John Reck8cc92352011-07-06 17:41:52 -070029import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reckd8c74522011-06-14 08:45:00 -070030
31import java.io.ByteArrayInputStream;
John Reckef654f12011-07-12 16:42:08 -070032import java.util.Map;
John Reck8cc92352011-07-06 17:41:52 -070033import java.util.zip.GZIPInputStream;
John Reckd8c74522011-06-14 08:45:00 -070034
35
36public class SnapshotTab extends Tab {
37
John Reck8cc92352011-07-06 17:41:52 -070038 private static final String LOGTAG = "SnapshotTab";
39
John Reckd8c74522011-06-14 08:45:00 -070040 private long mSnapshotId;
41 private LoadData mLoadTask;
42 private WebViewFactory mWebViewFactory;
John Reckd8c74522011-06-14 08:45:00 -070043 private int mBackgroundColor;
John Reckef654f12011-07-12 16:42:08 -070044 private long mDateCreated;
45 private boolean mIsLive;
John Reckd8c74522011-06-14 08:45:00 -070046
47 public SnapshotTab(WebViewController wvcontroller, long snapshotId) {
John Reck1cf4b792011-07-26 10:22:22 -070048 super(wvcontroller, null, null);
John Reckd8c74522011-06-14 08:45:00 -070049 mSnapshotId = snapshotId;
50 mWebViewFactory = mWebViewController.getWebViewFactory();
John Reckef654f12011-07-12 16:42:08 -070051 WebView web = mWebViewFactory.createWebView(false);
52 setWebView(web);
John Reckd8c74522011-06-14 08:45:00 -070053 loadData();
54 }
55
56 @Override
57 void putInForeground() {
58 if (getWebView() == null) {
59 WebView web = mWebViewFactory.createWebView(false);
60 if (mBackgroundColor != 0) {
61 web.setBackgroundColor(mBackgroundColor);
62 }
63 setWebView(web);
64 loadData();
65 }
66 super.putInForeground();
67 }
68
69 @Override
70 void putInBackground() {
71 if (getWebView() == null) return;
72 super.putInBackground();
John Reckd8c74522011-06-14 08:45:00 -070073 }
74
75 void loadData() {
76 if (mLoadTask == null) {
Michael Kolb14612442011-06-24 13:06:29 -070077 mLoadTask = new LoadData(this, mContext.getContentResolver());
John Reckd8c74522011-06-14 08:45:00 -070078 mLoadTask.execute();
79 }
80 }
81
82 @Override
83 void addChildTab(Tab child) {
John Recke91c7482011-08-23 14:04:29 -070084 if (mIsLive) {
85 super.addChildTab(child);
86 } else {
87 throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
88 }
John Reckd8c74522011-06-14 08:45:00 -070089 }
90
91 @Override
92 public boolean isSnapshot() {
John Reckef654f12011-07-12 16:42:08 -070093 return !mIsLive;
John Reckd8c74522011-06-14 08:45:00 -070094 }
95
96 public long getSnapshotId() {
97 return mSnapshotId;
98 }
99
100 @Override
101 public ContentValues createSnapshotValues() {
John Recke91c7482011-08-23 14:04:29 -0700102 if (mIsLive) {
103 return super.createSnapshotValues();
104 }
John Reckd8c74522011-06-14 08:45:00 -0700105 return null;
106 }
107
108 @Override
John Reck80a5fbb2011-07-27 15:05:16 -0700109 public Bundle saveState() {
John Recke91c7482011-08-23 14:04:29 -0700110 if (mIsLive) {
111 return super.saveState();
112 }
John Reck1cf4b792011-07-26 10:22:22 -0700113 return null;
John Reckd8c74522011-06-14 08:45:00 -0700114 }
115
John Reckef654f12011-07-12 16:42:08 -0700116 public long getDateCreated() {
117 return mDateCreated;
118 }
119
120 @Override
121 public void loadUrl(String url, Map<String, String> headers) {
122 if (!mIsLive) {
123 mIsLive = true;
124 getWebView().clearViewState();
125 }
126 super.loadUrl(url, headers);
127 }
128
129 @Override
130 public boolean canGoBack() {
131 return super.canGoBack() || mIsLive;
132 }
133
134 @Override
135 public boolean canGoForward() {
136 return mIsLive && super.canGoForward();
137 }
138
139 @Override
140 public void goBack() {
141 if (super.canGoBack()) {
142 super.goBack();
143 } else {
144 mIsLive = false;
145 getWebView().stopLoading();
146 loadData();
147 }
148 }
149
John Reckd8c74522011-06-14 08:45:00 -0700150 static class LoadData extends AsyncTask<Void, Void, Cursor> {
151
152 static final String[] PROJECTION = new String[] {
153 Snapshots._ID, // 0
154 Snapshots.TITLE, // 1
155 Snapshots.URL, // 2
156 Snapshots.FAVICON, // 3
157 Snapshots.VIEWSTATE, // 4
158 Snapshots.BACKGROUND, // 5
John Reckef654f12011-07-12 16:42:08 -0700159 Snapshots.DATE_CREATED, // 6
John Reckd8c74522011-06-14 08:45:00 -0700160 };
161
162 private SnapshotTab mTab;
163 private ContentResolver mContentResolver;
164
165 public LoadData(SnapshotTab t, ContentResolver cr) {
166 mTab = t;
167 mContentResolver = cr;
168 }
169
170 @Override
171 protected Cursor doInBackground(Void... params) {
172 long id = mTab.mSnapshotId;
173 Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
174 return mContentResolver.query(uri, PROJECTION, null, null, null);
175 }
176
177 @Override
178 protected void onPostExecute(Cursor result) {
179 try {
180 if (result.moveToFirst()) {
181 mTab.mCurrentState.mTitle = result.getString(1);
182 mTab.mCurrentState.mUrl = result.getString(2);
183 byte[] favicon = result.getBlob(3);
184 if (favicon != null) {
185 mTab.mCurrentState.mFavicon = BitmapFactory
186 .decodeByteArray(favicon, 0, favicon.length);
187 }
188 WebView web = mTab.getWebView();
189 if (web != null) {
190 byte[] data = result.getBlob(4);
John Reck8cc92352011-07-06 17:41:52 -0700191 ByteArrayInputStream bis = new ByteArrayInputStream(data);
192 try {
193 GZIPInputStream stream = new GZIPInputStream(bis);
194 web.loadViewState(stream);
195 } catch (Exception e) {
196 Log.w(LOGTAG, "Failed to load view state", e);
197 }
John Reckd8c74522011-06-14 08:45:00 -0700198 }
199 mTab.mBackgroundColor = result.getInt(5);
John Reckef654f12011-07-12 16:42:08 -0700200 mTab.mDateCreated = result.getLong(6);
John Reckd8c74522011-06-14 08:45:00 -0700201 mTab.mWebViewController.onPageFinished(mTab);
202 }
203 } finally {
204 if (result != null) {
205 result.close();
206 }
207 mTab.mLoadTask = null;
208 }
209 }
210
211 }
John Reck1cf4b792011-07-26 10:22:22 -0700212
213 @Override
214 protected void persistThumbnail() {
John Recke91c7482011-08-23 14:04:29 -0700215 if (mIsLive) {
216 super.persistThumbnail();
217 }
John Reck1cf4b792011-07-26 10:22:22 -0700218 }
John Reckd8c74522011-06-14 08:45:00 -0700219}