• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.content.Context;
21 
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.Transformations;
24 import androidx.lifecycle.ViewModel;
25 
26 import com.android.car.arch.common.LiveDataFunctions;
27 import com.android.car.dialer.livedata.CallHistoryLiveData;
28 import com.android.car.dialer.storage.FavoriteNumberRepository;
29 import com.android.car.dialer.ui.favorite.BluetoothFavoriteContactsLiveData;
30 import com.android.car.telephony.common.Contact;
31 import com.android.car.telephony.common.InMemoryPhoneBook;
32 import com.android.car.telephony.common.PhoneCallLog;
33 
34 import java.util.List;
35 
36 import javax.inject.Named;
37 import javax.inject.Singleton;
38 
39 import dagger.Module;
40 import dagger.Provides;
41 import dagger.hilt.InstallIn;
42 import dagger.hilt.android.components.ActivityRetainedComponent;
43 import dagger.hilt.android.qualifiers.ApplicationContext;
44 import dagger.hilt.android.scopes.ActivityRetainedScoped;
45 import dagger.hilt.components.SingletonComponent;
46 
47 /**
48  * Module providing dependencies for single hfp connection.
49  * This module is excluded in the emulator build variant.
50  */
51 public final class HfpDataModules {
52     /** Single hfp application level dependencies. */
53     @InstallIn(SingletonComponent.class)
54     @Module
55     public static final class AppModule {
56         /**
57          * This {@link LiveData} for call logs will be always be active. See {@link
58          * com.android.car.dialer.bluetooth.CallHistoryManager}.
59          */
60         @Singleton
61         @Provides
provideCallHistoryLiveData( @pplicationContext Context context, @Named("Hfp") LiveData<BluetoothDevice> currentHfpDevice)62         static LiveData<List<PhoneCallLog>> provideCallHistoryLiveData(
63                 @ApplicationContext Context context,
64                 @Named("Hfp") LiveData<BluetoothDevice> currentHfpDevice) {
65             return LiveDataFunctions.switchMapNonNull(currentHfpDevice,
66                     device -> CallHistoryLiveData.newInstance(context, device.getAddress()));
67         }
68 
69         @Singleton
70         @Provides
provideContactListLiveData( @amed"Hfp") LiveData<BluetoothDevice> currentHfpDevice)71         static LiveData<List<Contact>> provideContactListLiveData(
72                 @Named("Hfp") LiveData<BluetoothDevice> currentHfpDevice) {
73             return LiveDataFunctions.switchMapNonNull(currentHfpDevice,
74                     device -> InMemoryPhoneBook.get().getContactsLiveDataByAccount(
75                             device.getAddress()));
76         }
77     }
78 
79     /** {@link LiveData} instances that are shared across various {@link ViewModel}s. */
80     @InstallIn(ActivityRetainedComponent.class)
81     @Module
82     public static final class ActivityRetainedModule {
83         @ActivityRetainedScoped
84         @Named("Hfp")
85         @Provides
hasHfpDeviceConnectedLiveData( @amed"Hfp") LiveData<List<BluetoothDevice>> hfpDeviceListLiveData)86         static LiveData<Boolean> hasHfpDeviceConnectedLiveData(
87                 @Named("Hfp") LiveData<List<BluetoothDevice>> hfpDeviceListLiveData) {
88             return Transformations.map(hfpDeviceListLiveData,
89                     devices -> devices != null && !devices.isEmpty());
90         }
91 
92         @ActivityRetainedScoped
93         @Provides
94         @Named("BluetoothFavorite")
provideBluetoothFavoriteContactListLiveData( @amed"Hfp") LiveData<BluetoothDevice> currentHfpDevice, BluetoothFavoriteContactsLiveData.Factory bleFavoriteContactsLiveDataFactory)95         static LiveData<List<Contact>> provideBluetoothFavoriteContactListLiveData(
96                 @Named("Hfp") LiveData<BluetoothDevice> currentHfpDevice,
97                 BluetoothFavoriteContactsLiveData.Factory bleFavoriteContactsLiveDataFactory) {
98             return LiveDataFunctions.switchMapNonNull(currentHfpDevice,
99                     device -> bleFavoriteContactsLiveDataFactory.create(device.getAddress()));
100         }
101 
102         @ActivityRetainedScoped
103         @Provides
104         @Named("LocalFavorite")
provideLocalFavoriteContactListLiveData( @amed"Hfp") LiveData<BluetoothDevice> currentHfpDevice, FavoriteNumberRepository favoriteNumberRepository)105         static LiveData<List<Contact>> provideLocalFavoriteContactListLiveData(
106                 @Named("Hfp") LiveData<BluetoothDevice> currentHfpDevice,
107                 FavoriteNumberRepository favoriteNumberRepository) {
108             return LiveDataFunctions.switchMapNonNull(currentHfpDevice,
109                     device -> favoriteNumberRepository.getFavoriteContacts(device.getAddress()));
110         }
111     }
112 }
113