• 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.content.DialogInterface;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.os.Looper;
24 import android.util.Log;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.widget.TextView;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 import androidx.appcompat.app.AlertDialog;
32 import androidx.fragment.app.Fragment;
33 import androidx.fragment.app.FragmentManager;
34 import androidx.lifecycle.Lifecycle;
35 
36 import com.android.settings.R;
37 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
38 import com.android.settingslib.bluetooth.BluetoothUtils;
39 
40 import com.google.common.base.Strings;
41 
42 import java.util.concurrent.TimeUnit;
43 
44 public class AudioSharingProgressDialogFragment extends InstrumentedDialogFragment {
45     private static final String TAG = "AudioSharingProgressDlg";
46 
47     private static final String BUNDLE_KEY_MESSAGE = "bundle_key_message";
48     private static final long AUTO_DISMISS_TIME_THRESHOLD_MS = TimeUnit.SECONDS.toMillis(15);
49     private static final int AUTO_DISMISS_MESSAGE_ID = R.id.message;
50 
51     private static String sMessage = "";
52     @Nullable
53     private Handler mHandler;
54 
55     @Override
getMetricsCategory()56     public int getMetricsCategory() {
57         // TODO: add metrics
58         return 0;
59     }
60 
61     /**
62      * Display the {@link AudioSharingProgressDialogFragment} dialog.
63      *
64      * @param host    The Fragment this dialog will be hosted by.
65      * @param message The content to be shown on the dialog.
66      */
show(@ullable Fragment host, @NonNull String message)67     public static void show(@Nullable Fragment host, @NonNull String message) {
68         if (host == null) {
69             Log.d(TAG, "Fail to show dialog, host is null");
70             return;
71         }
72         if (BluetoothUtils.isAudioSharingUIAvailable(host.getContext())) {
73             final FragmentManager manager;
74             try {
75                 manager = host.getChildFragmentManager();
76             } catch (IllegalStateException e) {
77                 Log.d(TAG, "Fail to show dialog: " + e.getMessage());
78                 return;
79             }
80             Lifecycle.State currentState = host.getLifecycle().getCurrentState();
81             if (!currentState.isAtLeast(Lifecycle.State.STARTED)) {
82                 Log.d(TAG, "Fail to show dialog with state: " + currentState);
83                 return;
84             }
85             AlertDialog dialog = AudioSharingDialogHelper.getDialogIfShowing(manager, TAG);
86             if (dialog != null) {
87                 if (!sMessage.equals(message)) {
88                     Log.d(TAG, "Update dialog message.");
89                     TextView messageView = dialog.findViewById(R.id.message);
90                     if (messageView != null) {
91                         messageView.setText(message);
92                     }
93                     sMessage = message;
94                 }
95                 Log.d(TAG, "Dialog is showing, return.");
96                 return;
97             }
98             sMessage = message;
99             Log.d(TAG, "Show up the progress dialog.");
100             Bundle args = new Bundle();
101             args.putString(BUNDLE_KEY_MESSAGE, message);
102             AudioSharingProgressDialogFragment dialogFrag =
103                     new AudioSharingProgressDialogFragment();
104             dialogFrag.setArguments(args);
105             dialogFrag.show(manager, TAG);
106         }
107     }
108 
109     /** Dismiss the {@link AudioSharingProgressDialogFragment} dialog. */
dismiss(@ullable Fragment host)110     public static void dismiss(@Nullable Fragment host) {
111         if (host == null) {
112             Log.d(TAG, "Fail to dismiss dialog, host is null");
113             return;
114         }
115         if (BluetoothUtils.isAudioSharingUIAvailable(host.getContext())) {
116             final FragmentManager manager;
117             try {
118                 manager = host.getChildFragmentManager();
119             } catch (IllegalStateException e) {
120                 Log.d(TAG, "Fail to dismiss dialog: " + e.getMessage());
121                 return;
122             }
123             AlertDialog dialog = AudioSharingDialogHelper.getDialogIfShowing(manager, TAG);
124             if (dialog != null) {
125                 Log.d(TAG, "Dialog is showing, dismiss.");
126                 dialog.dismiss();
127             }
128         }
129     }
130 
131     @Override
132     @NonNull
onCreateDialog(@ullable Bundle savedInstanceState)133     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
134         mHandler = new Handler(Looper.getMainLooper());
135         Bundle args = requireArguments();
136         String message = args.getString(BUNDLE_KEY_MESSAGE, "");
137         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
138         LayoutInflater inflater = LayoutInflater.from(builder.getContext());
139         View customView = inflater.inflate(R.layout.dialog_audio_sharing_progress, /* root= */
140                 null);
141         TextView textView = customView.findViewById(R.id.message);
142         if (!Strings.isNullOrEmpty(message)) textView.setText(message);
143         AlertDialog dialog = builder.setView(customView).setCancelable(false).create();
144         dialog.setCanceledOnTouchOutside(false);
145         return dialog;
146     }
147 
148     @Override
onStart()149     public void onStart() {
150         super.onStart();
151         if (mHandler != null) {
152             Log.d(TAG, "onStart, postTimeOut for auto dismiss");
153             mHandler.postDelayed(() -> {
154                 Log.d(TAG, "Try to auto dismiss dialog after timeout");
155                 try {
156                     Dialog dialog = getDialog();
157                     if (dialog != null) {
158                         Log.d(TAG, "Dialog is not null, dismiss");
159                         dismissAllowingStateLoss();
160                     }
161                 } catch (IllegalStateException e) {
162                     Log.d(TAG, "Fail to dismiss: " + e.getMessage());
163                 }
164             }, AUTO_DISMISS_MESSAGE_ID, AUTO_DISMISS_TIME_THRESHOLD_MS);
165         }
166     }
167 
168     @Override
onDismiss(@onNull DialogInterface dialog)169     public void onDismiss(@NonNull DialogInterface dialog) {
170         super.onDismiss(dialog);
171         if (mHandler != null) {
172             Log.d(TAG, "Dialog dismissed, remove auto dismiss task");
173             mHandler.removeMessages(AUTO_DISMISS_MESSAGE_ID);
174         }
175     }
176 }
177