blob: 4fdd7754ff1bed90225f0169346d7dba2c1afbc7 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
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
17package com.android.contacts;
18
Jeff Hamiltondf4b1462010-06-28 10:40:26 -050019import com.android.contacts.activities.ContactsFrontDoor;
Dmitri Plotnikovcfa39002010-07-09 18:05:17 -070020import com.android.contacts.activities.ContactBrowserActivity;
Jeff Hamiltonb25c13e2009-09-21 09:22:16 -050021import com.android.internal.telephony.ITelephony;
22
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080023import android.app.Activity;
24import android.app.TabActivity;
25import android.content.Intent;
Jeff Hamilton90313772009-07-28 10:20:31 -050026import android.content.SharedPreferences;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080027import android.net.Uri;
28import android.os.Bundle;
29import android.os.RemoteException;
30import android.os.ServiceManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031import android.provider.CallLog.Calls;
Jeff Hamiltonb25c13e2009-09-21 09:22:16 -050032import android.provider.ContactsContract.Intents.UI;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080034import android.view.Window;
35import android.widget.TabHost;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080036
37/**
Nicolas Catania08832402009-09-24 09:52:50 -070038 * The dialer activity that has one tab with the virtual 12key
39 * dialer, a tab with recent calls in it, a tab with the contacts and
40 * a tab with the favorite. This is the container and the tabs are
41 * embedded using intents.
42 * The dialer tab's title is 'phone', a more common name (see strings.xml).
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080043 */
Jeff Hamilton90313772009-07-28 10:20:31 -050044public class DialtactsActivity extends TabActivity implements TabHost.OnTabChangeListener {
45 private static final String TAG = "Dailtacts";
46 private static final String FAVORITES_ENTRY_COMPONENT =
47 "com.android.contacts.DialtactsFavoritesEntryActivity";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048
Daniel Lehmann7675e122010-04-21 17:31:11 -070049 /** Opens the Contacts app in the state the user has last set it to */
50 private static final String CONTACTS_LAUNCH_ACTIVITY =
51 "com.android.contacts.ContactsLaunchActivity";
52
The Android Open Source Project1f620962009-03-09 11:52:14 -070053 private static final int TAB_INDEX_DIALER = 0;
54 private static final int TAB_INDEX_CALL_LOG = 1;
Jeff Hamilton90313772009-07-28 10:20:31 -050055 private static final int TAB_INDEX_CONTACTS = 2;
56 private static final int TAB_INDEX_FAVORITES = 3;
Nicolas Catania08832402009-09-24 09:52:50 -070057
The Android Open Source Project1f620962009-03-09 11:52:14 -070058 static final String EXTRA_IGNORE_STATE = "ignore-state";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080059
Jeff Hamilton90313772009-07-28 10:20:31 -050060 /** Name of the dialtacts shared preferences */
61 static final String PREFS_DIALTACTS = "dialtacts";
62 /** If true, when handling the contacts intent the favorites tab will be shown instead */
63 static final String PREF_FAVORITES_AS_CONTACTS = "favorites_as_contacts";
64 static final boolean PREF_FAVORITES_AS_CONTACTS_DEFAULT = false;
65
Daniel Lehmann7675e122010-04-21 17:31:11 -070066 /** Last manually selected tab index */
67 private static final String PREF_LAST_MANUALLY_SELECTED_TAB = "last_manually_selected_tab";
68 private static final int PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT = TAB_INDEX_DIALER;
69
The Android Open Source Project1f620962009-03-09 11:52:14 -070070 private TabHost mTabHost;
Nicolas Catania08832402009-09-24 09:52:50 -070071 private String mFilterText;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072 private Uri mDialUri;
73
Daniel Lehmann7675e122010-04-21 17:31:11 -070074 /**
75 * The index of the tab that has last been manually selected (the user clicked on a tab).
76 * This value does not keep track of programmatically set Tabs (e.g. Call Log after a Call)
77 */
78 private int mLastManuallySelectedTab;
79
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080 @Override
81 protected void onCreate(Bundle icicle) {
82 super.onCreate(icicle);
83
84 final Intent intent = getIntent();
85 fixIntent(intent);
Nicolas Catania08832402009-09-24 09:52:50 -070086
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087 requestWindowFeature(Window.FEATURE_NO_TITLE);
88 setContentView(R.layout.dialer_activity);
89
90 mTabHost = getTabHost();
91 mTabHost.setOnTabChangedListener(this);
92
93 // Setup the tabs
94 setupDialerTab();
95 setupCallLogTab();
Jeff Hamilton90313772009-07-28 10:20:31 -050096 setupContactsTab();
97 setupFavoritesTab();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098
Daniel Lehmann7675e122010-04-21 17:31:11 -070099 // Load the last manually loaded tab
100 final SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE);
101 mLastManuallySelectedTab = prefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB,
102 PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT);
103
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800104 setCurrentTab(intent);
Jeff Hamilton90313772009-07-28 10:20:31 -0500105
Jeff Hamilton7fa3cd62010-06-30 11:36:58 -0500106 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
Jeff Hamilton90313772009-07-28 10:20:31 -0500107 && icicle == null) {
108 setupFilterText(intent);
109 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800110 }
111
Jeff Hamilton90313772009-07-28 10:20:31 -0500112 @Override
113 protected void onPause() {
114 super.onPause();
Nicolas Catania08832402009-09-24 09:52:50 -0700115
Daniel Lehmann7675e122010-04-21 17:31:11 -0700116 final int currentTabIndex = mTabHost.getCurrentTab();
117 final SharedPreferences.Editor editor =
118 getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE).edit();
Jeff Hamilton90313772009-07-28 10:20:31 -0500119 if (currentTabIndex == TAB_INDEX_CONTACTS || currentTabIndex == TAB_INDEX_FAVORITES) {
Jeff Hamilton90313772009-07-28 10:20:31 -0500120 editor.putBoolean(PREF_FAVORITES_AS_CONTACTS, currentTabIndex == TAB_INDEX_FAVORITES);
Jeff Hamilton90313772009-07-28 10:20:31 -0500121 }
Daniel Lehmann7675e122010-04-21 17:31:11 -0700122 editor.putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedTab);
123
124 editor.commit();
Jeff Hamilton90313772009-07-28 10:20:31 -0500125 }
Nicolas Catania08832402009-09-24 09:52:50 -0700126
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800127 private void fixIntent(Intent intent) {
128 // This should be cleaned up: the call key used to send an Intent
129 // that just said to go to the recent calls list. It now sends this
130 // abstract action, but this class hasn't been rewritten to deal with it.
131 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
132 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
133 intent.putExtra("call_key", true);
134 setIntent(intent);
135 }
136 }
Nicolas Catania08832402009-09-24 09:52:50 -0700137
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800138 private void setupCallLogTab() {
The Android Open Source Project1f620962009-03-09 11:52:14 -0700139 // Force the class since overriding tab entries doesn't work
140 Intent intent = new Intent("com.android.phone.action.RECENT_CALLS");
141 intent.setClass(this, RecentCallsListActivity.class);
142
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800143 mTabHost.addTab(mTabHost.newTabSpec("call_log")
144 .setIndicator(getString(R.string.recentCallsIconLabel),
145 getResources().getDrawable(R.drawable.ic_tab_recent))
The Android Open Source Project1f620962009-03-09 11:52:14 -0700146 .setContent(intent));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800147 }
148
149 private void setupDialerTab() {
The Android Open Source Project1f620962009-03-09 11:52:14 -0700150 Intent intent = new Intent("com.android.phone.action.TOUCH_DIALER");
151 intent.setClass(this, TwelveKeyDialer.class);
152
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800153 mTabHost.addTab(mTabHost.newTabSpec("dialer")
154 .setIndicator(getString(R.string.dialerIconLabel),
155 getResources().getDrawable(R.drawable.ic_tab_dialer))
The Android Open Source Project1f620962009-03-09 11:52:14 -0700156 .setContent(intent));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800157 }
158
Jeff Hamilton90313772009-07-28 10:20:31 -0500159 private void setupContactsTab() {
160 Intent intent = new Intent(UI.LIST_DEFAULT);
Dmitri Plotnikovcfa39002010-07-09 18:05:17 -0700161 intent.setClass(this, ContactBrowserActivity.class);
Jeff Hamilton90313772009-07-28 10:20:31 -0500162
163 mTabHost.addTab(mTabHost.newTabSpec("contacts")
164 .setIndicator(getText(R.string.contactsIconLabel),
165 getResources().getDrawable(R.drawable.ic_tab_contacts))
166 .setContent(intent));
167 }
168
169 private void setupFavoritesTab() {
170 Intent intent = new Intent(UI.LIST_STREQUENT_ACTION);
Dmitri Plotnikovcfa39002010-07-09 18:05:17 -0700171 intent.setClass(this, ContactBrowserActivity.class);
Jeff Hamilton90313772009-07-28 10:20:31 -0500172
173 mTabHost.addTab(mTabHost.newTabSpec("favorites")
174 .setIndicator(getString(R.string.contactsFavoritesLabel),
175 getResources().getDrawable(R.drawable.ic_tab_starred))
176 .setContent(intent));
177 }
178
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800179 /**
180 * Returns true if the intent is due to hitting the green send key while in a call.
Nicolas Catania08832402009-09-24 09:52:50 -0700181 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800182 * @param intent the intent that launched this activity
183 * @param recentCallsRequest true if the intent is requesting to view recent calls
Nicolas Catania08832402009-09-24 09:52:50 -0700184 * @return true if the intent is due to hitting the green send key while in a call
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800185 */
186 private boolean isSendKeyWhileInCall(final Intent intent, final boolean recentCallsRequest) {
187 // If there is a call in progress go to the call screen
188 if (recentCallsRequest) {
189 final boolean callKey = intent.getBooleanExtra("call_key", false);
190
191 try {
192 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
193 if (callKey && phone != null && phone.showCallScreen()) {
194 return true;
195 }
196 } catch (RemoteException e) {
197 Log.e(TAG, "Failed to handle send while in call", e);
198 }
199 }
200
201 return false;
202 }
203
204 /**
205 * Sets the current tab based on the intent's request type
Nicolas Catania08832402009-09-24 09:52:50 -0700206 *
Daniel Lehmann7675e122010-04-21 17:31:11 -0700207 * @param intent Intent that contains information about which tab should be selected
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800208 */
209 private void setCurrentTab(Intent intent) {
The Android Open Source Project1f620962009-03-09 11:52:14 -0700210 // If we got here by hitting send and we're in call forward along to the in-call activity
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800211 final boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.getType());
212 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
213 finish();
214 return;
215 }
Nicolas Catania08832402009-09-24 09:52:50 -0700216
The Android Open Source Project1f620962009-03-09 11:52:14 -0700217 // Dismiss menu provided by any children activities
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800218 Activity activity = getLocalActivityManager().
219 getActivity(mTabHost.getCurrentTabTag());
220 if (activity != null) {
221 activity.closeOptionsMenu();
222 }
223
The Android Open Source Project1f620962009-03-09 11:52:14 -0700224 // Tell the children activities that they should ignore any possible saved
225 // state and instead reload their state from the parent's intent
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800226 intent.putExtra(EXTRA_IGNORE_STATE, true);
The Android Open Source Project1f620962009-03-09 11:52:14 -0700227
Daniel Lehmann7675e122010-04-21 17:31:11 -0700228 // Remember the old manually selected tab index so that it can be restored if it is
229 // overwritten by one of the programmatic tab selections
230 final int savedTabIndex = mLastManuallySelectedTab;
231
The Android Open Source Project1f620962009-03-09 11:52:14 -0700232 // Choose the tab based on the inbound intent
Jeff Hamiltondf4b1462010-06-28 10:40:26 -0500233 if (intent.getBooleanExtra(ContactsFrontDoor.EXTRA_FRONT_DOOR, false)) {
234 // Launched through the contacts front door, set the proper contacts tab
235 setContactsTab();
Jeff Hamilton90313772009-07-28 10:20:31 -0500236 } else {
Jeff Hamiltondf4b1462010-06-28 10:40:26 -0500237 // Not launched through the front door, look at the component to determine the tab
238 String componentName = intent.getComponent().getClassName();
239 if (getClass().getName().equals(componentName)) {
240 if (recentCallsRequest) {
241 mTabHost.setCurrentTab(TAB_INDEX_CALL_LOG);
242 } else {
243 mTabHost.setCurrentTab(TAB_INDEX_DIALER);
244 }
245 } else if (FAVORITES_ENTRY_COMPONENT.equals(componentName)) {
Jeff Hamilton90313772009-07-28 10:20:31 -0500246 mTabHost.setCurrentTab(TAB_INDEX_FAVORITES);
Jeff Hamiltondf4b1462010-06-28 10:40:26 -0500247 } else if (CONTACTS_LAUNCH_ACTIVITY.equals(componentName)) {
248 mTabHost.setCurrentTab(mLastManuallySelectedTab);
Jeff Hamilton90313772009-07-28 10:20:31 -0500249 } else {
Jeff Hamiltondf4b1462010-06-28 10:40:26 -0500250 setContactsTab();
Jeff Hamilton90313772009-07-28 10:20:31 -0500251 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 }
The Android Open Source Project1f620962009-03-09 11:52:14 -0700253
Daniel Lehmann7675e122010-04-21 17:31:11 -0700254 // Restore to the previous manual selection
255 mLastManuallySelectedTab = savedTabIndex;
256
The Android Open Source Project1f620962009-03-09 11:52:14 -0700257 // Tell the children activities that they should honor their saved states
258 // instead of the state from the parent's intent
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800259 intent.putExtra(EXTRA_IGNORE_STATE, false);
260 }
261
Jeff Hamiltondf4b1462010-06-28 10:40:26 -0500262 private void setContactsTab() {
263 SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE);
264 boolean favoritesAsContacts = prefs.getBoolean(PREF_FAVORITES_AS_CONTACTS,
265 PREF_FAVORITES_AS_CONTACTS_DEFAULT);
266 if (favoritesAsContacts) {
267 mTabHost.setCurrentTab(TAB_INDEX_FAVORITES);
268 } else {
269 mTabHost.setCurrentTab(TAB_INDEX_CONTACTS);
270 }
271 }
272
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800273 @Override
274 public void onNewIntent(Intent newIntent) {
275 setIntent(newIntent);
276 fixIntent(newIntent);
277 setCurrentTab(newIntent);
Jeff Hamilton90313772009-07-28 10:20:31 -0500278 final String action = newIntent.getAction();
Jeff Hamilton7fa3cd62010-06-30 11:36:58 -0500279 if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
Jeff Hamilton90313772009-07-28 10:20:31 -0500280 setupFilterText(newIntent);
281 } else if (isDialIntent(newIntent)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800282 setupDialUri(newIntent);
283 }
284 }
The Android Open Source Project1f620962009-03-09 11:52:14 -0700285
286 /** Returns true if the given intent contains a phone number to populate the dialer with */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800287 private boolean isDialIntent(Intent intent) {
288 final String action = intent.getAction();
289 if (Intent.ACTION_DIAL.equals(action)) {
290 return true;
291 }
292 if (Intent.ACTION_VIEW.equals(action)) {
293 final Uri data = intent.getData();
294 if (data != null && "tel".equals(data.getScheme())) {
295 return true;
296 }
297 }
298 return false;
299 }
Nicolas Catania08832402009-09-24 09:52:50 -0700300
Jeff Hamilton90313772009-07-28 10:20:31 -0500301 /**
302 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
303 * This text originally came from a FILTER_CONTACTS_ACTION intent received
304 * by this activity. The stored text will then be cleared after after this
305 * method returns.
Nicolas Catania08832402009-09-24 09:52:50 -0700306 *
Jeff Hamilton90313772009-07-28 10:20:31 -0500307 * @return The stored filter text
308 */
309 public String getAndClearFilterText() {
310 String filterText = mFilterText;
311 mFilterText = null;
312 return filterText;
313 }
314
315 /**
316 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
317 * This is so child activities can check if they are supposed to display a filter.
Nicolas Catania08832402009-09-24 09:52:50 -0700318 *
Jeff Hamilton90313772009-07-28 10:20:31 -0500319 * @param intent The intent received in {@link #onNewIntent(Intent)}
320 */
321 private void setupFilterText(Intent intent) {
322 // If the intent was relaunched from history, don't apply the filter text.
323 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
324 return;
325 }
Jeff Hamiltonb25c13e2009-09-21 09:22:16 -0500326 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
Jeff Hamilton90313772009-07-28 10:20:31 -0500327 if (filter != null && filter.length() > 0) {
328 mFilterText = filter;
329 }
330 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800331
332 /**
333 * Retrieves the uri stored in {@link #setupDialUri(Intent)}. This uri
334 * originally came from a dial intent received by this activity. The stored
335 * uri will then be cleared after after this method returns.
Nicolas Catania08832402009-09-24 09:52:50 -0700336 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800337 * @return The stored uri
338 */
339 public Uri getAndClearDialUri() {
340 Uri dialUri = mDialUri;
341 mDialUri = null;
342 return dialUri;
343 }
344
345 /**
346 * Stores the uri associated with a dial intent. This is so child activities can
347 * check if they are supposed to display new dial info.
Nicolas Catania08832402009-09-24 09:52:50 -0700348 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800349 * @param intent The intent received in {@link #onNewIntent(Intent)}
350 */
351 private void setupDialUri(Intent intent) {
352 // If the intent was relaunched from history, don't reapply the intent.
353 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
354 return;
355 }
356 mDialUri = intent.getData();
357 }
358
359 @Override
Dianne Hackborn242599a2009-09-14 21:33:24 -0700360 public void onBackPressed() {
361 if (isTaskRoot()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800362 // Instead of stopping, simply push this to the back of the stack.
363 // This is only done when running at the top of the stack;
364 // otherwise, we have been launched by someone else so need to
365 // allow the user to go back to the caller.
366 moveTaskToBack(false);
Dianne Hackborn242599a2009-09-14 21:33:24 -0700367 } else {
368 super.onBackPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800369 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800370 }
371
372 /** {@inheritDoc} */
373 public void onTabChanged(String tabId) {
374 // Because we're using Activities as our tab children, we trigger
375 // onWindowFocusChanged() to let them know when they're active. This may
376 // seem to duplicate the purpose of onResume(), but it's needed because
377 // onResume() can't reliably check if a keyguard is active.
378 Activity activity = getLocalActivityManager().getActivity(tabId);
379 if (activity != null) {
380 activity.onWindowFocusChanged(true);
381 }
Daniel Lehmann7675e122010-04-21 17:31:11 -0700382
383 // Remember this tab index. This function is also called, if the tab is set automatically
384 // in which case the setter (setCurrentTab) has to set this to its old value afterwards
385 mLastManuallySelectedTab = mTabHost.getCurrentTab();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800386 }
387}