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