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 | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 25 | import android.util.Log; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 26 | import android.webkit.WebView; |
| 27 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 28 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 29 | |
| 30 | import java.io.ByteArrayInputStream; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 31 | import java.util.Map; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 32 | import java.util.zip.GZIPInputStream; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 33 | |
| 34 | |
| 35 | public class SnapshotTab extends Tab { |
| 36 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 37 | private static final String LOGTAG = "SnapshotTab"; |
| 38 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 39 | private long mSnapshotId; |
| 40 | private LoadData mLoadTask; |
| 41 | private WebViewFactory mWebViewFactory; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 42 | private int mBackgroundColor; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 43 | private long mDateCreated; |
| 44 | private boolean mIsLive; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 45 | |
| 46 | public SnapshotTab(WebViewController wvcontroller, long snapshotId) { |
| 47 | super(wvcontroller, null); |
| 48 | mSnapshotId = snapshotId; |
| 49 | mWebViewFactory = mWebViewController.getWebViewFactory(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 50 | WebView web = mWebViewFactory.createWebView(false); |
| 51 | setWebView(web); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 52 | 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 Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void loadData() { |
| 75 | if (mLoadTask == null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 76 | mLoadTask = new LoadData(this, mContext.getContentResolver()); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 77 | 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 88 | return !mIsLive; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 89 | } |
| 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 105 | 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 Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 139 | 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 148 | Snapshots.DATE_CREATED, // 6 |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 149 | }; |
| 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 Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 180 | 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 Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 187 | } |
| 188 | mTab.mBackgroundColor = result.getInt(5); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 189 | mTab.mDateCreated = result.getLong(6); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 190 | mTab.mWebViewController.onPageFinished(mTab); |
| 191 | } |
| 192 | } finally { |
| 193 | if (result != null) { |
| 194 | result.close(); |
| 195 | } |
| 196 | mTab.mLoadTask = null; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | } |