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.graphics.Typeface;
20 import android.util.Log;
21 import android.util.TypedValue;
22 import android.view.View;
23 import android.widget.TextView;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.appcompat.app.AlertDialog;
28 import androidx.fragment.app.DialogFragment;
29 import androidx.fragment.app.Fragment;
30 import androidx.fragment.app.FragmentManager;
31 
32 public class AudioSharingDialogHelper {
33     private static final String TAG = "AudioSharingDialogHelper";
34 
35     /** Updates the alert dialog message style. */
updateMessageStyle(@onNull AlertDialog dialog)36     public static void updateMessageStyle(@NonNull AlertDialog dialog) {
37         TextView messageView = dialog.findViewById(android.R.id.message);
38         if (messageView != null) {
39             Typeface typeface = Typeface.create(Typeface.DEFAULT_FAMILY, Typeface.NORMAL);
40             messageView.setTypeface(typeface);
41             messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
42             messageView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
43             messageView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
44         } else {
45             Log.w(TAG, "Fail to update dialog: message view is null");
46         }
47     }
48 
49     /** Returns the alert dialog by tag if it is showing. */
50     @Nullable
getDialogIfShowing( @onNull FragmentManager manager, @NonNull String tag)51     public static AlertDialog getDialogIfShowing(
52             @NonNull FragmentManager manager, @NonNull String tag) {
53         Fragment dialog = manager.findFragmentByTag(tag);
54         return dialog != null
55                         && dialog instanceof DialogFragment
56                         && ((DialogFragment) dialog).getDialog() != null
57                         && ((DialogFragment) dialog).getDialog().isShowing()
58                         && ((DialogFragment) dialog).getDialog() instanceof AlertDialog
59                 ? (AlertDialog) ((DialogFragment) dialog).getDialog()
60                 : null;
61     }
62 }
63