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 | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 31 | import java.util.zip.GZIPInputStream; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | public class SnapshotTab extends Tab { |
| 35 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 36 | private static final String LOGTAG = "SnapshotTab"; |
| 37 | |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 38 | 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 Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 81 | mLoadTask = new LoadData(this, mContext.getContentResolver()); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 82 | 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 Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 150 | 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 Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 157 | } |
| 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 | } |