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.Car;
20 import android.car.content.pm.CarPackageManager;
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.text.TextUtils;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.TextView;
29 
30 import androidx.fragment.app.Fragment;
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.dialpad.DialpadFragment;
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     private Car mCar;
52     private CarPackageManager mCarPackageManager;
53 
54     /**
55      * Returns an instance of the {@link NoHfpFragment} with the given error message as the one to
56      * display.
57      */
newInstance(String errorMessage)58     public static NoHfpFragment newInstance(String errorMessage) {
59         NoHfpFragment fragment = new NoHfpFragment();
60 
61         Bundle args = new Bundle();
62         args.putString(ERROR_MESSAGE_KEY, errorMessage);
63         fragment.setArguments(args);
64 
65         return fragment;
66     }
67 
onCreate(Bundle savedInstanceState)68     public void onCreate(Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70 
71         Bundle args = getArguments();
72         if (args != null) {
73             mErrorMessage = args.getString(ERROR_MESSAGE_KEY);
74         }
75         mCar = Car.createCar(getActivity());
76         mCarPackageManager = (CarPackageManager) mCar.getCarManager(Car.PACKAGE_SERVICE);
77     }
78 
79     @Override
onDestroy()80     public void onDestroy() {
81         mCar.disconnect();
82         super.onDestroy();
83     }
84 
85     /**
86      * Sets the given error message to be displayed.
87      */
setErrorMessage(String errorMessage)88     public void setErrorMessage(String errorMessage) {
89         mErrorMessage = errorMessage;
90 
91         // If this method is called before the error message view is available, then no need to
92         // set the message. Instead, it will be set in onCreateView().
93         if (mErrorMessageView != null && !TextUtils.isEmpty(mErrorMessage)) {
94             mErrorMessageView.setText(mErrorMessage);
95         }
96     }
97 
98     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)99     public View onCreateView(LayoutInflater inflater, ViewGroup container,
100             Bundle savedInstanceState) {
101         View view = inflater.inflate(R.layout.no_hfp, container, false);
102         mErrorMessageView = view.findViewById(R.id.error_string);
103 
104         // If no error message is set, the default string from the layout will be used.
105         if (!TextUtils.isEmpty(mErrorMessage)) {
106             mErrorMessageView.setText(mErrorMessage);
107         }
108 
109         View emergencyButton = view.findViewById(R.id.emergency_call_button);
110         ViewUtils.setVisible(emergencyButton, UiCallManager.get().isEmergencyCallSupported());
111         emergencyButton.setOnClickListener(v -> getParentFragmentManager()
112                 .beginTransaction()
113                 .replace(android.R.id.content, DialpadFragment.newEmergencyDialpad())
114                 .addToBackStack(null)
115                 .commit());
116 
117         Intent launchIntent = new Intent();
118         launchIntent.setAction(Bluetooth_Setting_ACTION);
119         launchIntent.addCategory(Bluetooth_Setting_CATEGORY);
120 
121         UxrButton bluetoothButton = view.findViewById(R.id.connect_bluetooth_button);
122         boolean isDistractionOptimized = CarPackageManagerUtils.isDistractionOptimized(
123                 mCarPackageManager, getActivity().getPackageManager(), launchIntent);
124         bluetoothButton.setUxRestrictions(isDistractionOptimized
125                 ? CarUxRestrictions.UX_RESTRICTIONS_BASELINE
126                 : CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP);
127         bluetoothButton.setOnClickListener(v -> startActivity(launchIntent));
128 
129         return view;
130     }
131 }
132