blob: bd6dd5b87daed9208345653e0bf0814d55daf1e4 [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) {
84 throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
85 }
86
87 @Override
88 public boolean isSnapshot() {
John Reckef654f12011-07-12 16:42:08 -070089 return !mIsLive;
John Reckd8c74522011-06-14 08:45:00 -070090 }
91
92 public long getSnapshotId() {
93 return mSnapshotId;
94 }
95
96 @Override
97 public ContentValues createSnapshotValues() {
98 return null;
99 }
100
101 @Override
John Reck1cf4b792011-07-26 10:22:22 -0700102 Bundle saveState() {
103 return null;
John Reckd8c74522011-06-14 08:45:00 -0700104 }
105
John Reckef654f12011-07-12 16:42:08 -0700106 public long getDateCreated() {
107 return mDateCreated;
108 }
109
110 @Override
111 public void loadUrl(String url, Map<String, String> headers) {
112 if (!mIsLive) {
113 mIsLive = true;
114 getWebView().clearViewState();
115 }
116 super.loadUrl(url, headers);
117 }
118
119 @Override
120 public boolean canGoBack() {
121 return super.canGoBack() || mIsLive;
122 }
123
124 @Override
125 public boolean canGoForward() {
126 return mIsLive && super.canGoForward();
127 }
128
129 @Override
130 public void goBack() {
131 if (super.canGoBack()) {
132 super.goBack();
133 } else {
134 mIsLive = false;
135 getWebView().stopLoading();
136 loadData();
137 }
138 }
139
John Reckd8c74522011-06-14 08:45:00 -0700140 static class LoadData extends AsyncTask<Void, Void, Cursor> {
141
142 static final String[] PROJECTION = new String[] {
143 Snapshots._ID, // 0
144 Snapshots.TITLE, // 1
145 Snapshots.URL, // 2
146 Snapshots.FAVICON, // 3
147 Snapshots.VIEWSTATE, // 4
148 Snapshots.BACKGROUND, // 5
John Reckef654f12011-07-12 16:42:08 -0700149 Snapshots.DATE_CREATED, // 6
John Reckd8c74522011-06-14 08:45:00 -0700150 };
151
152 private SnapshotTab mTab;
153 private ContentResolver mContentResolver;
154
155 public LoadData(SnapshotTab t, ContentResolver cr) {
156 mTab = t;
157 mContentResolver = cr;
158 }
159
160 @Override
161 protected Cursor doInBackground(Void... params) {
162 long id = mTab.mSnapshotId;
163 Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
164 return mContentResolver.query(uri, PROJECTION, null, null, null);
165 }
166
167 @Override
168 protected void onPostExecute(Cursor result) {
169 try {
170 if (result.moveToFirst()) {
171 mTab.mCurrentState.mTitle = result.getString(1);
172 mTab.mCurrentState.mUrl = result.getString(2);
173 byte[] favicon = result.getBlob(3);
174 if (favicon != null) {
175 mTab.mCurrentState.mFavicon = BitmapFactory
176 .decodeByteArray(favicon, 0, favicon.length);
177 }
178 WebView web = mTab.getWebView();
179 if (web != null) {
180 byte[] data = result.getBlob(4);
John Reck8cc92352011-07-06 17:41:52 -0700181 ByteArrayInputStream bis = new ByteArrayInputStream(data);
182 try {
183 GZIPInputStream stream = new GZIPInputStream(bis);
184 web.loadViewState(stream);
185 } catch (Exception e) {
186 Log.w(LOGTAG, "Failed to load view state", e);
187 }
John Reckd8c74522011-06-14 08:45:00 -0700188 }
189 mTab.mBackgroundColor = result.getInt(5);
John Reckef654f12011-07-12 16:42:08 -0700190 mTab.mDateCreated = result.getLong(6);
John Reckd8c74522011-06-14 08:45:00 -0700191 mTab.mWebViewController.onPageFinished(mTab);
192 }
193 } finally {
194 if (result != null) {
195 result.close();
196 }
197 mTab.mLoadTask = null;
198 }
199 }
200
201 }
John Reck1cf4b792011-07-26 10:22:22 -0700202
203 @Override
204 protected void persistThumbnail() {
205 // Nope
206 }
207
208 @Override
209 protected void deleteThumbnail() {
210 // Nope
211 }
212
John Reckd8c74522011-06-14 08:45:00 -0700213}