blob: e57502f490c514cff2b7f0bad12799f445db7e61 [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 Reck8cc92352011-07-06 17:41:52 -070025import android.util.Log;
John Reckd8c74522011-06-14 08:45:00 -070026import android.webkit.WebView;
27
John Reck8cc92352011-07-06 17:41:52 -070028import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reckd8c74522011-06-14 08:45:00 -070029
30import java.io.ByteArrayInputStream;
John Reckef654f12011-07-12 16:42:08 -070031import java.util.Map;
John Reck8cc92352011-07-06 17:41:52 -070032import java.util.zip.GZIPInputStream;
John Reckd8c74522011-06-14 08:45:00 -070033
34
35public class SnapshotTab extends Tab {
36
John Reck8cc92352011-07-06 17:41:52 -070037 private static final String LOGTAG = "SnapshotTab";
38
John Reckd8c74522011-06-14 08:45:00 -070039 private long mSnapshotId;
40 private LoadData mLoadTask;
41 private WebViewFactory mWebViewFactory;
John Reckd8c74522011-06-14 08:45:00 -070042 private int mBackgroundColor;
John Reckef654f12011-07-12 16:42:08 -070043 private long mDateCreated;
44 private boolean mIsLive;
John Reckd8c74522011-06-14 08:45:00 -070045
46 public SnapshotTab(WebViewController wvcontroller, long snapshotId) {
47 super(wvcontroller, null);
48 mSnapshotId = snapshotId;
49 mWebViewFactory = mWebViewController.getWebViewFactory();
John Reckef654f12011-07-12 16:42:08 -070050 WebView web = mWebViewFactory.createWebView(false);
51 setWebView(web);
John Reckd8c74522011-06-14 08:45:00 -070052 loadData();
53 }
54
55 @Override
56 void putInForeground() {
57 if (getWebView() == null) {
58 WebView web = mWebViewFactory.createWebView(false);
59 if (mBackgroundColor != 0) {
60 web.setBackgroundColor(mBackgroundColor);
61 }
62 setWebView(web);
63 loadData();
64 }
65 super.putInForeground();
66 }
67
68 @Override
69 void putInBackground() {
70 if (getWebView() == null) return;
71 super.putInBackground();
John Reckd8c74522011-06-14 08:45:00 -070072 }
73
74 void loadData() {
75 if (mLoadTask == null) {
Michael Kolb14612442011-06-24 13:06:29 -070076 mLoadTask = new LoadData(this, mContext.getContentResolver());
John Reckd8c74522011-06-14 08:45:00 -070077 mLoadTask.execute();
78 }
79 }
80
81 @Override
82 void addChildTab(Tab child) {
83 throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
84 }
85
86 @Override
87 public boolean isSnapshot() {
John Reckef654f12011-07-12 16:42:08 -070088 return !mIsLive;
John Reckd8c74522011-06-14 08:45:00 -070089 }
90
91 public long getSnapshotId() {
92 return mSnapshotId;
93 }
94
95 @Override
96 public ContentValues createSnapshotValues() {
97 return null;
98 }
99
100 @Override
101 boolean saveState() {
102 return false;
103 }
104
John Reckef654f12011-07-12 16:42:08 -0700105 public long getDateCreated() {
106 return mDateCreated;
107 }
108
109 @Override
110 public void loadUrl(String url, Map<String, String> headers) {
111 if (!mIsLive) {
112 mIsLive = true;
113 getWebView().clearViewState();
114 }
115 super.loadUrl(url, headers);
116 }
117
118 @Override
119 public boolean canGoBack() {
120 return super.canGoBack() || mIsLive;
121 }
122
123 @Override
124 public boolean canGoForward() {
125 return mIsLive && super.canGoForward();
126 }
127
128 @Override
129 public void goBack() {
130 if (super.canGoBack()) {
131 super.goBack();
132 } else {
133 mIsLive = false;
134 getWebView().stopLoading();
135 loadData();
136 }
137 }
138
John Reckd8c74522011-06-14 08:45:00 -0700139 static class LoadData extends AsyncTask<Void, Void, Cursor> {
140
141 static final String[] PROJECTION = new String[] {
142 Snapshots._ID, // 0
143 Snapshots.TITLE, // 1
144 Snapshots.URL, // 2
145 Snapshots.FAVICON, // 3
146 Snapshots.VIEWSTATE, // 4
147 Snapshots.BACKGROUND, // 5
John Reckef654f12011-07-12 16:42:08 -0700148 Snapshots.DATE_CREATED, // 6
John Reckd8c74522011-06-14 08:45:00 -0700149 };
150
151 private SnapshotTab mTab;
152 private ContentResolver mContentResolver;
153
154 public LoadData(SnapshotTab t, ContentResolver cr) {
155 mTab = t;
156 mContentResolver = cr;
157 }
158
159 @Override
160 protected Cursor doInBackground(Void... params) {
161 long id = mTab.mSnapshotId;
162 Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
163 return mContentResolver.query(uri, PROJECTION, null, null, null);
164 }
165
166 @Override
167 protected void onPostExecute(Cursor result) {
168 try {
169 if (result.moveToFirst()) {
170 mTab.mCurrentState.mTitle = result.getString(1);
171 mTab.mCurrentState.mUrl = result.getString(2);
172 byte[] favicon = result.getBlob(3);
173 if (favicon != null) {
174 mTab.mCurrentState.mFavicon = BitmapFactory
175 .decodeByteArray(favicon, 0, favicon.length);
176 }
177 WebView web = mTab.getWebView();
178 if (web != null) {
179 byte[] data = result.getBlob(4);
John Reck8cc92352011-07-06 17:41:52 -0700180 ByteArrayInputStream bis = new ByteArrayInputStream(data);
181 try {
182 GZIPInputStream stream = new GZIPInputStream(bis);
183 web.loadViewState(stream);
184 } catch (Exception e) {
185 Log.w(LOGTAG, "Failed to load view state", e);
186 }
John Reckd8c74522011-06-14 08:45:00 -0700187 }
188 mTab.mBackgroundColor = result.getInt(5);
John Reckef654f12011-07-12 16:42:08 -0700189 mTab.mDateCreated = result.getLong(6);
John Reckd8c74522011-06-14 08:45:00 -0700190 mTab.mWebViewController.onPageFinished(mTab);
191 }
192 } finally {
193 if (result != null) {
194 result.close();
195 }
196 mTab.mLoadTask = null;
197 }
198 }
199
200 }
201}