1 /* 2 * Copyright (C) 2025 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.audiostreams; 18 19 import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.ACTION_LE_AUDIO_PRIVATE_BROADCAST_RECEIVED; 20 import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.EXTRA_PRIVATE_BROADCAST_RECEIVE_DATA; 21 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.util.Log; 26 27 import com.android.settingslib.bluetooth.BluetoothUtils; 28 import com.android.settingslib.bluetooth.PrivateBroadcastReceiveData; 29 import com.android.settingslib.flags.Flags; 30 31 /** 32 * A {@link BroadcastReceiver} that listens for a private broadcast state. 33 * Upon receiving the broadcast, it extracts data and starts an {@link AudioStreamMediaService} 34 */ 35 public class PrivateBroadcastReceiver extends BroadcastReceiver { 36 private static final String TAG = "PrivBroadcastReceiver"; 37 38 @Override onReceive(Context context, Intent intent)39 public void onReceive(Context context, Intent intent) { 40 String action = intent.getAction(); 41 if (action == null || !action.equals(ACTION_LE_AUDIO_PRIVATE_BROADCAST_RECEIVED)) { 42 Log.w(TAG, "Received unexpected intent action."); 43 return; 44 } 45 if (!Flags.audioStreamMediaServiceByReceiveState() 46 || !BluetoothUtils.isAudioSharingUIAvailable(context)) { 47 Log.d(TAG, "Skip, flag/feature off"); 48 return; 49 } 50 PrivateBroadcastReceiveData data = intent.getParcelableExtra( 51 EXTRA_PRIVATE_BROADCAST_RECEIVE_DATA, PrivateBroadcastReceiveData.class); 52 if (data == null || !PrivateBroadcastReceiveData.Companion.isValid(data)) { 53 Log.w(TAG, "PrivateBroadcastReceiveData is null or invalid."); 54 return; 55 } 56 startOrUpdateService(context, data); 57 } 58 startOrUpdateService(Context context, PrivateBroadcastReceiveData data)59 private static void startOrUpdateService(Context context, PrivateBroadcastReceiveData data) { 60 Log.d(TAG, "startOrUpdateService() with data:" + data); 61 Intent intent = new Intent(context, AudioStreamMediaService.class); 62 intent.putExtra(EXTRA_PRIVATE_BROADCAST_RECEIVE_DATA, data); 63 context.startService(intent); 64 } 65 } 66