• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
23 import android.net.Uri;
24 import android.os.AsyncTask;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.webkit.WebView;
28 
29 import com.android.browser.provider.SnapshotProvider.Snapshots;
30 
31 import java.io.ByteArrayInputStream;
32 import java.util.Map;
33 import java.util.zip.GZIPInputStream;
34 
35 
36 public class SnapshotTab extends Tab {
37 
38     private static final String LOGTAG = "SnapshotTab";
39 
40     private long mSnapshotId;
41     private LoadData mLoadTask;
42     private WebViewFactory mWebViewFactory;
43     private int mBackgroundColor;
44     private long mDateCreated;
45     private boolean mIsLive;
46 
SnapshotTab(WebViewController wvcontroller, long snapshotId)47     public SnapshotTab(WebViewController wvcontroller, long snapshotId) {
48         super(wvcontroller, null, null);
49         mSnapshotId = snapshotId;
50         mWebViewFactory = mWebViewController.getWebViewFactory();
51         WebView web = mWebViewFactory.createWebView(false);
52         setWebView(web);
53         loadData();
54     }
55 
56     @Override
putInForeground()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
putInBackground()70     void putInBackground() {
71         if (getWebView() == null) return;
72         super.putInBackground();
73     }
74 
loadData()75     void loadData() {
76         if (mLoadTask == null) {
77             mLoadTask = new LoadData(this, mContext.getContentResolver());
78             mLoadTask.execute();
79         }
80     }
81 
82     @Override
addChildTab(Tab child)83     void addChildTab(Tab child) {
84         if (mIsLive) {
85             super.addChildTab(child);
86         } else {
87             throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
88         }
89     }
90 
91     @Override
isSnapshot()92     public boolean isSnapshot() {
93         return !mIsLive;
94     }
95 
getSnapshotId()96     public long getSnapshotId() {
97         return mSnapshotId;
98     }
99 
100     @Override
createSnapshotValues()101     public ContentValues createSnapshotValues() {
102         if (mIsLive) {
103             return super.createSnapshotValues();
104         }
105         return null;
106     }
107 
108     @Override
saveState()109     public Bundle saveState() {
110         if (mIsLive) {
111             return super.saveState();
112         }
113         return null;
114     }
115 
getDateCreated()116     public long getDateCreated() {
117         return mDateCreated;
118     }
119 
120     @Override
loadUrl(String url, Map<String, String> headers)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
canGoBack()130     public boolean canGoBack() {
131         return super.canGoBack() || mIsLive;
132     }
133 
134     @Override
canGoForward()135     public boolean canGoForward() {
136         return mIsLive && super.canGoForward();
137     }
138 
139     @Override
goBack()140     public void goBack() {
141         if (super.canGoBack()) {
142             super.goBack();
143         } else {
144             mIsLive = false;
145             getWebView().stopLoading();
146             loadData();
147         }
148     }
149 
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
159             Snapshots.DATE_CREATED, // 6
160         };
161 
162         private SnapshotTab mTab;
163         private ContentResolver mContentResolver;
164 
LoadData(SnapshotTab t, ContentResolver cr)165         public LoadData(SnapshotTab t, ContentResolver cr) {
166             mTab = t;
167             mContentResolver = cr;
168         }
169 
170         @Override
doInBackground(Void... params)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
onPostExecute(Cursor result)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);
191                         ByteArrayInputStream bis = new ByteArrayInputStream(data);
192                         GZIPInputStream stream = new GZIPInputStream(bis);
193                         web.loadViewState(stream);
194                     }
195                     mTab.mBackgroundColor = result.getInt(5);
196                     mTab.mDateCreated = result.getLong(6);
197                     mTab.mWebViewController.onPageFinished(mTab);
198                 }
199             } catch (Exception e) {
200                 Log.w(LOGTAG, "Failed to load view state, closing tab", e);
201                 mTab.mWebViewController.closeTab(mTab);
202             } finally {
203                 if (result != null) {
204                     result.close();
205                 }
206                 mTab.mLoadTask = null;
207             }
208         }
209 
210     }
211 
212     @Override
persistThumbnail()213     protected void persistThumbnail() {
214         if (mIsLive) {
215             super.persistThumbnail();
216         }
217     }
218 }
219