DialtactsActivity.java revision 17c62974608af9b34908c4a14194d53fd89cca6f
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 17package com.android.contacts.activities; 18 19import com.android.contacts.R; 20import com.android.contacts.calllog.CallLogFragment; 21import com.android.contacts.dialpad.DialpadFragment; 22import com.android.contacts.interactions.PhoneNumberInteraction; 23import com.android.contacts.list.AccountFilterActivity; 24import com.android.contacts.list.ContactListFilter; 25import com.android.contacts.list.ContactListFilterController; 26import com.android.contacts.list.ContactListFilterController.ContactListFilterListener; 27import com.android.contacts.list.ContactTileAdapter.DisplayType; 28import com.android.contacts.list.ContactTileListFragment; 29import com.android.contacts.list.OnPhoneNumberPickerActionListener; 30import com.android.contacts.list.PhoneNumberPickerFragment; 31import com.android.internal.telephony.ITelephony; 32 33import android.app.ActionBar; 34import android.app.ActionBar.LayoutParams; 35import android.app.ActionBar.Tab; 36import android.app.ActionBar.TabListener; 37import android.app.Activity; 38import android.app.Fragment; 39import android.app.FragmentManager; 40import android.app.FragmentTransaction; 41import android.content.Context; 42import android.content.Intent; 43import android.content.SharedPreferences; 44import android.net.Uri; 45import android.os.Bundle; 46import android.os.RemoteException; 47import android.os.ServiceManager; 48import android.preference.PreferenceManager; 49import android.provider.CallLog.Calls; 50import android.provider.ContactsContract.Contacts; 51import android.provider.ContactsContract.Intents.UI; 52import android.support.v13.app.FragmentPagerAdapter; 53import android.support.v4.view.ViewPager; 54import android.support.v4.view.ViewPager.OnPageChangeListener; 55import android.text.TextUtils; 56import android.util.Log; 57import android.view.Menu; 58import android.view.MenuInflater; 59import android.view.MenuItem; 60import android.view.MenuItem.OnMenuItemClickListener; 61import android.view.View; 62import android.view.View.OnClickListener; 63import android.view.View.OnFocusChangeListener; 64import android.view.ViewConfiguration; 65import android.view.inputmethod.InputMethodManager; 66import android.widget.PopupMenu; 67import android.widget.SearchView; 68import android.widget.SearchView.OnCloseListener; 69import android.widget.SearchView.OnQueryTextListener; 70 71/** 72 * The dialer activity that has one tab with the virtual 12key 73 * dialer, a tab with recent calls in it, a tab with the contacts and 74 * a tab with the favorite. This is the container and the tabs are 75 * embedded using intents. 76 * The dialer tab's title is 'phone', a more common name (see strings.xml). 77 */ 78public class DialtactsActivity extends Activity { 79 private static final String TAG = "DialtactsActivity"; 80 81 /** Used to open Call Setting */ 82 private static final String PHONE_PACKAGE = "com.android.phone"; 83 private static final String CALL_SETTINGS_CLASS_NAME = 84 "com.android.phone.CallFeaturesSetting"; 85 86 /** 87 * Copied from PhoneApp. See comments in Phone app for more detail. 88 */ 89 public static final String EXTRA_CALL_ORIGIN = "com.android.phone.CALL_ORIGIN"; 90 public static final String CALL_ORIGIN_DIALTACTS = 91 "com.android.contacts.activities.DialtactsActivity"; 92 93 /** 94 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}. 95 */ 96 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER"; 97 98 /** Used both by {@link ActionBar} and {@link ViewPagerAdapter} */ 99 private static final int TAB_INDEX_DIALER = 0; 100 private static final int TAB_INDEX_CALL_LOG = 1; 101 private static final int TAB_INDEX_FAVORITES = 2; 102 103 private static final int TAB_INDEX_COUNT = 3; 104 105 private SharedPreferences mPrefs; 106 107 /** Last manually selected tab index */ 108 private static final String PREF_LAST_MANUALLY_SELECTED_TAB = 109 "DialtactsActivity_last_manually_selected_tab"; 110 private static final int PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT = TAB_INDEX_DIALER; 111 112 /** 113 * Listener interface for Fragments accommodated in {@link ViewPager} enabling them to know 114 * when it becomes visible or invisible inside the ViewPager. 115 */ 116 public interface ViewPagerVisibilityListener { 117 public void onVisibilityChanged(boolean visible); 118 } 119 120 public class ViewPagerAdapter extends FragmentPagerAdapter { 121 private DialpadFragment mDialpadFragment; 122 private CallLogFragment mCallLogFragment; 123 private ContactTileListFragment mContactTileListFragment; 124 125 public ViewPagerAdapter(FragmentManager fm) { 126 super(fm); 127 } 128 129 @Override 130 public Fragment getItem(int position) { 131 switch (position) { 132 case TAB_INDEX_DIALER: 133 if (mDialpadFragment == null) { 134 mDialpadFragment = new DialpadFragment(); 135 } 136 return mDialpadFragment; 137 case TAB_INDEX_CALL_LOG: 138 if (mCallLogFragment == null) { 139 mCallLogFragment = new CallLogFragment(); 140 } 141 return mCallLogFragment; 142 case TAB_INDEX_FAVORITES: 143 if (mContactTileListFragment == null) { 144 mContactTileListFragment = new ContactTileListFragment(); 145 } 146 return mContactTileListFragment; 147 } 148 throw new IllegalStateException("No fragment at position " + position); 149 } 150 151 @Override 152 public int getCount() { 153 return TAB_INDEX_COUNT; 154 } 155 } 156 157 private class PageChangeListener implements OnPageChangeListener { 158 private int mCurrentPosition = -1; 159 /** 160 * Used during page migration, to remember the next position {@link #onPageSelected(int)} 161 * specified. 162 */ 163 private int mNextPosition = -1; 164 165 @Override 166 public void onPageScrolled( 167 int position, float positionOffset, int positionOffsetPixels) { 168 } 169 170 @Override 171 public void onPageSelected(int position) { 172 final ActionBar actionBar = getActionBar(); 173 if (mCurrentPosition == position) { 174 Log.w(TAG, "Previous position and next position became same (" + position + ")"); 175 } 176 177 actionBar.selectTab(actionBar.getTabAt(position)); 178 mNextPosition = position; 179 } 180 181 public void setCurrentPosition(int position) { 182 mCurrentPosition = position; 183 } 184 185 @Override 186 public void onPageScrollStateChanged(int state) { 187 switch (state) { 188 case ViewPager.SCROLL_STATE_IDLE: { 189 if (mCurrentPosition >= 0) { 190 sendFragmentVisibilityChange(mCurrentPosition, false); 191 } 192 if (mNextPosition >= 0) { 193 sendFragmentVisibilityChange(mNextPosition, true); 194 } 195 invalidateOptionsMenu(); 196 197 mCurrentPosition = mNextPosition; 198 break; 199 } 200 case ViewPager.SCROLL_STATE_DRAGGING: 201 case ViewPager.SCROLL_STATE_SETTLING: 202 default: 203 break; 204 } 205 } 206 } 207 208 private String mFilterText; 209 private Uri mDialUri; 210 211 /** Enables horizontal swipe between Fragments. */ 212 private ViewPager mViewPager; 213 private final PageChangeListener mPageChangeListener = new PageChangeListener(); 214 private DialpadFragment mDialpadFragment; 215 private CallLogFragment mCallLogFragment; 216 private ContactTileListFragment mStrequentFragment; 217 218 private final TabListener mTabListener = new TabListener() { 219 @Override 220 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 221 } 222 223 @Override 224 public void onTabSelected(Tab tab, FragmentTransaction ft) { 225 if (mViewPager.getCurrentItem() != tab.getPosition()) { 226 mViewPager.setCurrentItem(tab.getPosition(), true); 227 } 228 229 // During the call, we don't remember the tab position. 230 if (!DialpadFragment.phoneIsInUse()) { 231 // Remember this tab index. This function is also called, if the tab is set 232 // automatically in which case the setter (setCurrentTab) has to set this to its old 233 // value afterwards 234 mLastManuallySelectedFragment = tab.getPosition(); 235 } 236 } 237 238 @Override 239 public void onTabReselected(Tab tab, FragmentTransaction ft) { 240 } 241 }; 242 243 /** 244 * Fragment for searching phone numbers. Unlike the other Fragments, this doesn't correspond 245 * to tab but is shown by a search action. 246 */ 247 private PhoneNumberPickerFragment mSearchFragment; 248 /** 249 * True when this Activity is in its search UI (with a {@link SearchView} and 250 * {@link PhoneNumberPickerFragment}). 251 */ 252 private boolean mInSearchUi; 253 private SearchView mSearchView; 254 255 private final OnClickListener mFilterOptionClickListener = new OnClickListener() { 256 @Override 257 public void onClick(View view) { 258 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view); 259 final Menu menu = popupMenu.getMenu(); 260 popupMenu.inflate(R.menu.dialtacts_search_options); 261 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); 262 filterOptionMenuItem.setOnMenuItemClickListener(mFilterOptionsMenuItemClickListener); 263 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); 264 addContactOptionMenuItem.setIntent( 265 new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI)); 266 popupMenu.show(); 267 } 268 }; 269 270 /** 271 * The index of the Fragment (or, the tab) that has last been manually selected. 272 * This value does not keep track of programmatically set Tabs (e.g. Call Log after a Call) 273 */ 274 private int mLastManuallySelectedFragment; 275 276 private ContactListFilterController mContactListFilterController; 277 private OnMenuItemClickListener mFilterOptionsMenuItemClickListener = 278 new OnMenuItemClickListener() { 279 @Override 280 public boolean onMenuItemClick(MenuItem item) { 281 final Intent intent = 282 new Intent(DialtactsActivity.this, AccountFilterActivity.class); 283 ContactListFilter filter = mContactListFilterController.getFilter(); 284 startActivityForResult(intent, AccountFilterActivity.DEFAULT_REQUEST_CODE); 285 return true; 286 } 287 }; 288 289 private OnMenuItemClickListener mSearchMenuItemClickListener = 290 new OnMenuItemClickListener() { 291 @Override 292 public boolean onMenuItemClick(MenuItem item) { 293 enterSearchUi(); 294 return true; 295 } 296 }; 297 298 /** 299 * Listener used when one of phone numbers in search UI is selected. This will initiate a 300 * phone call using the phone number. 301 */ 302 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener = 303 new OnPhoneNumberPickerActionListener() { 304 @Override 305 public void onPickPhoneNumberAction(Uri dataUri) { 306 PhoneNumberInteraction.startInteractionForPhoneCall( 307 DialtactsActivity.this, dataUri); 308 } 309 310 @Override 311 public void onShortcutIntentCreated(Intent intent) { 312 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring."); 313 } 314 315 @Override 316 public void onHomeInActionBarSelected() { 317 exitSearchUi(); 318 } 319 }; 320 321 /** 322 * Listener used to send search queries to the phone search fragment. 323 */ 324 private final OnQueryTextListener mPhoneSearchQueryTextListener = 325 new OnQueryTextListener() { 326 @Override 327 public boolean onQueryTextSubmit(String query) { 328 View view = getCurrentFocus(); 329 if (view != null) { 330 hideInputMethod(view); 331 view.clearFocus(); 332 } 333 return true; 334 } 335 336 @Override 337 public boolean onQueryTextChange(String newText) { 338 // Show search result with non-empty text. Show a bare list otherwise. 339 mSearchFragment.setQueryString(newText, true); 340 mSearchFragment.setSearchMode(!TextUtils.isEmpty(newText)); 341 return true; 342 } 343 }; 344 345 /** 346 * Listener used to handle the "close" button on the right side of {@link SearchView}. 347 * If some text is in the search view, this will clean it up. Otherwise this will exit 348 * the search UI and let users go back to usual Phone UI. 349 * 350 * This does _not_ handle back button. 351 */ 352 private final OnCloseListener mPhoneSearchCloseListener = 353 new OnCloseListener() { 354 @Override 355 public boolean onClose() { 356 if (!TextUtils.isEmpty(mSearchView.getQuery())) { 357 mSearchView.setQuery(null, true); 358 } 359 return true; 360 } 361 }; 362 363 @Override 364 protected void onCreate(Bundle icicle) { 365 super.onCreate(icicle); 366 367 final Intent intent = getIntent(); 368 fixIntent(intent); 369 370 setContentView(R.layout.dialtacts_activity); 371 372 mContactListFilterController = new ContactListFilterController(this); 373 mContactListFilterController.addListener(new ContactListFilterListener() { 374 @Override 375 public void onContactListFilterChanged() { 376 if (mSearchFragment == null || !mSearchFragment.isAdded()) { 377 Log.w(TAG, "Search Fragment isn't available when ContactListFilter is changed"); 378 return; 379 } 380 mSearchFragment.setFilter(mContactListFilterController.getFilter()); 381 382 invalidateOptionsMenu(); 383 } 384 }); 385 386 mViewPager = (ViewPager) findViewById(R.id.pager); 387 mViewPager.setAdapter(new ViewPagerAdapter(getFragmentManager())); 388 mViewPager.setOnPageChangeListener(mPageChangeListener); 389 390 prepareSearchView(); 391 392 // Setup the ActionBar tabs (the order matches the tab-index contants TAB_INDEX_*) 393 setupDialer(); 394 setupCallLog(); 395 setupFavorites(); 396 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 397 getActionBar().setDisplayShowTitleEnabled(false); 398 getActionBar().setDisplayShowHomeEnabled(false); 399 400 // Load the last manually loaded tab 401 mPrefs = PreferenceManager.getDefaultSharedPreferences(this); 402 mLastManuallySelectedFragment = mPrefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB, 403 PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT); 404 if (mLastManuallySelectedFragment >= TAB_INDEX_COUNT) { 405 // Stored value may have exceeded the number of current tabs. Reset it. 406 mLastManuallySelectedFragment = PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT; 407 } 408 409 setCurrentTab(intent); 410 411 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction()) 412 && icicle == null) { 413 setupFilterText(intent); 414 } 415 } 416 417 @Override 418 public void onStart() { 419 super.onStart(); 420 // Force filter reload to reflect possible filter changes done via People UI. 421 // 422 // Ideally both (People/Phone) UI should share the same instance for 423 // ContactListFilterController and they should be able to receive filter change event 424 // from the same controller (Bug 5165507) 425 mContactListFilterController.onStart(true); 426 if (mSearchFragment != null) { 427 mSearchFragment.setFilter(mContactListFilterController.getFilter()); 428 } 429 } 430 431 private void prepareSearchView() { 432 final View searchViewLayout = 433 getLayoutInflater().inflate(R.layout.dialtacts_custom_action_bar, null); 434 mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view); 435 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener); 436 mSearchView.setOnCloseListener(mPhoneSearchCloseListener); 437 // Since we're using a custom layout for showing SearchView instead of letting the 438 // search menu icon do that job, we need to manually configure the View so it looks 439 // "shown via search menu". 440 // - it should be iconified by default 441 // - it should not be iconified at this time 442 // See also comments for onActionViewExpanded()/onActionViewCollapsed() 443 mSearchView.setIconifiedByDefault(true); 444 mSearchView.setQueryHint(getString(R.string.hint_findContacts)); 445 mSearchView.setIconified(false); 446 mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() { 447 @Override 448 public void onFocusChange(View view, boolean hasFocus) { 449 if (hasFocus) { 450 showInputMethod(view.findFocus()); 451 } 452 } 453 }); 454 455 if (!ViewConfiguration.get(this).hasPermanentMenuKey()) { 456 // Filter option menu should be shown on the right side of SearchView. 457 final View filterOptionView = searchViewLayout.findViewById(R.id.search_option); 458 filterOptionView.setVisibility(View.VISIBLE); 459 filterOptionView.setOnClickListener(mFilterOptionClickListener); 460 } 461 462 getActionBar().setCustomView(searchViewLayout, 463 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 464 } 465 466 @Override 467 public void onAttachFragment(Fragment fragment) { 468 // This method can be called before onCreate(), at which point we cannot rely on ViewPager. 469 // In that case, we will setup the "current position" soon after the ViewPager is ready. 470 final int currentPosition = mViewPager != null ? mViewPager.getCurrentItem() : -1; 471 472 if (fragment instanceof DialpadFragment) { 473 mDialpadFragment = (DialpadFragment) fragment; 474 mDialpadFragment.setListener(mDialpadListener); 475 if (currentPosition == TAB_INDEX_DIALER) { 476 mDialpadFragment.onVisibilityChanged(true); 477 } 478 } else if (fragment instanceof CallLogFragment) { 479 mCallLogFragment = (CallLogFragment) fragment; 480 if (currentPosition == TAB_INDEX_CALL_LOG) { 481 mCallLogFragment.onVisibilityChanged(true); 482 } 483 } else if (fragment instanceof ContactTileListFragment) { 484 mStrequentFragment = (ContactTileListFragment) fragment; 485 mStrequentFragment.enableQuickContact(false); 486 mStrequentFragment.setListener(mStrequentListener); 487 mStrequentFragment.setDisplayType(DisplayType.STREQUENT_PHONE_ONLY); 488 } else if (fragment instanceof PhoneNumberPickerFragment) { 489 mSearchFragment = (PhoneNumberPickerFragment) fragment; 490 mSearchFragment.setOnPhoneNumberPickerActionListener(mPhoneNumberPickerActionListener); 491 mSearchFragment.setQuickContactEnabled(true); 492 mSearchFragment.setDarkTheme(true); 493 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 494 if (mInSearchUi) { 495 transaction.show(mSearchFragment); 496 } else { 497 transaction.hide(mSearchFragment); 498 } 499 transaction.commitAllowingStateLoss(); 500 } 501 } 502 503 @Override 504 protected void onPause() { 505 super.onPause(); 506 507 mPrefs.edit().putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedFragment) 508 .apply(); 509 } 510 511 private void fixIntent(Intent intent) { 512 // This should be cleaned up: the call key used to send an Intent 513 // that just said to go to the recent calls list. It now sends this 514 // abstract action, but this class hasn't been rewritten to deal with it. 515 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) { 516 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE); 517 intent.putExtra("call_key", true); 518 setIntent(intent); 519 } 520 } 521 522 private void setupDialer() { 523 final Tab tab = getActionBar().newTab(); 524 tab.setContentDescription(R.string.dialerIconLabel); 525 tab.setTabListener(mTabListener); 526 tab.setIcon(R.drawable.ic_tab_dialer); 527 getActionBar().addTab(tab); 528 } 529 530 private void setupCallLog() { 531 final Tab tab = getActionBar().newTab(); 532 tab.setContentDescription(R.string.recentCallsIconLabel); 533 tab.setIcon(R.drawable.ic_tab_recent); 534 tab.setTabListener(mTabListener); 535 getActionBar().addTab(tab); 536 } 537 538 private void setupFavorites() { 539 final Tab tab = getActionBar().newTab(); 540 tab.setContentDescription(R.string.contactsFavoritesLabel); 541 tab.setIcon(R.drawable.ic_tab_starred); 542 tab.setTabListener(mTabListener); 543 getActionBar().addTab(tab); 544 } 545 546 /** 547 * Returns true if the intent is due to hitting the green send key while in a call. 548 * 549 * @param intent the intent that launched this activity 550 * @param recentCallsRequest true if the intent is requesting to view recent calls 551 * @return true if the intent is due to hitting the green send key while in a call 552 */ 553 private boolean isSendKeyWhileInCall(final Intent intent, 554 final boolean recentCallsRequest) { 555 // If there is a call in progress go to the call screen 556 if (recentCallsRequest) { 557 final boolean callKey = intent.getBooleanExtra("call_key", false); 558 559 try { 560 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); 561 if (callKey && phone != null && phone.showCallScreen()) { 562 return true; 563 } 564 } catch (RemoteException e) { 565 Log.e(TAG, "Failed to handle send while in call", e); 566 } 567 } 568 569 return false; 570 } 571 572 /** 573 * Sets the current tab based on the intent's request type 574 * 575 * @param intent Intent that contains information about which tab should be selected 576 */ 577 private void setCurrentTab(Intent intent) { 578 // If we got here by hitting send and we're in call forward along to the in-call activity 579 final boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.getType()); 580 if (isSendKeyWhileInCall(intent, recentCallsRequest)) { 581 finish(); 582 return; 583 } 584 585 // Remember the old manually selected tab index so that it can be restored if it is 586 // overwritten by one of the programmatic tab selections 587 final int savedTabIndex = mLastManuallySelectedFragment; 588 589 final int tabIndex; 590 if (DialpadFragment.phoneIsInUse() || isDialIntent(intent)) { 591 tabIndex = TAB_INDEX_DIALER; 592 } else if (recentCallsRequest) { 593 tabIndex = TAB_INDEX_CALL_LOG; 594 } else { 595 tabIndex = mLastManuallySelectedFragment; 596 } 597 598 final int previousItemIndex = mViewPager.getCurrentItem(); 599 mViewPager.setCurrentItem(tabIndex, false /* smoothScroll */); 600 if (previousItemIndex != tabIndex) { 601 sendFragmentVisibilityChange(previousItemIndex, false); 602 } 603 mPageChangeListener.setCurrentPosition(tabIndex); 604 sendFragmentVisibilityChange(tabIndex, true); 605 606 // Restore to the previous manual selection 607 mLastManuallySelectedFragment = savedTabIndex; 608 } 609 610 @Override 611 public void onNewIntent(Intent newIntent) { 612 setIntent(newIntent); 613 fixIntent(newIntent); 614 setCurrentTab(newIntent); 615 final String action = newIntent.getAction(); 616 if (UI.FILTER_CONTACTS_ACTION.equals(action)) { 617 setupFilterText(newIntent); 618 } 619 if (mInSearchUi || mSearchFragment.isVisible()) { 620 exitSearchUi(); 621 } 622 623 if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) { 624 if (mDialpadFragment != null) { 625 mDialpadFragment.configureScreenFromIntent(newIntent); 626 } else { 627 Log.e(TAG, "DialpadFragment isn't ready yet when the tab is already selected."); 628 } 629 } 630 invalidateOptionsMenu(); 631 } 632 633 /** Returns true if the given intent contains a phone number to populate the dialer with */ 634 private boolean isDialIntent(Intent intent) { 635 final String action = intent.getAction(); 636 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) { 637 return true; 638 } 639 if (Intent.ACTION_VIEW.equals(action)) { 640 final Uri data = intent.getData(); 641 if (data != null && "tel".equals(data.getScheme())) { 642 return true; 643 } 644 } 645 return false; 646 } 647 648 /** 649 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}. 650 * This text originally came from a FILTER_CONTACTS_ACTION intent received 651 * by this activity. The stored text will then be cleared after after this 652 * method returns. 653 * 654 * @return The stored filter text 655 */ 656 public String getAndClearFilterText() { 657 String filterText = mFilterText; 658 mFilterText = null; 659 return filterText; 660 } 661 662 /** 663 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent. 664 * This is so child activities can check if they are supposed to display a filter. 665 * 666 * @param intent The intent received in {@link #onNewIntent(Intent)} 667 */ 668 private void setupFilterText(Intent intent) { 669 // If the intent was relaunched from history, don't apply the filter text. 670 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { 671 return; 672 } 673 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY); 674 if (filter != null && filter.length() > 0) { 675 mFilterText = filter; 676 } 677 } 678 679 @Override 680 public void onBackPressed() { 681 if (mInSearchUi) { 682 // We should let the user go back to usual screens with tabs. 683 exitSearchUi(); 684 } else if (isTaskRoot()) { 685 // Instead of stopping, simply push this to the back of the stack. 686 // This is only done when running at the top of the stack; 687 // otherwise, we have been launched by someone else so need to 688 // allow the user to go back to the caller. 689 moveTaskToBack(false); 690 } else { 691 super.onBackPressed(); 692 } 693 } 694 695 private DialpadFragment.Listener mDialpadListener = new DialpadFragment.Listener() { 696 @Override 697 public void onSearchButtonPressed() { 698 enterSearchUi(); 699 } 700 }; 701 702 private ContactTileListFragment.Listener mStrequentListener = 703 new ContactTileListFragment.Listener() { 704 @Override 705 public void onContactSelected(Uri contactUri) { 706 PhoneNumberInteraction.startInteractionForPhoneCall( 707 DialtactsActivity.this, contactUri, 708 CALL_ORIGIN_DIALTACTS); 709 } 710 }; 711 712 @Override 713 public boolean onCreateOptionsMenu(Menu menu) { 714 MenuInflater inflater = getMenuInflater(); 715 inflater.inflate(R.menu.dialtacts_options, menu); 716 return true; 717 } 718 719 @Override 720 public boolean onPrepareOptionsMenu(Menu menu) { 721 final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar); 722 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option); 723 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact); 724 final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings); 725 Tab tab = getActionBar().getSelectedTab(); 726 if (mInSearchUi) { 727 searchMenuItem.setVisible(false); 728 if (ViewConfiguration.get(this).hasPermanentMenuKey()) { 729 filterOptionMenuItem.setVisible(true); 730 filterOptionMenuItem.setOnMenuItemClickListener( 731 mFilterOptionsMenuItemClickListener); 732 addContactOptionMenuItem.setVisible(true); 733 addContactOptionMenuItem.setIntent( 734 new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI)); 735 } else { 736 // Filter option menu should be not be shown as a overflow menu. 737 filterOptionMenuItem.setVisible(false); 738 addContactOptionMenuItem.setVisible(false); 739 } 740 callSettingsMenuItem.setVisible(false); 741 } else { 742 final boolean showCallSettingsMenu; 743 if (tab != null && tab.getPosition() == TAB_INDEX_DIALER) { 744 searchMenuItem.setVisible(false); 745 // When permanent menu key is _not_ available, the call settings menu should be 746 // available via DialpadFragment. 747 showCallSettingsMenu = ViewConfiguration.get(this).hasPermanentMenuKey(); 748 } else { 749 searchMenuItem.setVisible(true); 750 searchMenuItem.setOnMenuItemClickListener(mSearchMenuItemClickListener); 751 showCallSettingsMenu = true; 752 } 753 filterOptionMenuItem.setVisible(false); 754 addContactOptionMenuItem.setVisible(false); 755 756 if (showCallSettingsMenu) { 757 callSettingsMenuItem.setVisible(true); 758 callSettingsMenuItem.setIntent(DialtactsActivity.getCallSettingsIntent()); 759 } else { 760 callSettingsMenuItem.setVisible(false); 761 } 762 } 763 764 return true; 765 } 766 767 @Override 768 public void startSearch(String initialQuery, boolean selectInitialQuery, 769 Bundle appSearchData, boolean globalSearch) { 770 if (mSearchFragment != null && mSearchFragment.isAdded() && !globalSearch) { 771 if (mInSearchUi) { 772 if (mSearchView.hasFocus()) { 773 showInputMethod(mSearchView.findFocus()); 774 } else { 775 mSearchView.requestFocus(); 776 } 777 } else { 778 enterSearchUi(); 779 } 780 } else { 781 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); 782 } 783 } 784 785 /** 786 * Hides every tab and shows search UI for phone lookup. 787 */ 788 private void enterSearchUi() { 789 final ActionBar actionBar = getActionBar(); 790 791 final Tab tab = actionBar.getSelectedTab(); 792 793 // User can search during the call, but we don't want to remember the status. 794 if (tab != null && !DialpadFragment.phoneIsInUse()) { 795 mLastManuallySelectedFragment = tab.getPosition(); 796 } 797 798 mSearchView.setQuery(null, true); 799 800 actionBar.setDisplayShowCustomEnabled(true); 801 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 802 actionBar.setDisplayShowHomeEnabled(true); 803 actionBar.setDisplayHomeAsUpEnabled(true); 804 805 sendFragmentVisibilityChange(mViewPager.getCurrentItem(), false); 806 807 // Show the search fragment and hide everything else. 808 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 809 transaction.show(mSearchFragment); 810 transaction.commitAllowingStateLoss(); 811 mViewPager.setVisibility(View.GONE); 812 813 // We need to call this and onActionViewCollapsed() manually, since we are using a custom 814 // layout instead of asking the search menu item to take care of SearchView. 815 mSearchView.onActionViewExpanded(); 816 mInSearchUi = true; 817 818 // Clear focus and suppress keyboard show-up. 819 mSearchView.clearFocus(); 820 } 821 822 private void showInputMethod(View view) { 823 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 824 if (imm != null) { 825 if (!imm.showSoftInput(view, 0)) { 826 Log.w(TAG, "Failed to show soft input method."); 827 } 828 } 829 } 830 831 private void hideInputMethod(View view) { 832 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 833 if (imm != null && view != null) { 834 imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 835 } 836 } 837 838 /** 839 * Goes back to usual Phone UI with tags. Previously selected Tag and associated Fragment 840 * should be automatically focused again. 841 */ 842 private void exitSearchUi() { 843 final ActionBar actionBar = getActionBar(); 844 845 final FragmentTransaction transaction = getFragmentManager().beginTransaction(); 846 transaction.hide(mSearchFragment); 847 transaction.commitAllowingStateLoss(); 848 849 // We want to hide SearchView and show Tabs. Also focus on previously selected one. 850 actionBar.setDisplayShowCustomEnabled(false); 851 actionBar.setDisplayShowHomeEnabled(false); 852 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 853 854 sendFragmentVisibilityChange(mViewPager.getCurrentItem(), true); 855 856 mViewPager.setVisibility(View.VISIBLE); 857 858 hideInputMethod(getCurrentFocus()); 859 860 // Request to update option menu. 861 invalidateOptionsMenu(); 862 863 // See comments in onActionViewExpanded() 864 mSearchView.onActionViewCollapsed(); 865 mInSearchUi = false; 866 } 867 868 private Fragment getFragmentAt(int position) { 869 switch (position) { 870 case TAB_INDEX_DIALER: 871 return mDialpadFragment; 872 case TAB_INDEX_CALL_LOG: 873 return mCallLogFragment; 874 case TAB_INDEX_FAVORITES: 875 return mStrequentFragment; 876 default: 877 throw new IllegalStateException("Unknown fragment index: " + position); 878 } 879 } 880 881 private void sendFragmentVisibilityChange(int position, boolean visibility) { 882 final Fragment fragment = getFragmentAt(position); 883 if (fragment instanceof ViewPagerVisibilityListener) { 884 ((ViewPagerVisibilityListener) fragment).onVisibilityChanged(visibility); 885 } 886 } 887 888 /** Returns an Intent to launch Call Settings screen */ 889 public static Intent getCallSettingsIntent() { 890 final Intent intent = new Intent(Intent.ACTION_MAIN); 891 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME); 892 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 893 return intent; 894 } 895 896 @Override 897 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 898 if (resultCode != Activity.RESULT_OK) { 899 return; 900 } 901 switch (requestCode) { 902 case AccountFilterActivity.DEFAULT_REQUEST_CODE: { 903 ContactListFilter filter = (ContactListFilter) data.getParcelableExtra( 904 AccountFilterActivity.KEY_EXTRA_CONTACT_LIST_FILTER); 905 if (filter == null) { 906 return; 907 } 908 if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) { 909 mContactListFilterController.selectCustomFilter(); 910 } else { 911 mContactListFilterController.setContactListFilter(filter, true); 912 } 913 } 914 break; 915 } 916 } 917} 918