blob: c9419a3c4e084e6da03c9dbc10b65195162bf953 [file] [log] [blame]
Chiao Cheng94b10b52012-08-17 16:59:12 -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.dialer;
18
19import android.app.ActionBar;
20import android.app.ActionBar.LayoutParams;
21import android.app.ActionBar.Tab;
22import android.app.ActionBar.TabListener;
23import android.app.Activity;
24import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
27import android.content.Context;
28import android.content.Intent;
29import android.content.SharedPreferences;
30import android.net.Uri;
31import android.os.Bundle;
32import android.os.RemoteException;
33import android.os.ServiceManager;
34import android.preference.PreferenceManager;
35import android.provider.CallLog.Calls;
36import android.provider.ContactsContract.Contacts;
37import android.provider.ContactsContract.Intents.UI;
38import android.support.v13.app.FragmentPagerAdapter;
39import android.support.v4.view.ViewPager;
40import android.support.v4.view.ViewPager.OnPageChangeListener;
41import android.text.TextUtils;
42import android.util.DisplayMetrics;
43import android.util.Log;
44import android.view.Menu;
45import android.view.MenuInflater;
46import android.view.MenuItem;
47import android.view.MenuItem.OnMenuItemClickListener;
48import android.view.View;
49import android.view.View.OnClickListener;
50import android.view.View.OnFocusChangeListener;
51import android.view.ViewConfiguration;
52import android.view.ViewGroup;
53import android.view.inputmethod.InputMethodManager;
54import android.widget.PopupMenu;
55import android.widget.SearchView;
56import android.widget.SearchView.OnCloseListener;
57import android.widget.SearchView.OnQueryTextListener;
58
Chiao Cheng9d4f3b22012-09-05 16:00:16 -070059import com.android.contacts.common.CallUtil;
Chiao Cheng603ff682012-10-24 15:18:40 -070060import com.android.contacts.common.activity.TransactionSafeActivity;
Chiao Cheng963216b2012-11-26 15:36:44 -080061import com.android.contacts.common.list.ContactListFilterController;
62import com.android.contacts.common.list.ContactListFilterController.ContactListFilterListener;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070063import com.android.contacts.common.list.ContactListItemView;
Chiao Cheng8efbcf92012-12-04 17:43:02 -080064import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
65import com.android.contacts.common.list.PhoneNumberPickerFragment;
Chiao Cheng82d544d2012-11-30 13:32:24 -080066import com.android.contacts.common.util.AccountFilterUtil;
Chiao Cheng91197042012-08-24 14:19:37 -070067import com.android.dialer.calllog.CallLogFragment;
68import com.android.dialer.dialpad.DialpadFragment;
Chiao Cheng1429f1a2012-11-01 16:35:28 -070069import com.android.dialer.interactions.PhoneNumberInteraction;
Chiao Cheng91197042012-08-24 14:19:37 -070070import com.android.dialer.list.PhoneFavoriteFragment;
Chiao Cheng35071c02012-10-15 18:36:24 -070071import com.android.dialer.util.OrientationUtil;
Chiao Cheng94b10b52012-08-17 16:59:12 -070072import com.android.internal.telephony.ITelephony;
73
74/**
75 * The dialer activity that has one tab with the virtual 12key
76 * dialer, a tab with recent calls in it, a tab with the contacts and
77 * a tab with the favorite. This is the container and the tabs are
78 * embedded using intents.
79 * The dialer tab's title is 'phone', a more common name (see strings.xml).
80 */
Chiao Cheng603ff682012-10-24 15:18:40 -070081public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener {
Chiao Cheng94b10b52012-08-17 16:59:12 -070082 private static final String TAG = "DialtactsActivity";
83
84 public static final boolean DEBUG = false;
85
86 /** Used to open Call Setting */
87 private static final String PHONE_PACKAGE = "com.android.phone";
88 private static final String CALL_SETTINGS_CLASS_NAME =
89 "com.android.phone.CallFeaturesSetting";
90
91 /** @see #getCallOrigin() */
92 private static final String CALL_ORIGIN_DIALTACTS =
93 "com.android.dialer.DialtactsActivity";
94
95 /**
96 * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
97 */
98 private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
99
100 /** Used both by {@link ActionBar} and {@link ViewPagerAdapter} */
101 private static final int TAB_INDEX_DIALER = 0;
102 private static final int TAB_INDEX_CALL_LOG = 1;
103 private static final int TAB_INDEX_FAVORITES = 2;
104
105 private static final int TAB_INDEX_COUNT = 3;
106
107 private SharedPreferences mPrefs;
108
109 /** Last manually selected tab index */
110 private static final String PREF_LAST_MANUALLY_SELECTED_TAB =
111 "DialtactsActivity_last_manually_selected_tab";
112 private static final int PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT = TAB_INDEX_DIALER;
113
114 private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
115
116 public class ViewPagerAdapter extends FragmentPagerAdapter {
117 public ViewPagerAdapter(FragmentManager fm) {
118 super(fm);
119 }
120
121 @Override
122 public Fragment getItem(int position) {
123 switch (position) {
124 case TAB_INDEX_DIALER:
125 return new DialpadFragment();
126 case TAB_INDEX_CALL_LOG:
127 return new CallLogFragment();
128 case TAB_INDEX_FAVORITES:
129 return new PhoneFavoriteFragment();
130 }
131 throw new IllegalStateException("No fragment at position " + position);
132 }
133
134 @Override
135 public void setPrimaryItem(ViewGroup container, int position, Object object) {
136 // The parent's setPrimaryItem() also calls setMenuVisibility(), so we want to know
137 // when it happens.
138 if (DEBUG) {
139 Log.d(TAG, "FragmentPagerAdapter#setPrimaryItem(), position: " + position);
140 }
141 super.setPrimaryItem(container, position, object);
142 }
143
144 @Override
145 public int getCount() {
146 return TAB_INDEX_COUNT;
147 }
148 }
149
150 /**
151 * True when the app detects user's drag event. This variable should not become true when
152 * mUserTabClick is true.
153 *
154 * During user's drag or tab click, we shouldn't show fake buttons but just show real
155 * ActionBar at the bottom of the screen, for transition animation.
156 */
157 boolean mDuringSwipe = false;
158 /**
159 * True when the app detects user's tab click (at the top of the screen). This variable should
160 * not become true when mDuringSwipe is true.
161 *
162 * During user's drag or tab click, we shouldn't show fake buttons but just show real
163 * ActionBar at the bottom of the screen, for transition animation.
164 */
165 boolean mUserTabClick = false;
166
167 private class PageChangeListener implements OnPageChangeListener {
168 private int mCurrentPosition = -1;
169 /**
170 * Used during page migration, to remember the next position {@link #onPageSelected(int)}
171 * specified.
172 */
173 private int mNextPosition = -1;
174
175 @Override
176 public void onPageScrolled(
177 int position, float positionOffset, int positionOffsetPixels) {
178 }
179
180 @Override
181 public void onPageSelected(int position) {
182 if (DEBUG) Log.d(TAG, "onPageSelected: position: " + position);
183 final ActionBar actionBar = getActionBar();
184 if (mDialpadFragment != null) {
185 if (mDuringSwipe && position == TAB_INDEX_DIALER) {
186 // TODO: Figure out if we want this or not. Right now
187 // - with this call, both fake buttons and real action bar overlap
188 // - without this call, there's tiny flicker happening to search/menu buttons.
189 // If we can reduce the flicker without this call, it would be much better.
190 // updateFakeMenuButtonsVisibility(true);
191 }
192 }
193
194 if (mCurrentPosition == position) {
195 Log.w(TAG, "Previous position and next position became same (" + position + ")");
196 }
197
198 actionBar.selectTab(actionBar.getTabAt(position));
199 mNextPosition = position;
200 }
201
202 public void setCurrentPosition(int position) {
203 mCurrentPosition = position;
204 }
205
206 public int getCurrentPosition() {
207 return mCurrentPosition;
208 }
209
210 @Override
211 public void onPageScrollStateChanged(int state) {
212 switch (state) {
213 case ViewPager.SCROLL_STATE_IDLE: {
214 if (mNextPosition == -1) {
215 // This happens when the user drags the screen just after launching the
216 // application, and settle down the same screen without actually swiping it.
217 // At that moment mNextPosition is apparently -1 yet, and we expect it
218 // being updated by onPageSelected(), which is *not* called if the user
219 // settle down the exact same tab after the dragging.
220 if (DEBUG) {
221 Log.d(TAG, "Next position is not specified correctly. Use current tab ("
222 + mViewPager.getCurrentItem() + ")");
223 }
224 mNextPosition = mViewPager.getCurrentItem();
225 }
226 if (DEBUG) {
227 Log.d(TAG, "onPageScrollStateChanged() with SCROLL_STATE_IDLE. "
228 + "mCurrentPosition: " + mCurrentPosition
229 + ", mNextPosition: " + mNextPosition);
230 }
231 // Interpret IDLE as the end of migration (both swipe and tab click)
232 mDuringSwipe = false;
233 mUserTabClick = false;
234
235 updateFakeMenuButtonsVisibility(mNextPosition == TAB_INDEX_DIALER);
236 sendFragmentVisibilityChange(mCurrentPosition, false);
237 sendFragmentVisibilityChange(mNextPosition, true);
238
239 invalidateOptionsMenu();
240
241 mCurrentPosition = mNextPosition;
242 break;
243 }
244 case ViewPager.SCROLL_STATE_DRAGGING: {
245 if (DEBUG) Log.d(TAG, "onPageScrollStateChanged() with SCROLL_STATE_DRAGGING");
246 mDuringSwipe = true;
247 mUserTabClick = false;
248 break;
249 }
250 case ViewPager.SCROLL_STATE_SETTLING: {
251 if (DEBUG) Log.d(TAG, "onPageScrollStateChanged() with SCROLL_STATE_SETTLING");
252 mDuringSwipe = true;
253 mUserTabClick = false;
254 break;
255 }
256 default:
257 break;
258 }
259 }
260 }
261
262 private String mFilterText;
263
264 /** Enables horizontal swipe between Fragments. */
265 private ViewPager mViewPager;
266 private final PageChangeListener mPageChangeListener = new PageChangeListener();
267 private DialpadFragment mDialpadFragment;
268 private CallLogFragment mCallLogFragment;
269 private PhoneFavoriteFragment mPhoneFavoriteFragment;
270
271 private View mSearchButton;
272 private View mMenuButton;
273
274 private final ContactListFilterListener mContactListFilterListener =
275 new ContactListFilterListener() {
276 @Override
277 public void onContactListFilterChanged() {
278 boolean doInvalidateOptionsMenu = false;
279
280 if (mPhoneFavoriteFragment != null && mPhoneFavoriteFragment.isAdded()) {
281 mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter());
282 doInvalidateOptionsMenu = true;
283 }
284
285 if (mSearchFragment != null && mSearchFragment.isAdded()) {
286 mSearchFragment.setFilter(mContactListFilterController.getFilter());
287 doInvalidateOptionsMenu = true;
288 } else {
289 Log.w(TAG, "Search Fragment isn't available when ContactListFilter is changed");
290 }
291
292 if (doInvalidateOptionsMenu) {
293 invalidateOptionsMenu();
294 }
295 }
296 };
297
298 private final TabListener mTabListener = new TabListener() {
299 @Override
300 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
301 if (DEBUG) Log.d(TAG, "onTabUnselected(). tab: " + tab);
302 }
303
304 @Override
305 public void onTabSelected(Tab tab, FragmentTransaction ft) {
306 if (DEBUG) {
307 Log.d(TAG, "onTabSelected(). tab: " + tab + ", mDuringSwipe: " + mDuringSwipe);
308 }
309 // When the user swipes the screen horizontally, this method will be called after
310 // ViewPager.SCROLL_STATE_DRAGGING and ViewPager.SCROLL_STATE_SETTLING events, while
311 // when the user clicks a tab at the ActionBar at the top, this will be called before
312 // them. This logic interprets the order difference as a difference of the user action.
313 if (!mDuringSwipe) {
314 if (DEBUG) {
315 Log.d(TAG, "Tab select. from: " + mPageChangeListener.getCurrentPosition()
316 + ", to: " + tab.getPosition());
317 }
318 if (mDialpadFragment != null) {
319 updateFakeMenuButtonsVisibility(tab.getPosition() == TAB_INDEX_DIALER);
320 }
321 mUserTabClick = true;
322 }
323
324 if (mViewPager.getCurrentItem() != tab.getPosition()) {
325 mViewPager.setCurrentItem(tab.getPosition(), true);
326 }
327
328 // During the call, we don't remember the tab position.
329 if (!DialpadFragment.phoneIsInUse()) {
330 // Remember this tab index. This function is also called, if the tab is set
331 // automatically in which case the setter (setCurrentTab) has to set this to its old
332 // value afterwards
333 mLastManuallySelectedFragment = tab.getPosition();
334 }
335 }
336
337 @Override
338 public void onTabReselected(Tab tab, FragmentTransaction ft) {
339 if (DEBUG) Log.d(TAG, "onTabReselected");
340 }
341 };
342
343 /**
344 * Fragment for searching phone numbers. Unlike the other Fragments, this doesn't correspond
345 * to tab but is shown by a search action.
346 */
347 private PhoneNumberPickerFragment mSearchFragment;
348 /**
349 * True when this Activity is in its search UI (with a {@link SearchView} and
350 * {@link PhoneNumberPickerFragment}).
351 */
352 private boolean mInSearchUi;
353 private SearchView mSearchView;
354
355 private final OnClickListener mFilterOptionClickListener = new OnClickListener() {
356 @Override
357 public void onClick(View view) {
358 final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
359 final Menu menu = popupMenu.getMenu();
360 popupMenu.inflate(R.menu.dialtacts_search_options);
361 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option);
362 filterOptionMenuItem.setOnMenuItemClickListener(mFilterOptionsMenuItemClickListener);
363 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact);
364 addContactOptionMenuItem.setIntent(
365 new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
366 popupMenu.show();
367 }
368 };
369
370 /**
371 * The index of the Fragment (or, the tab) that has last been manually selected.
372 * This value does not keep track of programmatically set Tabs (e.g. Call Log after a Call)
373 */
374 private int mLastManuallySelectedFragment;
375
376 private ContactListFilterController mContactListFilterController;
377 private OnMenuItemClickListener mFilterOptionsMenuItemClickListener =
378 new OnMenuItemClickListener() {
379 @Override
380 public boolean onMenuItemClick(MenuItem item) {
381 AccountFilterUtil.startAccountFilterActivityForResult(
382 DialtactsActivity.this, SUBACTIVITY_ACCOUNT_FILTER,
383 mContactListFilterController.getFilter());
384 return true;
385 }
386 };
387
388 private OnMenuItemClickListener mSearchMenuItemClickListener =
389 new OnMenuItemClickListener() {
390 @Override
391 public boolean onMenuItemClick(MenuItem item) {
392 enterSearchUi();
393 return true;
394 }
395 };
396
397 /**
398 * Listener used when one of phone numbers in search UI is selected. This will initiate a
399 * phone call using the phone number.
400 */
401 private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
402 new OnPhoneNumberPickerActionListener() {
403 @Override
404 public void onPickPhoneNumberAction(Uri dataUri) {
405 // Specify call-origin so that users will see the previous tab instead of
406 // CallLog screen (search UI will be automatically exited).
407 PhoneNumberInteraction.startInteractionForPhoneCall(
408 DialtactsActivity.this, dataUri, getCallOrigin());
409 }
410
411 @Override
412 public void onShortcutIntentCreated(Intent intent) {
413 Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
414 }
415
416 @Override
417 public void onHomeInActionBarSelected() {
418 exitSearchUi();
419 }
420 };
421
422 /**
423 * Listener used to send search queries to the phone search fragment.
424 */
425 private final OnQueryTextListener mPhoneSearchQueryTextListener =
426 new OnQueryTextListener() {
427 @Override
428 public boolean onQueryTextSubmit(String query) {
429 View view = getCurrentFocus();
430 if (view != null) {
431 hideInputMethod(view);
432 view.clearFocus();
433 }
434 return true;
435 }
436
437 @Override
438 public boolean onQueryTextChange(String newText) {
439 // Show search result with non-empty text. Show a bare list otherwise.
440 if (mSearchFragment != null) {
441 mSearchFragment.setQueryString(newText, true);
442 }
443 return true;
444 }
445 };
446
447 /**
448 * Listener used to handle the "close" button on the right side of {@link SearchView}.
449 * If some text is in the search view, this will clean it up. Otherwise this will exit
450 * the search UI and let users go back to usual Phone UI.
451 *
452 * This does _not_ handle back button.
453 */
454 private final OnCloseListener mPhoneSearchCloseListener =
455 new OnCloseListener() {
456 @Override
457 public boolean onClose() {
458 if (!TextUtils.isEmpty(mSearchView.getQuery())) {
459 mSearchView.setQuery(null, true);
460 }
461 return true;
462 }
463 };
464
465 private final View.OnLayoutChangeListener mFirstLayoutListener
466 = new View.OnLayoutChangeListener() {
467 @Override
468 public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
469 int oldTop, int oldRight, int oldBottom) {
470 v.removeOnLayoutChangeListener(this); // Unregister self.
471 addSearchFragment();
472 }
473 };
474
475 @Override
476 protected void onCreate(Bundle icicle) {
477 super.onCreate(icicle);
478
479 final Intent intent = getIntent();
480 fixIntent(intent);
481
482 setContentView(R.layout.dialtacts_activity);
483
484 mContactListFilterController = ContactListFilterController.getInstance(this);
485 mContactListFilterController.addListener(mContactListFilterListener);
486
487 findViewById(R.id.dialtacts_frame).addOnLayoutChangeListener(mFirstLayoutListener);
488
489 mViewPager = (ViewPager) findViewById(R.id.pager);
490 mViewPager.setAdapter(new ViewPagerAdapter(getFragmentManager()));
491 mViewPager.setOnPageChangeListener(mPageChangeListener);
492 mViewPager.setOffscreenPageLimit(2);
493
494 // Do same width calculation as ActionBar does
495 DisplayMetrics dm = getResources().getDisplayMetrics();
496 int minCellSize = getResources().getDimensionPixelSize(R.dimen.fake_menu_button_min_width);
497 int cellCount = dm.widthPixels / minCellSize;
498 int fakeMenuItemWidth = dm.widthPixels / cellCount;
499 if (DEBUG) Log.d(TAG, "The size of fake menu buttons (in pixel): " + fakeMenuItemWidth);
500
501 // Soft menu button should appear only when there's no hardware menu button.
502 mMenuButton = findViewById(R.id.overflow_menu);
503 if (mMenuButton != null) {
504 mMenuButton.setMinimumWidth(fakeMenuItemWidth);
505 if (ViewConfiguration.get(this).hasPermanentMenuKey()) {
506 // This is required for dialpad button's layout, so must not use GONE here.
507 mMenuButton.setVisibility(View.INVISIBLE);
508 } else {
509 mMenuButton.setOnClickListener(this);
510 }
511 }
512 mSearchButton = findViewById(R.id.searchButton);
513 if (mSearchButton != null) {
514 mSearchButton.setMinimumWidth(fakeMenuItemWidth);
515 mSearchButton.setOnClickListener(this);
516 }
517
518 // Setup the ActionBar tabs (the order matches the tab-index contants TAB_INDEX_*)
519 setupDialer();
520 setupCallLog();
521 setupFavorites();
522 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
523 getActionBar().setDisplayShowTitleEnabled(false);
524 getActionBar().setDisplayShowHomeEnabled(false);
525
526 // Load the last manually loaded tab
527 mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
528 mLastManuallySelectedFragment = mPrefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB,
529 PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT);
530 if (mLastManuallySelectedFragment >= TAB_INDEX_COUNT) {
531 // Stored value may have exceeded the number of current tabs. Reset it.
532 mLastManuallySelectedFragment = PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT;
533 }
534
535 setCurrentTab(intent);
536
537 if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
538 && icicle == null) {
539 setupFilterText(intent);
540 }
541 }
542
543 @Override
544 public void onStart() {
545 super.onStart();
546 if (mPhoneFavoriteFragment != null) {
547 mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter());
548 }
549 if (mSearchFragment != null) {
550 mSearchFragment.setFilter(mContactListFilterController.getFilter());
551 }
552
553 if (mDuringSwipe || mUserTabClick) {
554 if (DEBUG) Log.d(TAG, "reset buggy flag state..");
555 mDuringSwipe = false;
556 mUserTabClick = false;
557 }
558
559 final int currentPosition = mPageChangeListener.getCurrentPosition();
560 if (DEBUG) {
561 Log.d(TAG, "onStart(). current position: " + mPageChangeListener.getCurrentPosition()
562 + ". Reset all menu visibility state.");
563 }
564 updateFakeMenuButtonsVisibility(currentPosition == TAB_INDEX_DIALER && !mInSearchUi);
565 for (int i = 0; i < TAB_INDEX_COUNT; i++) {
566 sendFragmentVisibilityChange(i, i == currentPosition);
567 }
568 }
569
570 @Override
571 public void onDestroy() {
572 super.onDestroy();
573 mContactListFilterController.removeListener(mContactListFilterListener);
574 }
575
576 @Override
577 public void onClick(View view) {
578 switch (view.getId()) {
579 case R.id.searchButton: {
580 enterSearchUi();
581 break;
582 }
583 case R.id.overflow_menu: {
584 if (mDialpadFragment != null) {
585 PopupMenu popup = mDialpadFragment.constructPopupMenu(view);
586 if (popup != null) {
587 popup.show();
588 }
589 } else {
590 Log.w(TAG, "DialpadFragment is null during onClick() event for " + view);
591 }
592 break;
593 }
594 default: {
595 Log.wtf(TAG, "Unexpected onClick event from " + view);
596 break;
597 }
598 }
599 }
600
601 /**
602 * Add search fragment. Note this is called during onLayout, so there's some restrictions,
603 * such as executePendingTransaction can't be used in it.
604 */
605 private void addSearchFragment() {
606 // In order to take full advantage of "fragment deferred start", we need to create the
607 // search fragment after all other fragments are created.
608 // The other fragments are created by the ViewPager on the first onMeasure().
609 // We use the first onLayout call, which is after onMeasure().
610
611 // Just return if the fragment is already created, which happens after configuration
612 // changes.
613 if (mSearchFragment != null) return;
614
615 final FragmentTransaction ft = getFragmentManager().beginTransaction();
616 final Fragment searchFragment = new PhoneNumberPickerFragment();
617
618 searchFragment.setUserVisibleHint(false);
619 ft.add(R.id.dialtacts_frame, searchFragment);
620 ft.hide(searchFragment);
621 ft.commitAllowingStateLoss();
622 }
623
624 private void prepareSearchView() {
625 final View searchViewLayout =
626 getLayoutInflater().inflate(R.layout.dialtacts_custom_action_bar, null);
627 mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view);
628 mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
629 mSearchView.setOnCloseListener(mPhoneSearchCloseListener);
630 // Since we're using a custom layout for showing SearchView instead of letting the
631 // search menu icon do that job, we need to manually configure the View so it looks
632 // "shown via search menu".
633 // - it should be iconified by default
634 // - it should not be iconified at this time
635 // See also comments for onActionViewExpanded()/onActionViewCollapsed()
636 mSearchView.setIconifiedByDefault(true);
637 mSearchView.setQueryHint(getString(R.string.hint_findContacts));
638 mSearchView.setIconified(false);
639 mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
640 @Override
641 public void onFocusChange(View view, boolean hasFocus) {
642 if (hasFocus) {
643 showInputMethod(view.findFocus());
644 }
645 }
646 });
647
648 if (!ViewConfiguration.get(this).hasPermanentMenuKey()) {
649 // Filter option menu should be shown on the right side of SearchView.
650 final View filterOptionView = searchViewLayout.findViewById(R.id.search_option);
651 filterOptionView.setVisibility(View.VISIBLE);
652 filterOptionView.setOnClickListener(mFilterOptionClickListener);
653 }
654
655 getActionBar().setCustomView(searchViewLayout,
656 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
657 }
658
659 @Override
660 public void onAttachFragment(Fragment fragment) {
661 // This method can be called before onCreate(), at which point we cannot rely on ViewPager.
662 // In that case, we will setup the "current position" soon after the ViewPager is ready.
663 final int currentPosition = mViewPager != null ? mViewPager.getCurrentItem() : -1;
664
665 if (fragment instanceof DialpadFragment) {
666 mDialpadFragment = (DialpadFragment) fragment;
667 } else if (fragment instanceof CallLogFragment) {
668 mCallLogFragment = (CallLogFragment) fragment;
669 } else if (fragment instanceof PhoneFavoriteFragment) {
670 mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
671 mPhoneFavoriteFragment.setListener(mPhoneFavoriteListener);
672 if (mContactListFilterController != null
673 && mContactListFilterController.getFilter() != null) {
674 mPhoneFavoriteFragment.setFilter(mContactListFilterController.getFilter());
675 }
676 } else if (fragment instanceof PhoneNumberPickerFragment) {
677 mSearchFragment = (PhoneNumberPickerFragment) fragment;
678 mSearchFragment.setOnPhoneNumberPickerActionListener(mPhoneNumberPickerActionListener);
679 mSearchFragment.setQuickContactEnabled(true);
680 mSearchFragment.setDarkTheme(true);
681 mSearchFragment.setPhotoPosition(ContactListItemView.PhotoPosition.LEFT);
682 mSearchFragment.setUseCallableUri(true);
683 if (mContactListFilterController != null
684 && mContactListFilterController.getFilter() != null) {
685 mSearchFragment.setFilter(mContactListFilterController.getFilter());
686 }
687 // Here we assume that we're not on the search mode, so let's hide the fragment.
688 //
689 // We get here either when the fragment is created (normal case), or after configuration
690 // changes. In the former case, we're not in search mode because we can only
691 // enter search mode if the fragment is created. (see enterSearchUi())
692 // In the latter case we're not in search mode either because we don't retain
693 // mInSearchUi -- ideally we should but at this point it's not supported.
694 mSearchFragment.setUserVisibleHint(false);
695 // After configuration changes fragments will forget their "hidden" state, so make
696 // sure to hide it.
697 if (!mSearchFragment.isHidden()) {
698 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
699 transaction.hide(mSearchFragment);
700 transaction.commitAllowingStateLoss();
701 }
702 }
703 }
704
705 @Override
706 protected void onPause() {
707 super.onPause();
708
709 mPrefs.edit().putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedFragment)
710 .apply();
711 }
712
713 private void fixIntent(Intent intent) {
714 // This should be cleaned up: the call key used to send an Intent
715 // that just said to go to the recent calls list. It now sends this
716 // abstract action, but this class hasn't been rewritten to deal with it.
717 if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
718 intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
719 intent.putExtra("call_key", true);
720 setIntent(intent);
721 }
722 }
723
724 private void setupDialer() {
725 final Tab tab = getActionBar().newTab();
726 tab.setContentDescription(R.string.dialerIconLabel);
727 tab.setTabListener(mTabListener);
728 tab.setIcon(R.drawable.ic_tab_dialer);
729 getActionBar().addTab(tab);
730 }
731
732 private void setupCallLog() {
733 final Tab tab = getActionBar().newTab();
734 tab.setContentDescription(R.string.recentCallsIconLabel);
735 tab.setIcon(R.drawable.ic_tab_recent);
736 tab.setTabListener(mTabListener);
737 getActionBar().addTab(tab);
738 }
739
740 private void setupFavorites() {
741 final Tab tab = getActionBar().newTab();
742 tab.setContentDescription(R.string.contactsFavoritesLabel);
743 tab.setIcon(R.drawable.ic_tab_all);
744 tab.setTabListener(mTabListener);
745 getActionBar().addTab(tab);
746 }
747
748 /**
749 * Returns true if the intent is due to hitting the green send key (hardware call button:
750 * KEYCODE_CALL) while in a call.
751 *
752 * @param intent the intent that launched this activity
753 * @param recentCallsRequest true if the intent is requesting to view recent calls
754 * @return true if the intent is due to hitting the green send key while in a call
755 */
756 private boolean isSendKeyWhileInCall(final Intent intent,
757 final boolean recentCallsRequest) {
758 // If there is a call in progress go to the call screen
759 if (recentCallsRequest) {
760 final boolean callKey = intent.getBooleanExtra("call_key", false);
761
762 try {
763 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
764 if (callKey && phone != null && phone.showCallScreen()) {
765 return true;
766 }
767 } catch (RemoteException e) {
768 Log.e(TAG, "Failed to handle send while in call", e);
769 }
770 }
771
772 return false;
773 }
774
775 /**
776 * Sets the current tab based on the intent's request type
777 *
778 * @param intent Intent that contains information about which tab should be selected
779 */
780 private void setCurrentTab(Intent intent) {
781 // If we got here by hitting send and we're in call forward along to the in-call activity
782 boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
783 getContentResolver()));
784 if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
785 finish();
786 return;
787 }
788
789 // Remember the old manually selected tab index so that it can be restored if it is
790 // overwritten by one of the programmatic tab selections
791 final int savedTabIndex = mLastManuallySelectedFragment;
792
793 final int tabIndex;
794 if (DialpadFragment.phoneIsInUse() || isDialIntent(intent)) {
795 tabIndex = TAB_INDEX_DIALER;
796 } else if (recentCallsRequest) {
797 tabIndex = TAB_INDEX_CALL_LOG;
798 } else {
799 tabIndex = mLastManuallySelectedFragment;
800 }
801
802 final int previousItemIndex = mViewPager.getCurrentItem();
803 mViewPager.setCurrentItem(tabIndex, false /* smoothScroll */);
804 if (previousItemIndex != tabIndex) {
805 sendFragmentVisibilityChange(previousItemIndex, false /* not visible */ );
806 }
807 mPageChangeListener.setCurrentPosition(tabIndex);
808 sendFragmentVisibilityChange(tabIndex, true /* visible */ );
809
810 // Restore to the previous manual selection
811 mLastManuallySelectedFragment = savedTabIndex;
812 mDuringSwipe = false;
813 mUserTabClick = false;
814 }
815
816 @Override
817 public void onNewIntent(Intent newIntent) {
818 setIntent(newIntent);
819 fixIntent(newIntent);
820 setCurrentTab(newIntent);
821 final String action = newIntent.getAction();
822 if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
823 setupFilterText(newIntent);
824 }
825 if (mInSearchUi || (mSearchFragment != null && mSearchFragment.isVisible())) {
826 exitSearchUi();
827 }
828
829 if (mViewPager.getCurrentItem() == TAB_INDEX_DIALER) {
830 if (mDialpadFragment != null) {
Chiao Cheng291e9762012-11-05 18:27:03 -0800831 mDialpadFragment.setStartedFromNewIntent(true);
Chiao Cheng94b10b52012-08-17 16:59:12 -0700832 } else {
833 Log.e(TAG, "DialpadFragment isn't ready yet when the tab is already selected.");
834 }
835 } else if (mViewPager.getCurrentItem() == TAB_INDEX_CALL_LOG) {
836 if (mCallLogFragment != null) {
837 mCallLogFragment.configureScreenFromIntent(newIntent);
838 } else {
839 Log.e(TAG, "CallLogFragment isn't ready yet when the tab is already selected.");
840 }
841 }
842 invalidateOptionsMenu();
843 }
844
845 /** Returns true if the given intent contains a phone number to populate the dialer with */
846 private boolean isDialIntent(Intent intent) {
847 final String action = intent.getAction();
848 if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
849 return true;
850 }
851 if (Intent.ACTION_VIEW.equals(action)) {
852 final Uri data = intent.getData();
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700853 if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
Chiao Cheng94b10b52012-08-17 16:59:12 -0700854 return true;
855 }
856 }
857 return false;
858 }
859
860 /**
861 * Returns an appropriate call origin for this Activity. May return null when no call origin
862 * should be used (e.g. when some 3rd party application launched the screen. Call origin is
863 * for remembering the tab in which the user made a phone call, so the external app's DIAL
864 * request should not be counted.)
865 */
866 public String getCallOrigin() {
867 return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
868 }
869
870 /**
871 * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
872 * This text originally came from a FILTER_CONTACTS_ACTION intent received
873 * by this activity. The stored text will then be cleared after after this
874 * method returns.
875 *
876 * @return The stored filter text
877 */
878 public String getAndClearFilterText() {
879 String filterText = mFilterText;
880 mFilterText = null;
881 return filterText;
882 }
883
884 /**
885 * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
886 * This is so child activities can check if they are supposed to display a filter.
887 *
888 * @param intent The intent received in {@link #onNewIntent(Intent)}
889 */
890 private void setupFilterText(Intent intent) {
891 // If the intent was relaunched from history, don't apply the filter text.
892 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
893 return;
894 }
895 String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
896 if (filter != null && filter.length() > 0) {
897 mFilterText = filter;
898 }
899 }
900
901 @Override
902 public void onBackPressed() {
903 if (mInSearchUi) {
904 // We should let the user go back to usual screens with tabs.
905 exitSearchUi();
906 } else if (isTaskRoot()) {
907 // Instead of stopping, simply push this to the back of the stack.
908 // This is only done when running at the top of the stack;
909 // otherwise, we have been launched by someone else so need to
910 // allow the user to go back to the caller.
911 moveTaskToBack(false);
912 } else {
913 super.onBackPressed();
914 }
915 }
916
917 private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
918 new PhoneFavoriteFragment.Listener() {
919 @Override
920 public void onContactSelected(Uri contactUri) {
921 PhoneNumberInteraction.startInteractionForPhoneCall(
922 DialtactsActivity.this, contactUri, getCallOrigin());
923 }
924
925 @Override
926 public void onCallNumberDirectly(String phoneNumber) {
Chiao Cheng9d4f3b22012-09-05 16:00:16 -0700927 Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
Chiao Cheng94b10b52012-08-17 16:59:12 -0700928 startActivity(intent);
929 }
930 };
931
932 @Override
933 public boolean onCreateOptionsMenu(Menu menu) {
934 MenuInflater inflater = getMenuInflater();
935 inflater.inflate(R.menu.dialtacts_options, menu);
936
937 // set up intents and onClick listeners
938 final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings);
939 final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar);
940 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option);
941 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact);
942
943 callSettingsMenuItem.setIntent(DialtactsActivity.getCallSettingsIntent());
944 searchMenuItem.setOnMenuItemClickListener(mSearchMenuItemClickListener);
945 filterOptionMenuItem.setOnMenuItemClickListener(mFilterOptionsMenuItemClickListener);
946 addContactOptionMenuItem.setIntent(
947 new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
948
949 return true;
950 }
951
952 @Override
953 public boolean onPrepareOptionsMenu(Menu menu) {
954 if (mInSearchUi) {
955 prepareOptionsMenuInSearchMode(menu);
956 } else {
957 // get reference to the currently selected tab
958 final Tab tab = getActionBar().getSelectedTab();
959 if (tab != null) {
960 switch(tab.getPosition()) {
961 case TAB_INDEX_DIALER:
962 prepareOptionsMenuForDialerTab(menu);
963 break;
964 case TAB_INDEX_CALL_LOG:
965 prepareOptionsMenuForCallLogTab(menu);
966 break;
967 case TAB_INDEX_FAVORITES:
968 prepareOptionsMenuForFavoritesTab(menu);
969 break;
970 }
971 }
972 }
973 return true;
974 }
975
976 private void prepareOptionsMenuInSearchMode(Menu menu) {
977 // get references to menu items
978 final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar);
979 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option);
980 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact);
981 final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings);
982 final MenuItem emptyRightMenuItem = menu.findItem(R.id.empty_right_menu_item);
983
984 // prepare the menu items
985 searchMenuItem.setVisible(false);
986 filterOptionMenuItem.setVisible(ViewConfiguration.get(this).hasPermanentMenuKey());
987 addContactOptionMenuItem.setVisible(false);
988 callSettingsMenuItem.setVisible(false);
989 emptyRightMenuItem.setVisible(false);
990 }
991
992 private void prepareOptionsMenuForDialerTab(Menu menu) {
993 if (DEBUG) {
994 Log.d(TAG, "onPrepareOptionsMenu(dialer). swipe: " + mDuringSwipe
995 + ", user tab click: " + mUserTabClick);
996 }
997
998 // get references to menu items
999 final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar);
1000 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option);
1001 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact);
1002 final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings);
1003 final MenuItem emptyRightMenuItem = menu.findItem(R.id.empty_right_menu_item);
1004
1005 // prepare the menu items
1006 filterOptionMenuItem.setVisible(false);
1007 addContactOptionMenuItem.setVisible(false);
1008 if (mDuringSwipe || mUserTabClick) {
1009 // During horizontal movement, the real ActionBar menu items are shown
1010 searchMenuItem.setVisible(true);
1011 callSettingsMenuItem.setVisible(true);
1012 // When there is a permanent menu key, there is no overflow icon on the right of
1013 // the action bar which would force the search menu item (if it is visible) to the
1014 // left. This is the purpose of showing the emptyRightMenuItem.
1015 emptyRightMenuItem.setVisible(ViewConfiguration.get(this).hasPermanentMenuKey());
1016 } else {
1017 // This is when the user is looking at the dialer pad. In this case, the real
1018 // ActionBar is hidden and fake menu items are shown.
1019 // Except in landscape, in which case the real search menu item is shown.
Chiao Cheng35071c02012-10-15 18:36:24 -07001020 searchMenuItem.setVisible(OrientationUtil.isLandscape(this));
Chiao Cheng94b10b52012-08-17 16:59:12 -07001021 // If a permanent menu key is available, then we need to show the call settings item
1022 // so that the call settings item can be invoked by the permanent menu key.
1023 callSettingsMenuItem.setVisible(ViewConfiguration.get(this).hasPermanentMenuKey());
1024 emptyRightMenuItem.setVisible(false);
1025 }
1026 }
1027
1028 private void prepareOptionsMenuForCallLogTab(Menu menu) {
1029 // get references to menu items
1030 final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar);
1031 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option);
1032 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact);
1033 final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings);
1034 final MenuItem emptyRightMenuItem = menu.findItem(R.id.empty_right_menu_item);
1035
1036 // prepare the menu items
1037 searchMenuItem.setVisible(true);
1038 filterOptionMenuItem.setVisible(false);
1039 addContactOptionMenuItem.setVisible(false);
1040 callSettingsMenuItem.setVisible(true);
1041 emptyRightMenuItem.setVisible(ViewConfiguration.get(this).hasPermanentMenuKey());
1042 }
1043
1044 private void prepareOptionsMenuForFavoritesTab(Menu menu) {
1045 // get references to menu items
1046 final MenuItem searchMenuItem = menu.findItem(R.id.search_on_action_bar);
1047 final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option);
1048 final MenuItem addContactOptionMenuItem = menu.findItem(R.id.add_contact);
1049 final MenuItem callSettingsMenuItem = menu.findItem(R.id.menu_call_settings);
1050 final MenuItem emptyRightMenuItem = menu.findItem(R.id.empty_right_menu_item);
1051
1052 // prepare the menu items
1053 searchMenuItem.setVisible(true);
1054 filterOptionMenuItem.setVisible(true);
1055 addContactOptionMenuItem.setVisible(true);
1056 callSettingsMenuItem.setVisible(true);
1057 emptyRightMenuItem.setVisible(false);
1058 }
1059
1060 @Override
1061 public void startSearch(String initialQuery, boolean selectInitialQuery,
1062 Bundle appSearchData, boolean globalSearch) {
1063 if (mSearchFragment != null && mSearchFragment.isAdded() && !globalSearch) {
1064 if (mInSearchUi) {
1065 if (mSearchView.hasFocus()) {
1066 showInputMethod(mSearchView.findFocus());
1067 } else {
1068 mSearchView.requestFocus();
1069 }
1070 } else {
1071 enterSearchUi();
1072 }
1073 } else {
1074 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1075 }
1076 }
1077
1078 /**
1079 * Hides every tab and shows search UI for phone lookup.
1080 */
1081 private void enterSearchUi() {
1082 if (mSearchFragment == null) {
1083 // We add the search fragment dynamically in the first onLayoutChange() and
1084 // mSearchFragment is set sometime later when the fragment transaction is actually
1085 // executed, which means there's a window when users are able to hit the (physical)
1086 // search key but mSearchFragment is still null.
1087 // It's quite hard to handle this case right, so let's just ignore the search key
1088 // in this case. Users can just hit it again and it will work this time.
1089 return;
1090 }
1091 if (mSearchView == null) {
1092 prepareSearchView();
1093 }
1094
1095 final ActionBar actionBar = getActionBar();
1096
1097 final Tab tab = actionBar.getSelectedTab();
1098
1099 // User can search during the call, but we don't want to remember the status.
1100 if (tab != null && !DialpadFragment.phoneIsInUse()) {
1101 mLastManuallySelectedFragment = tab.getPosition();
1102 }
1103
1104 mSearchView.setQuery(null, true);
1105
1106 actionBar.setDisplayShowCustomEnabled(true);
1107 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
1108 actionBar.setDisplayShowHomeEnabled(true);
1109 actionBar.setDisplayHomeAsUpEnabled(true);
1110
1111 updateFakeMenuButtonsVisibility(false);
1112
1113 for (int i = 0; i < TAB_INDEX_COUNT; i++) {
1114 sendFragmentVisibilityChange(i, false /* not visible */ );
1115 }
1116
1117 // Show the search fragment and hide everything else.
1118 mSearchFragment.setUserVisibleHint(true);
1119 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
1120 transaction.show(mSearchFragment);
1121 transaction.commitAllowingStateLoss();
1122 mViewPager.setVisibility(View.GONE);
1123
1124 // We need to call this and onActionViewCollapsed() manually, since we are using a custom
1125 // layout instead of asking the search menu item to take care of SearchView.
1126 mSearchView.onActionViewExpanded();
1127 mInSearchUi = true;
1128 }
1129
1130 private void showInputMethod(View view) {
1131 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
1132 if (imm != null) {
1133 if (!imm.showSoftInput(view, 0)) {
1134 Log.w(TAG, "Failed to show soft input method.");
1135 }
1136 }
1137 }
1138
1139 private void hideInputMethod(View view) {
1140 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
1141 if (imm != null && view != null) {
1142 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
1143 }
1144 }
1145
1146 /**
1147 * Goes back to usual Phone UI with tags. Previously selected Tag and associated Fragment
1148 * should be automatically focused again.
1149 */
1150 private void exitSearchUi() {
1151 final ActionBar actionBar = getActionBar();
1152
1153 // Hide the search fragment, if exists.
1154 if (mSearchFragment != null) {
1155 mSearchFragment.setUserVisibleHint(false);
1156
1157 final FragmentTransaction transaction = getFragmentManager().beginTransaction();
1158 transaction.hide(mSearchFragment);
1159 transaction.commitAllowingStateLoss();
1160 }
1161
1162 // We want to hide SearchView and show Tabs. Also focus on previously selected one.
1163 actionBar.setDisplayShowCustomEnabled(false);
1164 actionBar.setDisplayShowHomeEnabled(false);
1165 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
1166
1167 for (int i = 0; i < TAB_INDEX_COUNT; i++) {
1168 sendFragmentVisibilityChange(i, i == mViewPager.getCurrentItem());
1169 }
1170
1171 // Before exiting the search screen, reset swipe state.
1172 mDuringSwipe = false;
1173 mUserTabClick = false;
1174
1175 mViewPager.setVisibility(View.VISIBLE);
1176
1177 hideInputMethod(getCurrentFocus());
1178
1179 // Request to update option menu.
1180 invalidateOptionsMenu();
1181
1182 // See comments in onActionViewExpanded()
1183 mSearchView.onActionViewCollapsed();
1184 mInSearchUi = false;
1185 }
1186
1187 private Fragment getFragmentAt(int position) {
1188 switch (position) {
1189 case TAB_INDEX_DIALER:
1190 return mDialpadFragment;
1191 case TAB_INDEX_CALL_LOG:
1192 return mCallLogFragment;
1193 case TAB_INDEX_FAVORITES:
1194 return mPhoneFavoriteFragment;
1195 default:
1196 throw new IllegalStateException("Unknown fragment index: " + position);
1197 }
1198 }
1199
1200 private void sendFragmentVisibilityChange(int position, boolean visibility) {
1201 if (DEBUG) {
1202 Log.d(TAG, "sendFragmentVisibiltyChange(). position: " + position
1203 + ", visibility: " + visibility);
1204 }
1205 // Position can be -1 initially. See PageChangeListener.
1206 if (position >= 0) {
1207 final Fragment fragment = getFragmentAt(position);
1208 if (fragment != null) {
1209 fragment.setMenuVisibility(visibility);
1210 fragment.setUserVisibleHint(visibility);
1211 }
1212 }
1213 }
1214
1215 /**
1216 * Update visibility of the search button and menu button at the bottom.
1217 * They should be invisible when bottom ActionBar's real items are available, and be visible
1218 * otherwise.
1219 *
1220 * @param visible True when visible.
1221 */
1222 private void updateFakeMenuButtonsVisibility(boolean visible) {
1223 // Note: Landscape mode does not have the fake menu and search buttons.
1224 if (DEBUG) {
1225 Log.d(TAG, "updateFakeMenuButtonVisibility(" + visible + ")");
1226 }
1227
1228 if (mSearchButton != null) {
1229 if (visible) {
1230 mSearchButton.setVisibility(View.VISIBLE);
1231 } else {
1232 mSearchButton.setVisibility(View.INVISIBLE);
1233 }
1234 }
1235 if (mMenuButton != null) {
1236 if (visible && !ViewConfiguration.get(this).hasPermanentMenuKey()) {
1237 mMenuButton.setVisibility(View.VISIBLE);
1238 } else {
1239 mMenuButton.setVisibility(View.INVISIBLE);
1240 }
1241 }
1242 }
1243
1244 /** Returns an Intent to launch Call Settings screen */
1245 public static Intent getCallSettingsIntent() {
1246 final Intent intent = new Intent(Intent.ACTION_MAIN);
1247 intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
1248 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
1249 return intent;
1250 }
1251
1252 @Override
1253 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1254 if (resultCode != Activity.RESULT_OK) {
1255 return;
1256 }
1257 switch (requestCode) {
1258 case SUBACTIVITY_ACCOUNT_FILTER: {
1259 AccountFilterUtil.handleAccountFilterResult(
1260 mContactListFilterController, resultCode, data);
1261 }
1262 break;
1263 }
1264 }
1265}