1 /*
2  * Copyright (C) 2021 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.inject;
18 
19 import android.bluetooth.BluetoothDevice;
20 
21 import androidx.lifecycle.LiveData;
22 import androidx.lifecycle.Transformations;
23 
24 import com.android.car.dialer.bluetooth.PhoneAccountManager;
25 import com.android.car.dialer.bluetooth.UiBluetoothMonitor;
26 
27 import java.util.List;
28 import java.util.Set;
29 
30 import javax.inject.Named;
31 import javax.inject.Singleton;
32 
33 import dagger.Module;
34 import dagger.Provides;
35 import dagger.hilt.InstallIn;
36 import dagger.hilt.components.SingletonComponent;
37 
38 /**
39  * Module providing dependencies that are overridden by car.dialer.framework modules.
40  * This module is excluded in the test and emulator build variants.
41  */
42 @InstallIn(SingletonComponent.class)
43 @Module
44 public final class BluetoothSingleHfpModule {
45     @Singleton
46     @Named("Bluetooth")
47     @Provides
provideBluetoothStateLiveData( UiBluetoothMonitor uiBluetoothMonitor)48     static LiveData<Integer> provideBluetoothStateLiveData(
49             UiBluetoothMonitor uiBluetoothMonitor) {
50         return uiBluetoothMonitor.getBluetoothStateLiveData();
51     }
52 
53     @Singleton
54     @Named("Bluetooth")
55     @Provides
provideBluetoothPairListLiveData( UiBluetoothMonitor uiBluetoothMonitor)56     static LiveData<Set<BluetoothDevice>> provideBluetoothPairListLiveData(
57             UiBluetoothMonitor uiBluetoothMonitor) {
58         return uiBluetoothMonitor.getPairListLiveData();
59     }
60 
61     @Singleton
62     @Named("Hfp")
63     @Provides
provideHfpDeviceListLiveData( UiBluetoothMonitor uiBluetoothMonitor)64     static LiveData<List<BluetoothDevice>> provideHfpDeviceListLiveData(
65             UiBluetoothMonitor uiBluetoothMonitor) {
66         return uiBluetoothMonitor.getHfpDeviceListLiveData();
67     }
68 
69     @Singleton
70     @Named("Hfp")
71     @Provides
provideCurrentHfpDeviceLiveData( @amedR) LiveData<List<BluetoothDevice>> hfpDeviceListLiveData, PhoneAccountManager phoneAccountManager)72     static LiveData<BluetoothDevice> provideCurrentHfpDeviceLiveData(
73             @Named("Hfp") LiveData<List<BluetoothDevice>> hfpDeviceListLiveData,
74             PhoneAccountManager phoneAccountManager) {
75         LiveData<BluetoothDevice> currentHfpDevice = Transformations.map(hfpDeviceListLiveData,
76                 devices -> devices != null && !devices.isEmpty() ? devices.get(0) : null);
77         currentHfpDevice.observeForever(
78                 device -> phoneAccountManager.setUserSelectedOutgoingDevice(device));
79         return currentHfpDevice;
80     }
81 }
82