John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.browser; |
| 17 | |
| 18 | import android.content.ContentResolver; |
| 19 | import android.content.ContentUris; |
| 20 | import android.content.ContentValues; |
| 21 | import android.database.Cursor; |
| 22 | import android.graphics.BitmapFactory; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 23 | import android.net.Uri; |
| 24 | import android.os.AsyncTask; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 26 | import android.util.Log; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 27 | import android.webkit.WebView; |
| 28 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 29 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 30 | |
| 31 | import java.io.ByteArrayInputStream; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 32 | import java.util.Map; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 33 | import java.util.zip.GZIPInputStream; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 34 | |
| 35 | |
| 36 | public class SnapshotTab extends Tab { |
| 37 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 38 | private static final String LOGTAG = "SnapshotTab"; |
| 39 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 40 | private long mSnapshotId; |
| 41 | private LoadData mLoadTask; |
| 42 | private WebViewFactory mWebViewFactory; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 43 | private int mBackgroundColor; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 44 | private long mDateCreated; |
| 45 | private boolean mIsLive; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 46 | |
| 47 | public SnapshotTab(WebViewController wvcontroller, long snapshotId) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 48 | super(wvcontroller, null, null); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 49 | mSnapshotId = snapshotId; |
| 50 | mWebViewFactory = mWebViewController.getWebViewFactory(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 51 | WebView web = mWebViewFactory.createWebView(false); |
| 52 | setWebView(web); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 53 | 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 Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void loadData() { |
| 76 | if (mLoadTask == null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 77 | mLoadTask = new LoadData(this, mContext.getContentResolver()); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 78 | mLoadTask.execute(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | void addChildTab(Tab child) { |
John Reck | e91c748 | 2011-08-23 14:04:29 -0700 | [diff] [blame^] | 84 | if (mIsLive) { |
| 85 | super.addChildTab(child); |
| 86 | } else { |
| 87 | throw new IllegalStateException("Snapshot tabs cannot have child tabs!"); |
| 88 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean isSnapshot() { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 93 | return !mIsLive; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public long getSnapshotId() { |
| 97 | return mSnapshotId; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public ContentValues createSnapshotValues() { |
John Reck | e91c748 | 2011-08-23 14:04:29 -0700 | [diff] [blame^] | 102 | if (mIsLive) { |
| 103 | return super.createSnapshotValues(); |
| 104 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 105 | return null; |
| 106 | } |
| 107 | |
| 108 | @Override |
John Reck | 80a5fbb | 2011-07-27 15:05:16 -0700 | [diff] [blame] | 109 | public Bundle saveState() { |
John Reck | e91c748 | 2011-08-23 14:04:29 -0700 | [diff] [blame^] | 110 | if (mIsLive) { |
| 111 | return super.saveState(); |
| 112 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 113 | return null; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 114 | } |
| 115 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 116 | 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 Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 150 | 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 159 | Snapshots.DATE_CREATED, // 6 |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 160 | }; |
| 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 Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 191 | 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 Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 198 | } |
| 199 | mTab.mBackgroundColor = result.getInt(5); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 200 | mTab.mDateCreated = result.getLong(6); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 201 | mTab.mWebViewController.onPageFinished(mTab); |
| 202 | } |
| 203 | } finally { |
| 204 | if (result != null) { |
| 205 | result.close(); |
| 206 | } |
| 207 | mTab.mLoadTask = null; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 212 | |
| 213 | @Override |
| 214 | protected void persistThumbnail() { |
John Reck | e91c748 | 2011-08-23 14:04:29 -0700 | [diff] [blame^] | 215 | if (mIsLive) { |
| 216 | super.persistThumbnail(); |
| 217 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 218 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 219 | } |