summaryrefslogtreecommitdiffstats
path: root/src/com/android/car/dialer/telecom/UiCallManager.java
blob: 0e9c2a1a3cd8a5cbc021ed3a8d4e42287c5749db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.car.dialer.telecom;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothHeadsetClientCall;
import android.bluetooth.BluetoothProfile;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.provider.CallLog;
import android.telecom.Call;
import android.telecom.CallAudioState;
import android.telecom.CallAudioState.CallAudioRoute;
import android.telecom.DisconnectCause;
import android.telecom.GatewayInfo;
import android.telecom.InCallService;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.android.car.dialer.CallListener;
import com.android.car.dialer.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * The entry point for all interactions between UI and telecom.
 */
public class UiCallManager {
    private static String TAG = "Em.TelecomMgr";

    private static final String HFP_CLIENT_CONNECTION_SERVICE_CLASS_NAME
            = "com.android.bluetooth.hfpclient.connserv.HfpClientConnectionService";
    // Rate limit how often you can place outgoing calls.
    private static final long MIN_TIME_BETWEEN_CALLS_MS = 3000;
    private static final List<Integer> sCallStateRank = new ArrayList<>();
    private static UiCallManager sUiCallManager;

    // Used to assign id's to UiCall objects as they're created.
    private static int nextCarPhoneCallId = 0;

    static {
        // States should be added from lowest rank to highest
        sCallStateRank.add(Call.STATE_DISCONNECTED);
        sCallStateRank.add(Call.STATE_DISCONNECTING);
        sCallStateRank.add(Call.STATE_NEW);
        sCallStateRank.add(Call.STATE_CONNECTING);
        sCallStateRank.add(Call.STATE_SELECT_PHONE_ACCOUNT);
        sCallStateRank.add(Call.STATE_HOLDING);
        sCallStateRank.add(Call.STATE_ACTIVE);
        sCallStateRank.add(Call.STATE_DIALING);
        sCallStateRank.add(Call.STATE_RINGING);
    }

    private Context mContext;
    private TelephonyManager mTelephonyManager;
    private long mLastPlacedCallTimeMs;

    private TelecomManager mTelecomManager;
    private InCallServiceImpl mInCallService;
    private BluetoothHeadsetClient mBluetoothHeadsetClient;
    private final Map<UiCall, Call> mCallMapping = new HashMap<>();
    private final List<CallListener> mCallListeners = new CopyOnWriteArrayList<>();

    /**
     * Initialized a globally accessible {@link UiCallManager} which can be retrieved by
     * {@link #get}. If this function is called a second time before calling {@link #tearDown()},
     * an exception will be thrown.
     *
     * @param applicationContext Application context.
     */
    public static UiCallManager init(Context applicationContext) {
        if (sUiCallManager == null) {
            sUiCallManager = new UiCallManager(applicationContext);
        } else {
            throw new IllegalStateException("UiCallManager has been initialized.");
        }
        return sUiCallManager;
    }

    /**
     * Gets the global {@link UiCallManager} instance. Make sure
     * {@link #init(Context)} is called before calling this method.
     */
    public static UiCallManager get() {
        if (sUiCallManager == null) {
            throw new IllegalStateException(
                    "Call UiCallManager.init(Context) before calling this function");
        }
        return sUiCallManager;
    }

    private UiCallManager(Context context) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "SetUp");
        }

        mContext = context;
        mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);

        mTelecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
        Intent intent = new Intent(context, InCallServiceImpl.class);
        intent.setAction(InCallServiceImpl.ACTION_LOCAL_BIND);
        context.bindService(intent, mInCallServiceConnection, Context.BIND_AUTO_CREATE);

        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        adapter.getProfileProxy(mContext, new BluetoothProfile.ServiceListener() {
            @Override
            public void onServiceConnected(int profile, BluetoothProfile proxy) {
                if (profile == BluetoothProfile.HEADSET_CLIENT) {
                    mBluetoothHeadsetClient = (BluetoothHeadsetClient) proxy;
                }
            }

            @Override
            public void onServiceDisconnected(int profile) {
            }
        }, BluetoothProfile.HEADSET_CLIENT);
    }

    private final ServiceConnection mInCallServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "onServiceConnected: " + name + ", service: " + binder);
            }
            mInCallService = ((InCallServiceImpl.LocalBinder) binder).getService();
            mInCallService.registerCallback(mInCallServiceCallback);

            // The InCallServiceImpl could be bound when we already have some active calls, let's
            // notify UI about these calls.
            for (Call telecomCall : mInCallService.getCalls()) {
                UiCall uiCall = doTelecomCallAdded(telecomCall);
                onStateChanged(uiCall, uiCall.getState());
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "onServiceDisconnected: " + name);
            }
            mInCallService.unregisterCallback(mInCallServiceCallback);
        }

        private InCallServiceImpl.Callback mInCallServiceCallback =
                new InCallServiceImpl.Callback() {
                    @Override
                    public void onTelecomCallAdded(Call telecomCall) {
                        doTelecomCallAdded(telecomCall);
                    }

                    @Override
                    public void onTelecomCallRemoved(Call telecomCall) {
                        doTelecomCallRemoved(telecomCall);
                    }

                    @Override
                    public void onCallAudioStateChanged(CallAudioState audioState) {
                        doCallAudioStateChanged(audioState);
                    }
                };
    };

    /**
     * Tears down the {@link UiCallManager}. Calling this function will null out the global
     * accessible {@link UiCallManager} instance. Remember to re-initialize the
     * {@link UiCallManager}.
     */
    public void tearDown() {
        if (mInCallService != null) {
            mContext.unbindService(mInCallServiceConnection);
            mInCallService = null;
        }
        mCallMapping.clear();
        // Clear out the mContext reference to avoid memory leak.
        mContext = null;
        sUiCallManager = null;
    }

    public void addListener(CallListener listener) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "addListener: " + listener);
        }
        mCallListeners.add(listener);
    }

    public void removeListener(CallListener listener) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "removeListener: " + listener);
        }
        mCallListeners.remove(listener);
    }

    protected void placeCall(String number) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "placeCall: " + number);
        }
        Uri uri = Uri.fromParts("tel", number, null);
        Log.d(TAG, "android.telecom.TelecomManager#placeCall: " + uri);
        mTelecomManager.placeCall(uri, null);
    }

    public void answerCall(UiCall uiCall) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "answerCall: " + uiCall);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.answer(0);
        }
    }

    public void rejectCall(UiCall uiCall, boolean rejectWithMessage, String textMessage) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "rejectCall: " + uiCall + ", rejectWithMessage: " + rejectWithMessage
                    + "textMessage: " + textMessage);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.reject(rejectWithMessage, textMessage);
        }
    }

    public void disconnectCall(UiCall uiCall) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "disconnectCall: " + uiCall);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.disconnect();
        }
    }

    public List<UiCall> getCalls() {
        return new ArrayList<>(mCallMapping.keySet());
    }

    public boolean getMuted() {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "getMuted");
        }
        if (mInCallService == null) {
            return false;
        }
        CallAudioState audioState = mInCallService.getCallAudioState();
        return audioState != null && audioState.isMuted();
    }

    public void setMuted(boolean muted) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "setMuted: " + muted);
        }
        if (mInCallService == null) {
            return;
        }
        mInCallService.setMuted(muted);
    }

    public int getSupportedAudioRouteMask() {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "getSupportedAudioRouteMask");
        }

        CallAudioState audioState = getCallAudioStateOrNull();
        return audioState != null ? audioState.getSupportedRouteMask() : 0;
    }

    public List<Integer> getSupportedAudioRoute() {
        List<Integer> audioRouteList = new ArrayList<>();

        boolean isBluetoothPhoneCall = isBluetoothCall();
        if (isBluetoothPhoneCall) {
            // if this is bluetooth phone call, we can only select audio route between vehicle
            // and phone.
            // Vehicle speaker route.
            audioRouteList.add(CallAudioState.ROUTE_BLUETOOTH);
            // Headset route.
            audioRouteList.add(CallAudioState.ROUTE_EARPIECE);
        } else {
            // Most likely we are making phone call with on board SIM card.
            int supportedAudioRouteMask = getSupportedAudioRouteMask();

            if ((supportedAudioRouteMask & CallAudioState.ROUTE_EARPIECE) != 0) {
                audioRouteList.add(CallAudioState.ROUTE_EARPIECE);
            } else if ((supportedAudioRouteMask & CallAudioState.ROUTE_BLUETOOTH) != 0) {
                audioRouteList.add(CallAudioState.ROUTE_BLUETOOTH);
            } else if ((supportedAudioRouteMask & CallAudioState.ROUTE_WIRED_HEADSET) != 0) {
                audioRouteList.add(CallAudioState.ROUTE_WIRED_HEADSET);
            } else if ((supportedAudioRouteMask & CallAudioState.ROUTE_SPEAKER) != 0) {
                audioRouteList.add(CallAudioState.ROUTE_SPEAKER);
            }
        }

        return audioRouteList;
    }

    public boolean isBluetoothCall() {
        PhoneAccountHandle phoneAccountHandle =
                mTelecomManager.getUserSelectedOutgoingPhoneAccount();
        if (phoneAccountHandle != null && phoneAccountHandle.getComponentName() != null) {
            return HFP_CLIENT_CONNECTION_SERVICE_CLASS_NAME.equals(
                    phoneAccountHandle.getComponentName().getClassName());
        } else {
            return false;
        }
    }

    public int getAudioRoute() {
        CallAudioState audioState = getCallAudioStateOrNull();
        int audioRoute = audioState != null ? audioState.getRoute() : 0;
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "getAudioRoute " + audioRoute);
        }
        return audioRoute;
    }

    /**
     * Re-route the audio out phone of the ongoing phone call.
     */
    public void setAudioRoute(@CallAudioRoute int audioRoute) {
        if (mBluetoothHeadsetClient != null && isBluetoothCall()) {
            for (BluetoothDevice device : mBluetoothHeadsetClient.getConnectedDevices()) {
                List<BluetoothHeadsetClientCall> currentCalls =
                        mBluetoothHeadsetClient.getCurrentCalls(device);
                if (currentCalls != null && !currentCalls.isEmpty()) {
                    if (audioRoute == CallAudioState.ROUTE_BLUETOOTH) {
                        mBluetoothHeadsetClient.connectAudio(device);
                    } else if ((audioRoute & CallAudioState.ROUTE_WIRED_OR_EARPIECE) != 0) {
                        mBluetoothHeadsetClient.disconnectAudio(device);
                    }
                }
            }
        }
        // TODO: Implement routing audio if current call is not a bluetooth call.
    }

    public void holdCall(UiCall uiCall) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "holdCall: " + uiCall);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.hold();
        }
    }

    public void unholdCall(UiCall uiCall) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "unholdCall: " + uiCall);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.unhold();
        }
    }

    public void playDtmfTone(UiCall uiCall, char digit) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "playDtmfTone: call: " + uiCall + ", digit: " + digit);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.playDtmfTone(digit);
        }
    }

    public void stopDtmfTone(UiCall uiCall) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "stopDtmfTone: call: " + uiCall);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.stopDtmfTone();
        }
    }

    public void postDialContinue(UiCall uiCall, boolean proceed) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "postDialContinue: call: " + uiCall + ", proceed: " + proceed);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.postDialContinue(proceed);
        }
    }

    public void conference(UiCall uiCall, UiCall otherUiCall) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "conference: call: " + uiCall + ", otherCall: " + otherUiCall);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        Call otherTelecomCall = mCallMapping.get(otherUiCall);
        if (telecomCall != null) {
            telecomCall.conference(otherTelecomCall);
        }
    }

    public void splitFromConference(UiCall uiCall) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "splitFromConference: call: " + uiCall);
        }

        Call telecomCall = mCallMapping.get(uiCall);
        if (telecomCall != null) {
            telecomCall.splitFromConference();
        }
    }

    private UiCall doTelecomCallAdded(final Call telecomCall) {
        Log.d(TAG, "doTelecomCallAdded: " + telecomCall);

        UiCall uiCall = getOrCreateCallContainer(telecomCall);
        telecomCall.registerCallback(new TelecomCallListener(this, uiCall));
        for (CallListener listener : mCallListeners) {
            listener.onCallAdded(uiCall);
        }
        Log.d(TAG, "Call backs registered");

        if (telecomCall.getState() == Call.STATE_SELECT_PHONE_ACCOUNT) {
            // TODO(b/26189994): need to show Phone Account picker to let user choose a phone
            // account. It should be an account from TelecomManager#getCallCapablePhoneAccounts
            // list.
            Log.w(TAG, "Need to select phone account for the given call: " + telecomCall + ", "
                    + "but this feature is not implemented yet.");
            telecomCall.disconnect();
        }
        return uiCall;
    }

    private void doTelecomCallRemoved(Call telecomCall) {
        UiCall uiCall = getOrCreateCallContainer(telecomCall);

        mCallMapping.remove(uiCall);

        for (CallListener listener : mCallListeners) {
            listener.onCallRemoved(uiCall);
        }
    }

    private void doCallAudioStateChanged(CallAudioState audioState) {
        for (CallListener listener : mCallListeners) {
            listener.onAudioStateChanged(audioState.isMuted(), audioState.getRoute(),
                    audioState.getSupportedRouteMask());
        }
    }

    private void onStateChanged(UiCall uiCall, int state) {
        for (CallListener listener : mCallListeners) {
            listener.onCallStateChanged(uiCall, state);
        }
    }

    private void onCallUpdated(UiCall uiCall) {
        for (CallListener listener : mCallListeners) {
            listener.onCallUpdated(uiCall);
        }
    }

    private UiCall getOrCreateCallContainer(Call telecomCall) {
        for (Map.Entry<UiCall, Call> entry : mCallMapping.entrySet()) {
            if (entry.getValue() == telecomCall) {
                return entry.getKey();
            }
        }

        UiCall uiCall = new UiCall(nextCarPhoneCallId++);
        updateCallContainerFromTelecom(uiCall, telecomCall);
        mCallMapping.put(uiCall, telecomCall);
        return uiCall;
    }

    private static void updateCallContainerFromTelecom(UiCall uiCall, Call telecomCall) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "updateCallContainerFromTelecom: call: " + uiCall + ", telecomCall: "
                    + telecomCall);
        }

        uiCall.setState(telecomCall.getState());
        uiCall.setHasChildren(!telecomCall.getChildren().isEmpty());
        uiCall.setHasParent(telecomCall.getParent() != null);

        Call.Details details = telecomCall.getDetails();
        if (details == null) {
            return;
        }

        uiCall.setConnectTimeMillis(details.getConnectTimeMillis());

        DisconnectCause cause = details.getDisconnectCause();
        uiCall.setDisconnectCause(cause == null ? null : cause.getLabel());

        GatewayInfo gatewayInfo = details.getGatewayInfo();
        uiCall.setGatewayInfoOriginalAddress(
                gatewayInfo == null ? null : gatewayInfo.getOriginalAddress());

        String number = "";
        if (gatewayInfo != null) {
            number = gatewayInfo.getOriginalAddress().getSchemeSpecificPart();
        } else if (details.getHandle() != null) {
            number = details.getHandle().getSchemeSpecificPart();
        }
        uiCall.setNumber(number);
    }

    private CallAudioState getCallAudioStateOrNull() {
        return mInCallService != null ? mInCallService.getCallAudioState() : null;
    }

    /** Returns a first call that matches at least one provided call state */
    public UiCall getCallWithState(int... callStates) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "getCallWithState: " + callStates);
        }
        for (UiCall call : getCalls()) {
            for (int callState : callStates) {
                if (call.getState() == callState) {
                    return call;
                }
            }
        }
        return null;
    }

    public UiCall getPrimaryCall() {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "getPrimaryCall");
        }
        List<UiCall> calls = getCalls();
        if (calls.isEmpty()) {
            return null;
        }

        Collections.sort(calls, getCallComparator());
        UiCall uiCall = calls.get(0);
        if (uiCall.hasParent()) {
            return null;
        }
        return uiCall;
    }

    public UiCall getSecondaryCall() {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "getSecondaryCall");
        }
        List<UiCall> calls = getCalls();
        if (calls.size() < 2) {
            return null;
        }

        Collections.sort(calls, getCallComparator());
        UiCall uiCall = calls.get(1);
        if (uiCall.hasParent()) {
            return null;
        }
        return uiCall;
    }

    public static final int CAN_PLACE_CALL_RESULT_OK = 0;
    public static final int CAN_PLACE_CALL_RESULT_NETWORK_UNAVAILABLE = 1;
    public static final int CAN_PLACE_CALL_RESULT_HFP_UNAVAILABLE = 2;
    public static final int CAN_PLACE_CALL_RESULT_AIRPLANE_MODE = 3;

    public int getCanPlaceCallStatus(String number, boolean bluetoothRequired) {
        // TODO(b/26191392): figure out the logic for projected and embedded modes
        return CAN_PLACE_CALL_RESULT_OK;
    }

    public String getFailToPlaceCallMessage(int canPlaceCallResult) {
        switch (canPlaceCallResult) {
            case CAN_PLACE_CALL_RESULT_OK:
                return "";
            case CAN_PLACE_CALL_RESULT_HFP_UNAVAILABLE:
                return mContext.getString(R.string.error_no_hfp);
            case CAN_PLACE_CALL_RESULT_AIRPLANE_MODE:
                return mContext.getString(R.string.error_airplane_mode);
            case CAN_PLACE_CALL_RESULT_NETWORK_UNAVAILABLE:
            default:
                return mContext.getString(R.string.error_network_not_available);
        }
    }

    /** Places call only if there's no outgoing call right now */
    public void safePlaceCall(String number, boolean bluetoothRequired) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "safePlaceCall: " + number);
        }

        int placeCallStatus = getCanPlaceCallStatus(number, bluetoothRequired);
        if (placeCallStatus != CAN_PLACE_CALL_RESULT_OK) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Unable to place a call: " + placeCallStatus);
            }
            return;
        }

        UiCall outgoingCall = getCallWithState(
                Call.STATE_CONNECTING, Call.STATE_NEW, Call.STATE_DIALING);
        if (outgoingCall == null) {
            long now = Calendar.getInstance().getTimeInMillis();
            if (now - mLastPlacedCallTimeMs > MIN_TIME_BETWEEN_CALLS_MS) {
                placeCall(number);
                mLastPlacedCallTimeMs = now;
            } else {
                if (Log.isLoggable(TAG, Log.INFO)) {
                    Log.i(TAG, "You have to wait " + MIN_TIME_BETWEEN_CALLS_MS
                            + "ms between making calls");
                }
            }
        }
    }

    public void callVoicemail() {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "callVoicemail");
        }

        String voicemailNumber = TelecomUtils.getVoicemailNumber(mContext);
        if (TextUtils.isEmpty(voicemailNumber)) {
            Log.w(TAG, "Unable to get voicemail number.");
            return;
        }
        safePlaceCall(voicemailNumber, false);
    }

    /**
     * Returns the call types for the given number of items in the cursor.
     * <p/>
     * It uses the next {@code count} rows in the cursor to extract the types.
     * <p/>
     * Its position in the cursor is unchanged by this function.
     */
    public int[] getCallTypes(Cursor cursor, int count) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "getCallTypes: cursor: " + cursor + ", count: " + count);
        }

        int position = cursor.getPosition();
        int[] callTypes = new int[count];
        String voicemailNumber = mTelephonyManager.getVoiceMailNumber();
        int column;
        for (int index = 0; index < count; ++index) {
            column = cursor.getColumnIndex(CallLog.Calls.NUMBER);
            String phoneNumber = cursor.getString(column);
            if (phoneNumber != null && phoneNumber.equals(voicemailNumber)) {
                callTypes[index] = PhoneLoader.VOICEMAIL_TYPE;
            } else {
                column = cursor.getColumnIndex(CallLog.Calls.TYPE);
                callTypes[index] = cursor.getInt(column);
            }
            cursor.moveToNext();
        }
        cursor.moveToPosition(position);
        return callTypes;
    }

    private static Comparator<UiCall> getCallComparator() {
        return new Comparator<UiCall>() {
            @Override
            public int compare(UiCall call, UiCall otherCall) {
                if (call.hasParent() && !otherCall.hasParent()) {
                    return 1;
                } else if (!call.hasParent() && otherCall.hasParent()) {
                    return -1;
                }
                int carCallRank = sCallStateRank.indexOf(call.getState());
                int otherCarCallRank = sCallStateRank.indexOf(otherCall.getState());

                return otherCarCallRank - carCallRank;
            }
        };
    }

    private static class TelecomCallListener extends Call.Callback {
        private final WeakReference<UiCallManager> mCarTelecomMangerRef;
        private final WeakReference<UiCall> mCallContainerRef;

        TelecomCallListener(UiCallManager carTelecomManager, UiCall uiCall) {
            mCarTelecomMangerRef = new WeakReference<>(carTelecomManager);
            mCallContainerRef = new WeakReference<>(uiCall);
        }

        @Override
        public void onStateChanged(Call telecomCall, int state) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "onStateChanged: " + state);
            }
            UiCallManager manager = mCarTelecomMangerRef.get();
            UiCall call = mCallContainerRef.get();
            if (manager != null && call != null) {
                call.setState(state);
                manager.onStateChanged(call, state);
            }
        }

        @Override
        public void onParentChanged(Call telecomCall, Call parent) {
            doCallUpdated(telecomCall);
        }

        @Override
        public void onCallDestroyed(Call telecomCall) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "onCallDestroyed");
            }
        }

        @Override
        public void onDetailsChanged(Call telecomCall, Call.Details details) {
            doCallUpdated(telecomCall);
        }

        @Override
        public void onVideoCallChanged(Call telecomCall, InCallService.VideoCall videoCall) {
            doCallUpdated(telecomCall);
        }

        @Override
        public void onCannedTextResponsesLoaded(Call telecomCall,
                List<String> cannedTextResponses) {
            doCallUpdated(telecomCall);
        }

        @Override
        public void onChildrenChanged(Call telecomCall, List<Call> children) {
            doCallUpdated(telecomCall);
        }

        private void doCallUpdated(Call telecomCall) {
            UiCallManager manager = mCarTelecomMangerRef.get();
            UiCall uiCall = mCallContainerRef.get();
            if (manager != null && uiCall != null) {
                updateCallContainerFromTelecom(uiCall, telecomCall);
                manager.onCallUpdated(uiCall);
            }
        }
    }
}