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