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