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 import android.util.Log; 23 import android.util.Pair; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 import androidx.annotation.VisibleForTesting; 28 import androidx.appcompat.app.AlertDialog; 29 import androidx.fragment.app.Fragment; 30 import androidx.fragment.app.FragmentActivity; 31 import androidx.fragment.app.FragmentManager; 32 import androidx.lifecycle.Lifecycle; 33 34 import com.android.settings.R; 35 import com.android.settings.bluetooth.Utils; 36 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 37 import com.android.settingslib.bluetooth.BluetoothUtils; 38 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 39 import com.android.settingslib.flags.Flags; 40 41 import java.util.List; 42 43 public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment { 44 private static final String TAG = "AudioSharingJoinDialog"; 45 46 private static final String BUNDLE_KEY_DEVICE_ITEMS = "bundle_key_device_items"; 47 private static final String BUNDLE_KEY_NEW_DEVICE_NAME = "bundle_key_new_device_name"; 48 49 // The host creates an instance of this dialog fragment must implement this interface to receive 50 // event callbacks. 51 public interface DialogEventListener { 52 /** Called when users click the share audio button in the dialog. */ onShareClick()53 void onShareClick(); 54 55 /** Called when users click the cancel button in the dialog. */ onCancelClick()56 void onCancelClick(); 57 } 58 59 @Nullable private static DialogEventListener sListener; 60 @Nullable private static CachedBluetoothDevice sNewDevice; 61 private static Pair<Integer, Object>[] sEventData = new Pair[0]; 62 63 @Override getMetricsCategory()64 public int getMetricsCategory() { 65 return BluetoothUtils.isBroadcasting(Utils.getLocalBtManager(getContext())) 66 ? SettingsEnums.DIALOG_AUDIO_SHARING_ADD_DEVICE 67 : SettingsEnums.DIALOG_START_AUDIO_SHARING; 68 } 69 70 /** 71 * Display the {@link AudioSharingJoinDialogFragment} dialog. 72 * 73 * <p>If the dialog is showing, update the dialog message and event listener. 74 * 75 * @param host The Fragment this dialog will be hosted. 76 * @param deviceItems The existing connected device items eligible for audio sharing. 77 * @param newDevice The latest connected device triggered this dialog. 78 * @param listener The callback to handle the user action on this dialog. 79 * @param eventData The eventData to log with for dialog onClick events. 80 * 81 * @return whether the dialog is shown 82 */ show( @ullable Fragment host, @NonNull List<AudioSharingDeviceItem> deviceItems, @NonNull CachedBluetoothDevice newDevice, @NonNull DialogEventListener listener, @NonNull Pair<Integer, Object>[] eventData)83 public static boolean show( 84 @Nullable Fragment host, 85 @NonNull List<AudioSharingDeviceItem> deviceItems, 86 @NonNull CachedBluetoothDevice newDevice, 87 @NonNull DialogEventListener listener, 88 @NonNull Pair<Integer, Object>[] eventData) { 89 if (host == null) { 90 Log.d(TAG, "Fail to show dialog, host is null"); 91 return false; 92 } 93 if (!BluetoothUtils.isAudioSharingUIAvailable(host.getContext())) { 94 Log.d(TAG, "Fail to show dialog, feature disabled"); 95 return false; 96 } 97 final FragmentManager manager; 98 try { 99 manager = host.getChildFragmentManager(); 100 } catch (IllegalStateException e) { 101 Log.d(TAG, "Fail to show dialog: " + e.getMessage()); 102 return false; 103 } 104 Lifecycle.State currentState = host.getLifecycle().getCurrentState(); 105 if (!currentState.isAtLeast(Lifecycle.State.CREATED)) { 106 Log.d(TAG, "Fail to show dialog with state: " + currentState); 107 return false; 108 } 109 sListener = listener; 110 sNewDevice = newDevice; 111 sEventData = eventData; 112 AudioSharingUtils.postOnMainThread( 113 host.getContext(), 114 () -> { 115 AlertDialog dialog = AudioSharingDialogHelper.getDialogIfShowing(manager, 116 TAG); 117 if (dialog != null) { 118 Log.d(TAG, "Dialog is showing, update the content."); 119 updateDialog(deviceItems, newDevice.getName(), dialog); 120 } else { 121 Log.d(TAG, "Show up the dialog."); 122 final Bundle bundle = new Bundle(); 123 bundle.putParcelableList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems); 124 bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDevice.getName()); 125 final AudioSharingJoinDialogFragment dialogFrag = 126 new AudioSharingJoinDialogFragment(); 127 dialogFrag.setArguments(bundle); 128 dialogFrag.show(manager, TAG); 129 } 130 }); 131 return true; 132 } 133 134 /** Return the tag of {@link AudioSharingJoinDialogFragment} dialog. */ tag()135 public static @NonNull String tag() { 136 return TAG; 137 } 138 139 /** Get the latest connected device which triggers the dialog. */ getDevice()140 public @Nullable CachedBluetoothDevice getDevice() { 141 return sNewDevice; 142 } 143 144 /** Test only: get the {@link DialogEventListener} passed to the dialog. */ 145 @VisibleForTesting 146 @Nullable getListener()147 DialogEventListener getListener() { 148 return sListener; 149 } 150 151 /** Test only: get the event data passed to the dialog. */ 152 @VisibleForTesting 153 @NonNull getEventData()154 Pair<Integer, Object>[] getEventData() { 155 return sEventData; 156 } 157 158 @Override 159 @NonNull onCreateDialog(@ullable Bundle savedInstanceState)160 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 161 Bundle arguments = requireArguments(); 162 List<AudioSharingDeviceItem> deviceItems = 163 arguments.getParcelable(BUNDLE_KEY_DEVICE_ITEMS, List.class); 164 String newDeviceName = arguments.getString(BUNDLE_KEY_NEW_DEVICE_NAME); 165 AlertDialog dialog = 166 AudioSharingDialogFactory.newBuilder(getActivity()) 167 .setTitle(R.string.audio_sharing_share_dialog_title) 168 .setTitleIcon(com.android.settingslib.R.drawable.ic_bt_le_audio_sharing) 169 .setIsCustomBodyEnabled(true) 170 .setCustomMessage(R.string.audio_sharing_dialog_share_content) 171 .setCustomPositiveButton( 172 R.string.audio_sharing_share_button_label, 173 v -> { 174 if (sListener != null) { 175 sListener.onShareClick(); 176 mMetricsFeatureProvider.action( 177 getContext(), 178 SettingsEnums 179 .ACTION_AUDIO_SHARING_DIALOG_POSITIVE_BTN_CLICKED, 180 sEventData); 181 } 182 dismiss(); 183 }) 184 .setCustomNegativeButton( 185 getMetricsCategory() == SettingsEnums.DIALOG_START_AUDIO_SHARING 186 ? getString( 187 R.string.audio_sharing_switch_active_button_label, 188 newDeviceName) 189 : getString(R.string.audio_sharing_no_thanks_button_label), 190 v -> { 191 if (sListener != null) { 192 sListener.onCancelClick(); 193 mMetricsFeatureProvider.action( 194 getContext(), 195 SettingsEnums 196 .ACTION_AUDIO_SHARING_DIALOG_NEGATIVE_BTN_CLICKED, 197 sEventData); 198 } 199 dismiss(); 200 }) 201 .build(); 202 if (deviceItems == null) { 203 Log.d(TAG, "Fail to create dialog: null deviceItems"); 204 } else { 205 updateDialog(deviceItems, newDeviceName, dialog); 206 } 207 dialog.show(); 208 AudioSharingDialogHelper.updateMessageStyle(dialog); 209 return dialog; 210 } 211 212 @Override onDestroy()213 public void onDestroy() { 214 super.onDestroy(); 215 FragmentActivity activity = getActivity(); 216 if (Flags.promoteAudioSharingForSecondAutoConnectedLeaDevice() 217 && activity instanceof AudioSharingJoinHandlerActivity 218 && !activity.isChangingConfigurations() && !activity.isFinishing()) { 219 Log.d(TAG, "onDestroy, finish activity = " + activity.getClass().getName()); 220 activity.finish(); 221 } 222 } 223 updateDialog( List<AudioSharingDeviceItem> deviceItems, String newDeviceName, @NonNull AlertDialog dialog)224 private static void updateDialog( 225 List<AudioSharingDeviceItem> deviceItems, 226 String newDeviceName, 227 @NonNull AlertDialog dialog) { 228 // Only dialog message can be updated when the dialog is showing. 229 // Thus we put the device name for sharing as the dialog message. 230 if (deviceItems.isEmpty()) { 231 dialog.setMessage(newDeviceName); 232 } else { 233 dialog.setMessage( 234 dialog.getContext() 235 .getString( 236 R.string.audio_sharing_share_dialog_subtitle, 237 deviceItems.get(0).getName(), 238 newDeviceName)); 239 } 240 } 241 } 242