1 /*
2  * Copyright (C) 2023 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.app.settings.SettingsEnums;
21 import android.os.Bundle;
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 
29 import com.android.settings.R;
30 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
31 
32 import java.util.List;
33 
34 /** Provides a dialog to choose the active device for calls and alarms. */
35 public class AudioSharingCallAudioDialogFragment extends InstrumentedDialogFragment {
36     private static final String TAG = "CallsAndAlarmsDialog";
37     private static final String BUNDLE_KEY_DEVICE_ITEMS = "bundle_key_device_items";
38 
39     // The host creates an instance of this dialog fragment must implement this interface to receive
40     // event callbacks.
41     public interface DialogEventListener {
42         /**
43          * Called when users click the device item to set active for calls and alarms in the dialog.
44          *
45          * @param item The device item clicked.
46          */
onItemClick(AudioSharingDeviceItem item)47         void onItemClick(AudioSharingDeviceItem item);
48     }
49 
50     @Nullable private static DialogEventListener sListener;
51 
52     @Override
getMetricsCategory()53     public int getMetricsCategory() {
54         return SettingsEnums.DIALOG_AUDIO_SHARING_CALL_AUDIO;
55     }
56 
57     /**
58      * Display the {@link AudioSharingCallAudioDialogFragment} dialog.
59      *
60      * @param host The Fragment this dialog will be hosted.
61      * @param deviceItems The connected device items in audio sharing session.
62      * @param listener The callback to handle the user action on this dialog.
63      */
show( @onNull Fragment host, @NonNull List<AudioSharingDeviceItem> deviceItems, @NonNull DialogEventListener listener)64     public static void show(
65             @NonNull Fragment host,
66             @NonNull List<AudioSharingDeviceItem> deviceItems,
67             @NonNull DialogEventListener listener) {
68         if (!AudioSharingUtils.isFeatureEnabled()) return;
69         final FragmentManager manager = host.getChildFragmentManager();
70         sListener = listener;
71         if (manager.findFragmentByTag(TAG) == null) {
72             final Bundle bundle = new Bundle();
73             bundle.putParcelableList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems);
74             final AudioSharingCallAudioDialogFragment dialog =
75                     new AudioSharingCallAudioDialogFragment();
76             dialog.setArguments(bundle);
77             dialog.show(manager, TAG);
78         }
79     }
80 
81     @Override
onCreateDialog(Bundle savedInstanceState)82     public Dialog onCreateDialog(Bundle savedInstanceState) {
83         Bundle arguments = requireArguments();
84         List<AudioSharingDeviceItem> deviceItems =
85                 arguments.getParcelable(BUNDLE_KEY_DEVICE_ITEMS, List.class);
86         int checkedItem = -1;
87         for (AudioSharingDeviceItem item : deviceItems) {
88             int fallbackActiveGroupId = AudioSharingUtils.getFallbackActiveGroupId(getContext());
89             if (item.getGroupId() == fallbackActiveGroupId) {
90                 checkedItem = deviceItems.indexOf(item);
91             }
92         }
93         String[] choices =
94                 deviceItems.stream().map(AudioSharingDeviceItem::getName).toArray(String[]::new);
95         AlertDialog.Builder builder =
96                 new AlertDialog.Builder(getActivity())
97                         .setTitle(R.string.audio_sharing_call_audio_title)
98                         .setSingleChoiceItems(
99                                 choices,
100                                 checkedItem,
101                                 (dialog, which) -> {
102                                     if (sListener != null) {
103                                         sListener.onItemClick(deviceItems.get(which));
104                                     }
105                                 });
106         return builder.create();
107     }
108 }
109