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