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(); |
Dmitri Plotnikov | 3a0e259 | 2009-05-11 17:49:53 -0700 | [diff] [blame] | 68 | setupSocialStreamTab(); |
Dmitri Plotnikov | 032bb36 | 2009-05-06 17:05:39 -0700 | [diff] [blame] | 69 | setupFavoritesTab(); |
| 70 | |
| 71 | setCurrentTab(intent); |
| 72 | |
| 73 | if (intent.getAction().equals(Contacts.Intents.UI.FILTER_CONTACTS_ACTION) |
| 74 | && icicle == null) { |
| 75 | setupFilterText(intent); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected void onPause() { |
| 81 | super.onPause(); |
| 82 | |
| 83 | int currentTabIndex = mTabHost.getCurrentTab(); |
| 84 | if (currentTabIndex == TAB_INDEX_CONTACTS || currentTabIndex == TAB_INDEX_FAVORITES) { |
| 85 | SharedPreferences.Editor editor = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE) |
| 86 | .edit(); |
| 87 | editor.putBoolean(PREF_FAVORITES_AS_CONTACTS, currentTabIndex == TAB_INDEX_FAVORITES); |
| 88 | editor.commit(); |
| 89 | } |
| 90 | } |
| 91 | |
Dmitri Plotnikov | 3a0e259 | 2009-05-11 17:49:53 -0700 | [diff] [blame] | 92 | private void setupSocialStreamTab() { |
Dmitri Plotnikov | 06191cd | 2009-05-07 14:11:52 -0700 | [diff] [blame] | 93 | // Just a placeholder for now |
Dmitri Plotnikov | 3a0e259 | 2009-05-11 17:49:53 -0700 | [diff] [blame] | 94 | Intent intent = new Intent("com.android.contacts.action.SOCIAL_STREAM"); |
| 95 | intent.setClass(this, SocialStreamActivity.class); |
Dmitri Plotnikov | 06191cd | 2009-05-07 14:11:52 -0700 | [diff] [blame] | 96 | |
Dmitri Plotnikov | 3a0e259 | 2009-05-11 17:49:53 -0700 | [diff] [blame] | 97 | mTabHost.addTab(mTabHost.newTabSpec("social") |
| 98 | .setIndicator(getText(R.string.socialStreamIconLabel), |
Dmitri Plotnikov | 06191cd | 2009-05-07 14:11:52 -0700 | [diff] [blame] | 99 | getResources().getDrawable(R.drawable.ic_tab_contacts)) |
| 100 | .setContent(intent)); |
| 101 | } |
| 102 | |
Dmitri Plotnikov | 032bb36 | 2009-05-06 17:05:39 -0700 | [diff] [blame] | 103 | private void setupContactsTab() { |
| 104 | Intent intent = new Intent(UI.LIST_DEFAULT); |
| 105 | intent.setClass(this, ContactsListActivity.class); |
| 106 | |
| 107 | mTabHost.addTab(mTabHost.newTabSpec("contacts") |
| 108 | .setIndicator(getText(R.string.contactsIconLabel), |
| 109 | getResources().getDrawable(R.drawable.ic_tab_contacts)) |
| 110 | .setContent(intent)); |
| 111 | } |
| 112 | |
| 113 | private void setupFavoritesTab() { |
| 114 | Intent intent = new Intent(UI.LIST_STREQUENT_ACTION); |
| 115 | intent.setClass(this, ContactsListActivity.class); |
| 116 | |
| 117 | mTabHost.addTab(mTabHost.newTabSpec("favorites") |
| 118 | .setIndicator(getString(R.string.contactsFavoritesLabel), |
| 119 | getResources().getDrawable(R.drawable.ic_tab_starred)) |
| 120 | .setContent(intent)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets the current tab based on the intent's request type |
| 125 | * |
| 126 | * @param recentCallsRequest true is the recent calls tab is desired, false otherwise |
| 127 | */ |
| 128 | private void setCurrentTab(Intent intent) { |
| 129 | |
| 130 | // Dismiss menu provided by any children activities |
| 131 | Activity activity = getLocalActivityManager(). |
| 132 | getActivity(mTabHost.getCurrentTabTag()); |
| 133 | if (activity != null) { |
| 134 | activity.closeOptionsMenu(); |
| 135 | } |
| 136 | |
| 137 | // Tell the children activities that they should ignore any possible saved |
| 138 | // state and instead reload their state from the parent's intent |
| 139 | intent.putExtra(EXTRA_IGNORE_STATE, true); |
| 140 | |
| 141 | // Choose the tab based on the inbound intent |
| 142 | String componentName = intent.getComponent().getClassName(); |
| 143 | if (FAVORITES_ENTRY_COMPONENT.equals(componentName)) { |
| 144 | mTabHost.setCurrentTab(TAB_INDEX_FAVORITES); |
| 145 | } else if (Contacts.Intents.UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())) { |
| 146 | mTabHost.setCurrentTab(TAB_INDEX_CONTACTS); |
| 147 | } else { |
| 148 | SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE); |
| 149 | boolean favoritesAsContacts = prefs.getBoolean(PREF_FAVORITES_AS_CONTACTS, |
| 150 | PREF_FAVORITES_AS_CONTACTS_DEFAULT); |
| 151 | if (favoritesAsContacts) { |
| 152 | mTabHost.setCurrentTab(TAB_INDEX_FAVORITES); |
| 153 | } else { |
| 154 | mTabHost.setCurrentTab(TAB_INDEX_CONTACTS); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Tell the children activities that they should honor their saved states |
| 159 | // instead of the state from the parent's intent |
| 160 | intent.putExtra(EXTRA_IGNORE_STATE, false); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void onNewIntent(Intent newIntent) { |
| 165 | setIntent(newIntent); |
| 166 | setCurrentTab(newIntent); |
| 167 | final String action = newIntent.getAction(); |
| 168 | if (action.equals(Contacts.Intents.UI.FILTER_CONTACTS_ACTION)) { |
| 169 | setupFilterText(newIntent); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Retrieves the filter text stored in {@link #setupFilterText(Intent)}. |
| 175 | * This text originally came from a FILTER_CONTACTS_ACTION intent received |
| 176 | * by this activity. The stored text will then be cleared after after this |
| 177 | * method returns. |
| 178 | * |
| 179 | * @return The stored filter text |
| 180 | */ |
| 181 | public String getAndClearFilterText() { |
| 182 | String filterText = mFilterText; |
| 183 | mFilterText = null; |
| 184 | return filterText; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent. |
| 189 | * This is so child activities can check if they are supposed to display a filter. |
| 190 | * |
| 191 | * @param intent The intent received in {@link #onNewIntent(Intent)} |
| 192 | */ |
| 193 | private void setupFilterText(Intent intent) { |
| 194 | // If the intent was relaunched from history, don't apply the filter text. |
| 195 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { |
| 196 | return; |
| 197 | } |
| 198 | String filter = intent.getStringExtra(Contacts.Intents.UI.FILTER_TEXT_EXTRA_KEY); |
| 199 | if (filter != null && filter.length() > 0) { |
| 200 | mFilterText = filter; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 206 | // Handle BACK |
| 207 | if (keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) { |
| 208 | // Instead of stopping, simply push this to the back of the stack. |
| 209 | // This is only done when running at the top of the stack; |
| 210 | // otherwise, we have been launched by someone else so need to |
| 211 | // allow the user to go back to the caller. |
| 212 | moveTaskToBack(false); |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | return super.onKeyDown(keyCode, event); |
| 217 | } |
| 218 | |
| 219 | /** {@inheritDoc} */ |
| 220 | public void onTabChanged(String tabId) { |
| 221 | // Because we're using Activities as our tab children, we trigger |
| 222 | // onWindowFocusChanged() to let them know when they're active. This may |
| 223 | // seem to duplicate the purpose of onResume(), but it's needed because |
| 224 | // onResume() can't reliably check if a keyguard is active. |
| 225 | Activity activity = getLocalActivityManager().getActivity(tabId); |
| 226 | if (activity != null) { |
| 227 | activity.onWindowFocusChanged(true); |
| 228 | } |
| 229 | } |
| 230 | } |