blob: d3f8981ffab4885e2c4c6b696295ef5b4408dc8a [file] [log] [blame]
The Android Open Source Project5dc3b4f2008-10-21 07:00:00 -07001/*
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
The Android Open Source Project9cb63a52009-01-09 17:51:25 -080019import android.app.Activity;
The Android Open Source Project5dc3b4f2008-10-21 07:00:00 -070020import android.app.TabActivity;
21import android.content.Intent;
22import android.graphics.drawable.Drawable;
23import android.net.Uri;
24import android.os.Bundle;
25import android.os.RemoteException;
26import android.os.ServiceManager;
27import android.provider.Contacts;
28import android.provider.CallLog.Calls;
29import android.provider.Contacts.Intents.UI;
30import android.util.Log;
31import android.view.KeyEvent;
32import android.view.Window;
33import android.widget.TabHost;
34import com.android.internal.telephony.ITelephony;
35
36/**
37 * The dialer activity that has one tab with the virtual 12key dialer,
38 * and another tab with recent calls in it. This is the container and the tabs
39 * are embedded using intents.
40 */
The Android Open Source Project9cb63a52009-01-09 17:51:25 -080041public class DialtactsActivity extends TabActivity implements TabHost.OnTabChangeListener {
The Android Open Source Project5dc3b4f2008-10-21 07:00:00 -070042 private static final String TAG = "Dailtacts";
43
44 public static final String EXTRA_IGNORE_STATE = "ignore-state";
45
46 private static final int FAVORITES_STARRED = 1;
47 private static final int FAVORITES_FREQUENT = 2;
48 private static final int FAVORITES_STREQUENT = 3;
49
50 /** Defines what is displayed in the right tab */
51 private static final int FAVORITES_TAB_MODE = FAVORITES_STREQUENT;
52
53 protected TabHost mTabHost;
54
55 private String mFilterText;
56
57 private Uri mDialUri;
58
59 @Override
60 protected void onCreate(Bundle icicle) {
61 super.onCreate(icicle);
62
63 final Intent intent = getIntent();
64 fixIntent(intent);
65
66 requestWindowFeature(Window.FEATURE_NO_TITLE);
67 setContentView(R.layout.dialer_activity);
68
69 mTabHost = getTabHost();
The Android Open Source Project9cb63a52009-01-09 17:51:25 -080070 mTabHost.setOnTabChangedListener(this);
The Android Open Source Project5dc3b4f2008-10-21 07:00:00 -070071
72 // Setup the tabs
73 setupDialerTab();
74 setupCallLogTab();
75 setupContactsTab();
76 setupFavoritesTab();
77
78 setCurrentTab(intent);
79
80 if (intent.getAction().equals(Contacts.Intents.UI.FILTER_CONTACTS_ACTION)
81 && icicle == null) {
82 setupFilterText(intent);
83 }
84 }
85
86 private void fixIntent(Intent intent) {
87 // This should be cleaned up: the call key used to send an Intent
88 // that just said to go to the recent calls list. It now sends this
89 // abstract action, but this class hasn't been rewritten to deal with it.
90 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
91 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
92 intent.putExtra("call_key", true);
93 setIntent(intent);
94 }
95 }
96
97 private void setupCallLogTab() {
98 mTabHost.addTab(mTabHost.newTabSpec("call_log")
99 .setIndicator(getString(R.string.recentCallsIconLabel),
100 getResources().getDrawable(R.drawable.ic_tab_recent))
101 .setContent(new Intent("com.android.phone.action.RECENT_CALLS")));
102 }
103
104 private void setupDialerTab() {
105 mTabHost.addTab(mTabHost.newTabSpec("dialer")
106 .setIndicator(getString(R.string.dialerIconLabel),
107 getResources().getDrawable(R.drawable.ic_tab_dialer))
108 .setContent(new Intent("com.android.phone.action.TOUCH_DIALER")));
109 }
110
111 private void setupContactsTab() {
112 mTabHost.addTab(mTabHost.newTabSpec("contacts")
113 .setIndicator(getText(R.string.contactsIconLabel),
114 getResources().getDrawable(R.drawable.ic_tab_contacts))
115 .setContent(new Intent(UI.LIST_DEFAULT)));
116 }
117
118 private void setupFavoritesTab() {
119 Intent tab2Intent;
120 switch (FAVORITES_TAB_MODE) {
121 case FAVORITES_STARRED:
122 tab2Intent = new Intent(UI.LIST_STARRED_ACTION);
123 break;
124
125 case FAVORITES_FREQUENT:
126 tab2Intent = new Intent(UI.LIST_FREQUENT_ACTION);
127 break;
128
129 case FAVORITES_STREQUENT:
130 tab2Intent = new Intent(UI.LIST_STREQUENT_ACTION);
131 break;
132
133 default:
134 throw new UnsupportedOperationException("unknown default mode");
135 }
136 Drawable tab2Icon = getResources().getDrawable(R.drawable.ic_tab_starred);
137
138 mTabHost.addTab(mTabHost.newTabSpec("favorites")
139 .setIndicator(getString(R.string.contactsFavoritesLabel), tab2Icon)
140 .setContent(tab2Intent));
141 }
142
143 /**
144 * Returns true if the intent is due to hitting the green send key while in a call.
145 *
146 * @param intent the intent that launched this activity
147 * @param recentCallsRequest true if the intent is requesting to view recent calls
148 * @return true if the intent is due to hitting the green send key while in a call
149 */
150 private boolean isSendKeyWhileInCall(final Intent intent, final boolean recentCallsRequest) {
151 // If there is a call in progress go to the call screen
152 if (recentCallsRequest) {
153 final boolean callKey = intent.getBooleanExtra("call_key", false);
154
155 try {
156 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
157 if (callKey && phone != null && phone.showCallScreen()) {
158 return true;
159 }
160 } catch (RemoteException e) {
161 Log.e(TAG, "Failed to handle send while in call", e);
162 }
163 }
164
165 return false;
166 }
167
168 /**
169 * Sets the current tab based on the intent's request type
170 *
171 * @param recentCallsRequest true is the recent calls tab is desired, false oltherwise
172 */
173 private void setCurrentTab(Intent intent) {
174 final boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.getType());
175 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
176 finish();
177 return;
178 }
The Android Open Source Project42e29b72009-02-10 15:44:04 -0800179
180 // Dismiss menu provided by any children activites
181 Activity activity = getLocalActivityManager().
182 getActivity(mTabHost.getCurrentTabTag());
183 if (activity != null) {
184 activity.closeOptionsMenu();
185 }
186
The Android Open Source Project5dc3b4f2008-10-21 07:00:00 -0700187 intent.putExtra(EXTRA_IGNORE_STATE, true);
188 if (intent.getComponent().getClassName().equals(getClass().getName())) {
189 if (recentCallsRequest) {
190 mTabHost.setCurrentTab(1);
191 } else {
192 mTabHost.setCurrentTab(0);
193 }
194 } else {
195 mTabHost.setCurrentTab(2);
196 }
197 intent.putExtra(EXTRA_IGNORE_STATE, false);
198 }
199
200 @Override
201 public void onNewIntent(Intent newIntent) {
202 setIntent(newIntent);
203 fixIntent(newIntent);
204 setCurrentTab(newIntent);
205 final String action = newIntent.getAction();
206 if (action.equals(Contacts.Intents.UI.FILTER_CONTACTS_ACTION)) {
207 setupFilterText(newIntent);
208 } else if (isDialIntent(newIntent)) {
209 setupDialUri(newIntent);
210 }
211 }
212
213 private boolean isDialIntent(Intent intent) {
214 final String action = intent.getAction();
215 if (Intent.ACTION_DIAL.equals(action)) {
216 return true;
217 }
218 if (Intent.ACTION_VIEW.equals(action)) {
219 final Uri data = intent.getData();
220 if (data != null && "tel".equals(data.getScheme())) {
221 return true;
222 }
223 }
224 return false;
225 }
226
227 /**
228 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
229 * This text originally came from a FILTER_CONTACTS_ACTION intent received
230 * by this activity. The stored text will then be cleared after after this
231 * method returns.
232 *
233 * @return The stored filter text
234 */
235 public String getAndClearFilterText() {
236 String filterText = mFilterText;
237 mFilterText = null;
238 return filterText;
239 }
240
241 /**
242 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
243 * This is so child activities can check if they are supposed to display a filter.
244 *
245 * @param intent The intent received in {@link #onNewIntent(Intent)}
246 */
247 private void setupFilterText(Intent intent) {
248 // If the intent was relaunched from history, don't apply the filter text.
249 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
250 return;
251 }
252 String filter = intent.getStringExtra(Contacts.Intents.UI.FILTER_TEXT_EXTRA_KEY);
253 if (filter != null && filter.length() > 0) {
254 mFilterText = filter;
255 }
256 }
257
258 /**
259 * Retrieves the uri stored in {@link #setupDialUri(Intent)}. This uri
260 * originally came from a dial intent received by this activity. The stored
261 * uri will then be cleared after after this method returns.
262 *
263 * @return The stored uri
264 */
265 public Uri getAndClearDialUri() {
266 Uri dialUri = mDialUri;
267 mDialUri = null;
268 return dialUri;
269 }
270
271 /**
272 * Stores the uri associated with a dial intent. This is so child activities can
273 * check if they are supposed to display new dial info.
274 *
275 * @param intent The intent received in {@link #onNewIntent(Intent)}
276 */
277 private void setupDialUri(Intent intent) {
278 // If the intent was relaunched from history, don't reapply the intent.
279 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
280 return;
281 }
282 mDialUri = intent.getData();
283 }
284
285 @Override
286 public boolean onKeyDown(int keyCode, KeyEvent event) {
287 // Handle BACK
288 if (keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) {
289 // Instead of stopping, simply push this to the back of the stack.
290 // This is only done when running at the top of the stack;
291 // otherwise, we have been launched by someone else so need to
292 // allow the user to go back to the caller.
293 moveTaskToBack(false);
294 return true;
295 }
296
297 return super.onKeyDown(keyCode, event);
298 }
The Android Open Source Project9cb63a52009-01-09 17:51:25 -0800299
300 /** {@inheritDoc} */
301 public void onTabChanged(String tabId) {
302 // Because we're using Activities as our tab children, we trigger
303 // onWindowFocusChanged() to let them know when they're active. This may
304 // seem to duplicate the purpose of onResume(), but it's needed because
305 // onResume() can't reliably check if a keyguard is active.
306 Activity activity = getLocalActivityManager().getActivity(tabId);
307 if (activity != null) {
308 activity.onWindowFocusChanged(true);
309 }
310 }
The Android Open Source Project5dc3b4f2008-10-21 07:00:00 -0700311}