Dmitri Plotnikov | 032bb36 | 2009-05-06 17:05:39 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 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.contacts; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.TabActivity; |
| 21 | import android.content.Intent; |
| 22 | import android.content.SharedPreferences; |
| 23 | import android.os.Bundle; |
| 24 | import android.provider.Contacts; |
| 25 | import android.provider.Contacts.Intents.UI; |
| 26 | import android.view.KeyEvent; |
| 27 | import android.view.Window; |
| 28 | import android.widget.TabHost; |
| 29 | |
| 30 | /** |
| 31 | * The contacts activity that has one tab with social activity stream and |
| 32 | * another with contact list. This is the container and the tabs are embedded |
| 33 | * using intents. |
| 34 | */ |
| 35 | public class ContactsActivity extends TabActivity implements TabHost.OnTabChangeListener { |
| 36 | private static final String TAG = "Contacts"; |
| 37 | private static final String FAVORITES_ENTRY_COMPONENT = |
| 38 | "com.android.contacts.DialtactsFavoritesEntryActivity"; |
| 39 | |
| 40 | private static final int TAB_INDEX_CONTACTS = 0; |
| 41 | private static final int TAB_INDEX_FAVORITES = 1; |
| 42 | |
| 43 | static final String EXTRA_IGNORE_STATE = "ignore-state"; |
| 44 | |
| 45 | /** Name of the dialtacts shared preferences */ |
| 46 | static final String PREFS_DIALTACTS = "dialtacts"; |
| 47 | /** If true, when handling the contacts intent the favorites tab will be shown instead */ |
| 48 | static final String PREF_FAVORITES_AS_CONTACTS = "favorites_as_contacts"; |
| 49 | static final boolean PREF_FAVORITES_AS_CONTACTS_DEFAULT = false; |
| 50 | |
| 51 | private TabHost mTabHost; |
| 52 | private String mFilterText; |
| 53 | |
| 54 | @Override |
| 55 | protected void onCreate(Bundle icicle) { |
| 56 | super.onCreate(icicle); |
| 57 | |
| 58 | final Intent intent = getIntent(); |
| 59 | |
| 60 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 61 | setContentView(R.layout.dialer_activity); |
| 62 | |
| 63 | mTabHost = getTabHost(); |
| 64 | mTabHost.setOnTabChangedListener(this); |
| 65 | |
| 66 | // Setup the tabs |
| 67 | setupContactsTab(); |
| 68 | setupFavoritesTab(); |
| 69 | |
| 70 | setCurrentTab(intent); |
| 71 | |
| 72 | if (intent.getAction().equals(Contacts.Intents.UI.FILTER_CONTACTS_ACTION) |
| 73 | && icicle == null) { |
| 74 | setupFilterText(intent); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void onPause() { |
| 80 | super.onPause(); |
| 81 | |
| 82 | int currentTabIndex = mTabHost.getCurrentTab(); |
| 83 | if (currentTabIndex == TAB_INDEX_CONTACTS || currentTabIndex == TAB_INDEX_FAVORITES) { |
| 84 | SharedPreferences.Editor editor = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE) |
| 85 | .edit(); |
| 86 | editor.putBoolean(PREF_FAVORITES_AS_CONTACTS, currentTabIndex == TAB_INDEX_FAVORITES); |
| 87 | editor.commit(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private void setupContactsTab() { |
| 92 | Intent intent = new Intent(UI.LIST_DEFAULT); |
| 93 | intent.setClass(this, ContactsListActivity.class); |
| 94 | |
| 95 | mTabHost.addTab(mTabHost.newTabSpec("contacts") |
| 96 | .setIndicator(getText(R.string.contactsIconLabel), |
| 97 | getResources().getDrawable(R.drawable.ic_tab_contacts)) |
| 98 | .setContent(intent)); |
| 99 | } |
| 100 | |
| 101 | private void setupFavoritesTab() { |
| 102 | Intent intent = new Intent(UI.LIST_STREQUENT_ACTION); |
| 103 | intent.setClass(this, ContactsListActivity.class); |
| 104 | |
| 105 | mTabHost.addTab(mTabHost.newTabSpec("favorites") |
| 106 | .setIndicator(getString(R.string.contactsFavoritesLabel), |
| 107 | getResources().getDrawable(R.drawable.ic_tab_starred)) |
| 108 | .setContent(intent)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Sets the current tab based on the intent's request type |
| 113 | * |
| 114 | * @param recentCallsRequest true is the recent calls tab is desired, false otherwise |
| 115 | */ |
| 116 | private void setCurrentTab(Intent intent) { |
| 117 | |
| 118 | // Dismiss menu provided by any children activities |
| 119 | Activity activity = getLocalActivityManager(). |
| 120 | getActivity(mTabHost.getCurrentTabTag()); |
| 121 | if (activity != null) { |
| 122 | activity.closeOptionsMenu(); |
| 123 | } |
| 124 | |
| 125 | // Tell the children activities that they should ignore any possible saved |
| 126 | // state and instead reload their state from the parent's intent |
| 127 | intent.putExtra(EXTRA_IGNORE_STATE, true); |
| 128 | |
| 129 | // Choose the tab based on the inbound intent |
| 130 | String componentName = intent.getComponent().getClassName(); |
| 131 | if (FAVORITES_ENTRY_COMPONENT.equals(componentName)) { |
| 132 | mTabHost.setCurrentTab(TAB_INDEX_FAVORITES); |
| 133 | } else if (Contacts.Intents.UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())) { |
| 134 | mTabHost.setCurrentTab(TAB_INDEX_CONTACTS); |
| 135 | } else { |
| 136 | SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE); |
| 137 | boolean favoritesAsContacts = prefs.getBoolean(PREF_FAVORITES_AS_CONTACTS, |
| 138 | PREF_FAVORITES_AS_CONTACTS_DEFAULT); |
| 139 | if (favoritesAsContacts) { |
| 140 | mTabHost.setCurrentTab(TAB_INDEX_FAVORITES); |
| 141 | } else { |
| 142 | mTabHost.setCurrentTab(TAB_INDEX_CONTACTS); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Tell the children activities that they should honor their saved states |
| 147 | // instead of the state from the parent's intent |
| 148 | intent.putExtra(EXTRA_IGNORE_STATE, false); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onNewIntent(Intent newIntent) { |
| 153 | setIntent(newIntent); |
| 154 | setCurrentTab(newIntent); |
| 155 | final String action = newIntent.getAction(); |
| 156 | if (action.equals(Contacts.Intents.UI.FILTER_CONTACTS_ACTION)) { |
| 157 | setupFilterText(newIntent); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Retrieves the filter text stored in {@link #setupFilterText(Intent)}. |
| 163 | * This text originally came from a FILTER_CONTACTS_ACTION intent received |
| 164 | * by this activity. The stored text will then be cleared after after this |
| 165 | * method returns. |
| 166 | * |
| 167 | * @return The stored filter text |
| 168 | */ |
| 169 | public String getAndClearFilterText() { |
| 170 | String filterText = mFilterText; |
| 171 | mFilterText = null; |
| 172 | return filterText; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent. |
| 177 | * This is so child activities can check if they are supposed to display a filter. |
| 178 | * |
| 179 | * @param intent The intent received in {@link #onNewIntent(Intent)} |
| 180 | */ |
| 181 | private void setupFilterText(Intent intent) { |
| 182 | // If the intent was relaunched from history, don't apply the filter text. |
| 183 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { |
| 184 | return; |
| 185 | } |
| 186 | String filter = intent.getStringExtra(Contacts.Intents.UI.FILTER_TEXT_EXTRA_KEY); |
| 187 | if (filter != null && filter.length() > 0) { |
| 188 | mFilterText = filter; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 194 | // Handle BACK |
| 195 | if (keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) { |
| 196 | // Instead of stopping, simply push this to the back of the stack. |
| 197 | // This is only done when running at the top of the stack; |
| 198 | // otherwise, we have been launched by someone else so need to |
| 199 | // allow the user to go back to the caller. |
| 200 | moveTaskToBack(false); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | return super.onKeyDown(keyCode, event); |
| 205 | } |
| 206 | |
| 207 | /** {@inheritDoc} */ |
| 208 | public void onTabChanged(String tabId) { |
| 209 | // Because we're using Activities as our tab children, we trigger |
| 210 | // onWindowFocusChanged() to let them know when they're active. This may |
| 211 | // seem to duplicate the purpose of onResume(), but it's needed because |
| 212 | // onResume() can't reliably check if a keyguard is active. |
| 213 | Activity activity = getLocalActivityManager().getActivity(tabId); |
| 214 | if (activity != null) { |
| 215 | activity.onWindowFocusChanged(true); |
| 216 | } |
| 217 | } |
| 218 | } |