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