SnapshotTab.java revision 1461244018a225006a8d4c203f9dfe294ffe94fa
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.graphics.Color;
24import android.net.Uri;
25import android.os.AsyncTask;
26import android.os.Bundle;
27import android.webkit.WebView;
28
29import com.android.browser.provider.BrowserProvider2.Snapshots;
30
31import java.io.ByteArrayInputStream;
32
33
34public class SnapshotTab extends Tab {
35
36    private long mSnapshotId;
37    private LoadData mLoadTask;
38    private WebViewFactory mWebViewFactory;
39    // TODO: Support non-persistent webview's on phone
40    private boolean mPersistentWebview;
41    private int mBackgroundColor;
42
43    public SnapshotTab(WebViewController wvcontroller, long snapshotId) {
44        super(wvcontroller, null);
45        mSnapshotId = snapshotId;
46        mWebViewFactory = mWebViewController.getWebViewFactory();
47        mPersistentWebview = !BrowserActivity.isTablet(wvcontroller.getActivity());
48        if (mPersistentWebview) {
49            WebView web = mWebViewFactory.createWebView(false);
50            setWebView(web);
51        }
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();
72        if (!mPersistentWebview) {
73            super.destroy();
74        }
75    }
76
77    void loadData() {
78        if (mLoadTask == null) {
79            mLoadTask = new LoadData(this, mContext.getContentResolver());
80            mLoadTask.execute();
81        }
82    }
83
84    @Override
85    void addChildTab(Tab child) {
86        throw new IllegalStateException("Snapshot tabs cannot have child tabs!");
87    }
88
89    @Override
90    public boolean isSnapshot() {
91        return true;
92    }
93
94    public long getSnapshotId() {
95        return mSnapshotId;
96    }
97
98    @Override
99    public ContentValues createSnapshotValues() {
100        return null;
101    }
102
103    @Override
104    boolean saveState() {
105        return false;
106    }
107
108    static class LoadData extends AsyncTask<Void, Void, Cursor> {
109
110        static final String[] PROJECTION = new String[] {
111            Snapshots._ID, // 0
112            Snapshots.TITLE, // 1
113            Snapshots.URL, // 2
114            Snapshots.FAVICON, // 3
115            Snapshots.VIEWSTATE, // 4
116            Snapshots.BACKGROUND, // 5
117        };
118
119        private SnapshotTab mTab;
120        private ContentResolver mContentResolver;
121
122        public LoadData(SnapshotTab t, ContentResolver cr) {
123            mTab = t;
124            mContentResolver = cr;
125        }
126
127        @Override
128        protected Cursor doInBackground(Void... params) {
129            long id = mTab.mSnapshotId;
130            Uri uri = ContentUris.withAppendedId(Snapshots.CONTENT_URI, id);
131            return mContentResolver.query(uri, PROJECTION, null, null, null);
132        }
133
134        @Override
135        protected void onPostExecute(Cursor result) {
136            try {
137                if (result.moveToFirst()) {
138                    mTab.mCurrentState.mTitle = result.getString(1);
139                    mTab.mCurrentState.mUrl = result.getString(2);
140                    byte[] favicon = result.getBlob(3);
141                    if (favicon != null) {
142                        mTab.mCurrentState.mFavicon = BitmapFactory
143                                .decodeByteArray(favicon, 0, favicon.length);
144                    }
145                    WebView web = mTab.getWebView();
146                    if (web != null) {
147                        byte[] data = result.getBlob(4);
148                        ByteArrayInputStream stream = new ByteArrayInputStream(data);
149                        web.loadViewState(stream);
150                    }
151                    mTab.mBackgroundColor = result.getInt(5);
152                    mTab.mWebViewController.onPageFinished(mTab);
153                }
154            } finally {
155                if (result != null) {
156                    result.close();
157                }
158                mTab.mLoadTask = null;
159            }
160        }
161
162    }
163}
164