1 /*
2  * Copyright (C) 2020 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.content.Intent;
20 import android.os.Bundle;
21 
22 import androidx.fragment.app.Fragment;
23 import androidx.fragment.app.FragmentActivity;
24 import androidx.lifecycle.ViewModelProvider;
25 
26 import com.android.car.dialer.livedata.BluetoothErrorStringLiveData;
27 import com.android.car.dialer.ui.warning.NoHfpFragment;
28 
29 /**
30  * An activity to host {@link NoHfpFragment} and
31  * {@link com.android.car.dialer.ui.dialpad.DialpadFragment#newEmergencyDialpad()}.
32  */
33 public class NoHfpActivity extends FragmentActivity {
34 
35     @Override
onCreate(Bundle savedInstanceState)36     public void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38 
39         BluetoothErrorStringLiveData errorStringLiveData = (new ViewModelProvider(this))
40                 .get(NoHfpActivityViewModel.class)
41                 .getBluetoothErrorStringLiveData();
42 
43         errorStringLiveData.observe(this, (String error) -> {
44             if (BluetoothErrorStringLiveData.NO_BT_ERROR.equals(error)) {
45                 startActivity(new Intent(this, TelecomActivity.class));
46                 finish();
47             } else {
48                 Fragment currentFragment = getSupportFragmentManager()
49                         .findFragmentById(android.R.id.content);
50                 if (currentFragment instanceof NoHfpFragment) {
51                     ((NoHfpFragment) currentFragment).setErrorMessage(error);
52                 }
53             }
54         });
55 
56         if (savedInstanceState == null) {
57             getSupportFragmentManager().beginTransaction()
58                     .replace(android.R.id.content,
59                             NoHfpFragment.newInstance(errorStringLiveData.getValue()))
60                     .commit();
61         }
62     }
63 }
64