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 17 package com.android.browser; 18 19 import android.app.Activity; 20 import android.app.TabActivity; 21 import android.content.Intent; 22 import android.content.res.Resources; 23 import android.graphics.Bitmap; 24 import android.os.AsyncTask; 25 import android.os.Bundle; 26 import android.provider.Browser; 27 import android.webkit.WebIconDatabase; 28 import android.webkit.WebIconDatabase.IconListener; 29 import android.widget.TabHost; 30 31 import java.util.HashMap; 32 import java.util.Vector; 33 34 public 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 if (extras != null) { 104 bookmarksIntent.putExtras(extras); 105 } 106 createTab(bookmarksIntent, R.string.tab_bookmarks, 107 R.drawable.browser_bookmark_tab, BOOKMARKS_TAB); 108 109 Intent visitedIntent = new Intent(this, BrowserBookmarksPage.class); 110 // Need to copy extras so the bookmarks activity and this one will be 111 // different 112 Bundle visitedExtras = extras == null ? new Bundle() : new Bundle(extras); 113 visitedExtras.putBoolean("mostVisited", true); 114 visitedIntent.putExtras(visitedExtras); 115 createTab(visitedIntent, R.string.tab_most_visited, 116 R.drawable.browser_visited_tab, VISITED_TAB); 117 118 Intent historyIntent = new Intent(this, BrowserHistoryPage.class); 119 String defaultTab = null; 120 if (extras != null) { 121 historyIntent.putExtras(extras); 122 defaultTab = extras.getString(STARTING_TAB); 123 } 124 createTab(historyIntent, R.string.tab_history, 125 R.drawable.browser_history_tab, HISTORY_TAB); 126 127 if (defaultTab != null) { 128 getTabHost().setCurrentTab(2); 129 } 130 131 // XXX: Must do this before launching the AsyncTask to avoid a 132 // potential crash if the icon database has not been created. 133 WebIconDatabase.getInstance(); 134 // Do this every time we launch the activity in case a new favicon was 135 // added to the webkit db. 136 (new AsyncTask<Void, Void, Void>() { 137 public Void doInBackground(Void... v) { 138 Browser.requestAllIcons(getContentResolver(), 139 Browser.BookmarkColumns.FAVICON + " is NULL", 140 getIconListenerSet()); 141 return null; 142 } 143 }).execute(); 144 } 145 146 private void createTab(Intent intent, int labelResId, int iconResId, 147 String tab) { 148 Resources resources = getResources(); 149 TabHost tabHost = getTabHost(); 150 tabHost.addTab(tabHost.newTabSpec(tab).setIndicator( 151 resources.getText(labelResId), resources.getDrawable(iconResId)) 152 .setContent(intent)); 153 } 154 // Copied from DialTacts Activity 155 /** {@inheritDoc} */ 156 public void onTabChanged(String tabId) { 157 Activity activity = getLocalActivityManager().getActivity(tabId); 158 if (activity != null) { 159 activity.onWindowFocusChanged(true); 160 } 161 } 162 163 /** 164 * Store extra data in the Intent to return to the calling Activity to tell 165 * it to clear the parent/child relationships from all tabs. 166 */ 167 /* package */ void removeParentChildRelationShips() { 168 mExtraData = BrowserSettings.PREF_CLEAR_HISTORY; 169 } 170 171 /** 172 * Custom setResult() method so that the Intent can have extra data attached 173 * if necessary. 174 * @param resultCode Uses same codes as Activity.setResult 175 * @param data Intent returned to onActivityResult. 176 */ 177 /* package */ void setResultFromChild(int resultCode, Intent data) { 178 mResultCode = resultCode; 179 mResultData = data; 180 } 181 182 @Override 183 public void finish() { 184 if (mExtraData != null) { 185 mResultCode = RESULT_OK; 186 if (mResultData == null) mResultData = new Intent(); 187 mResultData.putExtra(Intent.EXTRA_TEXT, mExtraData); 188 } 189 setResult(mResultCode, mResultData); 190 super.finish(); 191 } 192 } 193