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.ui;
18 
19 import android.app.Application;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.Context;
22 
23 import androidx.annotation.IntDef;
24 import androidx.lifecycle.AndroidViewModel;
25 import androidx.lifecycle.LiveData;
26 import androidx.lifecycle.MediatorLiveData;
27 import androidx.lifecycle.MutableLiveData;
28 import androidx.lifecycle.Transformations;
29 
30 import com.android.car.dialer.livedata.BluetoothErrorStringLiveData;
31 import com.android.car.dialer.livedata.HfpDeviceListLiveData;
32 
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 
36 /**
37  * View model for {@link TelecomActivity}.
38  */
39 public class TelecomActivityViewModel extends AndroidViewModel {
40     private static final String TAG = "CD.TelecomActivityViewModel";
41     /**
42      * A constant which indicates that there's no Bluetooth error.
43      */
44 
45     private final Context mApplicationContext;
46     private final LiveData<String> mErrorStringLiveData;
47     private final LiveData<Boolean> mRefreshTabsLiveData;
48 
49     private final ToolbarTitleLiveData mToolbarTitleLiveData;
50     private final MutableLiveData<Integer> mToolbarTitleMode;
51 
52     private BluetoothDevice mBluetoothDevice;
53 
54     /**
55      * App state indicates if bluetooth is connected or it should just show the content fragments.
56      */
57     @IntDef({DialerAppState.DEFAULT, DialerAppState.BLUETOOTH_ERROR,
58             DialerAppState.EMERGENCY_DIALPAD})
59     @Retention(RetentionPolicy.SOURCE)
60     public @interface DialerAppState {
61         int DEFAULT = 0;
62         int BLUETOOTH_ERROR = 1;
63         int EMERGENCY_DIALPAD = 2;
64     }
65 
TelecomActivityViewModel(Application application)66     public TelecomActivityViewModel(Application application) {
67         super(application);
68         mApplicationContext = application.getApplicationContext();
69 
70         mToolbarTitleMode = new MediatorLiveData<>();
71         mToolbarTitleLiveData = new ToolbarTitleLiveData(mApplicationContext, mToolbarTitleMode);
72         mErrorStringLiveData = BluetoothErrorStringLiveData.get(mApplicationContext);
73 
74         HfpDeviceListLiveData hfpDeviceListLiveData = new HfpDeviceListLiveData(getApplication());
75         mRefreshTabsLiveData = Transformations.map(hfpDeviceListLiveData, (hfpDeviceList) -> {
76             if (hfpDeviceList != null && !hfpDeviceList.isEmpty()) {
77                 if (!hfpDeviceList.contains(mBluetoothDevice)) {
78                     mBluetoothDevice = hfpDeviceList.get(0);
79                     return true;
80                 }
81             } else {
82                 if (mBluetoothDevice != null) {
83                     mBluetoothDevice = null;
84                     return true;
85                 }
86             }
87             return false;
88         });
89     }
90 
91     /**
92      * Returns the {@link LiveData} for the toolbar title, which provides the toolbar title
93      * depending on the {@link com.android.car.dialer.R.attr#toolbarTitleMode}.
94      */
getToolbarTitle()95     public LiveData<String> getToolbarTitle() {
96         return mToolbarTitleLiveData;
97     }
98 
99     /**
100      * Returns the {@link MutableLiveData} of the toolbar title mode. The value should be set by the
101      * {@link TelecomActivity}.
102      */
getToolbarTitleMode()103     public MutableLiveData<Integer> getToolbarTitleMode() {
104         return mToolbarTitleMode;
105     }
106 
107     /**
108      * Returns a LiveData which provides the warning string based on Bluetooth states. Returns
109      * {@link BluetoothErrorStringLiveData#NO_BT_ERROR} if there's no error.
110      */
getErrorMessage()111     public LiveData<String> getErrorMessage() {
112         return mErrorStringLiveData;
113     }
114 
115     /**
116      * Returns the live data which monitors whether to refresh Dialer.
117      */
getRefreshTabsLiveData()118     public LiveData<Boolean> getRefreshTabsLiveData() {
119         return mRefreshTabsLiveData;
120     }
121 }
122