SnapshotTab.java revision 1cf4b79a0020bc18c83ca8bde0e318ecd5252bc2
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 */
16package com.android.browser;
17
18import android.content.ContentResolver;
19import android.content.ContentUris;
20import android.content.ContentValues;
21import android.database.Cursor;
22import android.graphics.BitmapFactory;
23import android.net.Uri;
24import android.os.AsyncTask;
25import android.os.Bundle;
26import android.util.Log;
27import android.webkit.WebView;
28
29import com.android.browser.provider.SnapshotProvider.Snapshots;
30
31import java.io.ByteArrayInputStream;
32import java.util.Map;
33import java.util.zip.GZIPInputStream;
34
35
36public 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
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
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
70    void putInBackground() {
71        if (getWebView() == null) return;
72        super.putInBackground();
73    }
74
75    void loadData() {
76        if (mLoadTask == null) {
77            mLoadTask = new LoadData(this, mContext.getContentResolver());
78            mLoadTask.execute();
79        }
80    }
81
82    @Override
83    void addChildTab(Tab child) {
84        throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
85    }
86
87    @Override
88    public boolean isSnapshot() {
89        return !mIsLive;
90    }
91
92    public long getSnapshotId() {
93        return mSnapshotId;
94    }
95
96    @Override
97    public ContentValues createSnapshotValues() {
98        return null;
99    }
100
101    @Override
102    Bundle saveState() {
103        return null;
104    }
105
106    public long getDateCreated() {
107        return mDateCreated;
108    }
109
110    @Override
111    public void loadUrl(String url, Map<String, String> headers) {
112        if (!mIsLive) {
113            mIsLive = true;
114            getWebView().clearViewState();
115        }
116        super.loadUrl(url, headers);
117    }
118
119    @Override
120    public boolean canGoBack() {
121        return super.canGoBack() || mIsLive;
122    }
123
124    @Override
125    public boolean canGoForward() {
126        return mIsLive && super.canGoForward();
127    }
128
129    @Override
130    public void goBack() {
131        if (super.canGoBack()) {
132            super.goBack();
133        } else {
134            mIsLive = false;
135            getWebView().stopLoading();
136            loadData();
137        }
138    }
139
140    static class LoadData extends AsyncTask<Void, Void, Cursor> {
141
142        static final String[] PROJECTION = new String[] {
143            Snapshots._ID, // 0
144            Snapshots.TITLE, // 1
145            Snapshots.URL, // 2
146            Snapshots.FAVICON, // 3
147            Snapshots.VIEWSTATE, // 4
148            Snapshots.BACKGROUND, // 5
149            Snapshots.DATE_CREATED, // 6
150        };
151
152        private SnapshotTab mTab;
153        private ContentResolver mContentResolver;
154
155        public LoadData(SnapshotTab t, ContentResolver cr) {
156            mTab = t;
157            mContentResolver = cr;
158        }
159
160        @Override
161        protected Cursor doInBackground(Void... params) {
162            long id = mTab.mSnapshotId;
163            Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
164            return mContentResolver.query(uri, PROJECTION, null, null, null);
165        }
166
167        @Override
168        protected void onPostExecute(Cursor result) {
169            try {
170                if (result.moveToFirst()) {
171                    mTab.mCurrentState.mTitle = result.getString(1);
172                    mTab.mCurrentState.mUrl = result.getString(2);
173                    byte[] favicon = result.getBlob(3);
174                    if (favicon != null) {
175                        mTab.mCurrentState.mFavicon = BitmapFactory
176                                .decodeByteArray(favicon, 0, favicon.length);
177                    }
178                    WebView web = mTab.getWebView();
179                    if (web != null) {
180                        byte[] data = result.getBlob(4);
181                        ByteArrayInputStream bis = new ByteArrayInputStream(data);
182                        try {
183                            GZIPInputStream stream = new GZIPInputStream(bis);
184                            web.loadViewState(stream);
185                        } catch (Exception e) {
186                            Log.w(LOGTAG, "Failed to load view state", e);
187                        }
188                    }
189                    mTab.mBackgroundColor = result.getInt(5);
190                    mTab.mDateCreated = result.getLong(6);
191                    mTab.mWebViewController.onPageFinished(mTab);
192                }
193            } finally {
194                if (result != null) {
195                    result.close();
196                }
197                mTab.mLoadTask = null;
198            }
199        }
200
201    }
202
203    @Override
204    protected void persistThumbnail() {
205        // Nope
206    }
207
208    @Override
209    protected void deleteThumbnail() {
210        // Nope
211    }
212
213}
214