• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.dialer.livedata;
18 
19 import androidx.lifecycle.MediatorLiveData;
20 
21 import com.android.car.dialer.log.L;
22 import com.android.car.dialer.telecom.UiCallManager;
23 import com.android.car.telephony.common.CallDetail;
24 
25 import dagger.assisted.Assisted;
26 import dagger.assisted.AssistedFactory;
27 import dagger.assisted.AssistedInject;
28 
29 /**
30  * Provides the current connecting audio route.
31  */
32 public class AudioRouteLiveData extends MediatorLiveData<Integer> {
33     private static final String TAG = "CD.AudioRouteLiveData";
34 
35     private final UiCallManager mUiCallManager;
36     private final CallDetailLiveData mPrimaryCallDetailLiveData;
37 
38     @AssistedInject
AudioRouteLiveData( @ssisted CallDetailLiveData primaryCallDetailLiveData, UiCallManager callManager)39     public AudioRouteLiveData(
40             @Assisted CallDetailLiveData primaryCallDetailLiveData,
41             UiCallManager callManager) {
42         mUiCallManager = callManager;
43         mPrimaryCallDetailLiveData = primaryCallDetailLiveData;
44 
45         addSource(mPrimaryCallDetailLiveData, this::updateOngoingCallAudioRoute);
46     }
47 
48     @Override
onActive()49     protected void onActive() {
50         super.onActive();
51         updateAudioRoute();
52     }
53 
updateAudioRoute()54     private void updateAudioRoute() {
55         CallDetail primaryCallDetail = mPrimaryCallDetailLiveData.getValue();
56         updateOngoingCallAudioRoute(primaryCallDetail);
57     }
58 
updateOngoingCallAudioRoute(CallDetail callDetail)59     private void updateOngoingCallAudioRoute(CallDetail callDetail) {
60         if (callDetail == null) {
61             // Phone call might have disconnected, no action.
62             return;
63         }
64         int state = callDetail.getScoState();
65         int audioRoute = mUiCallManager.getAudioRoute(state);
66         if (getValue() == null || audioRoute != getValue()) {
67             L.d(TAG, "updateAudioRoute to %s", audioRoute);
68             setValue(audioRoute);
69         }
70     }
71 
72     /**
73      * Factory to create {@link AudioRouteLiveData} instances via the {@link AssistedInject}
74      * constructor.
75      */
76     @AssistedFactory
77     public interface Factory {
78         /** Creates an {@link AudioRouteLiveData} instance. */
create(CallDetailLiveData primaryCallDetailLiveData)79         AudioRouteLiveData create(CallDetailLiveData primaryCallDetailLiveData);
80     }
81 }
82