DialtactsActivity.java revision fce269a30e2ec112ea4c287d97e08ef7b3b31b89
1/*
2 * Copyright (C) 2013 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.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
22import android.app.Activity;
23import android.app.backup.BackupManager;
24import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
27import android.content.ActivityNotFoundException;
28import android.content.Context;
29import android.content.Intent;
30import android.content.SharedPreferences;
31import android.content.res.Resources;
32import android.net.Uri;
33import android.os.Bundle;
34import android.os.RemoteException;
35import android.os.ServiceManager;
36import android.provider.CallLog.Calls;
37import android.provider.ContactsContract;
38import android.provider.ContactsContract.Contacts;
39import android.provider.ContactsContract.Intents.UI;
40import android.provider.Settings;
41import android.speech.RecognizerIntent;
42import android.support.v4.app.NavUtils;
43import android.telephony.TelephonyManager;
44import android.text.Editable;
45import android.text.TextUtils;
46import android.text.TextWatcher;
47import android.util.Log;
48import android.view.Menu;
49import android.view.MenuItem;
50import android.view.View;
51import android.view.View.OnFocusChangeListener;
52import android.view.ViewConfiguration;
53import android.view.inputmethod.InputMethodManager;
54import android.widget.AbsListView.OnScrollListener;
55import android.widget.EditText;
56import android.widget.ImageView;
57import android.widget.PopupMenu;
58import android.widget.SearchView;
59import android.widget.SearchView.OnCloseListener;
60import android.widget.SearchView.OnQueryTextListener;
61import android.widget.Toast;
62
63import com.android.contacts.common.CallUtil;
64import com.android.contacts.common.activity.TransactionSafeActivity;
65import com.android.contacts.common.dialog.ClearFrequentsDialog;
66import com.android.contacts.common.interactions.ImportExportDialogFragment;
67import com.android.contacts.common.list.ContactListItemView;
68import com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
69import com.android.contacts.common.list.PhoneNumberPickerFragment;
70import com.android.dialer.calllog.CallLogActivity;
71import com.android.dialer.database.DialerDatabaseHelper;
72import com.android.dialer.dialpad.DialpadFragment;
73import com.android.dialer.dialpad.SmartDialNameMatcher;
74import com.android.dialer.dialpad.SmartDialPrefix;
75import com.android.dialer.interactions.PhoneNumberInteraction;
76import com.android.dialer.list.AllContactsActivity;
77import com.android.dialer.list.PhoneFavoriteFragment;
78import com.android.dialer.list.OnListFragmentScrolledListener;
79import com.android.dialer.list.SmartDialSearchFragment;
80import com.android.internal.telephony.ITelephony;
81
82import java.util.ArrayList;
83
84/**
85 * The dialer tab's title is 'phone', a more common name (see strings.xml).
86 */
87public class DialtactsActivity extends TransactionSafeActivity implements View.OnClickListener,
88        DialpadFragment.OnDialpadQueryChangedListener, PopupMenu.OnMenuItemClickListener,
89        OnListFragmentScrolledListener,
90        PhoneFavoriteFragment.OnPhoneFavoriteFragmentStartedListener,
91        DialpadFragment.OnDialpadFragmentStartedListener,
92        PhoneFavoriteFragment.OnShowAllContactsListener {
93    private static final String TAG = "DialtactsActivity";
94
95    public static final boolean DEBUG = false;
96
97    public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
98
99    /** Used to open Call Setting */
100    private static final String PHONE_PACKAGE = "com.android.phone";
101    private static final String CALL_SETTINGS_CLASS_NAME =
102            "com.android.phone.CallFeaturesSetting";
103    /** @see #getCallOrigin() */
104    private static final String CALL_ORIGIN_DIALTACTS =
105            "com.android.dialer.DialtactsActivity";
106
107    private static final String KEY_IN_SEARCH_UI = "in_search_ui";
108    private static final String KEY_SEARCH_QUERY = "search_query";
109    private static final String KEY_FIRST_LAUNCH = "first_launch";
110
111    private static final String TAG_DIALPAD_FRAGMENT = "dialpad";
112    private static final String TAG_REGULAR_SEARCH_FRAGMENT = "search";
113    private static final String TAG_SMARTDIAL_SEARCH_FRAGMENT = "smartdial";
114    private static final String TAG_FAVORITES_FRAGMENT = "favorites";
115
116    /**
117     * Just for backward compatibility. Should behave as same as {@link Intent#ACTION_DIAL}.
118     */
119    private static final String ACTION_TOUCH_DIALER = "com.android.phone.action.TOUCH_DIALER";
120
121    private static final int SUBACTIVITY_ACCOUNT_FILTER = 1;
122
123    private static final int ACTIVITY_REQUEST_CODE_VOICE_SEARCH = 1;
124
125    private String mFilterText;
126
127    /**
128     * The main fragment displaying the user's favorites and frequent contacts
129     */
130    private PhoneFavoriteFragment mPhoneFavoriteFragment;
131
132    /**
133     * Fragment containing the dialpad that slides into view
134     */
135    private DialpadFragment mDialpadFragment;
136
137    /**
138     * Fragment for searching phone numbers using the alphanumeric keyboard.
139     */
140    private SearchFragment mRegularSearchFragment;
141
142    /**
143     * Fragment for searching phone numbers using the dialpad.
144     */
145    private SmartDialSearchFragment mSmartDialSearchFragment;
146
147    private View mMenuButton;
148    private View mCallHistoryButton;
149    private View mDialpadButton;
150
151    // Padding view used to shift the fragments up when the dialpad is shown.
152    private View mBottomPaddingView;
153
154    /**
155     * True when this Activity is in its search UI (with a {@link SearchView} and
156     * {@link PhoneNumberPickerFragment}).
157     */
158    private boolean mInSearchUi;
159    /**
160     * True when this activity has been launched for the first time.
161     */
162    private boolean mFirstLaunch;
163    private View mSearchViewContainer;
164    private View mSearchViewCloseButton;
165    private View mVoiceSearchButton;
166    private EditText mSearchView;
167
168    private String mSearchQuery;
169
170    private DialerDatabaseHelper mDialerDatabaseHelper;
171
172    /**
173     * Listener used when one of phone numbers in search UI is selected. This will initiate a
174     * phone call using the phone number.
175     */
176    private final OnPhoneNumberPickerActionListener mPhoneNumberPickerActionListener =
177            new OnPhoneNumberPickerActionListener() {
178                @Override
179                public void onPickPhoneNumberAction(Uri dataUri) {
180                    // Specify call-origin so that users will see the previous tab instead of
181                    // CallLog screen (search UI will be automatically exited).
182                    PhoneNumberInteraction.startInteractionForPhoneCall(
183                        DialtactsActivity.this, dataUri, getCallOrigin());
184                }
185
186                @Override
187                public void onCallNumberDirectly(String phoneNumber) {
188                    Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
189                    startActivity(intent);
190                }
191
192                @Override
193                public void onShortcutIntentCreated(Intent intent) {
194                    Log.w(TAG, "Unsupported intent has come (" + intent + "). Ignoring.");
195                }
196
197                @Override
198                public void onHomeInActionBarSelected() {
199                    exitSearchUi();
200                }
201    };
202
203    /**
204     * Listener used to send search queries to the phone search fragment.
205     */
206    private final TextWatcher mPhoneSearchQueryTextListener = new TextWatcher() {
207            @Override
208            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
209            }
210
211            @Override
212            public void onTextChanged(CharSequence s, int start, int before, int count) {
213                final String newText = s.toString();
214                if (newText.equals(mSearchQuery)) {
215                    // If the query hasn't changed (perhaps due to activity being destroyed
216                    // and restored, or user launching the same DIAL intent twice), then there is
217                    // no need to do anything here.
218                    return;
219                }
220                mSearchQuery = newText;
221                if (DEBUG) {
222                    Log.d(TAG, "onTextChange for mSearchView called with new query: " + s);
223                }
224                final boolean smartDialSearch = isDialpadShowing();
225
226                // Show search result with non-empty text. Show a bare list otherwise.
227                if (TextUtils.isEmpty(newText) && mInSearchUi) {
228                    exitSearchUi();
229                    mSearchViewCloseButton.setVisibility(View.GONE);
230                    return;
231                } else if (!TextUtils.isEmpty(newText) && !mInSearchUi) {
232                    enterSearchUi(smartDialSearch, newText);
233                }
234
235                if (smartDialSearch && mSmartDialSearchFragment != null) {
236                        mSmartDialSearchFragment.setQueryString(newText, false);
237                } else if (mRegularSearchFragment != null) {
238                    mRegularSearchFragment.setQueryString(newText, false);
239                }
240                mSearchViewCloseButton.setVisibility(View.VISIBLE);
241                return;
242            }
243
244            @Override
245            public void afterTextChanged(Editable s) {
246            }
247    };
248
249    private boolean isDialpadShowing() {
250        return mDialpadFragment != null && mDialpadFragment.isVisible();
251    }
252
253    @Override
254    protected void onCreate(Bundle savedInstanceState) {
255        super.onCreate(savedInstanceState);
256        mFirstLaunch = true;
257
258        final Intent intent = getIntent();
259        fixIntent(intent);
260
261        setContentView(R.layout.dialtacts_activity);
262
263        getActionBar().hide();
264
265        // Add the favorites fragment, and the dialpad fragment, but only if savedInstanceState
266        // is null. Otherwise the fragment manager takes care of recreating these fragments.
267        if (savedInstanceState == null) {
268            final PhoneFavoriteFragment phoneFavoriteFragment = new PhoneFavoriteFragment();
269            phoneFavoriteFragment.setListener(mPhoneFavoriteListener);
270
271            final FragmentTransaction ft = getFragmentManager().beginTransaction();
272            ft.add(R.id.dialtacts_frame, phoneFavoriteFragment, TAG_FAVORITES_FRAGMENT);
273            ft.add(R.id.dialtacts_container, new DialpadFragment(), TAG_DIALPAD_FRAGMENT);
274            ft.commit();
275        } else {
276            mSearchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
277            mInSearchUi = savedInstanceState.getBoolean(KEY_IN_SEARCH_UI);
278            mFirstLaunch = savedInstanceState.getBoolean(KEY_FIRST_LAUNCH);
279        }
280
281        mBottomPaddingView = findViewById(R.id.dialtacts_bottom_padding);
282        prepareSearchView();
283
284        if (UI.FILTER_CONTACTS_ACTION.equals(intent.getAction())
285                && savedInstanceState == null) {
286            setupFilterText(intent);
287        }
288
289        mDialerDatabaseHelper = DialerDatabaseHelper.getInstance(this);
290        SmartDialPrefix.initializeNanpSettings(this);
291    }
292
293    @Override
294    protected void onResume() {
295        super.onResume();
296        if (mFirstLaunch) {
297            displayFragment(getIntent());
298        }
299        mFirstLaunch = false;
300        mDialerDatabaseHelper.startSmartDialUpdateThread();
301    }
302
303    @Override
304    protected void onSaveInstanceState(Bundle outState) {
305        super.onSaveInstanceState(outState);
306        outState.putString(KEY_SEARCH_QUERY, mSearchQuery);
307        outState.putBoolean(KEY_IN_SEARCH_UI, mInSearchUi);
308        outState.putBoolean(KEY_FIRST_LAUNCH, mFirstLaunch);
309    }
310
311    @Override
312    public void onAttachFragment(Fragment fragment) {
313        if (fragment instanceof DialpadFragment) {
314            mDialpadFragment = (DialpadFragment) fragment;
315            final FragmentTransaction transaction = getFragmentManager().beginTransaction();
316            transaction.hide(mDialpadFragment);
317            transaction.commit();
318        } else if (fragment instanceof SmartDialSearchFragment) {
319            mSmartDialSearchFragment = (SmartDialSearchFragment) fragment;
320        } else if (fragment instanceof SearchFragment) {
321            mRegularSearchFragment = (SearchFragment) fragment;
322        } else if (fragment instanceof PhoneFavoriteFragment) {
323            mPhoneFavoriteFragment = (PhoneFavoriteFragment) fragment;
324        }
325    }
326
327    @Override
328    public boolean onMenuItemClick(MenuItem item) {
329        switch (item.getItemId()) {
330            case R.id.menu_import_export:
331                // We hard-code the "contactsAreAvailable" argument because doing it properly would
332                // involve querying a {@link ProviderStatusLoader}, which we don't want to do right
333                // now in Dialtacts for (potential) performance reasons. Compare with how it is
334                // done in {@link PeopleActivity}.
335                ImportExportDialogFragment.show(getFragmentManager(), true,
336                        DialtactsActivity.class);
337                return true;
338            case R.id.menu_clear_frequents:
339                ClearFrequentsDialog.show(getFragmentManager());
340                return true;
341            case R.id.add_contact:
342                try {
343                    startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
344                } catch (ActivityNotFoundException e) {
345                    Toast toast = Toast.makeText(this,
346                            R.string.add_contact_not_available,
347                            Toast.LENGTH_SHORT);
348                    toast.show();
349                }
350                return true;
351            case R.id.menu_call_settings:
352                final Intent settingsIntent = DialtactsActivity.getCallSettingsIntent();
353                startActivity(settingsIntent);
354        }
355        return false;
356    }
357
358    @Override
359    public void onClick(View view) {
360        switch (view.getId()) {
361            case R.id.overflow_menu: {
362                final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
363                final Menu menu = popupMenu.getMenu();
364                popupMenu.inflate(R.menu.dialtacts_options);
365                final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
366                clearFrequents.setVisible(mPhoneFavoriteFragment.hasFrequents());
367                popupMenu.setOnMenuItemClickListener(this);
368                popupMenu.show();
369                break;
370            }
371            case R.id.dialpad_button:
372                showDialpadFragment(true);
373                break;
374            case R.id.call_history_on_dialpad_button:
375            case R.id.call_history_button:
376                // Use explicit CallLogActivity intent instead of ACTION_VIEW +
377                // CONTENT_TYPE, so that we always open our call log from our dialer
378                final Intent intent = new Intent(this, CallLogActivity.class);
379                startActivity(intent);
380                break;
381            case R.id.search_close_button:
382                // Clear the search field
383                if (!TextUtils.isEmpty(mSearchView.getText())) {
384                    mSearchView.setText("");
385                }
386                break;
387            case R.id.voice_search_button:
388                final Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
389                startActivityForResult(voiceIntent, ACTIVITY_REQUEST_CODE_VOICE_SEARCH);
390                break;
391            default: {
392                Log.wtf(TAG, "Unexpected onClick event from " + view);
393                break;
394            }
395        }
396    }
397
398    @Override
399    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
400        if (requestCode == ACTIVITY_REQUEST_CODE_VOICE_SEARCH) {
401            if (resultCode == RESULT_OK) {
402                final ArrayList<String> matches = data.getStringArrayListExtra(
403                        RecognizerIntent.EXTRA_RESULTS);
404                if (matches.size() > 0) {
405                    final String match = matches.get(0);
406                    mSearchView.setText(match);
407                } else {
408                    Log.e(TAG, "Voice search - nothing heard");
409                }
410            } else {
411                Log.e(TAG, "Voice search failed");
412            }
413        }
414        super.onActivityResult(requestCode, resultCode, data);
415    }
416
417    private void showDialpadFragment(boolean animate) {
418        mDialpadFragment.setAdjustTranslationForAnimation(animate);
419        final FragmentTransaction ft = getFragmentManager().beginTransaction();
420        if (animate) {
421            ft.setCustomAnimations(R.anim.slide_in, 0);
422        } else {
423            mDialpadFragment.setYFraction(0);
424        }
425        ft.show(mDialpadFragment);
426        ft.commit();
427    }
428
429    private void hideDialpadFragment(boolean animate) {
430        mDialpadFragment.setAdjustTranslationForAnimation(animate);
431        final FragmentTransaction ft = getFragmentManager().beginTransaction();
432        if (animate) {
433            ft.setCustomAnimations(0, R.anim.slide_out);
434        }
435        ft.hide(mDialpadFragment);
436        ft.commit();
437    }
438
439    private void prepareSearchView() {
440        mSearchViewContainer = findViewById(R.id.search_view_container);
441        mSearchViewCloseButton = findViewById(R.id.search_close_button);
442        mSearchViewCloseButton.setOnClickListener(this);
443        mVoiceSearchButton = findViewById(R.id.voice_search_button);
444        mVoiceSearchButton.setOnClickListener(this);
445        mSearchView = (EditText) findViewById(R.id.search_view);
446        mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
447        mSearchView.setHint(getString(R.string.dialer_hint_find_contact));
448        mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
449            @Override
450            public void onFocusChange(View view, boolean hasFocus) {
451                if (hasFocus) {
452                    showInputMethod(view.findFocus());
453                }
454            }
455        });
456    }
457
458    private void hideDialpadFragmentIfNecessary() {
459        if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
460            hideDialpadFragment(true);
461        }
462    }
463
464    final AnimatorListener mHideListener = new AnimatorListenerAdapter() {
465        @Override
466        public void onAnimationEnd(Animator animation) {
467            mSearchViewContainer.setVisibility(View.GONE);
468        }
469    };
470
471    public void hideSearchBar() {
472       hideSearchBar(true);
473    }
474
475    public void hideSearchBar(boolean shiftView) {
476        if (shiftView) {
477            mSearchViewContainer.animate().cancel();
478            mSearchViewContainer.setAlpha(1);
479            mSearchViewContainer.setTranslationY(0);
480            mSearchViewContainer.animate().withLayer().alpha(0).translationY(-mSearchView.getHeight())
481                    .setDuration(200).setListener(mHideListener);
482
483            if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
484                mBottomPaddingView.setVisibility(View.VISIBLE);
485                return;
486            }
487
488            mPhoneFavoriteFragment.getView().animate().withLayer()
489                    .translationY(-mSearchViewContainer.getHeight()).setDuration(200).setListener(
490                    new AnimatorListenerAdapter() {
491                        @Override
492                        public void onAnimationEnd(Animator animation) {
493                            mBottomPaddingView.setVisibility(View.VISIBLE);
494                            if (mPhoneFavoriteFragment.getView() != null) {
495                                mPhoneFavoriteFragment.getView().setTranslationY(0);
496                            }
497                        }
498                    });
499        } else {
500            mSearchViewContainer.setTranslationY(-mSearchView.getHeight());
501        }
502    }
503
504    public void showSearchBar() {
505
506
507        mSearchViewContainer.animate().cancel();
508        mSearchViewContainer.setAlpha(0);
509        mSearchViewContainer.setTranslationY(-mSearchViewContainer.getHeight());
510        mSearchViewContainer.animate().withLayer().alpha(1).translationY(0).setDuration(200)
511                .setListener(new AnimatorListenerAdapter() {
512                    @Override
513                    public void onAnimationStart(Animator animation) {
514                        mSearchViewContainer.setVisibility(View.VISIBLE);
515                    }
516                });
517
518        // If the favorites fragment hasn't been fully created before the dialpad fragment
519        // is hidden (i.e. onResume), don't bother animating
520        if (mPhoneFavoriteFragment == null || mPhoneFavoriteFragment.getView() == null) {
521            mBottomPaddingView.setVisibility(View.GONE);
522            return;
523        }
524        mPhoneFavoriteFragment.getView().setTranslationY(-mSearchViewContainer.getHeight());
525        mPhoneFavoriteFragment.getView().animate().withLayer().translationY(0).setDuration(200)
526                .setListener(
527                        new AnimatorListenerAdapter() {
528                            @Override
529                            public void onAnimationStart(Animator animation) {
530                                mBottomPaddingView.setVisibility(View.GONE);
531                            }
532                        });
533    }
534
535
536    public void setupFakeActionBarItemsForFavoritesFragment() {
537        mMenuButton = findViewById(R.id.overflow_menu);
538        if (mMenuButton != null) {
539            mMenuButton.setOnClickListener(this);
540        }
541
542        mCallHistoryButton = findViewById(R.id.call_history_button);
543        // mCallHistoryButton.setMinimumWidth(fakeMenuItemWidth);
544        mCallHistoryButton.setOnClickListener(this);
545
546        mDialpadButton = findViewById(R.id.dialpad_button);
547        // DialpadButton.setMinimumWidth(fakeMenuItemWidth);
548        mDialpadButton.setOnClickListener(this);
549    }
550
551    public void setupFakeActionBarItemsForDialpadFragment() {
552        final View callhistoryButton = findViewById(R.id.call_history_on_dialpad_button);
553        callhistoryButton.setOnClickListener(this);
554    }
555
556    private void fixIntent(Intent intent) {
557        // This should be cleaned up: the call key used to send an Intent
558        // that just said to go to the recent calls list.  It now sends this
559        // abstract action, but this class hasn't been rewritten to deal with it.
560        if (Intent.ACTION_CALL_BUTTON.equals(intent.getAction())) {
561            intent.setDataAndType(Calls.CONTENT_URI, Calls.CONTENT_TYPE);
562            intent.putExtra("call_key", true);
563            setIntent(intent);
564        }
565    }
566
567    /**
568     * Returns true if the intent is due to hitting the green send key (hardware call button:
569     * KEYCODE_CALL) while in a call.
570     *
571     * @param intent the intent that launched this activity
572     * @param recentCallsRequest true if the intent is requesting to view recent calls
573     * @return true if the intent is due to hitting the green send key while in a call
574     */
575    private boolean isSendKeyWhileInCall(Intent intent, boolean recentCallsRequest) {
576        // If there is a call in progress go to the call screen
577        if (recentCallsRequest) {
578            final boolean callKey = intent.getBooleanExtra("call_key", false);
579
580            try {
581                ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
582                if (callKey && phone != null && phone.showCallScreen()) {
583                    return true;
584                }
585            } catch (RemoteException e) {
586                Log.e(TAG, "Failed to handle send while in call", e);
587            }
588        }
589
590        return false;
591    }
592
593    /**
594     * Sets the current tab based on the intent's request type
595     *
596     * @param intent Intent that contains information about which tab should be selected
597     */
598    private void displayFragment(Intent intent) {
599        // If we got here by hitting send and we're in call forward along to the in-call activity
600        boolean recentCallsRequest = Calls.CONTENT_TYPE.equals(intent.resolveType(
601            getContentResolver()));
602        if (isSendKeyWhileInCall(intent, recentCallsRequest)) {
603            finish();
604            return;
605        }
606
607        if (mDialpadFragment != null && (phoneIsInUse() || isDialIntent(intent))) {
608            mDialpadFragment.setStartedFromNewIntent(true);
609            showDialpadFragment(false);
610        }
611    }
612
613    @Override
614    public void onNewIntent(Intent newIntent) {
615        setIntent(newIntent);
616        fixIntent(newIntent);
617        displayFragment(newIntent);
618        final String action = newIntent.getAction();
619
620        invalidateOptionsMenu();
621    }
622
623    /** Returns true if the given intent contains a phone number to populate the dialer with */
624    private boolean isDialIntent(Intent intent) {
625        final String action = intent.getAction();
626        if (Intent.ACTION_DIAL.equals(action) || ACTION_TOUCH_DIALER.equals(action)) {
627            return true;
628        }
629        if (Intent.ACTION_VIEW.equals(action)) {
630            final Uri data = intent.getData();
631            if (data != null && CallUtil.SCHEME_TEL.equals(data.getScheme())) {
632                return true;
633            }
634        }
635        return false;
636    }
637
638    /**
639     * Returns an appropriate call origin for this Activity. May return null when no call origin
640     * should be used (e.g. when some 3rd party application launched the screen. Call origin is
641     * for remembering the tab in which the user made a phone call, so the external app's DIAL
642     * request should not be counted.)
643     */
644    public String getCallOrigin() {
645        return !isDialIntent(getIntent()) ? CALL_ORIGIN_DIALTACTS : null;
646    }
647
648    /**
649     * Retrieves the filter text stored in {@link #setupFilterText(Intent)}.
650     * This text originally came from a FILTER_CONTACTS_ACTION intent received
651     * by this activity. The stored text will then be cleared after after this
652     * method returns.
653     *
654     * @return The stored filter text
655     */
656    public String getAndClearFilterText() {
657        String filterText = mFilterText;
658        mFilterText = null;
659        return filterText;
660    }
661
662    /**
663     * Stores the filter text associated with a FILTER_CONTACTS_ACTION intent.
664     * This is so child activities can check if they are supposed to display a filter.
665     *
666     * @param intent The intent received in {@link #onNewIntent(Intent)}
667     */
668    private void setupFilterText(Intent intent) {
669        // If the intent was relaunched from history, don't apply the filter text.
670        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
671            return;
672        }
673        String filter = intent.getStringExtra(UI.FILTER_TEXT_EXTRA_KEY);
674        if (filter != null && filter.length() > 0) {
675            mFilterText = filter;
676        }
677    }
678
679    private final PhoneFavoriteFragment.Listener mPhoneFavoriteListener =
680            new PhoneFavoriteFragment.Listener() {
681        @Override
682        public void onContactSelected(Uri contactUri) {
683            PhoneNumberInteraction.startInteractionForPhoneCall(
684                        DialtactsActivity.this, contactUri, getCallOrigin());
685        }
686
687        @Override
688        public void onCallNumberDirectly(String phoneNumber) {
689            Intent intent = CallUtil.getCallIntent(phoneNumber, getCallOrigin());
690            startActivity(intent);
691        }
692    };
693
694    /* TODO krelease: This is only relevant for phones that have a hard button search key (i.e.
695     * Nexus S). Supporting it is a little more tricky because of the dialpad fragment might
696     * be showing when the search key is pressed so there is more state management involved.
697
698    @Override
699    public void startSearch(String initialQuery, boolean selectInitialQuery,
700            Bundle appSearchData, boolean globalSearch) {
701        if (mRegularSearchFragment != null && mRegularSearchFragment.isAdded() && !globalSearch) {
702            if (mInSearchUi) {
703                if (mSearchView.hasFocus()) {
704                    showInputMethod(mSearchView.findFocus());
705                } else {
706                    mSearchView.requestFocus();
707                }
708            } else {
709                enterSearchUi();
710            }
711        } else {
712            super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
713        }
714    }*/
715
716    private void showInputMethod(View view) {
717        final InputMethodManager imm = (InputMethodManager) getSystemService(
718                Context.INPUT_METHOD_SERVICE);
719        if (imm != null) {
720            imm.showSoftInput(view, 0);
721        }
722    }
723
724    private void hideInputMethod(View view) {
725        final InputMethodManager imm = (InputMethodManager) getSystemService(
726                Context.INPUT_METHOD_SERVICE);
727        if (imm != null && view != null) {
728            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
729        }
730    }
731
732    /**
733     * Shows the search fragment
734     */
735    private void enterSearchUi(boolean smartDialSearch, String query) {
736        if (DEBUG) {
737            Log.d(TAG, "Entering search UI - smart dial " + smartDialSearch);
738        }
739        final String tag = smartDialSearch ? TAG_SMARTDIAL_SEARCH_FRAGMENT :
740                TAG_REGULAR_SEARCH_FRAGMENT;
741
742        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
743        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
744
745        SearchFragment fragment;
746
747        transaction.remove(mPhoneFavoriteFragment);
748        fragment = (SearchFragment) getFragmentManager().findFragmentByTag(tag);
749        if (fragment == null) {
750            if (smartDialSearch) {
751                fragment = new SmartDialSearchFragment();
752            } else {
753                fragment = new SearchFragment();
754            }
755            transaction.replace(R.id.dialtacts_frame, fragment, tag);
756        } else {
757            transaction.attach(fragment);
758        }
759
760        transaction.addToBackStack(null);
761        fragment.setQueryString(query, false);
762        transaction.commit();
763        mInSearchUi = true;
764    }
765
766    /**
767     * Hides the search fragment
768     */
769    private void exitSearchUi() {
770        getFragmentManager().popBackStack();
771        mInSearchUi = false;
772    }
773
774    /** Returns an Intent to launch Call Settings screen */
775    public static Intent getCallSettingsIntent() {
776        final Intent intent = new Intent(Intent.ACTION_MAIN);
777        intent.setClassName(PHONE_PACKAGE, CALL_SETTINGS_CLASS_NAME);
778        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
779        return intent;
780    }
781
782    @Override
783    public void onBackPressed() {
784        if (mDialpadFragment != null && mDialpadFragment.isVisible()) {
785            hideDialpadFragment(true);
786        } else if (mInSearchUi) {
787            mSearchView.setText(null);
788        } else if (isTaskRoot()) {
789            // Instead of stopping, simply push this to the back of the stack.
790            // This is only done when running at the top of the stack;
791            // otherwise, we have been launched by someone else so need to
792            // allow the user to go back to the caller.
793            moveTaskToBack(false);
794        } else {
795            super.onBackPressed();
796        }
797    }
798
799    @Override
800    public void onDialpadQueryChanged(String query) {
801        final String normalizedQuery = SmartDialNameMatcher.normalizeNumber(query,
802                SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
803        if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
804            if (DEBUG) {
805                Log.d(TAG, "onDialpadQueryChanged - new query: " + query);
806            }
807            if (mDialpadFragment == null || !mDialpadFragment.isVisible()) {
808                // This callback can happen if the dialpad fragment is recreated because of
809                // activity destruction. In that case, don't update the search view because
810                // that would bring the user back to the search fragment regardless of the
811                // previous state of the application. Instead, just return here and let the
812                // fragment manager correctly figure out whatever fragment was last displayed.
813                return;
814            }
815            mSearchView.setText(normalizedQuery);
816        }
817    }
818
819    @Override
820    public void onListFragmentScrollStateChange(int scrollState) {
821        if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
822            hideDialpadFragmentIfNecessary();
823            hideInputMethod(getCurrentFocus());
824        }
825    }
826
827    @Override
828    public void onPhoneFavoriteFragmentStarted() {
829        setupFakeActionBarItemsForFavoritesFragment();
830    }
831
832    @Override
833    public void onDialpadFragmentStarted() {
834        setupFakeActionBarItemsForDialpadFragment();
835    }
836
837    private boolean phoneIsInUse() {
838        final TelephonyManager tm = (TelephonyManager) getSystemService(
839                Context.TELEPHONY_SERVICE);
840        return tm.getCallState() != TelephonyManager.CALL_STATE_IDLE;
841    }
842
843    @Override
844    public void onShowAllContacts() {
845        final Intent intent = new Intent(this, AllContactsActivity.class);
846        startActivity(intent);
847    }
848}
849