The Android Open Source Project | 5dc3b4f | 2008-10-21 07:00:00 -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 | |
The Android Open Source Project | 9cb63a5 | 2009-01-09 17:51:25 -0800 | [diff] [blame^] | 19 | import android.app.Activity; |
The Android Open Source Project | 5dc3b4f | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import android.app.TabActivity; |
| 21 | import android.content.Intent; |
| 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.net.Uri; |
| 24 | import android.os.Bundle; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.ServiceManager; |
| 27 | import android.provider.Contacts; |
| 28 | import android.provider.CallLog.Calls; |
| 29 | import android.provider.Contacts.Intents.UI; |
| 30 | import android.util.Log; |
| 31 | import android.view.KeyEvent; |
| 32 | import android.view.Window; |
| 33 | import android.widget.TabHost; |
| 34 | import 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 Project | 9cb63a5 | 2009-01-09 17:51:25 -0800 | [diff] [blame^] | 41 | public class DialtactsActivity extends TabActivity implements TabHost.OnTabChangeListener { |
The Android Open Source Project | 5dc3b4f | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | 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 Project | 9cb63a5 | 2009-01-09 17:51:25 -0800 | [diff] [blame^] | 70 | mTabHost.setOnTabChangedListener(this); |
The Android Open Source Project | 5dc3b4f | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 71 | |
| 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 | } |
| 179 | intent.putExtra(EXTRA_IGNORE_STATE, true); |
| 180 | if (intent.getComponent().getClassName().equals(getClass().getName())) { |
| 181 | if (recentCallsRequest) { |
| 182 | mTabHost.setCurrentTab(1); |
| 183 | } else { |
| 184 | mTabHost.setCurrentTab(0); |
| 185 | } |
| 186 | } else { |
| 187 | mTabHost.setCurrentTab(2); |
| 188 | } |
| 189 | intent.putExtra(EXTRA_IGNORE_STATE, false); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void onNewIntent(Intent newIntent) { |
| 194 | setIntent(newIntent); |
| 195 | fixIntent(newIntent); |
| 196 | setCurrentTab(newIntent); |
| 197 | final String action = newIntent.getAction(); |
| 198 | if (action.equals(Contacts.Intents.UI.FILTER_CONTACTS_ACTION)) { |
| 199 | setupFilterText(newIntent); |
| 200 | } else if (isDialIntent(newIntent)) { |
| 201 | setupDialUri(newIntent); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private boolean isDialIntent(Intent intent) { |
| 206 | final String action = intent.getAction(); |
| 207 | if (Intent.ACTION_DIAL.equals(action)) { |
| 208 | return true; |
| 209 | } |
| 210 | if (Intent.ACTION_VIEW.equals(action)) { |
| 211 | final Uri data = intent.getData(); |
| 212 | if (data != null && "tel".equals(data.getScheme())) { |
| 213 | return true; |
| 214 | } |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Retrieves the filter text stored in {@link #setupFilterText(Intent)}. |
| 221 | * This text originally came from a FILTER_CONTACTS_ACTION intent received |
| 222 | * by this activity. The stored text will then be cleared after after this |
| 223 | * method returns. |
| 224 | * |
| 225 | * @return The stored filter text |
| 226 | */ |
| 227 | public String getAndClearFilterText() { |
| 228 | String filterText = mFilterText; |
| 229 | mFilterText = null; |
| 230 | return filterText; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent. |
| 235 | * This is so child activities can check if they are supposed to display a filter. |
| 236 | * |
| 237 | * @param intent The intent received in {@link #onNewIntent(Intent)} |
| 238 | */ |
| 239 | private void setupFilterText(Intent intent) { |
| 240 | // If the intent was relaunched from history, don't apply the filter text. |
| 241 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { |
| 242 | return; |
| 243 | } |
| 244 | String filter = intent.getStringExtra(Contacts.Intents.UI.FILTER_TEXT_EXTRA_KEY); |
| 245 | if (filter != null && filter.length() > 0) { |
| 246 | mFilterText = filter; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Retrieves the uri stored in {@link #setupDialUri(Intent)}. This uri |
| 252 | * originally came from a dial intent received by this activity. The stored |
| 253 | * uri will then be cleared after after this method returns. |
| 254 | * |
| 255 | * @return The stored uri |
| 256 | */ |
| 257 | public Uri getAndClearDialUri() { |
| 258 | Uri dialUri = mDialUri; |
| 259 | mDialUri = null; |
| 260 | return dialUri; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Stores the uri associated with a dial intent. This is so child activities can |
| 265 | * check if they are supposed to display new dial info. |
| 266 | * |
| 267 | * @param intent The intent received in {@link #onNewIntent(Intent)} |
| 268 | */ |
| 269 | private void setupDialUri(Intent intent) { |
| 270 | // If the intent was relaunched from history, don't reapply the intent. |
| 271 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { |
| 272 | return; |
| 273 | } |
| 274 | mDialUri = intent.getData(); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 279 | // Handle BACK |
| 280 | if (keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) { |
| 281 | // Instead of stopping, simply push this to the back of the stack. |
| 282 | // This is only done when running at the top of the stack; |
| 283 | // otherwise, we have been launched by someone else so need to |
| 284 | // allow the user to go back to the caller. |
| 285 | moveTaskToBack(false); |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | return super.onKeyDown(keyCode, event); |
| 290 | } |
The Android Open Source Project | 9cb63a5 | 2009-01-09 17:51:25 -0800 | [diff] [blame^] | 291 | |
| 292 | /** {@inheritDoc} */ |
| 293 | public void onTabChanged(String tabId) { |
| 294 | // Because we're using Activities as our tab children, we trigger |
| 295 | // onWindowFocusChanged() to let them know when they're active. This may |
| 296 | // seem to duplicate the purpose of onResume(), but it's needed because |
| 297 | // onResume() can't reliably check if a keyguard is active. |
| 298 | Activity activity = getLocalActivityManager().getActivity(tabId); |
| 299 | if (activity != null) { |
| 300 | activity.onWindowFocusChanged(true); |
| 301 | } |
| 302 | } |
The Android Open Source Project | 5dc3b4f | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 303 | } |