blob: 9fb5f41793cb87db9543b782c6213908a54eef6f [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 Hamiltonb25c13e2009-09-21 09:22:16 -050019import com.android.internal.telephony.ITelephony;
20
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080021import android.app.Activity;
22import android.app.TabActivity;
23import android.content.Intent;
Jeff Hamilton90313772009-07-28 10:20:31 -050024import android.content.SharedPreferences;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080025import android.net.Uri;
26import android.os.Bundle;
27import android.os.RemoteException;
28import android.os.ServiceManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029import android.provider.CallLog.Calls;
Jeff Hamiltonb25c13e2009-09-21 09:22:16 -050030import android.provider.ContactsContract.Intents.UI;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032import android.view.Window;
33import android.widget.TabHost;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080034
35/**
36 * The dialer activity that has one tab with the virtual 12key dialer,
37 * and another tab with recent calls in it. This is the container and the tabs
38 * are embedded using intents.
39 */
Jeff Hamilton90313772009-07-28 10:20:31 -050040public class DialtactsActivity extends TabActivity implements TabHost.OnTabChangeListener {
41 private static final String TAG = "Dailtacts";
42 private static final String FAVORITES_ENTRY_COMPONENT =
43 "com.android.contacts.DialtactsFavoritesEntryActivity";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080044
The Android Open Source Project1f620962009-03-09 11:52:14 -070045 private static final int TAB_INDEX_DIALER = 0;
46 private static final int TAB_INDEX_CALL_LOG = 1;
Jeff Hamilton90313772009-07-28 10:20:31 -050047 private static final int TAB_INDEX_CONTACTS = 2;
48 private static final int TAB_INDEX_FAVORITES = 3;
49
The Android Open Source Project1f620962009-03-09 11:52:14 -070050 static final String EXTRA_IGNORE_STATE = "ignore-state";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080051
Jeff Hamilton90313772009-07-28 10:20:31 -050052 /** Name of the dialtacts shared preferences */
53 static final String PREFS_DIALTACTS = "dialtacts";
54 /** If true, when handling the contacts intent the favorites tab will be shown instead */
55 static final String PREF_FAVORITES_AS_CONTACTS = "favorites_as_contacts";
56 static final boolean PREF_FAVORITES_AS_CONTACTS_DEFAULT = false;
57
The Android Open Source Project1f620962009-03-09 11:52:14 -070058 private TabHost mTabHost;
Jeff Hamilton90313772009-07-28 10:20:31 -050059 private String mFilterText;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080060 private Uri mDialUri;
61
62 @Override
63 protected void onCreate(Bundle icicle) {
64 super.onCreate(icicle);
65
66 final Intent intent = getIntent();
67 fixIntent(intent);
Jeff Hamilton90313772009-07-28 10:20:31 -050068
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069 requestWindowFeature(Window.FEATURE_NO_TITLE);
70 setContentView(R.layout.dialer_activity);
71
72 mTabHost = getTabHost();
73 mTabHost.setOnTabChangedListener(this);
74
75 // Setup the tabs
76 setupDialerTab();
77 setupCallLogTab();
Jeff Hamilton90313772009-07-28 10:20:31 -050078 setupContactsTab();
79 setupFavoritesTab();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080
81 setCurrentTab(intent);
Jeff Hamilton90313772009-07-28 10:20:31 -050082
Jeff Hamiltonb25c13e2009-09-21 09:22:16 -050083 if (intent.getAction().equals(UI.FILTER_CONTACTS_ACTION)
Jeff Hamilton90313772009-07-28 10:20:31 -050084 && icicle == null) {
85 setupFilterText(intent);
86 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087 }
88
Jeff Hamilton90313772009-07-28 10:20:31 -050089 @Override
90 protected void onPause() {
91 super.onPause();
92
93 int currentTabIndex = mTabHost.getCurrentTab();
94 if (currentTabIndex == TAB_INDEX_CONTACTS || currentTabIndex == TAB_INDEX_FAVORITES) {
95 SharedPreferences.Editor editor = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE)
96 .edit();
97 editor.putBoolean(PREF_FAVORITES_AS_CONTACTS, currentTabIndex == TAB_INDEX_FAVORITES);
98 editor.commit();
99 }
100 }
101
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800102 private void fixIntent(Intent intent) {
103 // This should be cleaned up: the call key used to send an Intent
104 // that just said to go to the recent calls list. It now sends this
105 // abstract action, but this class hasn't been rewritten to deal with it.
106 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
107 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
108 intent.putExtra("call_key", true);
109 setIntent(intent);
110 }
111 }
Jeff Hamilton90313772009-07-28 10:20:31 -0500112
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113 private void setupCallLogTab() {
The Android Open Source Project1f620962009-03-09 11:52:14 -0700114 // Force the class since overriding tab entries doesn't work
115 Intent intent = new Intent("com.android.phone.action.RECENT_CALLS");
116 intent.setClass(this, RecentCallsListActivity.class);
117
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800118 mTabHost.addTab(mTabHost.newTabSpec("call_log")
119 .setIndicator(getString(R.string.recentCallsIconLabel),
120 getResources().getDrawable(R.drawable.ic_tab_recent))
The Android Open Source Project1f620962009-03-09 11:52:14 -0700121 .setContent(intent));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800122 }
123
124 private void setupDialerTab() {
The Android Open Source Project1f620962009-03-09 11:52:14 -0700125 Intent intent = new Intent("com.android.phone.action.TOUCH_DIALER");
126 intent.setClass(this, TwelveKeyDialer.class);
127
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 mTabHost.addTab(mTabHost.newTabSpec("dialer")
129 .setIndicator(getString(R.string.dialerIconLabel),
130 getResources().getDrawable(R.drawable.ic_tab_dialer))
The Android Open Source Project1f620962009-03-09 11:52:14 -0700131 .setContent(intent));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800132 }
133
Jeff Hamilton90313772009-07-28 10:20:31 -0500134 private void setupContactsTab() {
135 Intent intent = new Intent(UI.LIST_DEFAULT);
136 intent.setClass(this, ContactsListActivity.class);
137
138 mTabHost.addTab(mTabHost.newTabSpec("contacts")
139 .setIndicator(getText(R.string.contactsIconLabel),
140 getResources().getDrawable(R.drawable.ic_tab_contacts))
141 .setContent(intent));
142 }
143
144 private void setupFavoritesTab() {
145 Intent intent = new Intent(UI.LIST_STREQUENT_ACTION);
146 intent.setClass(this, ContactsListActivity.class);
147
148 mTabHost.addTab(mTabHost.newTabSpec("favorites")
149 .setIndicator(getString(R.string.contactsFavoritesLabel),
150 getResources().getDrawable(R.drawable.ic_tab_starred))
151 .setContent(intent));
152 }
153
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800154 /**
155 * Returns true if the intent is due to hitting the green send key while in a call.
Jeff Hamilton90313772009-07-28 10:20:31 -0500156 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800157 * @param intent the intent that launched this activity
158 * @param recentCallsRequest true if the intent is requesting to view recent calls
Jeff Hamilton90313772009-07-28 10:20:31 -0500159 * @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 -0800160 */
161 private boolean isSendKeyWhileInCall(final Intent intent, final boolean recentCallsRequest) {
162 // If there is a call in progress go to the call screen
163 if (recentCallsRequest) {
164 final boolean callKey = intent.getBooleanExtra("call_key", false);
165
166 try {
167 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
168 if (callKey && phone != null && phone.showCallScreen()) {
169 return true;
170 }
171 } catch (RemoteException e) {
172 Log.e(TAG, "Failed to handle send while in call", e);
173 }
174 }
175
176 return false;
177 }
178
179 /**
180 * Sets the current tab based on the intent's request type
Jeff Hamilton90313772009-07-28 10:20:31 -0500181 *
The Android Open Source Project1f620962009-03-09 11:52:14 -0700182 * @param recentCallsRequest true is the recent calls tab is desired, false otherwise
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800183 */
184 private void setCurrentTab(Intent intent) {
The Android Open Source Project1f620962009-03-09 11:52:14 -0700185 // 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 -0800186 final boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.getType());
187 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
188 finish();
189 return;
190 }
Jeff Hamilton90313772009-07-28 10:20:31 -0500191
The Android Open Source Project1f620962009-03-09 11:52:14 -0700192 // Dismiss menu provided by any children activities
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800193 Activity activity = getLocalActivityManager().
194 getActivity(mTabHost.getCurrentTabTag());
195 if (activity != null) {
196 activity.closeOptionsMenu();
197 }
198
The Android Open Source Project1f620962009-03-09 11:52:14 -0700199 // Tell the children activities that they should ignore any possible saved
200 // state and instead reload their state from the parent's intent
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800201 intent.putExtra(EXTRA_IGNORE_STATE, true);
The Android Open Source Project1f620962009-03-09 11:52:14 -0700202
203 // Choose the tab based on the inbound intent
204 String componentName = intent.getComponent().getClassName();
205 if (getClass().getName().equals(componentName)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800206 if (recentCallsRequest) {
The Android Open Source Project1f620962009-03-09 11:52:14 -0700207 mTabHost.setCurrentTab(TAB_INDEX_CALL_LOG);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800208 } else {
The Android Open Source Project1f620962009-03-09 11:52:14 -0700209 mTabHost.setCurrentTab(TAB_INDEX_DIALER);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800210 }
Jeff Hamilton90313772009-07-28 10:20:31 -0500211 } else if (FAVORITES_ENTRY_COMPONENT.equals(componentName)) {
212 mTabHost.setCurrentTab(TAB_INDEX_FAVORITES);
213 } else {
214 SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE);
215 boolean favoritesAsContacts = prefs.getBoolean(PREF_FAVORITES_AS_CONTACTS,
216 PREF_FAVORITES_AS_CONTACTS_DEFAULT);
217 if (favoritesAsContacts) {
218 mTabHost.setCurrentTab(TAB_INDEX_FAVORITES);
219 } else {
220 mTabHost.setCurrentTab(TAB_INDEX_CONTACTS);
221 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800222 }
The Android Open Source Project1f620962009-03-09 11:52:14 -0700223
224 // Tell the children activities that they should honor their saved states
225 // instead of the state from the parent's intent
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800226 intent.putExtra(EXTRA_IGNORE_STATE, false);
227 }
228
229 @Override
230 public void onNewIntent(Intent newIntent) {
231 setIntent(newIntent);
232 fixIntent(newIntent);
233 setCurrentTab(newIntent);
Jeff Hamilton90313772009-07-28 10:20:31 -0500234 final String action = newIntent.getAction();
Jeff Hamiltonb25c13e2009-09-21 09:22:16 -0500235 if (action.equals(UI.FILTER_CONTACTS_ACTION)) {
Jeff Hamilton90313772009-07-28 10:20:31 -0500236 setupFilterText(newIntent);
237 } else if (isDialIntent(newIntent)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800238 setupDialUri(newIntent);
239 }
240 }
The Android Open Source Project1f620962009-03-09 11:52:14 -0700241
242 /** 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 -0800243 private boolean isDialIntent(Intent intent) {
244 final String action = intent.getAction();
245 if (Intent.ACTION_DIAL.equals(action)) {
246 return true;
247 }
248 if (Intent.ACTION_VIEW.equals(action)) {
249 final Uri data = intent.getData();
250 if (data != null && "tel".equals(data.getScheme())) {
251 return true;
252 }
253 }
254 return false;
255 }
Jeff Hamilton90313772009-07-28 10:20:31 -0500256
257 /**
258 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
259 * This text originally came from a FILTER_CONTACTS_ACTION intent received
260 * by this activity. The stored text will then be cleared after after this
261 * method returns.
262 *
263 * @return The stored filter text
264 */
265 public String getAndClearFilterText() {
266 String filterText = mFilterText;
267 mFilterText = null;
268 return filterText;
269 }
270
271 /**
272 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
273 * This is so child activities can check if they are supposed to display a filter.
274 *
275 * @param intent The intent received in {@link #onNewIntent(Intent)}
276 */
277 private void setupFilterText(Intent intent) {
278 // If the intent was relaunched from history, don't apply the filter text.
279 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
280 return;
281 }
Jeff Hamiltonb25c13e2009-09-21 09:22:16 -0500282 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
Jeff Hamilton90313772009-07-28 10:20:31 -0500283 if (filter != null && filter.length() > 0) {
284 mFilterText = filter;
285 }
286 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800287
288 /**
289 * Retrieves the uri stored in {@link #setupDialUri(Intent)}. This uri
290 * originally came from a dial intent received by this activity. The stored
291 * uri will then be cleared after after this method returns.
Jeff Hamilton90313772009-07-28 10:20:31 -0500292 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800293 * @return The stored uri
294 */
295 public Uri getAndClearDialUri() {
296 Uri dialUri = mDialUri;
297 mDialUri = null;
298 return dialUri;
299 }
300
301 /**
302 * Stores the uri associated with a dial intent. This is so child activities can
303 * check if they are supposed to display new dial info.
Jeff Hamilton90313772009-07-28 10:20:31 -0500304 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800305 * @param intent The intent received in {@link #onNewIntent(Intent)}
306 */
307 private void setupDialUri(Intent intent) {
308 // If the intent was relaunched from history, don't reapply the intent.
309 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
310 return;
311 }
312 mDialUri = intent.getData();
313 }
314
315 @Override
Dianne Hackborn242599a2009-09-14 21:33:24 -0700316 public void onBackPressed() {
317 if (isTaskRoot()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800318 // Instead of stopping, simply push this to the back of the stack.
319 // This is only done when running at the top of the stack;
320 // otherwise, we have been launched by someone else so need to
321 // allow the user to go back to the caller.
322 moveTaskToBack(false);
Dianne Hackborn242599a2009-09-14 21:33:24 -0700323 } else {
324 super.onBackPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800325 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800326 }
327
328 /** {@inheritDoc} */
329 public void onTabChanged(String tabId) {
330 // Because we're using Activities as our tab children, we trigger
331 // onWindowFocusChanged() to let them know when they're active. This may
332 // seem to duplicate the purpose of onResume(), but it's needed because
333 // onResume() can't reliably check if a keyguard is active.
334 Activity activity = getLocalActivityManager().getActivity(tabId);
335 if (activity != null) {
336 activity.onWindowFocusChanged(true);
337 }
338 }
339}