blob: f0abf582e4f697b460f56f6888320abb99158af0 [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 Reck8cc92352011-07-06 17:41:52 -070031import java.util.zip.GZIPInputStream;
John Reckd8c74522011-06-14 08:45:00 -070032
33
34public class SnapshotTab extends Tab {
35
John Reck8cc92352011-07-06 17:41:52 -070036 private static final String LOGTAG = "SnapshotTab";
37
John Reckd8c74522011-06-14 08:45:00 -070038 private long mSnapshotId;
39 private LoadData mLoadTask;
40 private WebViewFactory mWebViewFactory;
41 // TODO: Support non-persistent webview's on phone
42 private boolean mPersistentWebview;
43 private int mBackgroundColor;
44
45 public SnapshotTab(WebViewController wvcontroller, long snapshotId) {
46 super(wvcontroller, null);
47 mSnapshotId = snapshotId;
48 mWebViewFactory = mWebViewController.getWebViewFactory();
49 mPersistentWebview = !BrowserActivity.isTablet(wvcontroller.getActivity());
50 if (mPersistentWebview) {
51 WebView web = mWebViewFactory.createWebView(false);
52 setWebView(web);
53 }
54 loadData();
55 }
56
57 @Override
58 void putInForeground() {
59 if (getWebView() == null) {
60 WebView web = mWebViewFactory.createWebView(false);
61 if (mBackgroundColor != 0) {
62 web.setBackgroundColor(mBackgroundColor);
63 }
64 setWebView(web);
65 loadData();
66 }
67 super.putInForeground();
68 }
69
70 @Override
71 void putInBackground() {
72 if (getWebView() == null) return;
73 super.putInBackground();
74 if (!mPersistentWebview) {
75 super.destroy();
76 }
77 }
78
79 void loadData() {
80 if (mLoadTask == null) {
Michael Kolb14612442011-06-24 13:06:29 -070081 mLoadTask = new LoadData(this, mContext.getContentResolver());
John Reckd8c74522011-06-14 08:45:00 -070082 mLoadTask.execute();
83 }
84 }
85
86 @Override
87 void addChildTab(Tab child) {
88 throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
89 }
90
91 @Override
92 public boolean isSnapshot() {
93 return true;
94 }
95
96 public long getSnapshotId() {
97 return mSnapshotId;
98 }
99
100 @Override
101 public ContentValues createSnapshotValues() {
102 return null;
103 }
104
105 @Override
106 boolean saveState() {
107 return false;
108 }
109
110 static class LoadData extends AsyncTask<Void, Void, Cursor> {
111
112 static final String[] PROJECTION = new String[] {
113 Snapshots._ID, // 0
114 Snapshots.TITLE, // 1
115 Snapshots.URL, // 2
116 Snapshots.FAVICON, // 3
117 Snapshots.VIEWSTATE, // 4
118 Snapshots.BACKGROUND, // 5
119 };
120
121 private SnapshotTab mTab;
122 private ContentResolver mContentResolver;
123
124 public LoadData(SnapshotTab t, ContentResolver cr) {
125 mTab = t;
126 mContentResolver = cr;
127 }
128
129 @Override
130 protected Cursor doInBackground(Void... params) {
131 long id = mTab.mSnapshotId;
132 Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
133 return mContentResolver.query(uri, PROJECTION, null, null, null);
134 }
135
136 @Override
137 protected void onPostExecute(Cursor result) {
138 try {
139 if (result.moveToFirst()) {
140 mTab.mCurrentState.mTitle = result.getString(1);
141 mTab.mCurrentState.mUrl = result.getString(2);
142 byte[] favicon = result.getBlob(3);
143 if (favicon != null) {
144 mTab.mCurrentState.mFavicon = BitmapFactory
145 .decodeByteArray(favicon, 0, favicon.length);
146 }
147 WebView web = mTab.getWebView();
148 if (web != null) {
149 byte[] data = result.getBlob(4);
John Reck8cc92352011-07-06 17:41:52 -0700150 ByteArrayInputStream bis = new ByteArrayInputStream(data);
151 try {
152 GZIPInputStream stream = new GZIPInputStream(bis);
153 web.loadViewState(stream);
154 } catch (Exception e) {
155 Log.w(LOGTAG, "Failed to load view state", e);
156 }
John Reckd8c74522011-06-14 08:45:00 -0700157 }
158 mTab.mBackgroundColor = result.getInt(5);
159 mTab.mWebViewController.onPageFinished(mTab);
160 }
161 } finally {
162 if (result != null) {
163 result.close();
164 }
165 mTab.mLoadTask = null;
166 }
167 }
168
169 }
170}