CallCardPresenter.java revision e1c196e04154815e2fb55801af73beb9fb2aa324
1c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon/*
2c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * Copyright (C) 2013 The Android Open Source Project
3c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon *
4c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * Licensed under the Apache License, Version 2.0 (the "License");
5c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * you may not use this file except in compliance with the License.
6c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * You may obtain a copy of the License at
7c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon *
8c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon *      http://www.apache.org/licenses/LICENSE-2.0
9c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon *
10c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * Unless required by applicable law or agreed to in writing, software
11c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * distributed under the License is distributed on an "AS IS" BASIS,
12c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * See the License for the specific language governing permissions and
14c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * limitations under the License
15c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon */
16c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon
17c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordonpackage com.android.incallui;
18c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon
198c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwellimport com.google.common.base.Preconditions;
208c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwell
21f925b504266f80ec0b74212c403475f5cc790ab0Evan Charltonimport android.Manifest;
223d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordonimport android.content.Context;
23f925b504266f80ec0b74212c403475f5cc790ab0Evan Charltonimport android.content.Intent;
243d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordonimport android.content.pm.ApplicationInfo;
253d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordonimport android.content.pm.PackageManager;
266d231f52e4f00399330d772f2a337283803f3a9dYorke Leeimport android.graphics.drawable.Drawable;
27da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordonimport android.net.Uri;
280e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charltonimport android.os.Bundle;
299cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwellimport android.support.annotation.Nullable;
308cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Leeimport android.telecom.Call.Details;
3150dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Leeimport android.telecom.DisconnectCause;
324b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.PhoneAccount;
334b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.PhoneAccountHandle;
344b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.StatusHints;
354b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.TelecomManager;
364b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.VideoProfile;
370e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charltonimport android.telephony.PhoneNumberUtils;
38d28fc12696fa706fed70e04698275af7be7fee82Santos Cordonimport android.text.TextUtils;
39687390732d6253903141d89d734113373d0e3c13Nancy Chenimport android.view.View;
400dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Leeimport android.view.accessibility.AccessibilityManager;
41e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chenimport android.widget.ListAdapter;
42be8060acf5ecbb916c161d3249813098c0611ed5Yorke Lee
437b2b43c0589ec79ba74359f7d9d6e0450fc322abVictor Changimport com.android.contacts.common.ContactsUtils;
447952f0a140d290f45f76917a3005e56bcb9b52c2Nancy Chenimport com.android.contacts.common.compat.telecom.TelecomManagerCompat;
458c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwellimport com.android.contacts.common.preference.ContactsPreferences;
469cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwellimport com.android.contacts.common.testing.NeededForTesting;
478c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwellimport com.android.contacts.common.util.ContactDisplayUtils;
4868d86c656e5522dec1b04e85ebc0e0f78e436c3dSailesh Nepalimport com.android.dialer.R;
49e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chenimport com.android.incallui.Call.State;
50d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordonimport com.android.incallui.ContactInfoCache.ContactCacheEntry;
51d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordonimport com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
520e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charltonimport com.android.incallui.InCallPresenter.InCallDetailsListener;
53350fff554bf7ed5b9e91985935488771156953abTyler Gunnimport com.android.incallui.InCallPresenter.InCallEventListener;
547ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordonimport com.android.incallui.InCallPresenter.InCallState;
557ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordonimport com.android.incallui.InCallPresenter.InCallStateListener;
561c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordonimport com.android.incallui.InCallPresenter.IncomingCallListener;
57f925b504266f80ec0b74212c403475f5cc790ab0Evan Charltonimport com.android.incalluibind.ObjectFactory;
585b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
595b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Leeimport java.lang.ref.WeakReference;
60c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon
61f9aea95a921900b552bc808513f7c42c15e16f6cTony Makimport static com.android.contacts.common.compat.CallSdkCompat.Details.PROPERTY_WORK_CALL;
62c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon/**
63c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * Presenter for the Call Card Fragment.
64033ed5926d2924d35b9c82898ab4e835da56b3afChiao Cheng * <p>
657ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon * This class listens for changes to InCallState and passes it along to the fragment.
66c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon */
67d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordonpublic class CallCardPresenter extends Presenter<CallCardPresenter.CallCardUi>
6891a369b019b39c5335a4f5d251691c0cdb657f7dAndrew Lee        implements InCallStateListener, IncomingCallListener, InCallDetailsListener,
69e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        InCallEventListener, CallList.CallUpdateListener, DistanceHelper.Listener {
70be8060acf5ecbb916c161d3249813098c0611ed5Yorke Lee
7189aad4adce9cc66294f15391fa3e10f3123d3d92Andrew Lee    public interface EmergencyCallListener {
7289aad4adce9cc66294f15391fa3e10f3123d3d92Andrew Lee        public void onCallUpdated(BaseFragment fragment, boolean isEmergency);
7389aad4adce9cc66294f15391fa3e10f3123d3d92Andrew Lee    }
7489aad4adce9cc66294f15391fa3e10f3123d3d92Andrew Lee
75033ed5926d2924d35b9c82898ab4e835da56b3afChiao Cheng    private static final String TAG = CallCardPresenter.class.getSimpleName();
769c4226952c74f024c09e0bd381048493993c974eAndrew Lee    private static final long CALL_TIME_UPDATE_INTERVAL_MS = 1000;
778cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon
78061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    private final EmergencyCallListener mEmergencyCallListener =
79061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            ObjectFactory.newEmergencyCallListener();
80e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    private DistanceHelper mDistanceHelper;
81df3ef3fc3d10573507c6451bffd1f55dfcb475ddAndrew Lee
821b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    private Call mPrimary;
83d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    private Call mSecondary;
84d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    private ContactCacheEntry mPrimaryContactInfo;
85d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    private ContactCacheEntry mSecondaryContactInfo;
8686a7f5639fd0798c3b4500166f97649949237215Andrew Lee    private CallTimer mCallTimer;
873d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    private Context mContext;
889cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell    @Nullable private ContactsPreferences mContactsPreferences;
89b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    private boolean mSpinnerShowing = false;
9081ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn    private boolean mHasShownToast = false;
91e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    private InCallContactInteractions mInCallContactInteractions;
92a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn    private boolean mIsFullscreen = false;
936d231f52e4f00399330d772f2a337283803f3a9dYorke Lee
945b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    public static class ContactLookupCallback implements ContactInfoCacheCallback {
955b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        private final WeakReference<CallCardPresenter> mCallCardPresenter;
965b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        private final boolean mIsPrimary;
975b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
985b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        public ContactLookupCallback(CallCardPresenter callCardPresenter, boolean isPrimary) {
995b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            mCallCardPresenter = new WeakReference<CallCardPresenter>(callCardPresenter);
1005b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            mIsPrimary = isPrimary;
1015b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
1025b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
1035b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        @Override
1045b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
1055b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            CallCardPresenter presenter = mCallCardPresenter.get();
1065b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            if (presenter != null) {
1075b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee                presenter.onContactInfoComplete(callId, entry, mIsPrimary);
1085b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            }
1095b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
1105b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
1115b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        @Override
1125b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
1135b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            CallCardPresenter presenter = mCallCardPresenter.get();
1145b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            if (presenter != null) {
1155b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee                presenter.onImageLoadComplete(callId, entry);
1165b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            }
1175b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
1185b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
119e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        @Override
120e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        public void onContactInteractionsInfoComplete(String callId, ContactCacheEntry entry) {
121e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            CallCardPresenter presenter = mCallCardPresenter.get();
122e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            if (presenter != null) {
123e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen                presenter.onContactInteractionsInfoComplete(callId, entry);
124e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            }
125e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        }
1265b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    }
1275b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
128b8eedb3b9796873eaa22847205239a51092f1a43Santos Cordon    public CallCardPresenter() {
1298cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        // create the call timer
1308cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        mCallTimer = new CallTimer(new Runnable() {
1318cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            @Override
1328cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            public void run() {
1338cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon                updateCallTime();
1348cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            }
1358cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        });
1366d231f52e4f00399330d772f2a337283803f3a9dYorke Lee    }
1376d231f52e4f00399330d772f2a337283803f3a9dYorke Lee
1381c6bb983216392b5ceb36cea42a46631f68a3dd6Jay Shrauner    public void init(Context context, Call call) {
13942373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        mContext = Preconditions.checkNotNull(context);
140e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        mDistanceHelper = ObjectFactory.newDistanceHelper(mContext, this);
1419cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell        mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
14242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
1434a5339c4a266891ce560dfb6e348c3779a0a3ca1Chiao Cheng        // Call may be null if disconnect happened already.
1444a5339c4a266891ce560dfb6e348c3779a0a3ca1Chiao Cheng        if (call != null) {
145656dd4a0d8571b47ecf0ecf3e4c2eeacfd1099b3Chiao Cheng            mPrimary = call;
14681ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn            if (shouldShowNoteSentToast(mPrimary)) {
14781ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn                final CallCardUi ui = getUi();
14881ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn                if (ui != null) {
14981ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn                    ui.showNoteSentToast();
15081ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn                }
15181ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn            }
152b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn            CallList.getInstance().addCallUpdateListener(call.getId(), this);
153656dd4a0d8571b47ecf0ecf3e4c2eeacfd1099b3Chiao Cheng
1544a5339c4a266891ce560dfb6e348c3779a0a3ca1Chiao Cheng            // start processing lookups right away.
155812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            if (!call.isConferenceCall()) {
156248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal                startContactInfoSearch(call, true, call.getState() == Call.State.INCOMING);
157812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            } else {
158017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                updateContactEntry(null, true);
159812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            }
1604a5339c4a266891ce560dfb6e348c3779a0a3ca1Chiao Cheng        }
161fd0b577edffa5413adfda1503038a3bff3247106Yorke Lee
162fd0b577edffa5413adfda1503038a3bff3247106Yorke Lee        onStateChange(null, InCallPresenter.getInstance().getInCallState(), CallList.getInstance());
163033ed5926d2924d35b9c82898ab4e835da56b3afChiao Cheng    }
164033ed5926d2924d35b9c82898ab4e835da56b3afChiao Cheng
165c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon    @Override
166c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon    public void onUiReady(CallCardUi ui) {
167c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon        super.onUiReady(ui);
1681b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon
1698c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwell        if (mContactsPreferences != null) {
1708c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwell            mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
1718c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwell        }
1728c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwell
17342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        // Contact search may have completed before ui is ready.
17442373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        if (mPrimaryContactInfo != null) {
175017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updatePrimaryDisplayInfo();
17642373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
17742373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
1781f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon        // Register for call state changes last
1791f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon        InCallPresenter.getInstance().addListener(this);
1801c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon        InCallPresenter.getInstance().addIncomingCallListener(this);
1810e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton        InCallPresenter.getInstance().addDetailsListener(this);
182350fff554bf7ed5b9e91985935488771156953abTyler Gunn        InCallPresenter.getInstance().addInCallEventListener(this);
1831b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    }
1841b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon
1851b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    @Override
1861b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    public void onUiUnready(CallCardUi ui) {
1871b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon        super.onUiUnready(ui);
1881b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon
1891f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon        // stop getting call state changes
1901f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon        InCallPresenter.getInstance().removeListener(this);
1911c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon        InCallPresenter.getInstance().removeIncomingCallListener(this);
1920e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton        InCallPresenter.getInstance().removeDetailsListener(this);
193350fff554bf7ed5b9e91985935488771156953abTyler Gunn        InCallPresenter.getInstance().removeInCallEventListener(this);
194b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        if (mPrimary != null) {
195b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn            CallList.getInstance().removeCallUpdateListener(mPrimary.getId(), this);
196b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        }
1971f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon
198e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        if (mDistanceHelper != null) {
199e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            mDistanceHelper.cleanUp();
200e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        }
201e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen
2021b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon        mPrimary = null;
203d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        mPrimaryContactInfo = null;
204d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        mSecondaryContactInfo = null;
205c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon    }
206c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon
207c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon    @Override
208f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen    public void onIncomingCall(InCallState oldState, InCallState newState, Call call) {
2091c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon        // same logic should happen as with onStateChange()
210f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        onStateChange(oldState, newState, CallList.getInstance());
2111c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon    }
2121c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon
2131c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon    @Override
214f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen    public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
215f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        Log.d(this, "onStateChange() " + newState);
21675c86b55d45b1725c46ed2e5d77c9c43fcfd93daSantos Cordon        final CallCardUi ui = getUi();
2171b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon        if (ui == null) {
2181b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon            return;
2191b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon        }
220c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon
2217ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon        Call primary = null;
2227ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon        Call secondary = null;
2237ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon
224f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        if (newState == InCallState.INCOMING) {
2257ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon            primary = callList.getIncomingCall();
226f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
227950e3df8ac744daf2a890d279566bdba424710f9Santos Cordon            primary = callList.getOutgoingCall();
228f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen            if (primary == null) {
229f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen                primary = callList.getPendingOutgoingCall();
230f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen            }
231950e3df8ac744daf2a890d279566bdba424710f9Santos Cordon
23282f5d003e675dfd56735c745744c3d968883b23cSantos Cordon            // getCallToDisplay doesn't go through outgoing or incoming calls. It will return the
23382f5d003e675dfd56735c745744c3d968883b23cSantos Cordon            // highest priority call to display as the secondary call.
234d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            secondary = getCallToDisplay(callList, null, true);
235f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        } else if (newState == InCallState.INCALL) {
236d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            primary = getCallToDisplay(callList, null, false);
237d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            secondary = getCallToDisplay(callList, primary, true);
2387ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon        }
2397ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon
240687390732d6253903141d89d734113373d0e3c13Nancy Chen        if (mInCallContactInteractions != null &&
241687390732d6253903141d89d734113373d0e3c13Nancy Chen                (oldState == InCallState.INCOMING || newState == InCallState.INCOMING)) {
242e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            ui.showContactContext(newState != InCallState.INCOMING);
243e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        }
244e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen
245a4bd83ad639b349fa16dbd07225017fcbf6ed550Chiao Cheng        Log.d(this, "Primary call: " + primary);
246a4bd83ad639b349fa16dbd07225017fcbf6ed550Chiao Cheng        Log.d(this, "Secondary call: " + secondary);
2477ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon
248fb008c2423a510a57be251d16bc963f64e98c2daRoshan Pius        final boolean primaryChanged = !(Call.areSame(mPrimary, primary) &&
249fb008c2423a510a57be251d16bc963f64e98c2daRoshan Pius                Call.areSameNumber(mPrimary, primary));
250fb008c2423a510a57be251d16bc963f64e98c2daRoshan Pius        final boolean secondaryChanged = !(Call.areSame(mSecondary, secondary) &&
251fb008c2423a510a57be251d16bc963f64e98c2daRoshan Pius                Call.areSameNumber(mSecondary, secondary));
252061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        final boolean shouldShowCallSubject = shouldShowCallSubject(mPrimary);
2539c98346835b8e902e0c2c11f14a35f4e91578e9fAndrew Lee
254812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        mSecondary = secondary;
255b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        Call previousPrimary = mPrimary;
256812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        mPrimary = primary;
257812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon
25881ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn        if (primaryChanged && shouldShowNoteSentToast(primary)) {
25981ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn            ui.showNoteSentToast();
26081ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn        }
26181ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn
2620cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        // Refresh primary call information if either:
2630cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        // 1. Primary call changed.
2640cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        // 2. The call's ability to manage conference has changed.
2650cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        if (mPrimary != null && (primaryChanged ||
266061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                ui.isManageConferenceVisible() != shouldShowManageConference()) ||
267061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                ui.isCallSubjectVisible() != shouldShowCallSubject) {
268812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            // primary call has changed
269b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn            if (previousPrimary != null) {
27014c3e88a486cb5cfb9ef5fee107118266d305edfTyler Gunn                //clear progess spinner (if any) related to previous primary call
27114c3e88a486cb5cfb9ef5fee107118266d305edfTyler Gunn                maybeShowProgressSpinner(previousPrimary.getState(),
27214c3e88a486cb5cfb9ef5fee107118266d305edfTyler Gunn                        Call.SessionModificationState.NO_REQUEST);
273b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn                CallList.getInstance().removeCallUpdateListener(previousPrimary.getId(), this);
274b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn            }
275b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn            CallList.getInstance().addCallUpdateListener(mPrimary.getId(), this);
276b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn
277248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal            mPrimaryContactInfo = ContactInfoCache.buildCacheEntryFromCall(mContext, mPrimary,
278248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal                    mPrimary.getState() == Call.State.INCOMING);
279017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updatePrimaryDisplayInfo();
280812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            maybeStartSearch(mPrimary, true);
281207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn            maybeClearSessionModificationState(mPrimary);
28242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
28342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
2842d7bf90875bea954ceacdacd9e52c840200bde37Yorke Lee        if (previousPrimary != null && mPrimary == null) {
28514c3e88a486cb5cfb9ef5fee107118266d305edfTyler Gunn            //clear progess spinner (if any) related to previous primary call
28614c3e88a486cb5cfb9ef5fee107118266d305edfTyler Gunn            maybeShowProgressSpinner(previousPrimary.getState(),
28714c3e88a486cb5cfb9ef5fee107118266d305edfTyler Gunn                    Call.SessionModificationState.NO_REQUEST);
2882d7bf90875bea954ceacdacd9e52c840200bde37Yorke Lee            CallList.getInstance().removeCallUpdateListener(previousPrimary.getId(), this);
2892d7bf90875bea954ceacdacd9e52c840200bde37Yorke Lee        }
2902d7bf90875bea954ceacdacd9e52c840200bde37Yorke Lee
291812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        if (mSecondary == null) {
29242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            // Secondary call may have ended.  Update the ui.
29342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            mSecondaryContactInfo = null;
294017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updateSecondaryDisplayInfo();
295812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        } else if (secondaryChanged) {
296812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            // secondary call has changed
297248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal            mSecondaryContactInfo = ContactInfoCache.buildCacheEntryFromCall(mContext, mSecondary,
298248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal                    mSecondary.getState() == Call.State.INCOMING);
299017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updateSecondaryDisplayInfo();
300812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            maybeStartSearch(mSecondary, false);
301207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn            maybeClearSessionModificationState(mSecondary);
30242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
30342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
3049c4226952c74f024c09e0bd381048493993c974eAndrew Lee        // Start/stop timers.
3058cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (isPrimaryCallActive()) {
306a4bd83ad639b349fa16dbd07225017fcbf6ed550Chiao Cheng            Log.d(this, "Starting the calltime timer");
3079c4226952c74f024c09e0bd381048493993c974eAndrew Lee            mCallTimer.start(CALL_TIME_UPDATE_INTERVAL_MS);
3088cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        } else {
309a4bd83ad639b349fa16dbd07225017fcbf6ed550Chiao Cheng            Log.d(this, "Canceling the calltime timer");
3108cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            mCallTimer.cancel();
311f0dfbe2e89df0d58efde8b290662072c081e146aNancy Chen            ui.setPrimaryCallElapsedTime(false, 0);
3128cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        }
3138cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon
314d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        // Set the call state
3158aac1a358f2caf6c21c603bae9c9d4bd075eb155Evan Charlton        int callState = Call.State.IDLE;
316d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        if (mPrimary != null) {
31732027ae1f52232258cc8133d383459f3b4ee57e4Yorke Lee            callState = mPrimary.getState();
318ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            updatePrimaryCallState();
31982f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        } else {
3209c4226952c74f024c09e0bd381048493993c974eAndrew Lee            getUi().setCallState(
3219c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    callState,
322678775006531155eec0da47ae6889bf03ad9a710Yorke Lee                    VideoProfile.STATE_AUDIO_ONLY,
3239c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    Call.SessionModificationState.NO_REQUEST,
32450dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Lee                    new DisconnectCause(DisconnectCause.UNKNOWN),
3259c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    null,
3269c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    null,
32782332c921b8dcef213700822e2404b005adab00cAndrew Lee                    null,
328dc3122e9d7bc0c6f5fef00bed36bb300c40afadfAndrew Lee                    false /* isWifi */,
3297f07f9f6f1e209359cf069b1fa4b0dfc0b371f64Victor Chang                    false /* isConference */,
3307f07f9f6f1e209359cf069b1fa4b0dfc0b371f64Victor Chang                    false /* isWorkCall */);
3318cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            getUi().showHdAudioIndicator(false);
33282f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        }
333e26b887e90d5e9566a5054a1ccb3ed32ae2eea2dAndrew Lee
3341da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee        maybeShowManageConferenceCallButton();
3351da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee
336c44f654b992c6ab8a4acff9cb5aa2678c58755d9Andrew Lee        // Hide the end call button instantly if we're receiving an incoming call.
337fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee        getUi().setEndCallButtonEnabled(shouldShowEndCallButton(mPrimary, callState),
338fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee                callState != Call.State.INCOMING /* animate */);
3390dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee
340e1c196e04154815e2fb55801af73beb9fb2aa324Qi Wang        maybeSendAccessibilityEvent(oldState, newState, primaryChanged);
3411b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    }
3421b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon
3431b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    @Override
3448cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    public void onDetailsChanged(Call call, Details details) {
345ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        updatePrimaryCallState();
346e0ec202ff354be3cd3bded7af8fca678dea7fb61Andrew Lee
3478cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (call.can(Details.CAPABILITY_MANAGE_CONFERENCE) !=
3486a4e9d87031f1f803ce9b64844f71c73e9502a7eSailesh Nepal                details.can(Details.CAPABILITY_MANAGE_CONFERENCE)) {
349e0ec202ff354be3cd3bded7af8fca678dea7fb61Andrew Lee            maybeShowManageConferenceCallButton();
350e0ec202ff354be3cd3bded7af8fca678dea7fb61Andrew Lee        }
3510786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton    }
3520786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton
353b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    @Override
354b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    public void onCallChanged(Call call) {
355b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        // No-op; specific call updates handled elsewhere.
356b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    }
357b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn
358b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    /**
359b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     * Handles a change to the session modification state for a call.  Triggers showing the progress
360b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     * spinner, as well as updating the call state label.
361b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     *
362b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     * @param sessionModificationState The new session modification state.
363b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     */
364b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    @Override
365b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    public void onSessionModificationStateChange(int sessionModificationState) {
366b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        Log.d(this, "onSessionModificationStateChange : sessionModificationState = " +
367b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn                sessionModificationState);
368b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn
369b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        if (mPrimary == null) {
370b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn            return;
371b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        }
372b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        maybeShowProgressSpinner(mPrimary.getState(), sessionModificationState);
373b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        getUi().setEndCallButtonEnabled(sessionModificationState !=
374b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn                        Call.SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST,
375b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn                true /* shouldAnimate */);
376b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        updatePrimaryCallState();
377b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    }
378b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn
379061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    /**
380061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     * Handles a change to the last forwarding number by refreshing the primary call info.
381061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     */
382061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    @Override
383061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    public void onLastForwardedNumberChange() {
384061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        Log.v(this, "onLastForwardedNumberChange");
385061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn
386061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        if (mPrimary == null) {
387061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            return;
388061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        }
389061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        updatePrimaryDisplayInfo();
390061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    }
391061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn
392c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen    /**
393c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen     * Handles a change to the child number by refreshing the primary call info.
394c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen     */
395c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen    @Override
396c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen    public void onChildNumberChange() {
397c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen        Log.v(this, "onChildNumberChange");
398c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen
399c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen        if (mPrimary == null) {
400c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen            return;
401c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen        }
402c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen        updatePrimaryDisplayInfo();
403c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen    }
404c82f0a1f9826d0002812748afa4906b8631a7b61Ta-wei Yen
4050786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton    private String getSubscriptionNumber() {
4060786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        // If it's an emergency call, and they're not populating the callback number,
4070786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        // then try to fall back to the phone sub info (to hopefully get the SIM's
4080786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        // number directly from the telephony layer).
40976e1b4bfb2e21300068f10cc94922214e62c8f86Evan Charlton        PhoneAccountHandle accountHandle = mPrimary.getAccountHandle();
41076e1b4bfb2e21300068f10cc94922214e62c8f86Evan Charlton        if (accountHandle != null) {
411170e405691a237b74fbecbf41208cbaa5bd36442Nancy Chen            TelecomManager mgr = InCallPresenter.getInstance().getTelecomManager();
41229c01f36e9381b7bad135a9701aa3dffaf495b2fNancy Chen            PhoneAccount account = TelecomManagerCompat.getPhoneAccount(mgr, accountHandle);
413b6f45c2768f5af916ee3257542a3ab03611c1f5eEvan Charlton            if (account != null) {
4149e27dca4bc1be09bcb1d283b2fe3ba3d49f0748bAndrew Lee                return getNumberFromHandle(account.getSubscriptionAddress());
415c108b49ad5440fbf381c9a589d8a217da3efcb64Ihab Awad            }
4160e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton        }
4170786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        return null;
4180e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton    }
4190e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton
420ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal    private void updatePrimaryCallState() {
421ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        if (getUi() != null && mPrimary != null) {
4227f07f9f6f1e209359cf069b1fa4b0dfc0b371f64Victor Chang            boolean isWorkCall = mPrimary.hasProperty(PROPERTY_WORK_CALL)
4237f07f9f6f1e209359cf069b1fa4b0dfc0b371f64Victor Chang                    || (mPrimaryContactInfo == null ? false
4247f07f9f6f1e209359cf069b1fa4b0dfc0b371f64Victor Chang                            : mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
425ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            getUi().setCallState(
426ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    mPrimary.getState(),
427ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    mPrimary.getVideoState(),
428ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    mPrimary.getSessionModificationState(),
429ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    mPrimary.getDisconnectCause(),
430ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    getConnectionLabel(),
4316c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee                    getCallStateIcon(),
43282332c921b8dcef213700822e2404b005adab00cAndrew Lee                    getGatewayNumber(),
43359585f80dce30e53ea30f08f11089f6cdd6e41fbAndrew Lee                    mPrimary.hasProperty(Details.PROPERTY_WIFI),
4347f07f9f6f1e209359cf069b1fa4b0dfc0b371f64Victor Chang                    mPrimary.isConferenceCall(),
4357f07f9f6f1e209359cf069b1fa4b0dfc0b371f64Victor Chang                    isWorkCall);
4368cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
437061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            maybeShowHdAudioIcon();
438ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            setCallbackNumber();
439ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        }
440ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal    }
4410786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton
4421da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee    /**
443061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     * Show the HD icon if the call is active and has {@link Details#PROPERTY_HIGH_DEF_AUDIO},
444061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     * except if the call has a last forwarded number (we will show that icon instead).
445061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     */
446061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    private void maybeShowHdAudioIcon() {
447061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        boolean showHdAudioIndicator =
448061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                isPrimaryCallActive() && mPrimary.hasProperty(Details.PROPERTY_HIGH_DEF_AUDIO) &&
449061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                TextUtils.isEmpty(mPrimary.getLastForwardedNumber());
450061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        getUi().showHdAudioIndicator(showHdAudioIndicator);
451061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    }
452061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn
453061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    /**
454289d53ed1c879e983dcb021fa677212ab4566bbdSantos Cordon     * Only show the conference call button if we can manage the conference.
4551da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee     */
4561da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee    private void maybeShowManageConferenceCallButton() {
4570cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        getUi().showManageConferenceCallButton(shouldShowManageConference());
4580cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn    }
4590cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn
460b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    /**
461b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     * Determines if a pending session modification exists for the current call.  If so, the
462b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     * progress spinner is shown, and the call state is updated.
463b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     *
464b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     * @param callState The call state.
465b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     * @param sessionModificationState The session modification state.
466b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn     */
467b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn    private void maybeShowProgressSpinner(int callState, int sessionModificationState) {
468b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        final boolean show = sessionModificationState ==
469b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn                Call.SessionModificationState.WAITING_FOR_RESPONSE
470b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn                && callState == Call.State.ACTIVE;
471b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        if (show != mSpinnerShowing) {
472b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn            getUi().setProgressSpinnerVisible(show);
473b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn            mSpinnerShowing = show;
474b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        }
47555576e529a3fa7183a698669374b9c2dd28ad5c5Yorke Lee    }
47655576e529a3fa7183a698669374b9c2dd28ad5c5Yorke Lee
4770cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn    /**
4780cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     * Determines if the manage conference button should be visible, based on the current primary
4790cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     * call.
4800cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     *
4810cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     * @return {@code True} if the manage conference button should be visible.
4820cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     */
4830cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn    private boolean shouldShowManageConference() {
484e16e53167b367656840df665457e71e66bc966deAndrew Lee        if (mPrimary == null) {
4850cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn            return false;
486e16e53167b367656840df665457e71e66bc966deAndrew Lee        }
487e16e53167b367656840df665457e71e66bc966deAndrew Lee
48818c0feda76fe333f3db1bf7bd307458a9e6b6005Rekha Kumar        return mPrimary.can(android.telecom.Call.Details.CAPABILITY_MANAGE_CONFERENCE)
48918c0feda76fe333f3db1bf7bd307458a9e6b6005Rekha Kumar                && !mPrimary.isVideoCall(mContext);
4901da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee    }
4911da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee
492ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal    private void setCallbackNumber() {
493ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        String callbackNumber = null;
4940786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton
495c4fac46c65d20da815b37f3d1dbe2ff748fa1f00Tyler Gunn        // Show the emergency callback number if either:
496c4fac46c65d20da815b37f3d1dbe2ff748fa1f00Tyler Gunn        // 1. This is an emergency call.
497c4fac46c65d20da815b37f3d1dbe2ff748fa1f00Tyler Gunn        // 2. The phone is in Emergency Callback Mode, which means we should show the callback
498c4fac46c65d20da815b37f3d1dbe2ff748fa1f00Tyler Gunn        //    number.
49959585f80dce30e53ea30f08f11089f6cdd6e41fbAndrew Lee        boolean showCallbackNumber = mPrimary.hasProperty(Details.PROPERTY_EMERGENCY_CALLBACK_MODE);
500c4fac46c65d20da815b37f3d1dbe2ff748fa1f00Tyler Gunn
50193a6fd8d4a6f3ff4732cb4fb3b9df7c522b92823Tyler Gunn        if (mPrimary.isEmergencyCall() || showCallbackNumber) {
502ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            callbackNumber = getSubscriptionNumber();
503ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        } else {
5049124b77e489772fb7c6aac36f83d86ffdf83748fJay Shrauner            StatusHints statusHints = mPrimary.getTelecomCall().getDetails().getStatusHints();
505ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            if (statusHints != null) {
506ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                Bundle extras = statusHints.getExtras();
507ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                if (extras != null) {
5084b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunn                    callbackNumber = extras.getString(TelecomManager.EXTRA_CALL_BACK_NUMBER);
5090786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton                }
5100e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton            }
5110786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        }
5120786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton
5139b70c293ad0a2bf343a93701a37a5f152c6a4214Brandon Maxwell        final String simNumber = TelecomManagerCompat.getLine1Number(
5149b70c293ad0a2bf343a93701a37a5f152c6a4214Brandon Maxwell                InCallPresenter.getInstance().getTelecomManager(),
5159b70c293ad0a2bf343a93701a37a5f152c6a4214Brandon Maxwell                InCallPresenter.getInstance().getTelephonyManager(),
5169b70c293ad0a2bf343a93701a37a5f152c6a4214Brandon Maxwell                mPrimary.getAccountHandle());
517c4fac46c65d20da815b37f3d1dbe2ff748fa1f00Tyler Gunn        if (!showCallbackNumber && PhoneNumberUtils.compare(callbackNumber, simNumber)) {
518c4fac46c65d20da815b37f3d1dbe2ff748fa1f00Tyler Gunn            Log.d(this, "Numbers are the same (and callback number is not being forced to show);" +
5193326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn                    " not showing the callback number");
520ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            callbackNumber = null;
5210786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        }
522ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal
52393a6fd8d4a6f3ff4732cb4fb3b9df7c522b92823Tyler Gunn        getUi().setCallbackNumber(callbackNumber, mPrimary.isEmergencyCall() || showCallbackNumber);
5240e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton    }
5250e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton
5268cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon    public void updateCallTime() {
5278cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        final CallCardUi ui = getUi();
5288cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon
5298cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (ui == null) {
5308cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            mCallTimer.cancel();
5318cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        } else if (!isPrimaryCallActive()) {
5328cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            ui.setPrimaryCallElapsedTime(false, 0);
5338cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            mCallTimer.cancel();
534899251968ee4ee3cb375032185653ecd888e3f8dSantos Cordon        } else {
535c6f4e50a4130fb8f7247f5eab6849829701ad484Sailesh Nepal            final long callStart = mPrimary.getConnectTimeMillis();
536899251968ee4ee3cb375032185653ecd888e3f8dSantos Cordon            final long duration = System.currentTimeMillis() - callStart;
537f0dfbe2e89df0d58efde8b290662072c081e146aNancy Chen            ui.setPrimaryCallElapsedTime(true, duration);
5388cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        }
5398cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon    }
5408cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon
541f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton    public void onCallStateButtonTouched() {
54271afa1c7d1e1f49af56c24bdc64d30d80cf1ea68Evan Charlton        Intent broadcastIntent = ObjectFactory.getCallStateButtonBroadcastIntent(mContext);
543f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton        if (broadcastIntent != null) {
544f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton            Log.d(this, "Sending call state button broadcast: ", broadcastIntent);
545f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton            mContext.sendBroadcast(broadcastIntent, Manifest.permission.READ_PHONE_STATE);
546f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton        }
547f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton    }
548f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton
5493326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn    /**
5503326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn     * Handles click on the contact photo by toggling fullscreen mode if the current call is a video
5513326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn     * call.
5523326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn     */
5533326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn    public void onContactPhotoClick() {
5543326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn        if (mPrimary != null && mPrimary.isVideoCall(mContext)) {
5553326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn            InCallPresenter.getInstance().toggleFullscreenMode();
5563326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn        }
5573326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn    }
5583326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn
559812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon    private void maybeStartSearch(Call call, boolean isPrimary) {
560812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        // no need to start search for conference calls which show generic info.
561812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        if (call != null && !call.isConferenceCall()) {
562248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal            startContactInfoSearch(call, isPrimary, call.getState() == Call.State.INCOMING);
563812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        }
564812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon    }
565812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon
566207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn    private void maybeClearSessionModificationState(Call call) {
567207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn        if (call.getSessionModificationState() !=
568207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn                Call.SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
569207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn            call.setSessionModificationState(Call.SessionModificationState.NO_REQUEST);
570207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn        }
571207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn    }
572207029be4c95b6d66ef4bd3c2277a9ba9e0c62a1Tyler Gunn
57382f5d003e675dfd56735c745744c3d968883b23cSantos Cordon    /**
574d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon     * Starts a query for more contact data for the save primary and secondary calls.
575d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon     */
576248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal    private void startContactInfoSearch(final Call call, final boolean isPrimary,
577248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal            boolean isIncoming) {
57842373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        final ContactInfoCache cache = ContactInfoCache.getInstance(mContext);
57942373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
5805b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        cache.findInfo(call, isIncoming, new ContactLookupCallback(this, isPrimary));
5815b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    }
5827d2fb8620b808c28c49bde99dc54f6de46fdcb44Chiao Cheng
5835b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    private void onContactInfoComplete(String callId, ContactCacheEntry entry, boolean isPrimary) {
584a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee        final boolean entryMatchesExistingCall =
585a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee                (isPrimary && mPrimary != null && TextUtils.equals(callId,  mPrimary.getId())) ||
586a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee                (!isPrimary && mSecondary != null && TextUtils.equals(callId, mSecondary.getId()));
587a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee        if (entryMatchesExistingCall) {
588a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee            updateContactEntry(entry, isPrimary);
589a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee        } else {
590a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee            Log.w(this, "Dropping stale contact lookup info for " + callId);
591a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee        }
592a0eeeaa20c895d4ac15d7dd7045c816c81cedaf5Yorke Lee
59389fa8c5b9f3bcd0bdf4c3409216b7fe88a1486e4Yorke Lee        final Call call = CallList.getInstance().getCallById(callId);
59489fa8c5b9f3bcd0bdf4c3409216b7fe88a1486e4Yorke Lee        if (call != null) {
59589fa8c5b9f3bcd0bdf4c3409216b7fe88a1486e4Yorke Lee            call.getLogState().contactLookupResult = entry.contactLookupResult;
59689fa8c5b9f3bcd0bdf4c3409216b7fe88a1486e4Yorke Lee        }
5975b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        if (entry.contactUri != null) {
5985b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            CallerInfoUtils.sendViewNotification(mContext, entry.contactUri);
5995b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
6005b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    }
6015b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
6025b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    private void onImageLoadComplete(String callId, ContactCacheEntry entry) {
6035b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        if (getUi() == null) {
6045b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            return;
6055b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
606f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen
6075b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        if (entry.photo != null) {
6085b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            if (mPrimary != null && callId.equals(mPrimary.getId())) {
609fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn                boolean showContactPhoto = !VideoCallPresenter.showIncomingVideo(
610fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn                        mPrimary.getVideoState(), mPrimary.getState());
611fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn                getUi().setPrimaryImage(entry.photo, showContactPhoto);
6125b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            }
6135b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
61442373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng    }
61542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
616e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    private void onContactInteractionsInfoComplete(String callId, ContactCacheEntry entry) {
617e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        if (getUi() == null) {
618e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            return;
619e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        }
620e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen
621e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        if (mPrimary != null && callId.equals(mPrimary.getId())) {
622e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            mPrimaryContactInfo.locationAddress = entry.locationAddress;
623e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            updateContactInteractions();
624e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        }
625e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    }
626e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen
627e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    @Override
628e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    public void onLocationReady() {
629e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        // This will only update the contacts interactions data if the location returns after
630e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        // the contact information is found.
631e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        updateContactInteractions();
632e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    }
633e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen
634e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    private void updateContactInteractions() {
635e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        if (mPrimary != null && mPrimaryContactInfo != null
636d2fda4ab69205fc672c02141f9b62c1958f73aceNancy Chen                && (mPrimaryContactInfo.locationAddress != null
637d2fda4ab69205fc672c02141f9b62c1958f73aceNancy Chen                        || mPrimaryContactInfo.openingHours != null)) {
638687390732d6253903141d89d734113373d0e3c13Nancy Chen            // TODO: This is hardcoded to "isBusiness" because functionality to differentiate
639687390732d6253903141d89d734113373d0e3c13Nancy Chen            // between business and personal has not yet been added.
640687390732d6253903141d89d734113373d0e3c13Nancy Chen            if (setInCallContactInteractionsType(true /* isBusiness */)) {
641687390732d6253903141d89d734113373d0e3c13Nancy Chen                getUi().setContactContextTitle(
642687390732d6253903141d89d734113373d0e3c13Nancy Chen                        mInCallContactInteractions.getBusinessListHeaderView());
643687390732d6253903141d89d734113373d0e3c13Nancy Chen            }
644e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen
645e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            mInCallContactInteractions.setBusinessInfo(
646e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen                    mPrimaryContactInfo.locationAddress,
647d2fda4ab69205fc672c02141f9b62c1958f73aceNancy Chen                    mDistanceHelper.calculateDistance(mPrimaryContactInfo.locationAddress),
648d2fda4ab69205fc672c02141f9b62c1958f73aceNancy Chen                    mPrimaryContactInfo.openingHours);
649687390732d6253903141d89d734113373d0e3c13Nancy Chen            getUi().setContactContextContent(mInCallContactInteractions.getListAdapter());
650e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            getUi().showContactContext(mPrimary.getState() != State.INCOMING);
651107c7da5da4e689b38433756244971d7543f7272Nancy Chen        } else {
652107c7da5da4e689b38433756244971d7543f7272Nancy Chen            getUi().showContactContext(false);
653e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        }
654e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    }
655e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen
656e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    /**
657e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen     * Update the contact interactions type so that the correct UI is shown.
658e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen     *
659e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen     * @param isBusiness {@code true} if the interaction is a business interaction, {@code false} if
660e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen     * it is a personal contact.
661687390732d6253903141d89d734113373d0e3c13Nancy Chen     *
662687390732d6253903141d89d734113373d0e3c13Nancy Chen     * @return {@code true} if this is a new type of contact interaction (business or personal).
663687390732d6253903141d89d734113373d0e3c13Nancy Chen     * {@code false} if it hasn't changed.
664e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen     */
665687390732d6253903141d89d734113373d0e3c13Nancy Chen    private boolean setInCallContactInteractionsType(boolean isBusiness) {
666e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        if (mInCallContactInteractions == null) {
667e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen            mInCallContactInteractions =
668e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen                    new InCallContactInteractions(mContext, isBusiness);
669687390732d6253903141d89d734113373d0e3c13Nancy Chen            return true;
670e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        }
671687390732d6253903141d89d734113373d0e3c13Nancy Chen
672687390732d6253903141d89d734113373d0e3c13Nancy Chen        return mInCallContactInteractions.switchContactType(isBusiness);
673e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen    }
674e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen
675017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private void updateContactEntry(ContactCacheEntry entry, boolean isPrimary) {
67642373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        if (isPrimary) {
67742373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            mPrimaryContactInfo = entry;
678017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updatePrimaryDisplayInfo();
679d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        } else {
68042373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            mSecondaryContactInfo = entry;
681017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updateSecondaryDisplayInfo();
682d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        }
683d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    }
684d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon
685d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    /**
68682f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * Get the highest priority call to display.
68782f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * Goes through the calls and chooses which to return based on priority of which type of call
68882f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * to display to the user. Callers can use the "ignore" feature to get the second best call
68982f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * by passing a previously found primary call as ignore.
69082f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     *
69182f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * @param ignore A call to ignore if found.
69282f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     */
693d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    private Call getCallToDisplay(CallList callList, Call ignore, boolean skipDisconnected) {
694d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // Active calls come second.  An active call always gets precedent.
695d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        Call retval = callList.getActiveCall();
69682f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        if (retval != null && retval != ignore) {
69782f5d003e675dfd56735c745744c3d968883b23cSantos Cordon            return retval;
69882f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        }
69982f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
700d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // Disconnected calls get primary position if there are no active calls
701d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // to let user know quickly what call has disconnected. Disconnected
702d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // calls are very short lived.
703d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        if (!skipDisconnected) {
7046258ff60a7edc760facc3f387fc9c8ceee3fb518Christine Chen            retval = callList.getDisconnectingCall();
7056258ff60a7edc760facc3f387fc9c8ceee3fb518Christine Chen            if (retval != null && retval != ignore) {
7066258ff60a7edc760facc3f387fc9c8ceee3fb518Christine Chen                return retval;
7076258ff60a7edc760facc3f387fc9c8ceee3fb518Christine Chen            }
708d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            retval = callList.getDisconnectedCall();
709d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            if (retval != null && retval != ignore) {
710d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon                return retval;
711d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            }
71282f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        }
71382f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
71482f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        // Then we go to background call (calls on hold)
71582f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        retval = callList.getBackgroundCall();
71682f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        if (retval != null && retval != ignore) {
71782f5d003e675dfd56735c745744c3d968883b23cSantos Cordon            return retval;
71882f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        }
71982f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
72082f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        // Lastly, we go to a second background call.
72182f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        retval = callList.getSecondBackgroundCall();
72282f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
72382f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        return retval;
72482f5d003e675dfd56735c745744c3d968883b23cSantos Cordon    }
72582f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
726017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private void updatePrimaryDisplayInfo() {
7276d231f52e4f00399330d772f2a337283803f3a9dYorke Lee        final CallCardUi ui = getUi();
728d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        if (ui == null) {
72942373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            // TODO: May also occur if search result comes back after ui is destroyed. Look into
73042373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            // removing that case completely.
73142373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            Log.d(TAG, "updatePrimaryDisplayInfo called but ui is null!");
732d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon            return;
7336d231f52e4f00399330d772f2a337283803f3a9dYorke Lee        }
734be8060acf5ecbb916c161d3249813098c0611ed5Yorke Lee
735017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        if (mPrimary == null) {
736017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            // Clear the primary display info.
7377b2b43c0589ec79ba74359f7d9d6e0450fc322abVictor Chang            ui.setPrimary(null, null, false, null, null, false, false, false);
738017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            return;
739017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        }
740017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
741fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn        // Hide the contact photo if we are in a video call and the incoming video surface is
742fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn        // showing.
743fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn        boolean showContactPhoto = !VideoCallPresenter
744fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn                .showIncomingVideo(mPrimary.getVideoState(), mPrimary.getState());
745fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn
746f9aea95a921900b552bc808513f7c42c15e16f6cTony Mak        // Call placed through a work phone account.
747f9aea95a921900b552bc808513f7c42c15e16f6cTony Mak        boolean hasWorkCallProperty = mPrimary.hasProperty(PROPERTY_WORK_CALL);
748f9aea95a921900b552bc808513f7c42c15e16f6cTony Mak
749017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        if (mPrimary.isConferenceCall()) {
750017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            Log.d(TAG, "Update primary display info for conference call.");
751017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
752017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setPrimary(
753017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    null /* number */,
754017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getConferenceString(mPrimary),
755017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    false /* nameIsNumber */,
756017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    null /* label */,
757017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getConferencePhoto(mPrimary),
758fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn                    false /* isSipCall */,
7597b2b43c0589ec79ba74359f7d9d6e0450fc322abVictor Chang                    showContactPhoto,
760f9aea95a921900b552bc808513f7c42c15e16f6cTony Mak                    hasWorkCallProperty);
761017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        } else if (mPrimaryContactInfo != null) {
762017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            Log.d(TAG, "Update primary display info for " + mPrimaryContactInfo);
763017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
764017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            String name = getNameForCall(mPrimaryContactInfo);
765460ac2da6d15bd94fe31c1a30d12418d01d8976aTyler Gunn            String number;
766460ac2da6d15bd94fe31c1a30d12418d01d8976aTyler Gunn
767460ac2da6d15bd94fe31c1a30d12418d01d8976aTyler Gunn            boolean isChildNumberShown = !TextUtils.isEmpty(mPrimary.getChildNumber());
768061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            boolean isForwardedNumberShown = !TextUtils.isEmpty(mPrimary.getLastForwardedNumber());
769061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            boolean isCallSubjectShown = shouldShowCallSubject(mPrimary);
770061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn
771061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            if (isCallSubjectShown) {
772061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                ui.setCallSubject(mPrimary.getCallSubject());
773061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            } else {
774061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                ui.setCallSubject(null);
775061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            }
776061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn
777061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            if (isCallSubjectShown) {
778061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                number = null;
779061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            } else if (isChildNumberShown) {
780460ac2da6d15bd94fe31c1a30d12418d01d8976aTyler Gunn                number = mContext.getString(R.string.child_number, mPrimary.getChildNumber());
781061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            } else if (isForwardedNumberShown) {
782061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                // Use last forwarded number instead of second line, if present.
783061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                number = mPrimary.getLastForwardedNumber();
784460ac2da6d15bd94fe31c1a30d12418d01d8976aTyler Gunn            } else {
785460ac2da6d15bd94fe31c1a30d12418d01d8976aTyler Gunn                number = getNumberForCall(mPrimaryContactInfo);
786460ac2da6d15bd94fe31c1a30d12418d01d8976aTyler Gunn            }
787460ac2da6d15bd94fe31c1a30d12418d01d8976aTyler Gunn
788061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            ui.showForwardIndicator(isForwardedNumberShown);
789061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            maybeShowHdAudioIcon();
790061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn
791017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            boolean nameIsNumber = name != null && name.equals(mPrimaryContactInfo.number);
792f9aea95a921900b552bc808513f7c42c15e16f6cTony Mak            // Call with caller that is a work contact.
7937b2b43c0589ec79ba74359f7d9d6e0450fc322abVictor Chang            boolean isWorkContact = (mPrimaryContactInfo.userType == ContactsUtils.USER_TYPE_WORK);
794017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setPrimary(
795017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    number,
796017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    name,
797017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    nameIsNumber,
798061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                    isChildNumberShown || isCallSubjectShown ? null : mPrimaryContactInfo.label,
799017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    mPrimaryContactInfo.photo,
800fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn                    mPrimaryContactInfo.isSipCall,
8017b2b43c0589ec79ba74359f7d9d6e0450fc322abVictor Chang                    showContactPhoto,
802f9aea95a921900b552bc808513f7c42c15e16f6cTony Mak                    hasWorkCallProperty || isWorkContact);
803687390732d6253903141d89d734113373d0e3c13Nancy Chen
804687390732d6253903141d89d734113373d0e3c13Nancy Chen            updateContactInteractions();
8056d231f52e4f00399330d772f2a337283803f3a9dYorke Lee        } else {
806017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            // Clear the primary display info.
8077b2b43c0589ec79ba74359f7d9d6e0450fc322abVictor Chang            ui.setPrimary(null, null, false, null, null, false, false, false);
808d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        }
809d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon
81089aad4adce9cc66294f15391fa3e10f3123d3d92Andrew Lee        if (mEmergencyCallListener != null) {
81193a6fd8d4a6f3ff4732cb4fb3b9df7c522b92823Tyler Gunn            boolean isEmergencyCall = mPrimary.isEmergencyCall();
81289aad4adce9cc66294f15391fa3e10f3123d3d92Andrew Lee            mEmergencyCallListener.onCallUpdated((BaseFragment) ui, isEmergencyCall);
81389aad4adce9cc66294f15391fa3e10f3123d3d92Andrew Lee        }
814d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    }
815d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon
816017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private void updateSecondaryDisplayInfo() {
81742373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        final CallCardUi ui = getUi();
81842373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        if (ui == null) {
81942373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            return;
82042373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
82142373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
822017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        if (mSecondary == null) {
823017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            // Clear the secondary display info.
824e96c83ad0a8f52a060d0ba7eac2290eb95b3b1c5Tyler Gunn            ui.setSecondary(false, null, false, null, null, false /* isConference */,
825a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn                    false /* isVideoCall */, mIsFullscreen);
826017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            return;
827017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        }
828c4bacb09c398a615154dc97b066179e77dc57599Yorke Lee
829017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        if (mSecondary.isConferenceCall()) {
830017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setSecondary(
831017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    true /* show */,
832017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getConferenceString(mSecondary),
833017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    false /* nameIsNumber */,
834017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    null /* label */,
835017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getCallProviderLabel(mSecondary),
836e96c83ad0a8f52a060d0ba7eac2290eb95b3b1c5Tyler Gunn                    true /* isConference */,
837a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn                    mSecondary.isVideoCall(mContext),
838a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn                    mIsFullscreen);
839017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        } else if (mSecondaryContactInfo != null) {
840017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            Log.d(TAG, "updateSecondaryDisplayInfo() " + mSecondaryContactInfo);
841017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            String name = getNameForCall(mSecondaryContactInfo);
842017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            boolean nameIsNumber = name != null && name.equals(mSecondaryContactInfo.number);
843017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setSecondary(
844017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    true /* show */,
845017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    name,
846017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    nameIsNumber,
847017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    mSecondaryContactInfo.label,
848017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getCallProviderLabel(mSecondary),
849e96c83ad0a8f52a060d0ba7eac2290eb95b3b1c5Tyler Gunn                    false /* isConference */,
850a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn                    mSecondary.isVideoCall(mContext),
851a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn                    mIsFullscreen);
85242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        } else {
853017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            // Clear the secondary display info.
854e96c83ad0a8f52a060d0ba7eac2290eb95b3b1c5Tyler Gunn            ui.setSecondary(false, null, false, null, null, false /* isConference */,
855a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn                    false /* isVideoCall */, mIsFullscreen);
85642373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
85742373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng    }
85842373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
8598a80413884d1afbd7e021d581546c2245f068619Nancy Chen
8608a80413884d1afbd7e021d581546c2245f068619Nancy Chen    /**
8618a80413884d1afbd7e021d581546c2245f068619Nancy Chen     * Gets the phone account to display for a call.
8628a80413884d1afbd7e021d581546c2245f068619Nancy Chen     */
8638a80413884d1afbd7e021d581546c2245f068619Nancy Chen    private PhoneAccount getAccountForCall(Call call) {
8648a80413884d1afbd7e021d581546c2245f068619Nancy Chen        PhoneAccountHandle accountHandle = call.getAccountHandle();
8658a80413884d1afbd7e021d581546c2245f068619Nancy Chen        if (accountHandle == null) {
8668a80413884d1afbd7e021d581546c2245f068619Nancy Chen            return null;
8678a80413884d1afbd7e021d581546c2245f068619Nancy Chen        }
86829c01f36e9381b7bad135a9701aa3dffaf495b2fNancy Chen        return TelecomManagerCompat.getPhoneAccount(
86929c01f36e9381b7bad135a9701aa3dffaf495b2fNancy Chen                InCallPresenter.getInstance().getTelecomManager(),
87029c01f36e9381b7bad135a9701aa3dffaf495b2fNancy Chen                accountHandle);
8718a80413884d1afbd7e021d581546c2245f068619Nancy Chen    }
8728a80413884d1afbd7e021d581546c2245f068619Nancy Chen
8733d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    /**
8743d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon     * Returns the gateway number for any existing outgoing call.
8753d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon     */
8763d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    private String getGatewayNumber() {
8773d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        if (hasOutgoingGatewayCall()) {
87807fd3fd3ac627faf1771ba4699fe53a0479f2293Nancy Chen            return getNumberFromHandle(mPrimary.getGatewayInfo().getGatewayAddress());
8793d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        }
8803d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        return null;
8813d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    }
8823d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon
8833d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    /**
8848a80413884d1afbd7e021d581546c2245f068619Nancy Chen     * Return the string label to represent the call provider
8858a80413884d1afbd7e021d581546c2245f068619Nancy Chen     */
8868a80413884d1afbd7e021d581546c2245f068619Nancy Chen    private String getCallProviderLabel(Call call) {
8878a80413884d1afbd7e021d581546c2245f068619Nancy Chen        PhoneAccount account = getAccountForCall(call);
888170e405691a237b74fbecbf41208cbaa5bd36442Nancy Chen        TelecomManager mgr = InCallPresenter.getInstance().getTelecomManager();
88915b3fb7207bc356b2ee6ce1e8264d8242bb092ecJay Shrauner        if (account != null && !TextUtils.isEmpty(account.getLabel())
89029c01f36e9381b7bad135a9701aa3dffaf495b2fNancy Chen                && TelecomManagerCompat.getCallCapablePhoneAccounts(mgr).size() > 1) {
8918a80413884d1afbd7e021d581546c2245f068619Nancy Chen            return account.getLabel().toString();
892bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen        }
893bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen        return null;
894bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen    }
895bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen
896bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen    /**
897bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen     * Returns the label (line of text above the number/name) for any given call.
898f8b0677bed382046cb26e38a8442a9509d1e835cSailesh Nepal     * For example, "calling via [Account/Google Voice]" for outgoing calls.
8993d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon     */
900bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen    private String getConnectionLabel() {
9019124b77e489772fb7c6aac36f83d86ffdf83748fJay Shrauner        StatusHints statusHints = mPrimary.getTelecomCall().getDetails().getStatusHints();
902ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        if (statusHints != null && !TextUtils.isEmpty(statusHints.getLabel())) {
903ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            return statusHints.getLabel().toString();
904ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        }
905ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal
9063d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        if (hasOutgoingGatewayCall() && getUi() != null) {
907bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen            // Return the label for the gateway app on outgoing calls.
9083d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon            final PackageManager pm = mContext.getPackageManager();
9093d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon            try {
910ae73c9aab1c716d9ef85f4f0dbc6f69e97a8f7e6Sailesh Nepal                ApplicationInfo info = pm.getApplicationInfo(
911ae73c9aab1c716d9ef85f4f0dbc6f69e97a8f7e6Sailesh Nepal                        mPrimary.getGatewayInfo().getGatewayProviderPackageName(), 0);
912bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen                return pm.getApplicationLabel(info).toString();
9133d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon            } catch (PackageManager.NameNotFoundException e) {
914bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen                Log.e(this, "Gateway Application Not Found.", e);
915bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen                return null;
9163d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon            }
9173d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        }
9188a80413884d1afbd7e021d581546c2245f068619Nancy Chen        return getCallProviderLabel(mPrimary);
9193d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    }
9203d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon
9216c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee    private Drawable getCallStateIcon() {
9226c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee        // Return connection icon if one exists.
9239124b77e489772fb7c6aac36f83d86ffdf83748fJay Shrauner        StatusHints statusHints = mPrimary.getTelecomCall().getDetails().getStatusHints();
92418aaec1d250a5c5239ec5f06363f6fbb2519c55eIhab Awad        if (statusHints != null && statusHints.getIcon() != null) {
92518aaec1d250a5c5239ec5f06363f6fbb2519c55eIhab Awad            Drawable icon = statusHints.getIcon().loadDrawable(mContext);
926ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            if (icon != null) {
927ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                return icon;
928ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            }
929ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        }
9306c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee
931ab86f04c5ed385eafcd9099fa4ca50fd2f4c5c33Nancy Chen        return null;
932ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal    }
933ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal
9343d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    private boolean hasOutgoingGatewayCall() {
935b040158d7bb2f5c7d06bd2c1db5e96adafbcff3eTyler Gunn        // We only display the gateway information while STATE_DIALING so return false for any other
9363d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        // call state.
93742373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        // TODO: mPrimary can be null because this is called from updatePrimaryDisplayInfo which
93842373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        // is also called after a contact search completes (call is not present yet).  Split the
93942373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        // UI update so it can receive independent updates.
94042373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        if (mPrimary == null) {
94142373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            return false;
94242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
943ae73c9aab1c716d9ef85f4f0dbc6f69e97a8f7e6Sailesh Nepal        return Call.State.isDialing(mPrimary.getState()) && mPrimary.getGatewayInfo() != null &&
944ae73c9aab1c716d9ef85f4f0dbc6f69e97a8f7e6Sailesh Nepal                !mPrimary.getGatewayInfo().isEmpty();
9453d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    }
9463d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon
947d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    /**
948d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon     * Gets the name to display for the call.
949d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon     */
9509cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell    @NeededForTesting
9519cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell    String getNameForCall(ContactCacheEntry contactInfo) {
9529cc21097e06859c8e3b36b9f18e5bad7c739c5f8Brandon Maxwell        String preferredName = ContactDisplayUtils.getPreferredDisplayName(
9539cc21097e06859c8e3b36b9f18e5bad7c739c5f8Brandon Maxwell                contactInfo.namePrimary,
9549cc21097e06859c8e3b36b9f18e5bad7c739c5f8Brandon Maxwell                contactInfo.nameAlternative,
9559cc21097e06859c8e3b36b9f18e5bad7c739c5f8Brandon Maxwell                mContactsPreferences);
9568c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwell        if (TextUtils.isEmpty(preferredName)) {
957d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            return contactInfo.number;
9586d231f52e4f00399330d772f2a337283803f3a9dYorke Lee        }
9598c03464c2c348af3bcd3bdfb911cd1bf17a760d0Brandon Maxwell        return preferredName;
960d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    }
9616d231f52e4f00399330d772f2a337283803f3a9dYorke Lee
962d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    /**
963d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon     * Gets the number to display for a call.
964d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon     */
9659cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell    @NeededForTesting
9669cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell    String getNumberForCall(ContactCacheEntry contactInfo) {
9679cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell        // If the name is empty, we use the number for the name...so don't show a second
968d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // number in the number field
9699cc21097e06859c8e3b36b9f18e5bad7c739c5f8Brandon Maxwell        String preferredName = ContactDisplayUtils.getPreferredDisplayName(
9709cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell                    contactInfo.namePrimary,
9719cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell                    contactInfo.nameAlternative,
9729cc21097e06859c8e3b36b9f18e5bad7c739c5f8Brandon Maxwell                    mContactsPreferences);
9739cd03e1e3c319f7633638615074da3028eb45c2bBrandon Maxwell        if (TextUtils.isEmpty(preferredName)) {
9741c6bb983216392b5ceb36cea42a46631f68a3dd6Jay Shrauner            return contactInfo.location;
975d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        }
976d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        return contactInfo.number;
977d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    }
9786d231f52e4f00399330d772f2a337283803f3a9dYorke Lee
979e5c0d8bdf8269f9435e0381889e4870f36a43f07Andrew Lee    public void secondaryInfoClicked() {
98080c49a823543c9fb93420d05a6375fb1ff4ca631Yorke Lee        if (mSecondary == null) {
9818b514c9571c587b2e87edfffa38afde4f0458579Nancy Chen            Log.w(this, "Secondary info clicked but no secondary call.");
98280c49a823543c9fb93420d05a6375fb1ff4ca631Yorke Lee            return;
98380c49a823543c9fb93420d05a6375fb1ff4ca631Yorke Lee        }
98480c49a823543c9fb93420d05a6375fb1ff4ca631Yorke Lee
985248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal        Log.i(this, "Swapping call to foreground: " + mSecondary);
9864b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunn        TelecomAdapter.getInstance().unholdCall(mSecondary.getId());
98790995fcb6778a9cb6dc35be12aeb1a5d0dcbb5aeYorke Lee    }
98890995fcb6778a9cb6dc35be12aeb1a5d0dcbb5aeYorke Lee
9897e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee    public void endCallClicked() {
9907e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee        if (mPrimary == null) {
9917e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee            return;
9927e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee        }
9937e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee
994cb2f51bf1acf387f391ca9c8cae8a2344eb2936aEvan Charlton        Log.i(this, "Disconnecting call: " + mPrimary);
995d274e92414988c72708af270e3799b83b4e3b40cJay Shrauner        final String callId = mPrimary.getId();
99633478eae2d380adfcc7ef3f51816ff391a42ebb7Ihab Awad        mPrimary.setState(Call.State.DISCONNECTING);
99733478eae2d380adfcc7ef3f51816ff391a42ebb7Ihab Awad        CallList.getInstance().onUpdate(mPrimary);
998d274e92414988c72708af270e3799b83b4e3b40cJay Shrauner        TelecomAdapter.getInstance().disconnectCall(callId);
9997e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee    }
10007e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee
1001da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordon    private String getNumberFromHandle(Uri handle) {
1002da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordon        return handle == null ? "" : handle.getSchemeSpecificPart();
1003da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordon    }
1004da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordon
1005350fff554bf7ed5b9e91985935488771156953abTyler Gunn    /**
10063326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn     * Handles a change to the fullscreen mode of the in-call UI.
1007350fff554bf7ed5b9e91985935488771156953abTyler Gunn     *
10083326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn     * @param isFullscreenMode {@code True} if the in-call UI is entering full screen mode.
1009350fff554bf7ed5b9e91985935488771156953abTyler Gunn     */
1010350fff554bf7ed5b9e91985935488771156953abTyler Gunn    @Override
10113326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn    public void onFullscreenModeChanged(boolean isFullscreenMode) {
1012a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn        mIsFullscreen = isFullscreenMode;
1013350fff554bf7ed5b9e91985935488771156953abTyler Gunn        final CallCardUi ui = getUi();
1014350fff554bf7ed5b9e91985935488771156953abTyler Gunn        if (ui == null) {
1015350fff554bf7ed5b9e91985935488771156953abTyler Gunn            return;
1016350fff554bf7ed5b9e91985935488771156953abTyler Gunn        }
10173326f42f13f6f8cd8934aec615078a8890212402Tyler Gunn        ui.setCallCardVisible(!isFullscreenMode);
1018a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn        ui.setSecondaryInfoVisible(!isFullscreenMode);
1019a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn    }
1020a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn
1021a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn    @Override
1022a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn    public void onSecondaryCallerInfoVisibilityChanged(boolean isVisible, int height) {
1023a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn        // No-op - the Call Card is the origin of this event.
1024350fff554bf7ed5b9e91985935488771156953abTyler Gunn    }
1025350fff554bf7ed5b9e91985935488771156953abTyler Gunn
10268cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    private boolean isPrimaryCallActive() {
10278cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        return mPrimary != null && mPrimary.getState() == Call.State.ACTIVE;
10288cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    }
10298cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
1030017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private String getConferenceString(Call call) {
103159585f80dce30e53ea30f08f11089f6cdd6e41fbAndrew Lee        boolean isGenericConference = call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE);
1032017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        Log.v(this, "getConferenceString: " + isGenericConference);
1033017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
1034017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        final int resId = isGenericConference
1035017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                ? R.string.card_title_in_call : R.string.card_title_conf_call;
1036017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        return mContext.getResources().getString(resId);
1037017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    }
1038017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
1039017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private Drawable getConferencePhoto(Call call) {
104059585f80dce30e53ea30f08f11089f6cdd6e41fbAndrew Lee        boolean isGenericConference = call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE);
1041017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        Log.v(this, "getConferencePhoto: " + isGenericConference);
1042017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
1043017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        final int resId = isGenericConference
1044017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                ? R.drawable.img_phone : R.drawable.img_conference;
1045017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        Drawable photo = mContext.getResources().getDrawable(resId);
1046017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        photo.setAutoMirrored(true);
1047017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        return photo;
1048017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    }
1049017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
1050fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee    private boolean shouldShowEndCallButton(Call primary, int callState) {
1051fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee        if (primary == null) {
1052fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee            return false;
1053fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee        }
10544c058612097eba72b33d83f5612012a90167957aYorke Lee        if ((!Call.State.isConnectingOrConnected(callState)
10554c058612097eba72b33d83f5612012a90167957aYorke Lee                && callState != Call.State.DISCONNECTING) || callState == Call.State.INCOMING) {
1056fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee            return false;
1057fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee        }
1058fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee        if (mPrimary.getSessionModificationState()
1059fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee                == Call.SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
1060fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee            return false;
1061fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee        }
1062fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee        return true;
1063fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee    }
1064fdf53779ba6d68e28e005e3645378aea7bd76ec0Yorke Lee
1065e1c196e04154815e2fb55801af73beb9fb2aa324Qi Wang    private void maybeSendAccessibilityEvent(InCallState oldState, InCallState newState,
1066e1c196e04154815e2fb55801af73beb9fb2aa324Qi Wang                                             boolean primaryChanged) {
10670dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee        if (mContext == null) {
10680dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee            return;
10690dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee        }
10700dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee        final AccessibilityManager am = (AccessibilityManager) mContext.getSystemService(
10710dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee                Context.ACCESSIBILITY_SERVICE);
10720dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee        if (!am.isEnabled()) {
10730dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee            return;
10740dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee        }
1075e1c196e04154815e2fb55801af73beb9fb2aa324Qi Wang        // Announce the current call if it's new incoming/outgoing call or primary call is changed
1076e1c196e04154815e2fb55801af73beb9fb2aa324Qi Wang        // due to switching calls between two ongoing calls (one is on hold).
10770dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee        if ((oldState != InCallState.OUTGOING && newState == InCallState.OUTGOING)
1078e1c196e04154815e2fb55801af73beb9fb2aa324Qi Wang                || (oldState != InCallState.INCOMING && newState == InCallState.INCOMING)
1079e1c196e04154815e2fb55801af73beb9fb2aa324Qi Wang                || primaryChanged) {
10800dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee            if (getUi() != null) {
10810dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee                getUi().sendAccessibilityAnnouncement();
10820dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee            }
10830dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee        }
10840dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee    }
10850dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee
1086061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    /**
1087061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     * Determines whether the call subject should be visible on the UI.  For the call subject to be
1088061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     * visible, the call has to be in an incoming or waiting state, and the subject must not be
1089061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     * empty.
1090061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     *
1091061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     * @param call The call.
1092061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     * @return {@code true} if the subject should be shown, {@code false} otherwise.
1093061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn     */
1094061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    private boolean shouldShowCallSubject(Call call) {
1095061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        if (call == null) {
1096061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn            return false;
1097061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        }
1098061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn
1099061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        boolean isIncomingOrWaiting = mPrimary.getState() == Call.State.INCOMING ||
1100061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn                mPrimary.getState() == Call.State.CALL_WAITING;
1101edf496040d42e16909ffdbf4810dc9b1e5b026f8Tyler Gunn        return isIncomingOrWaiting && !TextUtils.isEmpty(call.getCallSubject()) &&
1102edf496040d42e16909ffdbf4810dc9b1e5b026f8Tyler Gunn                call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED &&
1103edf496040d42e16909ffdbf4810dc9b1e5b026f8Tyler Gunn                call.isCallSubjectSupported();
1104061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn    }
1105061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn
110681ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn    /**
110781ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn     * Determines whether the "note sent" toast should be shown.  It should be shown for a new
110881ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn     * outgoing call with a subject.
110981ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn     *
111081ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn     * @param call The call
111181ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn     * @return {@code true} if the toast should be shown, {@code false} otherwise.
111281ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn     */
111381ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn    private boolean shouldShowNoteSentToast(Call call) {
111441c304dae70a534f0e9b345e905f30ec76b9ea73Brandon Maxwell        return call != null && hasCallSubject(call) && (call.getState() == Call.State.DIALING
111541c304dae70a534f0e9b345e905f30ec76b9ea73Brandon Maxwell                || call.getState() == Call.State.CONNECTING);
111641c304dae70a534f0e9b345e905f30ec76b9ea73Brandon Maxwell    }
111741c304dae70a534f0e9b345e905f30ec76b9ea73Brandon Maxwell
111841c304dae70a534f0e9b345e905f30ec76b9ea73Brandon Maxwell    private static boolean hasCallSubject(Call call) {
11196a4e9d87031f1f803ce9b64844f71c73e9502a7eSailesh Nepal        return !TextUtils.isEmpty(call.getTelecomCall().getDetails().getIntentExtras()
11206a4e9d87031f1f803ce9b64844f71c73e9502a7eSailesh Nepal                .getString(TelecomManager.EXTRA_CALL_SUBJECT));
112181ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn    }
112281ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn
1123d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    public interface CallCardUi extends Ui {
1124d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        void setVisible(boolean on);
1125687390732d6253903141d89d734113373d0e3c13Nancy Chen        void setContactContextTitle(View listHeaderView);
1126687390732d6253903141d89d734113373d0e3c13Nancy Chen        void setContactContextContent(ListAdapter listAdapter);
1127e838ba2b69cf85b0d938467477e47cce4af2bfb6Nancy Chen        void showContactContext(boolean show);
1128350fff554bf7ed5b9e91985935488771156953abTyler Gunn        void setCallCardVisible(boolean visible);
1129d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        void setPrimary(String number, String name, boolean nameIsNumber, String label,
1130f9aea95a921900b552bc808513f7c42c15e16f6cTony Mak                Drawable photo, boolean isSipCall, boolean isContactPhotoShown, boolean isWorkCall);
1131c4bacb09c398a615154dc97b066179e77dc57599Yorke Lee        void setSecondary(boolean show, String name, boolean nameIsNumber, String label,
1132a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn                String providerLabel, boolean isConference, boolean isVideoCall,
1133a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn                boolean isFullscreen);
1134a860052619d1c3d13a33629e97a8e05509fa0e1bTyler Gunn        void setSecondaryInfoVisible(boolean visible);
113550dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Lee        void setCallState(int state, int videoState, int sessionModificationState,
113650dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Lee                DisconnectCause disconnectCause, String connectionLabel,
1137dc3122e9d7bc0c6f5fef00bed36bb300c40afadfAndrew Lee                Drawable connectionIcon, String gatewayNumber, boolean isWifi,
11387f07f9f6f1e209359cf069b1fa4b0dfc0b371f64Victor Chang                boolean isConference, boolean isWorkCall);
1139f0dfbe2e89df0d58efde8b290662072c081e146aNancy Chen        void setPrimaryCallElapsedTime(boolean show, long duration);
114042373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        void setPrimaryName(String name, boolean nameIsNumber);
1141fd640ab7b1efe30574c96a12b06627f365d0432aTyler Gunn        void setPrimaryImage(Drawable image, boolean isVisible);
114242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        void setPrimaryPhoneNumber(String phoneNumber);
114342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        void setPrimaryLabel(String label);
1144c44f654b992c6ab8a4acff9cb5aa2678c58755d9Andrew Lee        void setEndCallButtonEnabled(boolean enabled, boolean animate);
11450786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        void setCallbackNumber(String number, boolean isEmergencyCalls);
1146061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        void setCallSubject(String callSubject);
1147350fff554bf7ed5b9e91985935488771156953abTyler Gunn        void setProgressSpinnerVisible(boolean visible);
11488cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        void showHdAudioIndicator(boolean visible);
1149061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        void showForwardIndicator(boolean visible);
11501da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee        void showManageConferenceCallButton(boolean visible);
11510cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        boolean isManageConferenceVisible();
1152061fc22101930c3c69f454c85fbb85ff2c5c92fbTyler Gunn        boolean isCallSubjectVisible();
11536596e1be9d3e15e2cbe4a5e9c3824ee47a0e2744Yorke Lee        void animateForNewOutgoingCall();
11540dbdccd2aa0e6828e1332adf83e630a74527a248Yorke Lee        void sendAccessibilityAnnouncement();
115581ae21863de1fe836f5e62aa5e324a35a8eb7d06Tyler Gunn        void showNoteSentToast();
1156be8060acf5ecbb916c161d3249813098c0611ed5Yorke Lee    }
1157c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon}
1158