CombinedBookmarkHistoryActivity.java revision 387a46efb8397ef8bf732d399b6eb7b3b9ad8c13
1/*
2 * Copyright (C) 2009 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
17package com.android.browser;
18
19import android.app.Activity;
20import android.app.TabActivity;
21import android.content.Intent;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.os.AsyncTask;
25import android.os.Bundle;
26import android.provider.Browser;
27import android.webkit.WebIconDatabase;
28import android.webkit.WebIconDatabase.IconListener;
29import android.widget.TabHost;
30
31import java.util.HashMap;
32import java.util.Vector;
33
34public class CombinedBookmarkHistoryActivity extends TabActivity
35        implements TabHost.OnTabChangeListener {
36    /**
37     * Used to inform BrowserActivity to remove the parent/child relationships
38     * from all the tabs.
39     */
40    private String mExtraData;
41    /**
42     * Intent to be passed to calling Activity when finished.  Keep a pointer to
43     * it locally so mExtraData can be added.
44     */
45    private Intent mResultData;
46    /**
47     * Result code to pass back to calling Activity when finished.
48     */
49    private int mResultCode;
50
51    /* package */ static String BOOKMARKS_TAB = "bookmark";
52    /* package */ static String VISITED_TAB = "visited";
53    /* package */ static String HISTORY_TAB = "history";
54    /* package */ static String STARTING_TAB = "tab";
55
56    static class IconListenerSet implements IconListener {
57        // Used to store favicons as we get them from the database
58        // FIXME: We use a different method to get the Favicons in
59        // BrowserBookmarksAdapter. They should probably be unified.
60        private HashMap<String, Bitmap> mUrlsToIcons;
61        private Vector<IconListener> mListeners;
62
63        public IconListenerSet() {
64            mUrlsToIcons = new HashMap<String, Bitmap>();
65            mListeners = new Vector<IconListener>();
66        }
67        public void onReceivedIcon(String url, Bitmap icon) {
68            mUrlsToIcons.put(url, icon);
69            for (IconListener listener : mListeners) {
70                listener.onReceivedIcon(url, icon);
71            }
72        }
73        public void addListener(IconListener listener) {
74            mListeners.add(listener);
75        }
76        public void removeListener(IconListener listener) {
77            mListeners.remove(listener);
78        }
79        public Bitmap getFavicon(String url) {
80            return (Bitmap) mUrlsToIcons.get(url);
81        }
82    }
83    private static IconListenerSet sIconListenerSet;
84    static IconListenerSet getIconListenerSet() {
85        if (null == sIconListenerSet) {
86            sIconListenerSet = new IconListenerSet();
87        }
88        return sIconListenerSet;
89    }
90
91    @Override
92    protected void onCreate(Bundle savedInstanceState) {
93        super.onCreate(savedInstanceState);
94        setContentView(R.layout.tabs);
95
96        setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
97
98        getTabHost().setOnTabChangedListener(this);
99
100        Bundle extras = getIntent().getExtras();
101
102        Intent bookmarksIntent = new Intent(this, BrowserBookmarksPage.class);
103        bookmarksIntent.putExtras(extras);
104        createTab(bookmarksIntent, R.string.tab_bookmarks,
105                R.drawable.browser_bookmark_tab, BOOKMARKS_TAB);
106
107        Intent visitedIntent = new Intent(this, BrowserBookmarksPage.class);
108        // Need to copy extras so the bookmarks activity and this one will be
109        // different
110        Bundle visitedExtras = new Bundle(extras);
111        visitedExtras.putBoolean("mostVisited", true);
112        visitedIntent.putExtras(visitedExtras);
113        createTab(visitedIntent, R.string.tab_most_visited,
114                R.drawable.browser_visited_tab, VISITED_TAB);
115
116        Intent historyIntent = new Intent(this, BrowserHistoryPage.class);
117        historyIntent.putExtras(extras);
118        createTab(historyIntent, R.string.tab_history,
119                R.drawable.browser_history_tab, HISTORY_TAB);
120
121        String defaultTab = extras.getString(STARTING_TAB);
122        if (defaultTab != null) {
123            getTabHost().setCurrentTab(2);
124        }
125
126        // XXX: Must do this before launching the AsyncTask to avoid a
127        // potential crash if the icon database has not been created.
128        WebIconDatabase.getInstance();
129        // Do this every time we launch the activity in case a new favicon was
130        // added to the webkit db.
131        (new AsyncTask<Void, Void, Void>() {
132            public Void doInBackground(Void... v) {
133                Browser.requestAllIcons(getContentResolver(),
134                    Browser.BookmarkColumns.FAVICON + " is NULL",
135                    getIconListenerSet());
136                return null;
137            }
138        }).execute();
139    }
140
141    private void createTab(Intent intent, int labelResId, int iconResId,
142            String tab) {
143        Resources resources = getResources();
144        TabHost tabHost = getTabHost();
145        tabHost.addTab(tabHost.newTabSpec(tab).setIndicator(
146                resources.getText(labelResId), resources.getDrawable(iconResId))
147                .setContent(intent));
148    }
149    // Copied from DialTacts Activity
150    /** {@inheritDoc} */
151    public void onTabChanged(String tabId) {
152        Activity activity = getLocalActivityManager().getActivity(tabId);
153        if (activity != null) {
154            activity.onWindowFocusChanged(true);
155        }
156    }
157
158    /**
159     * Store extra data in the Intent to return to the calling Activity to tell
160     * it to clear the parent/child relationships from all tabs.
161     */
162    /* package */ void removeParentChildRelationShips() {
163        mExtraData = BrowserSettings.PREF_CLEAR_HISTORY;
164    }
165
166    /**
167     * Custom setResult() method so that the Intent can have extra data attached
168     * if necessary.
169     * @param resultCode Uses same codes as Activity.setResult
170     * @param data Intent returned to onActivityResult.
171     */
172    /* package */ void setResultFromChild(int resultCode, Intent data) {
173        mResultCode = resultCode;
174        mResultData = data;
175    }
176
177    @Override
178    public void finish() {
179        if (mExtraData != null) {
180            mResultCode = RESULT_OK;
181            if (mResultData == null) mResultData = new Intent();
182            mResultData.putExtra(Intent.EXTRA_TEXT, mExtraData);
183        }
184        setResult(mResultCode, mResultData);
185        super.finish();
186    }
187}
188