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.bluetooth.BluetoothDevice;
20 import android.content.Context;
21 
22 import androidx.annotation.IntDef;
23 import androidx.lifecycle.LiveData;
24 import androidx.lifecycle.MediatorLiveData;
25 import androidx.lifecycle.MutableLiveData;
26 
27 import com.android.car.dialer.R;
28 
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 
32 import dagger.hilt.android.qualifiers.ApplicationContext;
33 import dagger.hilt.android.scopes.ViewModelScoped;
34 
35 /**
36  * LiveData for the toolbar title of the  {@link com.android.car.dialer.ui.TelecomActivity}. The
37  * attribute {@link R.attr#toolbarTitleMode} is activity scope attribute. Supported values are:
38  * <ul>
39  *     <li> app_name: 0
40  *     <li> none: 1
41  *     <li> device_name: 2
42  */
43 @ViewModelScoped
44 class ToolbarTitleLiveData extends MediatorLiveData<String> {
45 
46     @IntDef({
47             ToolbarTitleMode.APP_NAME,
48             ToolbarTitleMode.NONE,
49             ToolbarTitleMode.DEVICE_NAME
50     })
51     private @interface ToolbarTitleMode {
52         int APP_NAME = 0;
53         int NONE = 1;
54         int DEVICE_NAME = 2;
55     }
56 
57     private final LiveData<BluetoothDevice> mCurrentHfpDeviceLiveData;
58     private final MutableLiveData<Integer> mToolbarTitleModeLiveData;
59     private final Context mContext;
60 
61     @Inject
ToolbarTitleLiveData( @pplicationContext Context context, @Named(R) LiveData<BluetoothDevice> currentHfpDeviceLiveData)62     ToolbarTitleLiveData(
63             @ApplicationContext Context context,
64             @Named("Hfp") LiveData<BluetoothDevice> currentHfpDeviceLiveData) {
65         mContext = context;
66         mToolbarTitleModeLiveData = new MutableLiveData<>();
67         mCurrentHfpDeviceLiveData = currentHfpDeviceLiveData;
68 
69         addSource(mToolbarTitleModeLiveData, this::updateToolbarTitle);
70         addSource(mCurrentHfpDeviceLiveData, this::updateDeviceName);
71     }
72 
73     /** Exposes the {@link MutableLiveData} for the toolbar title mode. */
getToolbarTitleModeLiveData()74     public MutableLiveData<Integer> getToolbarTitleModeLiveData() {
75         return mToolbarTitleModeLiveData;
76     }
77 
updateToolbarTitle(int toolbarTitleMode)78     private void updateToolbarTitle(int toolbarTitleMode) {
79         switch (toolbarTitleMode) {
80             case ToolbarTitleMode.NONE:
81                 setValue(null);
82                 return;
83             case ToolbarTitleMode.DEVICE_NAME:
84                 updateDeviceName(mCurrentHfpDeviceLiveData.getValue());
85                 return;
86             case ToolbarTitleMode.APP_NAME:
87             default:
88                 setValue(mContext.getString(R.string.phone_app_name));
89                 return;
90         }
91     }
92 
updateDeviceName(BluetoothDevice currentHfpDevice)93     private void updateDeviceName(BluetoothDevice currentHfpDevice) {
94         Integer toolbarTitleMode = mToolbarTitleModeLiveData.getValue();
95         if (toolbarTitleMode != null
96                 && ToolbarTitleMode.DEVICE_NAME == toolbarTitleMode.intValue()) {
97             // When there is no hfp device connected, use the app name as title.
98             if (currentHfpDevice == null) {
99                 setValue(mContext.getString(R.string.phone_app_name));
100                 return;
101             }
102 
103             setValue(currentHfpDevice.getName());
104         }
105     }
106 }
107