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; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 21 | import android.content.Context; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 22 | import android.database.Cursor; |
| 23 | import android.graphics.BitmapFactory; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
| 25 | import android.os.AsyncTask; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 26 | import android.os.Bundle; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 27 | import android.text.TextUtils; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 28 | import android.util.Log; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 29 | import android.webkit.WebView; |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 30 | import android.webkit.WebViewClassic; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 31 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 32 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 33 | |
| 34 | import java.io.ByteArrayInputStream; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 35 | import java.io.FileNotFoundException; |
| 36 | import java.io.InputStream; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 37 | import java.util.Map; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 38 | import java.util.zip.GZIPInputStream; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | public class SnapshotTab extends Tab { |
| 42 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 43 | private static final String LOGTAG = "SnapshotTab"; |
| 44 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 45 | private long mSnapshotId; |
| 46 | private LoadData mLoadTask; |
| 47 | private WebViewFactory mWebViewFactory; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 48 | private int mBackgroundColor; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 49 | private long mDateCreated; |
| 50 | private boolean mIsLive; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 51 | |
| 52 | public SnapshotTab(WebViewController wvcontroller, long snapshotId) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 53 | super(wvcontroller, null, null); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 54 | mSnapshotId = snapshotId; |
| 55 | mWebViewFactory = mWebViewController.getWebViewFactory(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 56 | WebView web = mWebViewFactory.createWebView(false); |
| 57 | setWebView(web); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 58 | loadData(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | void putInForeground() { |
| 63 | if (getWebView() == null) { |
| 64 | WebView web = mWebViewFactory.createWebView(false); |
| 65 | if (mBackgroundColor != 0) { |
| 66 | web.setBackgroundColor(mBackgroundColor); |
| 67 | } |
| 68 | setWebView(web); |
| 69 | loadData(); |
| 70 | } |
| 71 | super.putInForeground(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | void putInBackground() { |
| 76 | if (getWebView() == null) return; |
| 77 | super.putInBackground(); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void loadData() { |
| 81 | if (mLoadTask == null) { |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 82 | mLoadTask = new LoadData(this, mContext); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 83 | mLoadTask.execute(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | void addChildTab(Tab child) { |
John Reck | e91c748 | 2011-08-23 14:04:29 -0700 | [diff] [blame] | 89 | if (mIsLive) { |
| 90 | super.addChildTab(child); |
| 91 | } else { |
| 92 | throw new IllegalStateException("Snapshot tabs cannot have child tabs!"); |
| 93 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean isSnapshot() { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 98 | return !mIsLive; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | public long getSnapshotId() { |
| 102 | return mSnapshotId; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public ContentValues createSnapshotValues() { |
John Reck | e91c748 | 2011-08-23 14:04:29 -0700 | [diff] [blame] | 107 | if (mIsLive) { |
| 108 | return super.createSnapshotValues(); |
| 109 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 110 | return null; |
| 111 | } |
| 112 | |
| 113 | @Override |
John Reck | 80a5fbb | 2011-07-27 15:05:16 -0700 | [diff] [blame] | 114 | public Bundle saveState() { |
John Reck | e91c748 | 2011-08-23 14:04:29 -0700 | [diff] [blame] | 115 | if (mIsLive) { |
| 116 | return super.saveState(); |
| 117 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 118 | return null; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 119 | } |
| 120 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 121 | public long getDateCreated() { |
| 122 | return mDateCreated; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void loadUrl(String url, Map<String, String> headers) { |
| 127 | if (!mIsLive) { |
| 128 | mIsLive = true; |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 129 | getWebViewClassic().clearViewState(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 130 | } |
| 131 | super.loadUrl(url, headers); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean canGoBack() { |
| 136 | return super.canGoBack() || mIsLive; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public boolean canGoForward() { |
| 141 | return mIsLive && super.canGoForward(); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void goBack() { |
| 146 | if (super.canGoBack()) { |
| 147 | super.goBack(); |
| 148 | } else { |
| 149 | mIsLive = false; |
| 150 | getWebView().stopLoading(); |
| 151 | loadData(); |
| 152 | } |
| 153 | } |
| 154 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 155 | static class LoadData extends AsyncTask<Void, Void, Cursor> { |
| 156 | |
| 157 | static final String[] PROJECTION = new String[] { |
| 158 | Snapshots._ID, // 0 |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 159 | Snapshots.URL, // 1 |
| 160 | Snapshots.TITLE, // 2 |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 161 | Snapshots.FAVICON, // 3 |
| 162 | Snapshots.VIEWSTATE, // 4 |
| 163 | Snapshots.BACKGROUND, // 5 |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 164 | Snapshots.DATE_CREATED, // 6 |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 165 | Snapshots.VIEWSTATE_PATH, // 7 |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 166 | }; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 167 | static final int SNAPSHOT_ID = 0; |
| 168 | static final int SNAPSHOT_URL = 1; |
| 169 | static final int SNAPSHOT_TITLE = 2; |
| 170 | static final int SNAPSHOT_FAVICON = 3; |
| 171 | static final int SNAPSHOT_VIEWSTATE = 4; |
| 172 | static final int SNAPSHOT_BACKGROUND = 5; |
| 173 | static final int SNAPSHOT_DATE_CREATED = 6; |
| 174 | static final int SNAPSHOT_VIEWSTATE_PATH = 7; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 175 | |
| 176 | private SnapshotTab mTab; |
| 177 | private ContentResolver mContentResolver; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 178 | private Context mContext; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 179 | |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 180 | public LoadData(SnapshotTab t, Context context) { |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 181 | mTab = t; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 182 | mContentResolver = context.getContentResolver(); |
| 183 | mContext = context; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | protected Cursor doInBackground(Void... params) { |
| 188 | long id = mTab.mSnapshotId; |
| 189 | Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id); |
| 190 | return mContentResolver.query(uri, PROJECTION, null, null, null); |
| 191 | } |
| 192 | |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 193 | private InputStream getInputStream(Cursor c) throws FileNotFoundException { |
| 194 | String path = c.getString(SNAPSHOT_VIEWSTATE_PATH); |
| 195 | if (!TextUtils.isEmpty(path)) { |
| 196 | return mContext.openFileInput(path); |
| 197 | } |
| 198 | byte[] data = c.getBlob(SNAPSHOT_VIEWSTATE); |
| 199 | ByteArrayInputStream bis = new ByteArrayInputStream(data); |
| 200 | return bis; |
| 201 | } |
| 202 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 203 | @Override |
| 204 | protected void onPostExecute(Cursor result) { |
| 205 | try { |
| 206 | if (result.moveToFirst()) { |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 207 | mTab.mCurrentState.mTitle = result.getString(SNAPSHOT_TITLE); |
| 208 | mTab.mCurrentState.mUrl = result.getString(SNAPSHOT_URL); |
| 209 | byte[] favicon = result.getBlob(SNAPSHOT_FAVICON); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 210 | if (favicon != null) { |
| 211 | mTab.mCurrentState.mFavicon = BitmapFactory |
| 212 | .decodeByteArray(favicon, 0, favicon.length); |
| 213 | } |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 214 | WebViewClassic web = mTab.getWebViewClassic(); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 215 | if (web != null) { |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 216 | InputStream ins = getInputStream(result); |
| 217 | GZIPInputStream stream = new GZIPInputStream(ins); |
John Reck | 2826377 | 2011-10-11 17:13:42 -0700 | [diff] [blame] | 218 | web.loadViewState(stream); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 219 | } |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame^] | 220 | mTab.mBackgroundColor = result.getInt(SNAPSHOT_BACKGROUND); |
| 221 | mTab.mDateCreated = result.getLong(SNAPSHOT_DATE_CREATED); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 222 | mTab.mWebViewController.onPageFinished(mTab); |
| 223 | } |
John Reck | 2826377 | 2011-10-11 17:13:42 -0700 | [diff] [blame] | 224 | } catch (Exception e) { |
| 225 | Log.w(LOGTAG, "Failed to load view state, closing tab", e); |
| 226 | mTab.mWebViewController.closeTab(mTab); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 227 | } finally { |
| 228 | if (result != null) { |
| 229 | result.close(); |
| 230 | } |
| 231 | mTab.mLoadTask = null; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 236 | |
| 237 | @Override |
| 238 | protected void persistThumbnail() { |
John Reck | e91c748 | 2011-08-23 14:04:29 -0700 | [diff] [blame] | 239 | if (mIsLive) { |
| 240 | super.persistThumbnail(); |
| 241 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 242 | } |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 243 | } |