• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.settings.connecteddevice.audiosharing;
18 
19 import android.app.Dialog;
20 import android.os.Bundle;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.appcompat.app.AlertDialog;
26 import androidx.fragment.app.Fragment;
27 import androidx.fragment.app.FragmentManager;
28 import androidx.lifecycle.Lifecycle;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
32 import com.android.settingslib.bluetooth.BluetoothUtils;
33 
34 public class AudioSharingErrorDialogFragment extends InstrumentedDialogFragment {
35     private static final String TAG = "AudioSharingErrorDialog";
36 
37     @Override
getMetricsCategory()38     public int getMetricsCategory() {
39         // TODO: add metrics
40         return 0;
41     }
42 
43     /**
44      * Display the {@link AudioSharingErrorDialogFragment} dialog.
45      *
46      * @param host The Fragment this dialog will be hosted.
47      */
show(@ullable Fragment host)48     public static void show(@Nullable Fragment host) {
49         if (host == null) {
50             Log.d(TAG, "Fail to show dialog, host is null");
51             return;
52         }
53         if (BluetoothUtils.isAudioSharingUIAvailable(host.getContext())) {
54             final FragmentManager manager;
55             try {
56                 manager = host.getChildFragmentManager();
57             } catch (IllegalStateException e) {
58                 Log.d(TAG, "Fail to show dialog: " + e.getMessage());
59                 return;
60             }
61             Lifecycle.State currentState = host.getLifecycle().getCurrentState();
62             if (!currentState.isAtLeast(Lifecycle.State.STARTED)) {
63                 Log.d(TAG, "Fail to show dialog with state: " + currentState);
64                 return;
65             }
66             AlertDialog dialog = AudioSharingDialogHelper.getDialogIfShowing(manager, TAG);
67             if (dialog != null) {
68                 Log.d(TAG, "Dialog is showing, return.");
69                 return;
70             }
71             Log.d(TAG, "Show up the error dialog.");
72             AudioSharingErrorDialogFragment dialogFrag = new AudioSharingErrorDialogFragment();
73             dialogFrag.show(manager, TAG);
74         }
75     }
76 
77     @Override
78     @NonNull
onCreateDialog(@ullable Bundle savedInstanceState)79     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
80         AlertDialog dialog =
81                 AudioSharingDialogFactory.newBuilder(getActivity())
82                         .setTitle(R.string.audio_sharing_retry_dialog_title)
83                         .setTitleIcon(R.drawable.ic_warning_24dp)
84                         .setIsCustomBodyEnabled(true)
85                         .setCustomMessage(R.string.audio_sharing_retry_dialog_content)
86                         .setPositiveButton(R.string.okay, (d, w) -> {})
87                         .build();
88         dialog.setCanceledOnTouchOutside(true);
89         return dialog;
90     }
91 }
92