CallCardPresenter.java revision 8cdac35f2aa80d1732dfc5fdaee1a21035a1bfcb
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
19f925b504266f80ec0b74212c403475f5cc790ab0Evan Charltonimport android.Manifest;
203d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordonimport android.content.Context;
21f925b504266f80ec0b74212c403475f5cc790ab0Evan Charltonimport android.content.Intent;
223d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordonimport android.content.pm.ApplicationInfo;
233d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordonimport android.content.pm.PackageManager;
246d231f52e4f00399330d772f2a337283803f3a9dYorke Leeimport android.graphics.drawable.Drawable;
25da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordonimport android.net.Uri;
268cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Leeimport android.net.wifi.WifiManager;
270e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charltonimport android.os.Bundle;
288cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Leeimport android.telecom.Call.Details;
2950dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Leeimport android.telecom.DisconnectCause;
304b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.PhoneAccount;
314b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.PhoneAccountHandle;
324b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.StatusHints;
334b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.TelecomManager;
344b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunnimport android.telecom.VideoProfile;
350e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charltonimport android.telephony.PhoneNumberUtils;
360786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charltonimport android.telephony.TelephonyManager;
37d28fc12696fa706fed70e04698275af7be7fee82Santos Cordonimport android.text.TextUtils;
38be8060acf5ecbb916c161d3249813098c0611ed5Yorke Lee
39d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordonimport com.android.incallui.ContactInfoCache.ContactCacheEntry;
40d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordonimport com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
410e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charltonimport com.android.incallui.InCallPresenter.InCallDetailsListener;
42350fff554bf7ed5b9e91985935488771156953abTyler Gunnimport com.android.incallui.InCallPresenter.InCallEventListener;
437ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordonimport com.android.incallui.InCallPresenter.InCallState;
447ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordonimport com.android.incallui.InCallPresenter.InCallStateListener;
451c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordonimport com.android.incallui.InCallPresenter.IncomingCallListener;
46f925b504266f80ec0b74212c403475f5cc790ab0Evan Charltonimport com.android.incalluibind.ObjectFactory;
475b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
485b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Leeimport java.lang.ref.WeakReference;
495b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
5042373eb59cbef15ec61ebb5c919031f293291a53Chiao Chengimport com.google.common.base.Preconditions;
51c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon
52c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon/**
53c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon * Presenter for the Call Card Fragment.
54033ed5926d2924d35b9c82898ab4e835da56b3afChiao Cheng * <p>
557ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon * This class listens for changes to InCallState and passes it along to the fragment.
56c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon */
57d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordonpublic class CallCardPresenter extends Presenter<CallCardPresenter.CallCardUi>
5891a369b019b39c5335a4f5d251691c0cdb657f7dAndrew Lee        implements InCallStateListener, IncomingCallListener, InCallDetailsListener,
599c98346835b8e902e0c2c11f14a35f4e91578e9fAndrew Lee        InCallEventListener {
60be8060acf5ecbb916c161d3249813098c0611ed5Yorke Lee
61033ed5926d2924d35b9c82898ab4e835da56b3afChiao Cheng    private static final String TAG = CallCardPresenter.class.getSimpleName();
629c4226952c74f024c09e0bd381048493993c974eAndrew Lee    private static final long CALL_TIME_UPDATE_INTERVAL_MS = 1000;
638cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon
641b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    private Call mPrimary;
65d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    private Call mSecondary;
66d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    private ContactCacheEntry mPrimaryContactInfo;
67d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    private ContactCacheEntry mSecondaryContactInfo;
6886a7f5639fd0798c3b4500166f97649949237215Andrew Lee    private CallTimer mCallTimer;
698cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
703d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    private Context mContext;
718cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    private WifiManager mWifiManager;
726d231f52e4f00399330d772f2a337283803f3a9dYorke Lee
735b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    public static class ContactLookupCallback implements ContactInfoCacheCallback {
745b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        private final WeakReference<CallCardPresenter> mCallCardPresenter;
755b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        private final boolean mIsPrimary;
765b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
775b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        public ContactLookupCallback(CallCardPresenter callCardPresenter, boolean isPrimary) {
785b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            mCallCardPresenter = new WeakReference<CallCardPresenter>(callCardPresenter);
795b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            mIsPrimary = isPrimary;
805b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
815b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
825b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        @Override
835b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
845b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            CallCardPresenter presenter = mCallCardPresenter.get();
855b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            if (presenter != null) {
865b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee                presenter.onContactInfoComplete(callId, entry, mIsPrimary);
875b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            }
885b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
895b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
905b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        @Override
915b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
925b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            CallCardPresenter presenter = mCallCardPresenter.get();
935b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            if (presenter != null) {
945b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee                presenter.onImageLoadComplete(callId, entry);
955b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            }
965b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
975b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
985b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    }
995b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
100b8eedb3b9796873eaa22847205239a51092f1a43Santos Cordon    public CallCardPresenter() {
1018cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        // create the call timer
1028cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        mCallTimer = new CallTimer(new Runnable() {
1038cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            @Override
1048cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            public void run() {
1058cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon                updateCallTime();
1068cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            }
1078cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        });
1086d231f52e4f00399330d772f2a337283803f3a9dYorke Lee    }
1096d231f52e4f00399330d772f2a337283803f3a9dYorke Lee
1101c6bb983216392b5ceb36cea42a46631f68a3dd6Jay Shrauner    public void init(Context context, Call call) {
11142373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        mContext = Preconditions.checkNotNull(context);
1128cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
11342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
1144a5339c4a266891ce560dfb6e348c3779a0a3ca1Chiao Cheng        // Call may be null if disconnect happened already.
1154a5339c4a266891ce560dfb6e348c3779a0a3ca1Chiao Cheng        if (call != null) {
116656dd4a0d8571b47ecf0ecf3e4c2eeacfd1099b3Chiao Cheng            mPrimary = call;
117656dd4a0d8571b47ecf0ecf3e4c2eeacfd1099b3Chiao Cheng
1184a5339c4a266891ce560dfb6e348c3779a0a3ca1Chiao Cheng            // start processing lookups right away.
119812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            if (!call.isConferenceCall()) {
120248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal                startContactInfoSearch(call, true, call.getState() == Call.State.INCOMING);
121812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            } else {
122017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                updateContactEntry(null, true);
123812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            }
1244a5339c4a266891ce560dfb6e348c3779a0a3ca1Chiao Cheng        }
125033ed5926d2924d35b9c82898ab4e835da56b3afChiao Cheng    }
126033ed5926d2924d35b9c82898ab4e835da56b3afChiao Cheng
127c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon    @Override
128c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon    public void onUiReady(CallCardUi ui) {
129c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon        super.onUiReady(ui);
1301b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon
13142373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        // Contact search may have completed before ui is ready.
13242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        if (mPrimaryContactInfo != null) {
133017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updatePrimaryDisplayInfo();
13442373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
13542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
1361f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon        // Register for call state changes last
1371f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon        InCallPresenter.getInstance().addListener(this);
1381c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon        InCallPresenter.getInstance().addIncomingCallListener(this);
1390e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton        InCallPresenter.getInstance().addDetailsListener(this);
140350fff554bf7ed5b9e91985935488771156953abTyler Gunn        InCallPresenter.getInstance().addInCallEventListener(this);
1411b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    }
1421b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon
1431b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    @Override
1441b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    public void onUiUnready(CallCardUi ui) {
1451b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon        super.onUiUnready(ui);
1461b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon
1471f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon        // stop getting call state changes
1481f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon        InCallPresenter.getInstance().removeListener(this);
1491c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon        InCallPresenter.getInstance().removeIncomingCallListener(this);
1500e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton        InCallPresenter.getInstance().removeDetailsListener(this);
151350fff554bf7ed5b9e91985935488771156953abTyler Gunn        InCallPresenter.getInstance().removeInCallEventListener(this);
1521f63d2d8e8db3616c16886952813b3d0473216e7Santos Cordon
1531b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon        mPrimary = null;
154d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        mPrimaryContactInfo = null;
155d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        mSecondaryContactInfo = null;
156c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon    }
157c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon
158c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon    @Override
159f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen    public void onIncomingCall(InCallState oldState, InCallState newState, Call call) {
1601c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon        // same logic should happen as with onStateChange()
161f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        onStateChange(oldState, newState, CallList.getInstance());
1621c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon    }
1631c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon
1641c22bde38cd15919e91afb0f12c558aa66f0bc91Santos Cordon    @Override
165f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen    public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
166f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        Log.d(this, "onStateChange() " + newState);
16775c86b55d45b1725c46ed2e5d77c9c43fcfd93daSantos Cordon        final CallCardUi ui = getUi();
1681b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon        if (ui == null) {
1691b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon            return;
1701b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon        }
171c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon
1727ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon        Call primary = null;
1737ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon        Call secondary = null;
1747ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon
175f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        if (newState == InCallState.INCOMING) {
1767ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon            primary = callList.getIncomingCall();
177f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
178950e3df8ac744daf2a890d279566bdba424710f9Santos Cordon            primary = callList.getOutgoingCall();
179f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen            if (primary == null) {
180f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen                primary = callList.getPendingOutgoingCall();
181f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen            }
182950e3df8ac744daf2a890d279566bdba424710f9Santos Cordon
18382f5d003e675dfd56735c745744c3d968883b23cSantos Cordon            // getCallToDisplay doesn't go through outgoing or incoming calls. It will return the
18482f5d003e675dfd56735c745744c3d968883b23cSantos Cordon            // highest priority call to display as the secondary call.
185d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            secondary = getCallToDisplay(callList, null, true);
186f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        } else if (newState == InCallState.INCALL) {
187d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            primary = getCallToDisplay(callList, null, false);
188d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            secondary = getCallToDisplay(callList, primary, true);
1897ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon        }
1907ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon
191a4bd83ad639b349fa16dbd07225017fcbf6ed550Chiao Cheng        Log.d(this, "Primary call: " + primary);
192a4bd83ad639b349fa16dbd07225017fcbf6ed550Chiao Cheng        Log.d(this, "Secondary call: " + secondary);
1937ac6cb331919db2a3a8985e4cb3299b309d9012aSantos Cordon
1949c98346835b8e902e0c2c11f14a35f4e91578e9fAndrew Lee        final boolean primaryChanged = !Call.areSame(mPrimary, primary);
1959c98346835b8e902e0c2c11f14a35f4e91578e9fAndrew Lee        final boolean secondaryChanged = !Call.areSame(mSecondary, secondary);
1969c98346835b8e902e0c2c11f14a35f4e91578e9fAndrew Lee
197812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        mSecondary = secondary;
198812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        mPrimary = primary;
199812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon
2000cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        // Refresh primary call information if either:
2010cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        // 1. Primary call changed.
2020cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        // 2. The call's ability to manage conference has changed.
2030cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        if (mPrimary != null && (primaryChanged ||
2040cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn                ui.isManageConferenceVisible() != shouldShowManageConference())) {
205812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            // primary call has changed
206248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal            mPrimaryContactInfo = ContactInfoCache.buildCacheEntryFromCall(mContext, mPrimary,
207248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal                    mPrimary.getState() == Call.State.INCOMING);
208017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updatePrimaryDisplayInfo();
209812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            maybeStartSearch(mPrimary, true);
2109c4226952c74f024c09e0bd381048493993c974eAndrew Lee            mPrimary.setSessionModificationState(Call.SessionModificationState.NO_REQUEST);
21142373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
21242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
213812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        if (mSecondary == null) {
21442373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            // Secondary call may have ended.  Update the ui.
21542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            mSecondaryContactInfo = null;
216017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updateSecondaryDisplayInfo();
217812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        } else if (secondaryChanged) {
218812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            // secondary call has changed
219248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal            mSecondaryContactInfo = ContactInfoCache.buildCacheEntryFromCall(mContext, mSecondary,
220248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal                    mSecondary.getState() == Call.State.INCOMING);
221017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updateSecondaryDisplayInfo();
222812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon            maybeStartSearch(mSecondary, false);
2239c4226952c74f024c09e0bd381048493993c974eAndrew Lee            mSecondary.setSessionModificationState(Call.SessionModificationState.NO_REQUEST);
22442373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
22542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
2269c4226952c74f024c09e0bd381048493993c974eAndrew Lee        // Start/stop timers.
2278cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (isPrimaryCallActive()) {
228a4bd83ad639b349fa16dbd07225017fcbf6ed550Chiao Cheng            Log.d(this, "Starting the calltime timer");
2299c4226952c74f024c09e0bd381048493993c974eAndrew Lee            mCallTimer.start(CALL_TIME_UPDATE_INTERVAL_MS);
2308cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        } else {
231a4bd83ad639b349fa16dbd07225017fcbf6ed550Chiao Cheng            Log.d(this, "Canceling the calltime timer");
2328cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            mCallTimer.cancel();
233f0dfbe2e89df0d58efde8b290662072c081e146aNancy Chen            ui.setPrimaryCallElapsedTime(false, 0);
2348cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        }
2358cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon
236d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        // Set the call state
2378aac1a358f2caf6c21c603bae9c9d4bd075eb155Evan Charlton        int callState = Call.State.IDLE;
238d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        if (mPrimary != null) {
23932027ae1f52232258cc8133d383459f3b4ee57e4Yorke Lee            callState = mPrimary.getState();
240ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            updatePrimaryCallState();
24182f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        } else {
2429c4226952c74f024c09e0bd381048493993c974eAndrew Lee            getUi().setCallState(
2439c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    callState,
2443718dcb345e77e421ab18a760e48f0c3a760fccaIhab Awad                    VideoProfile.VideoState.AUDIO_ONLY,
2459c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    Call.SessionModificationState.NO_REQUEST,
24650dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Lee                    new DisconnectCause(DisconnectCause.UNKNOWN),
2479c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    null,
2489c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    null,
2499c4226952c74f024c09e0bd381048493993c974eAndrew Lee                    null);
2508cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            getUi().showHdAudioIndicator(false);
25182f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        }
252e26b887e90d5e9566a5054a1ccb3ed32ae2eea2dAndrew Lee
253350fff554bf7ed5b9e91985935488771156953abTyler Gunn        // Hide/show the contact photo based on the video state.
254350fff554bf7ed5b9e91985935488771156953abTyler Gunn        // If the primary call is a video call on hold, still show the contact photo.
255350fff554bf7ed5b9e91985935488771156953abTyler Gunn        // If the primary call is an active video call, hide the contact photo.
256804baccc3aeed4ed00d1f0a62ecc37fb5ea6d9fdTyler Gunn        if (mPrimary != null) {
257cda81c2ef1ba595c35ae1d4ad72c44f3443e08fdTyler Gunn            getUi().setPhotoVisible(!(mPrimary.isVideoCall(mContext) &&
258cda81c2ef1ba595c35ae1d4ad72c44f3443e08fdTyler Gunn                    callState != Call.State.ONHOLD));
259804baccc3aeed4ed00d1f0a62ecc37fb5ea6d9fdTyler Gunn        }
260804baccc3aeed4ed00d1f0a62ecc37fb5ea6d9fdTyler Gunn
2611da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee        maybeShowManageConferenceCallButton();
2621da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee
263f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen        final boolean enableEndCallButton = Call.State.isConnectingOrConnected(callState) &&
2648aac1a358f2caf6c21c603bae9c9d4bd075eb155Evan Charlton                callState != Call.State.INCOMING && mPrimary != null;
265c44f654b992c6ab8a4acff9cb5aa2678c58755d9Andrew Lee        // Hide the end call button instantly if we're receiving an incoming call.
266c44f654b992c6ab8a4acff9cb5aa2678c58755d9Andrew Lee        getUi().setEndCallButtonEnabled(
267c44f654b992c6ab8a4acff9cb5aa2678c58755d9Andrew Lee                enableEndCallButton, callState != Call.State.INCOMING /* animate */);
2681b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    }
2691b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon
2701b3e3f1056317fca5be4e7535d722da8876b2cbfSantos Cordon    @Override
2718cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    public void onDetailsChanged(Call call, Details details) {
272ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        updatePrimaryCallState();
273e0ec202ff354be3cd3bded7af8fca678dea7fb61Andrew Lee
2748cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (call.can(Details.CAPABILITY_MANAGE_CONFERENCE) !=
2758cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee                Details.can(details.getCallCapabilities(), Details.CAPABILITY_MANAGE_CONFERENCE)) {
276e0ec202ff354be3cd3bded7af8fca678dea7fb61Andrew Lee            maybeShowManageConferenceCallButton();
277e0ec202ff354be3cd3bded7af8fca678dea7fb61Andrew Lee        }
2780786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton    }
2790786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton
2800786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton    private String getSubscriptionNumber() {
2810786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        // If it's an emergency call, and they're not populating the callback number,
2820786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        // then try to fall back to the phone sub info (to hopefully get the SIM's
2830786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        // number directly from the telephony layer).
28476e1b4bfb2e21300068f10cc94922214e62c8f86Evan Charlton        PhoneAccountHandle accountHandle = mPrimary.getAccountHandle();
28576e1b4bfb2e21300068f10cc94922214e62c8f86Evan Charlton        if (accountHandle != null) {
286170e405691a237b74fbecbf41208cbaa5bd36442Nancy Chen            TelecomManager mgr = InCallPresenter.getInstance().getTelecomManager();
287b6f45c2768f5af916ee3257542a3ab03611c1f5eEvan Charlton            PhoneAccount account = mgr.getPhoneAccount(accountHandle);
288b6f45c2768f5af916ee3257542a3ab03611c1f5eEvan Charlton            if (account != null) {
2899e27dca4bc1be09bcb1d283b2fe3ba3d49f0748bAndrew Lee                return getNumberFromHandle(account.getSubscriptionAddress());
290c108b49ad5440fbf381c9a589d8a217da3efcb64Ihab Awad            }
2910e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton        }
2920786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        return null;
2930e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton    }
2940e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton
295ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal    private void updatePrimaryCallState() {
296ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        if (getUi() != null && mPrimary != null) {
297ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            getUi().setCallState(
298ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    mPrimary.getState(),
299ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    mPrimary.getVideoState(),
300ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    mPrimary.getSessionModificationState(),
301ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    mPrimary.getDisconnectCause(),
302ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    getConnectionLabel(),
3036c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee                    getCallStateIcon(),
304ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                    getGatewayNumber());
3058cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
3068cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            boolean showHdAudioIndicator =
3078cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee                    isPrimaryCallActive() && primaryCallCan(Details.CAPABILITY_HIGH_DEF_AUDIO);
3088cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            getUi().showHdAudioIndicator(showHdAudioIndicator);
3098cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
310ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            setCallbackNumber();
311ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        }
312ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal    }
3130786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton
3141da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee    /**
315289d53ed1c879e983dcb021fa677212ab4566bbdSantos Cordon     * Only show the conference call button if we can manage the conference.
3161da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee     */
3171da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee    private void maybeShowManageConferenceCallButton() {
3180cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        getUi().showManageConferenceCallButton(shouldShowManageConference());
3190cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn    }
3200cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn
3210cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn    /**
3220cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     * Determines if the manage conference button should be visible, based on the current primary
3230cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     * call.
3240cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     *
3250cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     * @return {@code True} if the manage conference button should be visible.
3260cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn     */
3270cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn    private boolean shouldShowManageConference() {
328e16e53167b367656840df665457e71e66bc966deAndrew Lee        if (mPrimary == null) {
3290cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn            return false;
330e16e53167b367656840df665457e71e66bc966deAndrew Lee        }
331e16e53167b367656840df665457e71e66bc966deAndrew Lee
3328cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        return mPrimary.can(Details.CAPABILITY_MANAGE_CONFERENCE);
3331da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee    }
3341da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee
335ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal    private void setCallbackNumber() {
336ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        String callbackNumber = null;
3370786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton
338ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        boolean isEmergencyCall = PhoneNumberUtils.isEmergencyNumber(
339ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                getNumberFromHandle(mPrimary.getHandle()));
340ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        if (isEmergencyCall) {
341ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            callbackNumber = getSubscriptionNumber();
342ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        } else {
343ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            StatusHints statusHints = mPrimary.getTelecommCall().getDetails().getStatusHints();
344ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            if (statusHints != null) {
345ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                Bundle extras = statusHints.getExtras();
346ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                if (extras != null) {
3474b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunn                    callbackNumber = extras.getString(TelecomManager.EXTRA_CALL_BACK_NUMBER);
3480786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton                }
3490e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton            }
3500786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        }
3510786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton
3526dfd1963ecc89ecb2c76793ab40af26e35654285Nancy Chen        TelecomManager mgr = InCallPresenter.getInstance().getTelecomManager();
3536dfd1963ecc89ecb2c76793ab40af26e35654285Nancy Chen        String simNumber = mgr.getLine1Number(mPrimary.getAccountHandle());
354ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        if (PhoneNumberUtils.compare(callbackNumber, simNumber)) {
3550786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton            Log.d(this, "Numbers are the same; not showing the callback number");
356ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            callbackNumber = null;
3570786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        }
358ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal
3590786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        getUi().setCallbackNumber(callbackNumber, isEmergencyCall);
3600e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton    }
3610e84b83f23af354872b64fc1d1d77d22d825c71cEvan Charlton
3628cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon    public void updateCallTime() {
3638cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        final CallCardUi ui = getUi();
3648cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon
3658cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (ui == null) {
3668cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            mCallTimer.cancel();
3678cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        } else if (!isPrimaryCallActive()) {
3688cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            ui.setPrimaryCallElapsedTime(false, 0);
3698cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon            mCallTimer.cancel();
370899251968ee4ee3cb375032185653ecd888e3f8dSantos Cordon        } else {
371c6f4e50a4130fb8f7247f5eab6849829701ad484Sailesh Nepal            final long callStart = mPrimary.getConnectTimeMillis();
372899251968ee4ee3cb375032185653ecd888e3f8dSantos Cordon            final long duration = System.currentTimeMillis() - callStart;
373f0dfbe2e89df0d58efde8b290662072c081e146aNancy Chen            ui.setPrimaryCallElapsedTime(true, duration);
3748cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon        }
3758cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon    }
3768cd746f0342f8c5def42f64b08cf6df741bb61bbSantos Cordon
377f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton    public void onCallStateButtonTouched() {
37871afa1c7d1e1f49af56c24bdc64d30d80cf1ea68Evan Charlton        Intent broadcastIntent = ObjectFactory.getCallStateButtonBroadcastIntent(mContext);
379f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton        if (broadcastIntent != null) {
380f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton            Log.d(this, "Sending call state button broadcast: ", broadcastIntent);
381f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton            mContext.sendBroadcast(broadcastIntent, Manifest.permission.READ_PHONE_STATE);
382f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton        }
383f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton    }
384f925b504266f80ec0b74212c403475f5cc790ab0Evan Charlton
385812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon    private void maybeStartSearch(Call call, boolean isPrimary) {
386812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        // no need to start search for conference calls which show generic info.
387812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        if (call != null && !call.isConferenceCall()) {
388248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal            startContactInfoSearch(call, isPrimary, call.getState() == Call.State.INCOMING);
389812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon        }
390812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon    }
391812c0e88d5d86c29e4942f9a1010a95cce789657Santos Cordon
39282f5d003e675dfd56735c745744c3d968883b23cSantos Cordon    /**
393d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon     * Starts a query for more contact data for the save primary and secondary calls.
394d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon     */
395248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal    private void startContactInfoSearch(final Call call, final boolean isPrimary,
396248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal            boolean isIncoming) {
39742373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        final ContactInfoCache cache = ContactInfoCache.getInstance(mContext);
39842373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
3995b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        cache.findInfo(call, isIncoming, new ContactLookupCallback(this, isPrimary));
4005b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    }
4017d2fb8620b808c28c49bde99dc54f6de46fdcb44Chiao Cheng
4025b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    private void onContactInfoComplete(String callId, ContactCacheEntry entry, boolean isPrimary) {
403017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        updateContactEntry(entry, isPrimary);
4045b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        if (entry.name != null) {
4055b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            Log.d(TAG, "Contact found: " + entry);
4065b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
4075b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        if (entry.contactUri != null) {
4085b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            CallerInfoUtils.sendViewNotification(mContext, entry.contactUri);
4095b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
4105b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    }
4115b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee
4125b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee    private void onImageLoadComplete(String callId, ContactCacheEntry entry) {
4135b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        if (getUi() == null) {
4145b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            return;
4155b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
416f003346e5f9ba29e869a881bcca2e59d6ea8e0f6Nancy Chen
4175b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        if (entry.photo != null) {
4185b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            if (mPrimary != null && callId.equals(mPrimary.getId())) {
4195b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee                getUi().setPrimaryImage(entry.photo);
4205b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee            }
4215b3f5ed7d9ffa02c2255a68e6a4be969f0116104Yorke Lee        }
42242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng    }
42342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
424017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private void updateContactEntry(ContactCacheEntry entry, boolean isPrimary) {
42542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        if (isPrimary) {
42642373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            mPrimaryContactInfo = entry;
427017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updatePrimaryDisplayInfo();
428d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        } else {
42942373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            mSecondaryContactInfo = entry;
430017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            updateSecondaryDisplayInfo();
431d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        }
432d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    }
433d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon
434d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    /**
43582f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * Get the highest priority call to display.
43682f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * Goes through the calls and chooses which to return based on priority of which type of call
43782f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * to display to the user. Callers can use the "ignore" feature to get the second best call
43882f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * by passing a previously found primary call as ignore.
43982f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     *
44082f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     * @param ignore A call to ignore if found.
44182f5d003e675dfd56735c745744c3d968883b23cSantos Cordon     */
442d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    private Call getCallToDisplay(CallList callList, Call ignore, boolean skipDisconnected) {
44382f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
444d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // Active calls come second.  An active call always gets precedent.
445d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        Call retval = callList.getActiveCall();
44682f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        if (retval != null && retval != ignore) {
44782f5d003e675dfd56735c745744c3d968883b23cSantos Cordon            return retval;
44882f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        }
44982f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
450d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // Disconnected calls get primary position if there are no active calls
451d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // to let user know quickly what call has disconnected. Disconnected
452d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // calls are very short lived.
453d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        if (!skipDisconnected) {
4546258ff60a7edc760facc3f387fc9c8ceee3fb518Christine Chen            retval = callList.getDisconnectingCall();
4556258ff60a7edc760facc3f387fc9c8ceee3fb518Christine Chen            if (retval != null && retval != ignore) {
4566258ff60a7edc760facc3f387fc9c8ceee3fb518Christine Chen                return retval;
4576258ff60a7edc760facc3f387fc9c8ceee3fb518Christine Chen            }
458d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            retval = callList.getDisconnectedCall();
459d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            if (retval != null && retval != ignore) {
460d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon                return retval;
461d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            }
46282f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        }
46382f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
46482f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        // Then we go to background call (calls on hold)
46582f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        retval = callList.getBackgroundCall();
46682f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        if (retval != null && retval != ignore) {
46782f5d003e675dfd56735c745744c3d968883b23cSantos Cordon            return retval;
46882f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        }
46982f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
47082f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        // Lastly, we go to a second background call.
47182f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        retval = callList.getSecondBackgroundCall();
47282f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
47382f5d003e675dfd56735c745744c3d968883b23cSantos Cordon        return retval;
47482f5d003e675dfd56735c745744c3d968883b23cSantos Cordon    }
47582f5d003e675dfd56735c745744c3d968883b23cSantos Cordon
476017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private void updatePrimaryDisplayInfo() {
4776d231f52e4f00399330d772f2a337283803f3a9dYorke Lee        final CallCardUi ui = getUi();
478d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        if (ui == null) {
47942373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            // TODO: May also occur if search result comes back after ui is destroyed. Look into
48042373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            // removing that case completely.
48142373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            Log.d(TAG, "updatePrimaryDisplayInfo called but ui is null!");
482d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon            return;
4836d231f52e4f00399330d772f2a337283803f3a9dYorke Lee        }
484be8060acf5ecbb916c161d3249813098c0611ed5Yorke Lee
485017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        if (mPrimary == null) {
486017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            // Clear the primary display info.
487017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setPrimary(null, null, false, null, null, false);
488017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            return;
489017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        }
490017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
491017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        if (mPrimary.isConferenceCall()) {
492017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            Log.d(TAG, "Update primary display info for conference call.");
493017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
494017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setPrimary(
495017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    null /* number */,
496017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getConferenceString(mPrimary),
497017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    false /* nameIsNumber */,
498017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    null /* label */,
499017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getConferencePhoto(mPrimary),
500017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    false /* isSipCall */);
501017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        } else if (mPrimaryContactInfo != null) {
502017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            Log.d(TAG, "Update primary display info for " + mPrimaryContactInfo);
503017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
504017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            String name = getNameForCall(mPrimaryContactInfo);
505017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            String number = getNumberForCall(mPrimaryContactInfo);
506017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            boolean nameIsNumber = name != null && name.equals(mPrimaryContactInfo.number);
507017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setPrimary(
508017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    number,
509017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    name,
510017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    nameIsNumber,
511017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    mPrimaryContactInfo.label,
512017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    mPrimaryContactInfo.photo,
513017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    mPrimaryContactInfo.isSipCall);
5146d231f52e4f00399330d772f2a337283803f3a9dYorke Lee        } else {
515017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            // Clear the primary display info.
516017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setPrimary(null, null, false, null, null, false);
517d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        }
518d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon
519d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    }
520d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon
521017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private void updateSecondaryDisplayInfo() {
52242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        final CallCardUi ui = getUi();
52342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        if (ui == null) {
52442373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            return;
52542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
52642373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
527017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        if (mSecondary == null) {
528017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            // Clear the secondary display info.
529017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setSecondary(false, null, false, null, null, false /* isConference */);
530017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            return;
531017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        }
532c4bacb09c398a615154dc97b066179e77dc57599Yorke Lee
533017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        if (mSecondary.isConferenceCall()) {
534017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setSecondary(
535017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    true /* show */,
536017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getConferenceString(mSecondary),
537017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    false /* nameIsNumber */,
538017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    null /* label */,
539017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getCallProviderLabel(mSecondary),
540017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    true /* isConference */);
541017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        } else if (mSecondaryContactInfo != null) {
542017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            Log.d(TAG, "updateSecondaryDisplayInfo() " + mSecondaryContactInfo);
543017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            String name = getNameForCall(mSecondaryContactInfo);
544017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            boolean nameIsNumber = name != null && name.equals(mSecondaryContactInfo.number);
545017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setSecondary(
546017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    true /* show */,
547017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    name,
548017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    nameIsNumber,
549017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    mSecondaryContactInfo.label,
550017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    getCallProviderLabel(mSecondary),
551017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                    false /* isConference */);
55242373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        } else {
553017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            // Clear the secondary display info.
554017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee            ui.setSecondary(false, null, false, null, null, false /* isConference */);
55542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
55642373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng    }
55742373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng
5588a80413884d1afbd7e021d581546c2245f068619Nancy Chen
5598a80413884d1afbd7e021d581546c2245f068619Nancy Chen    /**
5608a80413884d1afbd7e021d581546c2245f068619Nancy Chen     * Gets the phone account to display for a call.
5618a80413884d1afbd7e021d581546c2245f068619Nancy Chen     */
5628a80413884d1afbd7e021d581546c2245f068619Nancy Chen    private PhoneAccount getAccountForCall(Call call) {
5638a80413884d1afbd7e021d581546c2245f068619Nancy Chen        PhoneAccountHandle accountHandle = call.getAccountHandle();
5648a80413884d1afbd7e021d581546c2245f068619Nancy Chen        if (accountHandle == null) {
5658a80413884d1afbd7e021d581546c2245f068619Nancy Chen            return null;
5668a80413884d1afbd7e021d581546c2245f068619Nancy Chen        }
567170e405691a237b74fbecbf41208cbaa5bd36442Nancy Chen        return InCallPresenter.getInstance().getTelecomManager().getPhoneAccount(accountHandle);
5688a80413884d1afbd7e021d581546c2245f068619Nancy Chen    }
5698a80413884d1afbd7e021d581546c2245f068619Nancy Chen
5703d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    /**
5713d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon     * Returns the gateway number for any existing outgoing call.
5723d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon     */
5733d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    private String getGatewayNumber() {
5743d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        if (hasOutgoingGatewayCall()) {
57507fd3fd3ac627faf1771ba4699fe53a0479f2293Nancy Chen            return getNumberFromHandle(mPrimary.getGatewayInfo().getGatewayAddress());
5763d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        }
5773d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        return null;
5783d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    }
5793d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon
5803d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    /**
5818a80413884d1afbd7e021d581546c2245f068619Nancy Chen     * Return the string label to represent the call provider
5828a80413884d1afbd7e021d581546c2245f068619Nancy Chen     */
5838a80413884d1afbd7e021d581546c2245f068619Nancy Chen    private String getCallProviderLabel(Call call) {
5848a80413884d1afbd7e021d581546c2245f068619Nancy Chen        PhoneAccount account = getAccountForCall(call);
585170e405691a237b74fbecbf41208cbaa5bd36442Nancy Chen        TelecomManager mgr = InCallPresenter.getInstance().getTelecomManager();
58615b3fb7207bc356b2ee6ce1e8264d8242bb092ecJay Shrauner        if (account != null && !TextUtils.isEmpty(account.getLabel())
587170e405691a237b74fbecbf41208cbaa5bd36442Nancy Chen                && mgr.hasMultipleCallCapableAccounts()) {
5888a80413884d1afbd7e021d581546c2245f068619Nancy Chen            return account.getLabel().toString();
589bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen        }
590bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen        return null;
591bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen    }
592bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen
593bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen    /**
594bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen     * Returns the label (line of text above the number/name) for any given call.
595f8b0677bed382046cb26e38a8442a9509d1e835cSailesh Nepal     * For example, "calling via [Account/Google Voice]" for outgoing calls.
5963d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon     */
597bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen    private String getConnectionLabel() {
598ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        StatusHints statusHints = mPrimary.getTelecommCall().getDetails().getStatusHints();
599ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        if (statusHints != null && !TextUtils.isEmpty(statusHints.getLabel())) {
600ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            return statusHints.getLabel().toString();
601ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        }
602ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal
6038cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        // Assume the SSID of the connection reported by the WifiManager is the name of the wifi
6048cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        // network used for calling, since currently a phone only connects to a single wifi network.
6058cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (isPrimaryCallActive() && primaryCallCan(Details.CAPABILITY_WIFI)
6068cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee                && mWifiManager.getConnectionInfo() != null) {
6078cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            return formatWifiSSID(mWifiManager.getConnectionInfo().getSSID());
6088cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        }
6098cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
6103d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        if (hasOutgoingGatewayCall() && getUi() != null) {
611bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen            // Return the label for the gateway app on outgoing calls.
6123d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon            final PackageManager pm = mContext.getPackageManager();
6133d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon            try {
614ae73c9aab1c716d9ef85f4f0dbc6f69e97a8f7e6Sailesh Nepal                ApplicationInfo info = pm.getApplicationInfo(
615ae73c9aab1c716d9ef85f4f0dbc6f69e97a8f7e6Sailesh Nepal                        mPrimary.getGatewayInfo().getGatewayProviderPackageName(), 0);
616bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen                return pm.getApplicationLabel(info).toString();
6173d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon            } catch (PackageManager.NameNotFoundException e) {
618bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen                Log.e(this, "Gateway Application Not Found.", e);
619bd93cb5444e886266b47e504138bcc65e6f09cbeNancy Chen                return null;
6203d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon            }
6213d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        }
6228a80413884d1afbd7e021d581546c2245f068619Nancy Chen        return getCallProviderLabel(mPrimary);
6233d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    }
6243d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon
6256c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee    private Drawable getCallStateIcon() {
6266c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee        // Return connection icon if one exists.
627ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        StatusHints statusHints = mPrimary.getTelecommCall().getDetails().getStatusHints();
6283718dcb345e77e421ab18a760e48f0c3a760fccaIhab Awad        if (statusHints != null && statusHints.getIconResId() != 0) {
629ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            Drawable icon = statusHints.getIcon(mContext);
630ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            if (icon != null) {
631ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal                return icon;
632ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal            }
633ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal        }
6346c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee
6358cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (isPrimaryCallActive() && primaryCallCan(Details.CAPABILITY_WIFI)) {
6368cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            return mContext.getResources().getDrawable(R.drawable.ic_signal_wifi_4_bar_18dp);
6376c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee        }
6386c3061091e59e9ab06b620c2615b897e2eca233cAndrew Lee
639ab86f04c5ed385eafcd9099fa4ca50fd2f4c5c33Nancy Chen        return null;
640ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal    }
641ee9ec0928055307dc5d1f5c7509c301951a444a5Sailesh Nepal
6423d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    private boolean hasOutgoingGatewayCall() {
6433718dcb345e77e421ab18a760e48f0c3a760fccaIhab Awad        // We only display the gateway information while STATE_DIALING so return false for any othe
6443d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon        // call state.
64542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        // TODO: mPrimary can be null because this is called from updatePrimaryDisplayInfo which
64642373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        // is also called after a contact search completes (call is not present yet).  Split the
64742373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        // UI update so it can receive independent updates.
64842373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        if (mPrimary == null) {
64942373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng            return false;
65042373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        }
651ae73c9aab1c716d9ef85f4f0dbc6f69e97a8f7e6Sailesh Nepal        return Call.State.isDialing(mPrimary.getState()) && mPrimary.getGatewayInfo() != null &&
652ae73c9aab1c716d9ef85f4f0dbc6f69e97a8f7e6Sailesh Nepal                !mPrimary.getGatewayInfo().isEmpty();
6533d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon    }
6543d3d25f2bee7cf9cdb91ef35d3028a6a62317e08Santos Cordon
655d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    /**
656d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon     * Gets the name to display for the call.
657d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon     */
658d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    private static String getNameForCall(ContactCacheEntry contactInfo) {
659d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        if (TextUtils.isEmpty(contactInfo.name)) {
660d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon            return contactInfo.number;
6616d231f52e4f00399330d772f2a337283803f3a9dYorke Lee        }
662d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        return contactInfo.name;
663d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    }
6646d231f52e4f00399330d772f2a337283803f3a9dYorke Lee
665d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    /**
666d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon     * Gets the number to display for a call.
667d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon     */
668d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon    private static String getNumberForCall(ContactCacheEntry contactInfo) {
669d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // If the name is empty, we use the number for the name...so dont show a second
670d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        // number in the number field
671d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        if (TextUtils.isEmpty(contactInfo.name)) {
6721c6bb983216392b5ceb36cea42a46631f68a3dd6Jay Shrauner            return contactInfo.location;
673d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        }
674d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        return contactInfo.number;
675d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    }
6766d231f52e4f00399330d772f2a337283803f3a9dYorke Lee
677e5c0d8bdf8269f9435e0381889e4870f36a43f07Andrew Lee    public void secondaryInfoClicked() {
67880c49a823543c9fb93420d05a6375fb1ff4ca631Yorke Lee        if (mSecondary == null) {
6798b514c9571c587b2e87edfffa38afde4f0458579Nancy Chen            Log.w(this, "Secondary info clicked but no secondary call.");
68080c49a823543c9fb93420d05a6375fb1ff4ca631Yorke Lee            return;
68180c49a823543c9fb93420d05a6375fb1ff4ca631Yorke Lee        }
68280c49a823543c9fb93420d05a6375fb1ff4ca631Yorke Lee
683248a6687e8075e9730217be73c54ee4f95501a0eSailesh Nepal        Log.i(this, "Swapping call to foreground: " + mSecondary);
6844b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunn        TelecomAdapter.getInstance().unholdCall(mSecondary.getId());
68590995fcb6778a9cb6dc35be12aeb1a5d0dcbb5aeYorke Lee    }
68690995fcb6778a9cb6dc35be12aeb1a5d0dcbb5aeYorke Lee
6877e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee    public void endCallClicked() {
6887e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee        if (mPrimary == null) {
6897e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee            return;
6907e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee        }
6917e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee
692cb2f51bf1acf387f391ca9c8cae8a2344eb2936aEvan Charlton        Log.i(this, "Disconnecting call: " + mPrimary);
69333478eae2d380adfcc7ef3f51816ff391a42ebb7Ihab Awad        mPrimary.setState(Call.State.DISCONNECTING);
69433478eae2d380adfcc7ef3f51816ff391a42ebb7Ihab Awad        CallList.getInstance().onUpdate(mPrimary);
6954b293f0f08030c97e1bb54d2f79ac2fb1a6e29aeTyler Gunn        TelecomAdapter.getInstance().disconnectCall(mPrimary.getId());
6967e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee    }
6977e5763d2b5ee7211c5d19d2b1fd2736e407a7e83Andrew Lee
698da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordon    private String getNumberFromHandle(Uri handle) {
699da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordon        return handle == null ? "" : handle.getSchemeSpecificPart();
700da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordon    }
701da7f422a46db4bfe5cf76312d23b44e60fa9d806Santos Cordon
702350fff554bf7ed5b9e91985935488771156953abTyler Gunn    /**
703350fff554bf7ed5b9e91985935488771156953abTyler Gunn     * Handles a change to the full screen video state.
704350fff554bf7ed5b9e91985935488771156953abTyler Gunn     *
705350fff554bf7ed5b9e91985935488771156953abTyler Gunn     * @param isFullScreenVideo {@code True} if the application is entering full screen video mode.
706350fff554bf7ed5b9e91985935488771156953abTyler Gunn     */
707350fff554bf7ed5b9e91985935488771156953abTyler Gunn    @Override
708350fff554bf7ed5b9e91985935488771156953abTyler Gunn    public void onFullScreenVideoStateChanged(boolean isFullScreenVideo) {
709350fff554bf7ed5b9e91985935488771156953abTyler Gunn        final CallCardUi ui = getUi();
710350fff554bf7ed5b9e91985935488771156953abTyler Gunn        if (ui == null) {
711350fff554bf7ed5b9e91985935488771156953abTyler Gunn            return;
712350fff554bf7ed5b9e91985935488771156953abTyler Gunn        }
713350fff554bf7ed5b9e91985935488771156953abTyler Gunn        ui.setCallCardVisible(!isFullScreenVideo);
714350fff554bf7ed5b9e91985935488771156953abTyler Gunn    }
715350fff554bf7ed5b9e91985935488771156953abTyler Gunn
7168cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    private boolean isPrimaryCallActive() {
7178cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        return mPrimary != null && mPrimary.getState() == Call.State.ACTIVE;
7188cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    }
7198cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
7208cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    private boolean primaryCallCan(int capability) {
7218cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        return mPrimary.getTelecommCall().getDetails().can(capability);
7228cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    }
7238cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
724017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private String getConferenceString(Call call) {
7258cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        boolean isGenericConference = call.can(Details.CAPABILITY_GENERIC_CONFERENCE);
726017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        Log.v(this, "getConferenceString: " + isGenericConference);
727017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
728017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        final int resId = isGenericConference
729017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                ? R.string.card_title_in_call : R.string.card_title_conf_call;
730017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        return mContext.getResources().getString(resId);
731017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    }
732017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
733017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    private Drawable getConferencePhoto(Call call) {
7348cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        boolean isGenericConference = call.can(Details.CAPABILITY_GENERIC_CONFERENCE);
735017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        Log.v(this, "getConferencePhoto: " + isGenericConference);
736017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
737017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        final int resId = isGenericConference
738017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                ? R.drawable.img_phone : R.drawable.img_conference;
739017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        Drawable photo = mContext.getResources().getDrawable(resId);
740017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        photo.setAutoMirrored(true);
741017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee        return photo;
742017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee    }
743017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee
7448cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    /**
7458cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee     * Strip quotations off the outside of a Wifi SSID identifier. For example, \"GoogleGuest\"
7468cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee     * becomes GoogleGuest.
7478cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee     *
7488cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee     * TODO: Move into utility class.
7498cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee     * TODO: Add unit tests.
7508cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee     *
7518cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee     * @param ssid The ssid of the wifi network.
7528cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee     */
7538cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    private String formatWifiSSID(String ssid) {
7548cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (TextUtils.isEmpty(ssid)) {
7558cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            return "";
7568cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        }
7578cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
7588cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        // Trim quotation if first character.
7598cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (ssid.charAt(0) == '\"') {
7608cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            ssid = ssid.substring(1);
7618cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        }
7628cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
7638cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        // Trim quotation if last character.
7648cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        int lastIndex = ssid.length() - 1;
7658cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        if (lastIndex >= 0 && ssid.charAt(lastIndex) == '\"') {
7668cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee            ssid = ssid.substring(0, lastIndex);
7678cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        }
7688cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
7698cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        return ssid;
7708cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee    }
7718cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee
772d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon    public interface CallCardUi extends Ui {
773d2bd93fe3929403f84367f826f07cbadc0c6366bSantos Cordon        void setVisible(boolean on);
774350fff554bf7ed5b9e91985935488771156953abTyler Gunn        void setCallCardVisible(boolean visible);
775d28fc12696fa706fed70e04698275af7be7fee82Santos Cordon        void setPrimary(String number, String name, boolean nameIsNumber, String label,
776017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                Drawable photo, boolean isSipCall);
777c4bacb09c398a615154dc97b066179e77dc57599Yorke Lee        void setSecondary(boolean show, String name, boolean nameIsNumber, String label,
778017ebaba75180c0a8deb9de3f819cbae92d45e72Andrew Lee                String providerLabel, boolean isConference);
77950dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Lee        void setCallState(int state, int videoState, int sessionModificationState,
78050dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Lee                DisconnectCause disconnectCause, String connectionLabel,
78150dbb6eeca55cf78e0fd9eb132a26787d293620aAndrew Lee                Drawable connectionIcon, String gatewayNumber);
782f0dfbe2e89df0d58efde8b290662072c081e146aNancy Chen        void setPrimaryCallElapsedTime(boolean show, long duration);
78342373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        void setPrimaryName(String name, boolean nameIsNumber);
78449ff6571403695e81dbbd83e4f61790ce9c75f6dSantos Cordon        void setPrimaryImage(Drawable image);
78542373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        void setPrimaryPhoneNumber(String phoneNumber);
78642373eb59cbef15ec61ebb5c919031f293291a53Chiao Cheng        void setPrimaryLabel(String label);
787c44f654b992c6ab8a4acff9cb5aa2678c58755d9Andrew Lee        void setEndCallButtonEnabled(boolean enabled, boolean animate);
7880786dba0baa54fd12697b4795e7c646cfe4ae5c3Evan Charlton        void setCallbackNumber(String number, boolean isEmergencyCalls);
789804baccc3aeed4ed00d1f0a62ecc37fb5ea6d9fdTyler Gunn        void setPhotoVisible(boolean isVisible);
790350fff554bf7ed5b9e91985935488771156953abTyler Gunn        void setProgressSpinnerVisible(boolean visible);
7918cdac35f2aa80d1732dfc5fdaee1a21035a1bfcbAndrew Lee        void showHdAudioIndicator(boolean visible);
7921da81ce59cfbebd4e53fdfd2011e859069120de7Andrew Lee        void showManageConferenceCallButton(boolean visible);
7930cff18feff43a6915919ac8e32fa44502aa6c320Tyler Gunn        boolean isManageConferenceVisible();
794be8060acf5ecbb916c161d3249813098c0611ed5Yorke Lee    }
795c2b430394ac612ba4dd455fb988e29bfcc4d5c5fSantos Cordon}
796