1 /*
2  * Copyright (C) 2018 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.warning;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.text.TextUtils;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.TextView;
27 
28 import androidx.fragment.app.Fragment;
29 import androidx.lifecycle.MutableLiveData;
30 import androidx.lifecycle.ViewModelProviders;
31 
32 import com.android.car.apps.common.UxrButton;
33 import com.android.car.apps.common.util.CarPackageManagerUtils;
34 import com.android.car.apps.common.util.ViewUtils;
35 import com.android.car.dialer.R;
36 import com.android.car.dialer.telecom.UiCallManager;
37 import com.android.car.dialer.ui.TelecomActivityViewModel;
38 
39 /**
40  * A fragment that informs the user that there is no bluetooth device attached that can make
41  * phone calls.
42  */
43 public class NoHfpFragment extends Fragment {
44     private static final String ERROR_MESSAGE_KEY = "ERROR_MESSAGE_KEY";
45     private static final String Bluetooth_Setting_ACTION = "android.settings.BLUETOOTH_SETTINGS";
46     private static final String Bluetooth_Setting_CATEGORY = "android.intent.category.DEFAULT";
47 
48     private TextView mErrorMessageView;
49     private String mErrorMessage;
50 
51     /**
52      * Returns an instance of the {@link NoHfpFragment} with the given error message as the one to
53      * display.
54      */
newInstance(String errorMessage)55     public static NoHfpFragment newInstance(String errorMessage) {
56         NoHfpFragment fragment = new NoHfpFragment();
57 
58         Bundle args = new Bundle();
59         args.putString(ERROR_MESSAGE_KEY, errorMessage);
60         fragment.setArguments(args);
61 
62         return fragment;
63     }
64 
onCreate(Bundle savedInstanceState)65     public void onCreate(Bundle savedInstanceState) {
66         super.onCreate(savedInstanceState);
67 
68         Bundle args = getArguments();
69         if (args != null) {
70             mErrorMessage = args.getString(ERROR_MESSAGE_KEY);
71         }
72     }
73 
74     /**
75      * Sets the given error message to be displayed.
76      */
setErrorMessage(String errorMessage)77     public void setErrorMessage(String errorMessage) {
78         mErrorMessage = errorMessage;
79 
80         // If this method is called before the error message view is available, then no need to
81         // set the message. Instead, it will be set in onCreateView().
82         if (mErrorMessageView != null && !TextUtils.isEmpty(mErrorMessage)) {
83             mErrorMessageView.setText(mErrorMessage);
84         }
85     }
86 
87     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)88     public View onCreateView(LayoutInflater inflater, ViewGroup container,
89             Bundle savedInstanceState) {
90         View view = inflater.inflate(R.layout.no_hfp, container, false);
91         mErrorMessageView = view.findViewById(R.id.error_string);
92 
93         // If no error message is set, the default string from the layout will be used.
94         if (!TextUtils.isEmpty(mErrorMessage)) {
95             mErrorMessageView.setText(mErrorMessage);
96         }
97 
98         TelecomActivityViewModel viewModel = ViewModelProviders.of(getActivity()).get(
99                 TelecomActivityViewModel.class);
100         MutableLiveData<Integer> dialerAppStateLiveData = viewModel.getDialerAppState();
101         View emergencyButton = view.findViewById(R.id.emergency_call_button);
102         ViewUtils.setVisible(emergencyButton, UiCallManager.get().isEmergencyCallSupported());
103         emergencyButton.setOnClickListener(v -> dialerAppStateLiveData.setValue(
104                 TelecomActivityViewModel.DialerAppState.EMERGENCY_DIALPAD));
105 
106         Intent launchIntent = new Intent();
107         launchIntent.setAction(Bluetooth_Setting_ACTION);
108         launchIntent.addCategory(Bluetooth_Setting_CATEGORY);
109 
110         UxrButton bluetoothButton = view.findViewById(R.id.connect_bluetooth_button);
111         boolean isDistractionOptimized = CarPackageManagerUtils.getInstance(getActivity())
112                 .isDistractionOptimized(getActivity().getPackageManager(), launchIntent);
113         bluetoothButton.setUxRestrictions(isDistractionOptimized
114                 ? CarUxRestrictions.UX_RESTRICTIONS_BASELINE
115                 : CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP);
116         bluetoothButton.setOnClickListener(v -> startActivity(launchIntent));
117 
118         return view;
119     }
120 }
121