PageRenderTime 54ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/WebVox/src/com/marvin/webvox/CombinedBookmarkHistoryActivity.java

http://eyes-free.googlecode.com/
Java | 138 lines | 95 code | 19 blank | 24 comment | 7 complexity | fbe6817e9a397d4e38875222b29e0863 MD5 | raw file
  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.marvin.webvox;
 18
 19import com.marvin.webvox.R;
 20
 21import android.app.Activity;
 22import android.app.TabActivity;
 23import android.content.ContentResolver;
 24import android.content.Intent;
 25import android.content.res.Resources;
 26import android.graphics.Bitmap;
 27import android.os.Bundle;
 28import android.provider.Browser;
 29import android.view.Window;
 30import android.webkit.WebIconDatabase.IconListener;
 31import android.widget.TabHost;
 32import android.widget.TabHost.TabSpec;
 33
 34import java.util.HashMap;
 35import java.util.Vector;
 36
 37public class CombinedBookmarkHistoryActivity extends TabActivity
 38        implements TabHost.OnTabChangeListener {
 39    /* package */ static String BOOKMARKS_TAB = "bookmark";
 40    /* package */ static String VISITED_TAB = "visited";
 41    /* package */ static String HISTORY_TAB = "history";
 42    /* package */ static String STARTING_TAB = "tab";
 43
 44    static class IconListenerSet implements IconListener {
 45        // Used to store favicons as we get them from the database
 46        // FIXME: We use a different method to get the Favicons in
 47        // BrowserBookmarksAdapter. They should probably be unified.
 48        private HashMap<String, Bitmap> mUrlsToIcons;
 49        private Vector<IconListener> mListeners;
 50
 51        public IconListenerSet() {
 52            mUrlsToIcons = new HashMap<String, Bitmap>();
 53            mListeners = new Vector<IconListener>();
 54        }
 55        public void onReceivedIcon(String url, Bitmap icon) {
 56            mUrlsToIcons.put(url, icon);
 57            for (IconListener listener : mListeners) {
 58                listener.onReceivedIcon(url, icon);
 59            }
 60        }
 61        public void addListener(IconListener listener) {
 62            mListeners.add(listener);
 63        }
 64        public void removeListener(IconListener listener) {
 65            mListeners.remove(listener);
 66        }
 67        public Bitmap getFavicon(String url) {
 68            return (Bitmap) mUrlsToIcons.get(url);
 69        }
 70    }
 71    private static IconListenerSet sIconListenerSet;
 72    static IconListenerSet getIconListenerSet() {
 73        if (null == sIconListenerSet) {
 74            sIconListenerSet = new IconListenerSet();
 75        }
 76        return sIconListenerSet;
 77    }
 78
 79    @Override
 80    protected void onCreate(Bundle savedInstanceState) {
 81        super.onCreate(savedInstanceState);
 82        setContentView(R.layout.tabs);
 83
 84        setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
 85
 86        getTabHost().setOnTabChangedListener(this);
 87
 88        Bundle extras = getIntent().getExtras();
 89
 90        getIconListenerSet();
 91        // Do this every time we create a new activity so that we get the
 92        // newest icons.
 93        Browser.requestAllIcons(getContentResolver(), null, sIconListenerSet);
 94
 95        Intent bookmarksIntent = new Intent(this, BrowserBookmarksPage.class);
 96        bookmarksIntent.putExtras(extras);
 97        createTab(bookmarksIntent, R.string.tab_bookmarks,
 98                R.drawable.browser_bookmark_tab, BOOKMARKS_TAB);
 99
100        Intent visitedIntent = new Intent(this, BrowserBookmarksPage.class);
101        // Need to copy extras so the bookmarks activity and this one will be
102        // different
103        Bundle visitedExtras = new Bundle(extras);
104        visitedExtras.putBoolean("mostVisited", true);
105        visitedIntent.putExtras(visitedExtras);
106        createTab(visitedIntent, R.string.tab_most_visited,
107                R.drawable.browser_visited_tab, VISITED_TAB);
108
109        Intent historyIntent = new Intent(this, BrowserHistoryPage.class);
110        historyIntent.putExtras(extras);
111        createTab(historyIntent, R.string.tab_history,
112                R.drawable.browser_history_tab, HISTORY_TAB);
113
114        String defaultTab = extras.getString(STARTING_TAB);
115        if (defaultTab != null) {
116            getTabHost().setCurrentTab(2);
117        }
118    }
119
120    private void createTab(Intent intent, int labelResId, int iconResId,
121            String tab) {
122        Resources resources = getResources();
123        TabHost tabHost = getTabHost();
124        tabHost.addTab(tabHost.newTabSpec(tab).setIndicator(
125                resources.getText(labelResId), resources.getDrawable(iconResId))
126                .setContent(intent));
127    }
128    // Copied from DialTacts Activity
129    /** {@inheritDoc} */
130    public void onTabChanged(String tabId) {
131        Activity activity = getLocalActivityManager().getActivity(tabId);
132        if (activity != null) {
133            activity.onWindowFocusChanged(true);
134        }
135    }
136
137    
138}