[Audiosharing] Clean up audio sharing from Settings

Bug: 324023639
Test: atest
Change-Id: Ie73790442f39af60b125bbd48182a700bb0b290a
This commit is contained in:
Yiyi Shen
2024-02-26 11:29:59 +08:00
parent be3a609c25
commit dd7626f55b
72 changed files with 1 additions and 9803 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

View File

@@ -1,22 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="?android:colorButtonNormal" />
<corners android:radius="12dp" />
</shape>

View File

@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:drawable="@drawable/audio_sharing_rounded_bg"/>
</ripple>

View File

@@ -1,32 +0,0 @@
<!--
Copyright (C) 2018 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
android:tint="?android:attr/colorControlNormal">
<path
android:pathData="M3,15V9H7L12,4V20L7,15H3ZM10,15.17V8.83L7.83,11H5V13H7.83L10,15.17Z"
android:fillType="evenOdd"
android:fillColor="?android:attr/colorPrimary"/>
<path
android:pathData="M16.5,12C16.5,10.23 15.48,8.71 14,7.97V16.02C15.48,15.29 16.5,13.77 16.5,12Z"
android:fillColor="?android:attr/colorPrimary"/>
<path
android:pathData="M14,3.23V5.29C16.89,6.15 19,8.83 19,12C19,15.17 16.89,17.85 14,18.71V20.77C18.01,19.86 21,16.28 21,12C21,7.72 18.01,4.14 14,3.23Z"
android:fillColor="?android:attr/colorPrimary"/>
</vector>

View File

@@ -1,32 +0,0 @@
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?android:attr/colorControlNormal">
<path
android:pathData="M14,8C9.6,8 6,11.6 6,16H8C8,12.7 10.7,10 14,10V8Z"
android:fillColor="#4E4639"/>
<path
android:pathData="M14,6V4C7.4,4 2,9.4 2,16H4C4,10.5 8.5,6 14,6Z"
android:fillColor="#4E4639"/>
<path
android:pathData="M16,4V12.6C15.4,12.3 14.7,12 14,12C11.8,12 10,13.8 10,16C10,18.2 11.8,20 14,20C16.2,20 18,18.2 18,16V7H22V4H16ZM14,18C12.9,18 12,17.1 12,16C12,14.9 12.9,14 14,14C15.1,14 16,14.9 16,16C16,17.1 15.1,18 14,18Z"
android:fillColor="#4E4639"/>
</vector>

View File

@@ -1,33 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/device_button"
style="@style/SettingsLibActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="@drawable/audio_sharing_rounded_bg_ripple"
android:textAlignment="center" />
</FrameLayout>

View File

@@ -1,82 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="?android:dialogPreferredPadding"
android:paddingBottom="?android:dialogPreferredPadding">
<TextView
android:id="@+id/share_audio_subtitle1"
style="@style/DeviceAudioSharingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="14dp"
android:textFontWeight="500"
android:visibility="gone" />
<TextView
android:id="@+id/share_audio_subtitle2"
style="@style/DeviceAudioSharingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="24dp"
android:textFontWeight="400" />
<ImageView
android:id="@+id/share_audio_guidance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/audio_sharing_guidance"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/btn_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:nestedScrollingEnabled="false"
android:overScrollMode="never"
android:visibility="gone" />
<Button
android:id="@+id/share_btn"
style="@style/SettingsLibActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@drawable/audio_sharing_rounded_bg_ripple"
android:visibility="gone" />
<Button
android:id="@+id/cancel_btn"
style="@style/SettingsLibActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@drawable/audio_sharing_rounded_bg_ripple"
android:text="Not now"
android:visibility="gone" />
</LinearLayout>

View File

@@ -1,51 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="?android:dialogPreferredPadding"
android:paddingBottom="?android:dialogPreferredPadding">
<TextView
android:id="@+id/share_audio_disconnect_description"
style="@style/DeviceAudioSharingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="24dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/device_btn_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:nestedScrollingEnabled="false"
android:overScrollMode="never" />
<Button
android:id="@+id/cancel_btn"
style="@style/SettingsLibActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@drawable/audio_sharing_rounded_bg_ripple"
android:text="@string/cancel" />
</LinearLayout>

View File

@@ -1,53 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="?android:dialogPreferredPadding"
android:paddingBottom="?android:dialogPreferredPadding">
<TextView
android:id="@+id/share_audio_subtitle"
style="@style/DeviceAudioSharingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="24dp"
android:textFontWeight="400" />
<Button
android:id="@+id/share_btn"
style="@style/SettingsLibActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@drawable/audio_sharing_rounded_bg_ripple" />
<Button
android:id="@+id/cancel_btn"
style="@style/SettingsLibActionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="@drawable/audio_sharing_rounded_bg_ripple"
android:text="Not now" />
</LinearLayout>

View File

@@ -1,41 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="?android:dialogPreferredPadding">
<ImageView
android:id="@+id/title_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:tint="?android:attr/colorControlNormal" />
<TextView
android:id="@+id/title_text"
style="?android:attr/windowTitleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="2"
android:paddingTop="14dp"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>

View File

@@ -300,21 +300,6 @@
<!-- Name shown in the title of individual stylus preference in the connected devices page [CHAR LIMIT=60] --> <!-- Name shown in the title of individual stylus preference in the connected devices page [CHAR LIMIT=60] -->
<string name="stylus_connected_devices_title">Stylus</string> <string name="stylus_connected_devices_title">Stylus</string>
<!-- Title for audio sharing page [CHAR LIMIT=none]-->
<string name="audio_sharing_title">Audio sharing</string>
<!-- Title for audio sharing primary switch [CHAR LIMIT=none]-->
<string name="audio_sharing_switch_title">Share audio</string>
<!-- Title for calls and alarms device on audio sharing page [CHAR LIMIT=none]-->
<string name="calls_and_alarms_device_title">Calls and alarms</string>
<!-- Title for audio streams preference category [CHAR LIMIT=none]-->
<string name="audio_streams_category_title">Connect to a LE audio stream</string>
<!-- Title for audio streams preference [CHAR LIMIT=none]-->
<string name="audio_streams_pref_title">Nearby audio streams</string>
<!-- Title for audio streams page [CHAR LIMIT=none]-->
<string name="audio_streams_title">Audio streams</string>
<!-- Summary for QR code scanning in audio streams page [CHAR LIMIT=none]-->
<string name="audio_streams_qr_code_summary">Connect to an audio stream using QR code</string>
<!--Text that appears when scanning for nearby audio streams is finished and no streams were found [CHAR LIMIT=40]--> <!--Text that appears when scanning for nearby audio streams is finished and no streams were found [CHAR LIMIT=40]-->
<string name="audio_streams_empty">No nearby audio streams were found.</string> <string name="audio_streams_empty">No nearby audio streams were found.</string>

View File

@@ -1,80 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/audio_sharing_title">
<com.android.settingslib.widget.TopIntroPreference
android:key="audio_sharing_top_intro"
android:title="Let others listen to your media along with you using their own compatible headphones"
settings:searchable="false" />
<PreferenceCategory
android:key="audio_sharing_device_volume_group"
android:title="Devices listening"
settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingDeviceVolumeGroupController" />
<Preference
android:icon="@drawable/ic_audio_calls_and_alarms"
android:key="calls_and_alarms"
android:summary=""
android:title="@string/calls_and_alarms_device_title"
settings:controller="com.android.settings.connecteddevice.audiosharing.CallsAndAlarmsPreferenceController" />
<Preference
android:icon="@drawable/ic_audio_play_sample"
android:key="audio_sharing_play_sound"
android:summary="Everyone listening should hear it"
android:title="Play a test sound"
settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingPlaySoundPreferenceController" />
<PreferenceCategory
android:key="audio_sharing_stream_settings_category"
android:title="Stream settings"
settings:controller="com.android.settings.connecteddevice.audiosharing.StreamSettingsCategoryController">
<com.android.settings.connecteddevice.audiosharing.AudioSharingNamePreference
android:key="audio_sharing_stream_name"
android:title="Name"
settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingNamePreferenceController" />
<com.android.settings.widget.ValidatedEditTextPreference
android:key="audio_sharing_stream_password"
android:summary="********"
android:title="Password"
settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingPasswordPreferenceController" />
<SwitchPreferenceCompat
android:key="audio_sharing_stream_compatibility"
android:title="Improve compatibility"
settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingCompatibilityPreferenceController" />
</PreferenceCategory>
<PreferenceCategory
android:key="audio_streams_settings_category"
android:title="@string/audio_streams_category_title"
settings:controller="com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsCategoryController">
<Preference
android:fragment="com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsDashboardFragment"
android:icon="@drawable/ic_chevron_right_24dp"
android:key="audio_streams_settings"
android:title="@string/audio_streams_pref_title" />
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -1,48 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="Find an audio stream">
<com.android.settingslib.widget.TopIntroPreference
android:key="audio_streams_top_intro"
android:title="Listen to a device that's sharing audio or to a nearby Auracast broadcast"
settings:searchable="false"/>
<Preference
android:key="audio_streams_active_device"
android:title="Your audio device"
settings:controller="com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsActiveDeviceController" />
<com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsProgressCategoryPreference
android:key="audio_streams_nearby_category"
android:title="Audio streams nearby"
settings:controller="com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsProgressCategoryController">
<Preference
android:key="audio_streams_scan_qr_code"
android:title="Scan a QR code"
android:icon="@drawable/ic_add_24dp"
android:summary="Start listening by scanning a stream's QR code"
android:order="0"
settings:controller="com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsScanQrCodeController" />
</com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsProgressCategoryPreference>
</PreferenceScreen>

View File

@@ -26,19 +26,6 @@
settings:allowDividerBelow="true" settings:allowDividerBelow="true"
settings:controller="com.android.settings.slices.SlicePreferenceController" /> settings:controller="com.android.settings.slices.SlicePreferenceController" />
<PreferenceCategory
android:key="audio_sharing_device_list"
android:title="@string/audio_sharing_title"
settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingDevicePreferenceController">
<Preference
android:fragment="com.android.settings.connecteddevice.audiosharing.AudioSharingDashboardFragment"
android:icon="@drawable/ic_bt_audio_sharing"
android:key="connected_device_audio_sharing_settings"
android:order="10"
android:title="@string/audio_sharing_title"
settings:searchable="false" />
</PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:key="available_device_list" android:key="available_device_list"
android:title="@string/connected_device_media_device_title"/> android:title="@string/connected_device_media_device_title"/>

View File

@@ -26,15 +26,6 @@
android:order="-10" android:order="-10"
android:title="@string/bluetooth_settings_title" /> android:title="@string/bluetooth_settings_title" />
<Preference
android:fragment="com.android.settings.connecteddevice.audiosharing.AudioSharingDashboardFragment"
android:icon="@drawable/ic_bt_audio_sharing"
android:key="audio_sharing_settings"
android:order="-9"
android:title="@string/audio_sharing_title"
settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingPreferenceController"
settings:searchable="true" />
<com.android.settingslib.RestrictedPreference <com.android.settingslib.RestrictedPreference
android:fragment="com.android.settings.connecteddevice.NfcAndPaymentFragment" android:fragment="com.android.settings.connecteddevice.NfcAndPaymentFragment"
android:icon="@drawable/ic_nfc" android:icon="@drawable/ic_nfc"

View File

@@ -1,89 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
public abstract class AudioSharingBasePreferenceController extends BasePreferenceController
implements DefaultLifecycleObserver {
private final BluetoothAdapter mBluetoothAdapter;
private final LocalBluetoothManager mBtManager;
protected final LocalBluetoothLeBroadcast mBroadcast;
protected Preference mPreference;
public AudioSharingBasePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBtManager = Utils.getLocalBtManager(context);
mBroadcast =
mBtManager == null
? null
: mBtManager.getProfileManager().getLeAudioBroadcastProfile();
}
@Override
public int getAvailabilityStatus() {
return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (isAvailable()) {
updateVisibility();
}
}
/** Update the visibility of the preference. */
protected void updateVisibility() {
if (mPreference != null) {
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
boolean isVisible = isBroadcasting() && isBluetoothStateOn();
ThreadUtils.postOnMainThread(
() -> mPreference.setVisible(isVisible));
});
}
}
protected boolean isBroadcasting() {
return mBroadcast != null && mBroadcast.isEnabled(null);
}
protected boolean isBluetoothStateOn() {
return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
}
}

View File

@@ -1,91 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.content.Context;
import android.util.Log;
import androidx.preference.Preference;
import com.android.settings.bluetooth.BluetoothDevicePreference;
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
public class AudioSharingBluetoothDeviceUpdater extends BluetoothDeviceUpdater
implements Preference.OnPreferenceClickListener {
private static final String TAG = "AudioSharingBluetoothDeviceUpdater";
private static final String PREF_KEY = "audio_sharing_bt";
private LocalBluetoothManager mLocalBluetoothManager;
public AudioSharingBluetoothDeviceUpdater(
Context context,
DevicePreferenceCallback devicePreferenceCallback,
int metricsCategory) {
super(context, devicePreferenceCallback, metricsCategory);
mLocalBluetoothManager = Utils.getLocalBluetoothManager(context);
}
@Override
public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
boolean isFilterMatched = false;
if (isDeviceConnected(cachedDevice) && isDeviceInCachedDevicesList(cachedDevice)) {
// If device is LE audio device and has a broadcast source,
// it would show in audio sharing devices group.
if (cachedDevice.isConnectedLeAudioDevice()
&& AudioSharingUtils.hasBroadcastSource(cachedDevice, mLocalBluetoothManager)) {
isFilterMatched = true;
}
}
Log.d(
TAG,
"isFilterMatched() device : "
+ cachedDevice.getName()
+ ", isFilterMatched : "
+ isFilterMatched);
return isFilterMatched;
}
@Override
public boolean onPreferenceClick(Preference preference) {
mMetricsFeatureProvider.logClickedPreference(preference, mMetricsCategory);
final CachedBluetoothDevice device =
((BluetoothDevicePreference) preference).getBluetoothDevice();
return device.setActive();
}
@Override
protected String getPreferenceKey() {
return PREF_KEY;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected void update(CachedBluetoothDevice cachedBluetoothDevice) {
super.update(cachedBluetoothDevice);
Log.d(TAG, "Map : " + mPreferenceMap);
}
}

View File

@@ -1,177 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import androidx.preference.TwoStatePreference;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AudioSharingCompatibilityPreferenceController extends TogglePreferenceController
implements DefaultLifecycleObserver {
private static final String TAG = "AudioSharingCompatibilityPrefController";
private static final String PREF_KEY = "audio_sharing_stream_compatibility";
private static final String SHARING_OFF_SUMMARY =
"Helps some devices like hearing aids connect by reducing audio quality";
private static final String SHARING_ON_SUMMARY =
"Turns off the audio sharing to config the compatibility";
private final LocalBluetoothManager mBtManager;
private final Executor mExecutor;
private final LocalBluetoothLeBroadcast mBroadcast;
@Nullable private TwoStatePreference mPreference;
private final BluetoothLeBroadcast.Callback mBroadcastCallback =
new BluetoothLeBroadcast.Callback() {
@Override
public void onBroadcastStarted(int reason, int broadcastId) {
Log.d(
TAG,
"onBroadcastStarted(), reason = "
+ reason
+ ", broadcastId = "
+ broadcastId);
updateEnabled();
}
@Override
public void onBroadcastStartFailed(int reason) {}
@Override
public void onBroadcastMetadataChanged(
int broadcastId, @NonNull BluetoothLeBroadcastMetadata metadata) {}
@Override
public void onBroadcastStopped(int reason, int broadcastId) {
Log.d(
TAG,
"onBroadcastStopped(), reason = "
+ reason
+ ", broadcastId = "
+ broadcastId);
updateEnabled();
}
@Override
public void onBroadcastStopFailed(int reason) {}
@Override
public void onBroadcastUpdated(int reason, int broadcastId) {}
@Override
public void onBroadcastUpdateFailed(int reason, int broadcastId) {}
@Override
public void onPlaybackStarted(int reason, int broadcastId) {}
@Override
public void onPlaybackStopped(int reason, int broadcastId) {}
};
public AudioSharingCompatibilityPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mBtManager = Utils.getLocalBtManager(context);
mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile();
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mBroadcast != null) {
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mBroadcast != null) {
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
}
}
@Override
public int getAvailabilityStatus() {
return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
updateEnabled();
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
}
@Override
public boolean isChecked() {
return mBroadcast != null && mBroadcast.getImproveCompatibility();
}
@Override
public boolean setChecked(boolean isChecked) {
if (mBroadcast == null || mBroadcast.getImproveCompatibility() == isChecked) {
Log.d(
TAG,
"Skip setting improveCompatibility, unchanged = "
+ (mBroadcast.getImproveCompatibility() == isChecked));
return false;
}
mBroadcast.setImproveCompatibility(isChecked);
// TODO: call updateBroadcast once framework change ready.
return true;
}
@Override
public int getSliceHighlightMenuRes() {
return 0;
}
private void updateEnabled() {
mContext.getMainExecutor()
.execute(
() -> {
if (mPreference != null) {
boolean isBroadcasting =
AudioSharingUtils.isBroadcasting(mBtManager);
mPreference.setEnabled(!isBroadcasting);
mPreference.setSummary(
isBroadcasting ? SHARING_ON_SUMMARY : SHARING_OFF_SUMMARY);
}
});
}
}

View File

@@ -1,107 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsCategoryController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.SettingsMainSwitchBar;
public class AudioSharingDashboardFragment extends DashboardFragment
implements AudioSharingSwitchBarController.OnSwitchBarChangedListener {
private static final String TAG = "AudioSharingDashboardFrag";
SettingsMainSwitchBar mMainSwitchBar;
private AudioSharingSwitchBarController mSwitchBarController;
private AudioSharingDeviceVolumeGroupController mAudioSharingDeviceVolumeGroupController;
private CallsAndAlarmsPreferenceController mCallsAndAlarmsPreferenceController;
private AudioSharingPlaySoundPreferenceController mAudioSharingPlaySoundPreferenceController;
private AudioStreamsCategoryController mAudioStreamsCategoryController;
public AudioSharingDashboardFragment() {
super();
}
@Override
public int getMetricsCategory() {
return SettingsEnums.AUDIO_SHARING_SETTINGS;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
public int getHelpResource() {
return R.string.help_url_audio_sharing;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.bluetooth_audio_sharing;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mAudioSharingDeviceVolumeGroupController =
use(AudioSharingDeviceVolumeGroupController.class);
mAudioSharingDeviceVolumeGroupController.init(this);
mCallsAndAlarmsPreferenceController = use(CallsAndAlarmsPreferenceController.class);
mCallsAndAlarmsPreferenceController.init(this);
mAudioSharingPlaySoundPreferenceController =
use(AudioSharingPlaySoundPreferenceController.class);
mAudioStreamsCategoryController = use(AudioStreamsCategoryController.class);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Assume we are in a SettingsActivity. This is only safe because we currently use
// SettingsActivity as base for all preference fragments.
final SettingsActivity activity = (SettingsActivity) getActivity();
mMainSwitchBar = activity.getSwitchBar();
mMainSwitchBar.setTitle(getText(R.string.audio_sharing_switch_title));
mSwitchBarController = new AudioSharingSwitchBarController(activity, mMainSwitchBar, this);
mSwitchBarController.init(this);
getSettingsLifecycle().addObserver(mSwitchBarController);
mMainSwitchBar.show();
}
@Override
public void onSwitchBarChanged() {
updateVisibilityForAttachedPreferences();
}
private void updateVisibilityForAttachedPreferences() {
mAudioSharingDeviceVolumeGroupController.updateVisibility();
mCallsAndAlarmsPreferenceController.updateVisibility();
mAudioSharingPlaySoundPreferenceController.updateVisibility();
mAudioStreamsCategoryController.updateVisibility();
}
}

View File

@@ -1,86 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import java.util.ArrayList;
public class AudioSharingDeviceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = "AudioSharingDeviceAdapter";
private final ArrayList<AudioSharingDeviceItem> mDevices;
private final OnClickListener mOnClickListener;
private final String mPrefix;
public AudioSharingDeviceAdapter(
ArrayList<AudioSharingDeviceItem> devices, OnClickListener listener, String prefix) {
mDevices = devices;
mOnClickListener = listener;
mPrefix = prefix;
}
private class AudioSharingDeviceViewHolder extends RecyclerView.ViewHolder {
private final Button mButtonView;
AudioSharingDeviceViewHolder(View view) {
super(view);
mButtonView = view.findViewById(R.id.device_button);
}
public void bindView(int position) {
if (mButtonView != null) {
mButtonView.setText(mPrefix + mDevices.get(position).getName());
mButtonView.setOnClickListener(
v -> mOnClickListener.onClick(mDevices.get(position)));
} else {
Log.w(TAG, "bind view skipped due to button view is null");
}
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext())
.inflate(R.layout.audio_sharing_device_item, parent, false);
return new AudioSharingDeviceViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((AudioSharingDeviceViewHolder) holder).bindView(position);
}
@Override
public int getItemCount() {
return mDevices.size();
}
public interface OnClickListener {
/** Called when an item has been clicked. */
void onClick(AudioSharingDeviceItem item);
}
}

View File

@@ -1,75 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.os.Parcel;
import android.os.Parcelable;
public final class AudioSharingDeviceItem implements Parcelable {
private final String mName;
private final int mGroupId;
private final boolean mIsActive;
public AudioSharingDeviceItem(String name, int groupId, boolean isActive) {
mName = name;
mGroupId = groupId;
mIsActive = isActive;
}
public String getName() {
return mName;
}
public int getGroupId() {
return mGroupId;
}
public boolean isActive() {
return mIsActive;
}
public AudioSharingDeviceItem(Parcel in) {
mName = in.readString();
mGroupId = in.readInt();
mIsActive = in.readBoolean();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mName);
dest.writeInt(mGroupId);
dest.writeBoolean(mIsActive);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<AudioSharingDeviceItem> CREATOR =
new Creator<AudioSharingDeviceItem>() {
@Override
public AudioSharingDeviceItem createFromParcel(Parcel in) {
return new AudioSharingDeviceItem(in);
}
@Override
public AudioSharingDeviceItem[] newArray(int size) {
return new AudioSharingDeviceItem[size];
}
};
}

View File

@@ -1,677 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AudioSharingDevicePreferenceController extends BasePreferenceController
implements DefaultLifecycleObserver, DevicePreferenceCallback, BluetoothCallback {
private static final boolean DEBUG = BluetoothUtils.D;
private static final String TAG = "AudioSharingDevicePrefController";
private static final String KEY = "audio_sharing_device_list";
private static final String KEY_AUDIO_SHARING_SETTINGS =
"connected_device_audio_sharing_settings";
private final LocalBluetoothManager mLocalBtManager;
private final Executor mExecutor;
private CachedBluetoothDeviceManager mDeviceManager;
private LocalBluetoothLeBroadcast mBroadcast;
private LocalBluetoothLeBroadcastAssistant mAssistant;
private PreferenceGroup mPreferenceGroup;
private Preference mAudioSharingSettingsPreference;
private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
private DashboardFragment mFragment;
private List<BluetoothDevice> mTargetSinks = new ArrayList<>();
private final BluetoothLeBroadcast.Callback mBroadcastCallback =
new BluetoothLeBroadcast.Callback() {
@Override
public void onBroadcastStarted(int reason, int broadcastId) {
Log.d(
TAG,
"onBroadcastStarted(), reason = "
+ reason
+ ", broadcastId = "
+ broadcastId);
}
@Override
public void onBroadcastStartFailed(int reason) {
Log.d(TAG, "onBroadcastStartFailed(), reason = " + reason);
// TODO: handle broadcast start fail
}
@Override
public void onBroadcastMetadataChanged(
int broadcastId, @NonNull BluetoothLeBroadcastMetadata metadata) {
Log.d(
TAG,
"onBroadcastMetadataChanged(), broadcastId = "
+ broadcastId
+ ", metadata = "
+ metadata);
addSourceToTargetDevices(mTargetSinks);
mTargetSinks = new ArrayList<>();
}
@Override
public void onBroadcastStopped(int reason, int broadcastId) {
Log.d(
TAG,
"onBroadcastStopped(), reason = "
+ reason
+ ", broadcastId = "
+ broadcastId);
}
@Override
public void onBroadcastStopFailed(int reason) {
Log.d(TAG, "onBroadcastStopFailed(), reason = " + reason);
// TODO: handle broadcast stop fail
}
@Override
public void onBroadcastUpdated(int reason, int broadcastId) {}
@Override
public void onBroadcastUpdateFailed(int reason, int broadcastId) {}
@Override
public void onPlaybackStarted(int reason, int broadcastId) {}
@Override
public void onPlaybackStopped(int reason, int broadcastId) {}
};
private BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
new BluetoothLeBroadcastAssistant.Callback() {
@Override
public void onSearchStarted(int reason) {}
@Override
public void onSearchStartFailed(int reason) {}
@Override
public void onSearchStopped(int reason) {}
@Override
public void onSearchStopFailed(int reason) {}
@Override
public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {}
@Override
public void onSourceAdded(@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(
TAG,
"onSourceAdded(), sink = "
+ sink
+ ", sourceId = "
+ sourceId
+ ", reason = "
+ reason);
mBluetoothDeviceUpdater.forceUpdate();
if (mDeviceManager != null) {
CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(sink);
if (cachedDevice != null) {
closeOpeningDialogsForLeaDevice(cachedDevice);
}
}
}
@Override
public void onSourceAddFailed(
@NonNull BluetoothDevice sink,
@NonNull BluetoothLeBroadcastMetadata source,
int reason) {
Log.d(
TAG,
"onSourceAddFailed(), sink = "
+ sink
+ ", source = "
+ source
+ ", reason = "
+ reason);
AudioSharingUtils.toastMessage(
mContext,
String.format(
Locale.US,
"Fail to add source to %s reason %d",
sink.getAddress(),
reason));
}
@Override
public void onSourceModified(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceModifyFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceRemoved(
@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(
TAG,
"onSourceRemoved(), sink = "
+ sink
+ ", sourceId = "
+ sourceId
+ ", reason = "
+ reason);
mBluetoothDeviceUpdater.forceUpdate();
}
@Override
public void onSourceRemoveFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(
TAG,
"onSourceRemoveFailed(), sink = "
+ sink
+ ", sourceId = "
+ sourceId
+ ", reason = "
+ reason);
AudioSharingUtils.toastMessage(
mContext,
String.format(
Locale.US,
"Fail to remove source from %s reason %d",
sink.getAddress(),
reason));
}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink,
int sourceId,
BluetoothLeBroadcastReceiveState state) {}
};
public AudioSharingDevicePreferenceController(Context context) {
super(context, KEY);
mLocalBtManager = Utils.getLocalBtManager(mContext);
if (mLocalBtManager != null) {
mDeviceManager = mLocalBtManager.getCachedDeviceManager();
mBroadcast = mLocalBtManager.getProfileManager().getLeAudioBroadcastProfile();
mAssistant = mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
}
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mLocalBtManager == null) {
Log.d(TAG, "onStart() Bluetooth is not supported on this device");
return;
}
if (mBroadcast == null || mAssistant == null) {
Log.d(TAG, "onStart() Broadcast or assistant is not supported on this device");
return;
}
if (mBluetoothDeviceUpdater == null) {
Log.d(TAG, "onStart() Bluetooth device updater is not initialized");
return;
}
mLocalBtManager.getEventManager().registerCallback(this);
if (DEBUG) {
Log.d(TAG, "onStart() Register callbacks for broadcast and assistant.");
}
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
mBluetoothDeviceUpdater.registerCallback();
mBluetoothDeviceUpdater.refreshPreference();
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mLocalBtManager == null) {
Log.d(TAG, "onStop() Bluetooth is not supported on this device");
return;
}
if (mBroadcast == null || mAssistant == null) {
Log.d(TAG, "onStop() Broadcast or assistant is not supported on this device");
return;
}
if (mBluetoothDeviceUpdater == null) {
Log.d(TAG, "onStop() Bluetooth device updater is not initialized");
return;
}
mLocalBtManager.getEventManager().unregisterCallback(this);
if (DEBUG) {
Log.d(TAG, "onStop() Unregister callbacks for broadcast and assistant.");
}
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
mAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
mBluetoothDeviceUpdater.unregisterCallback();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreferenceGroup = screen.findPreference(KEY);
mAudioSharingSettingsPreference =
mPreferenceGroup.findPreference(KEY_AUDIO_SHARING_SETTINGS);
mPreferenceGroup.setVisible(false);
mAudioSharingSettingsPreference.setVisible(false);
if (isAvailable()) {
mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
mBluetoothDeviceUpdater.forceUpdate();
}
}
@Override
public int getAvailabilityStatus() {
return AudioSharingUtils.isFeatureEnabled() && mBluetoothDeviceUpdater != null
? AVAILABLE_UNSEARCHABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public void onDeviceAdded(Preference preference) {
if (mPreferenceGroup.getPreferenceCount() == 1) {
mPreferenceGroup.setVisible(true);
mAudioSharingSettingsPreference.setVisible(true);
}
mPreferenceGroup.addPreference(preference);
}
@Override
public void onDeviceRemoved(Preference preference) {
mPreferenceGroup.removePreference(preference);
if (mPreferenceGroup.getPreferenceCount() == 1) {
mPreferenceGroup.setVisible(false);
mAudioSharingSettingsPreference.setVisible(false);
}
}
@Override
public void onProfileConnectionStateChanged(
@NonNull CachedBluetoothDevice cachedDevice,
@ConnectionState int state,
int bluetoothProfile) {
if (state == BluetoothAdapter.STATE_DISCONNECTED) {
boolean isLeAudio = isLeAudioSupported(cachedDevice);
if (isLeAudio && bluetoothProfile == BluetoothProfile.LE_AUDIO) {
closeOpeningDialogsForLeaDevice(cachedDevice);
return;
}
if (!isLeAudio && !cachedDevice.isConnected()) {
closeOpeningDialogsForNonLeaDevice(cachedDevice);
return;
}
}
if (state != BluetoothAdapter.STATE_CONNECTED || !cachedDevice.getDevice().isConnected()) {
Log.d(TAG, "Ignore onProfileConnectionStateChanged, not connected state");
return;
}
if (mFragment == null) {
Log.d(TAG, "Ignore onProfileConnectionStateChanged, no host fragment");
return;
}
if (mAssistant == null && mBroadcast == null) {
Log.d(
TAG,
"Ignore onProfileConnectionStateChanged, no broadcast or assistant supported");
return;
}
var unused =
ThreadUtils.postOnBackgroundThread(
() -> handleOnProfileStateChanged(cachedDevice, bluetoothProfile));
}
/**
* Initialize the controller.
*
* @param fragment The fragment to provide the context and metrics category for {@link
* AudioSharingBluetoothDeviceUpdater} and provide the host for dialogs.
*/
public void init(DashboardFragment fragment) {
mFragment = fragment;
mBluetoothDeviceUpdater =
new AudioSharingBluetoothDeviceUpdater(
fragment.getContext(),
AudioSharingDevicePreferenceController.this,
fragment.getMetricsCategory());
}
private void handleOnProfileStateChanged(
@NonNull CachedBluetoothDevice cachedDevice, int bluetoothProfile) {
boolean isLeAudioSupported = isLeAudioSupported(cachedDevice);
// For eligible (LE audio) remote device, we only check its connected LE audio profile.
if (isLeAudioSupported && bluetoothProfile != BluetoothProfile.LE_AUDIO) {
Log.d(
TAG,
"Ignore onProfileConnectionStateChanged, not the le profile for le audio"
+ " device");
return;
}
boolean isFirstConnectedProfile = isFirstConnectedProfile(cachedDevice, bluetoothProfile);
// For ineligible (non LE audio) remote device, we only check its first connected profile.
if (!isLeAudioSupported && !isFirstConnectedProfile) {
Log.d(
TAG,
"Ignore onProfileConnectionStateChanged, not the first connected profile for"
+ " non le audio device");
return;
}
if (DEBUG) {
Log.d(
TAG,
"Start handling onProfileConnectionStateChanged for "
+ cachedDevice.getDevice().getAnonymizedAddress());
}
if (!isLeAudioSupported) {
// Handle connected ineligible (non LE audio) remote device
handleOnProfileStateChangedForNonLeAudioDevice(cachedDevice);
} else {
// Handle connected eligible (LE audio) remote device
handleOnProfileStateChangedForLeAudioDevice(cachedDevice);
}
}
private void handleOnProfileStateChangedForNonLeAudioDevice(
@NonNull CachedBluetoothDevice cachedDevice) {
if (isBroadcasting()) {
// Show stop audio sharing dialog when an ineligible (non LE audio) remote device
// connected during a sharing session.
postOnMainThread(
() -> {
closeOpeningDialogsOtherThan(AudioSharingStopDialogFragment.tag());
AudioSharingStopDialogFragment.show(
mFragment,
cachedDevice,
() -> mBroadcast.stopBroadcast(mBroadcast.getLatestBroadcastId()));
});
} else {
// Do nothing for ineligible (non LE audio) remote device when no sharing session.
if (DEBUG) {
Log.d(
TAG,
"Ignore onProfileConnectionStateChanged for non LE audio without"
+ " sharing session");
}
}
}
private void handleOnProfileStateChangedForLeAudioDevice(
@NonNull CachedBluetoothDevice cachedDevice) {
Map<Integer, List<CachedBluetoothDevice>> groupedDevices =
AudioSharingUtils.fetchConnectedDevicesByGroupId(mLocalBtManager);
if (isBroadcasting()) {
int groupId = AudioSharingUtils.getGroupId(cachedDevice);
if (groupedDevices.containsKey(groupId)
&& groupedDevices.get(groupId).stream()
.anyMatch(
device ->
AudioSharingUtils.hasBroadcastSource(
device, mLocalBtManager))) {
Log.d(
TAG,
"Automatically add another device within the same group to the sharing: "
+ cachedDevice.getDevice().getAnonymizedAddress());
addSourceToTargetDevices(ImmutableList.of(cachedDevice.getDevice()));
return;
}
// Show audio sharing switch or join dialog according to device count in the sharing
// session.
ArrayList<AudioSharingDeviceItem> deviceItemsInSharingSession =
AudioSharingUtils.buildOrderedConnectedLeadAudioSharingDeviceItem(
mLocalBtManager, groupedDevices, /* filterByInSharing= */ true);
// Show audio sharing switch dialog when the third eligible (LE audio) remote device
// connected during a sharing session.
if (deviceItemsInSharingSession.size() >= 2) {
postOnMainThread(
() -> {
closeOpeningDialogsOtherThan(
AudioSharingDisconnectDialogFragment.tag());
AudioSharingDisconnectDialogFragment.show(
mFragment,
deviceItemsInSharingSession,
cachedDevice,
(AudioSharingDeviceItem item) -> {
// Remove all sources from the device user clicked
if (groupedDevices.containsKey(item.getGroupId())) {
for (CachedBluetoothDevice device :
groupedDevices.get(item.getGroupId())) {
for (BluetoothLeBroadcastReceiveState source :
mAssistant.getAllSources(
device.getDevice())) {
mAssistant.removeSource(
device.getDevice(),
source.getSourceId());
}
}
}
// Add current broadcast to the latest connected device
mAssistant.addSource(
cachedDevice.getDevice(),
mBroadcast.getLatestBluetoothLeBroadcastMetadata(),
/* isGroupOp= */ true);
});
});
} else {
// Show audio sharing join dialog when the first or second eligible (LE audio)
// remote device connected during a sharing session.
postOnMainThread(
() -> {
closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment.show(
mFragment,
deviceItemsInSharingSession,
cachedDevice,
() -> {
// Add current broadcast to the latest connected device
mAssistant.addSource(
cachedDevice.getDevice(),
mBroadcast.getLatestBluetoothLeBroadcastMetadata(),
/* isGroupOp= */ true);
});
});
}
} else {
ArrayList<AudioSharingDeviceItem> deviceItems = new ArrayList<>();
for (List<CachedBluetoothDevice> devices : groupedDevices.values()) {
// Use random device in the group within the sharing session to represent the group.
CachedBluetoothDevice device = devices.get(0);
if (AudioSharingUtils.getGroupId(device)
== AudioSharingUtils.getGroupId(cachedDevice)) {
continue;
}
deviceItems.add(AudioSharingUtils.buildAudioSharingDeviceItem(device));
}
// Show audio sharing join dialog when the second eligible (LE audio) remote
// device connect and no sharing session.
if (deviceItems.size() == 1) {
postOnMainThread(
() -> {
closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment.show(
mFragment,
deviceItems,
cachedDevice,
() -> {
mTargetSinks = new ArrayList<>();
for (List<CachedBluetoothDevice> devices :
groupedDevices.values()) {
for (CachedBluetoothDevice device : devices) {
mTargetSinks.add(device.getDevice());
}
}
mBroadcast.startPrivateBroadcast();
});
});
}
}
}
private boolean isLeAudioSupported(CachedBluetoothDevice cachedDevice) {
return cachedDevice.getProfiles().stream()
.anyMatch(
profile ->
profile instanceof LeAudioProfile
&& profile.isEnabled(cachedDevice.getDevice()));
}
private boolean isFirstConnectedProfile(
CachedBluetoothDevice cachedDevice, int bluetoothProfile) {
return cachedDevice.getProfiles().stream()
.noneMatch(
profile ->
profile.getProfileId() != bluetoothProfile
&& profile.getConnectionStatus(cachedDevice.getDevice())
== BluetoothProfile.STATE_CONNECTED);
}
private boolean isBroadcasting() {
return mBroadcast != null && mBroadcast.isEnabled(null);
}
private void addSourceToTargetDevices(List<BluetoothDevice> sinks) {
if (sinks.isEmpty() || mBroadcast == null || mAssistant == null) {
Log.d(TAG, "Skip adding source to target.");
return;
}
BluetoothLeBroadcastMetadata broadcastMetadata =
mBroadcast.getLatestBluetoothLeBroadcastMetadata();
if (broadcastMetadata == null) {
Log.e(TAG, "Error: There is no broadcastMetadata.");
return;
}
for (BluetoothDevice sink : sinks) {
Log.d(
TAG,
"Add broadcast with broadcastId: "
+ broadcastMetadata.getBroadcastId()
+ "to the device: "
+ sink.getAnonymizedAddress());
mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
}
}
private void closeOpeningDialogsOtherThan(String tag) {
if (mFragment == null) return;
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) {
if (fragment instanceof DialogFragment && !fragment.getTag().equals(tag)) {
Log.d(TAG, "Remove staled opening dialog " + fragment.getTag());
((DialogFragment) fragment).dismiss();
}
}
}
private void closeOpeningDialogsForLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
if (mFragment == null) return;
int groupId = AudioSharingUtils.getGroupId(cachedDevice);
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null
&& groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID
&& AudioSharingUtils.getGroupId(device) == groupId) {
Log.d(TAG, "Remove staled opening dialog for group " + groupId);
((DialogFragment) fragment).dismiss();
}
}
}
private void closeOpeningDialogsForNonLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
if (mFragment == null) return;
String address = cachedDevice.getAddress();
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null && address != null && address.equals(device.getAddress())) {
Log.d(
TAG,
"Remove staled opening dialog for device "
+ cachedDevice.getDevice().getAnonymizedAddress());
((DialogFragment) fragment).dismiss();
}
}
}
@Nullable
private CachedBluetoothDevice getCachedBluetoothDeviceFromDialog(Fragment fragment) {
CachedBluetoothDevice device = null;
if (fragment instanceof AudioSharingJoinDialogFragment) {
device = ((AudioSharingJoinDialogFragment) fragment).getDevice();
} else if (fragment instanceof AudioSharingStopDialogFragment) {
device = ((AudioSharingStopDialogFragment) fragment).getDevice();
} else if (fragment instanceof AudioSharingDisconnectDialogFragment) {
device = ((AudioSharingDisconnectDialogFragment) fragment).getDevice();
}
return device;
}
private void postOnMainThread(@NonNull Runnable runnable) {
mContext.getMainExecutor().execute(runnable);
}
}

View File

@@ -1,142 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import android.widget.SeekBar;
import androidx.preference.Preference;
import com.android.settings.bluetooth.BluetoothDevicePreference;
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
public class AudioSharingDeviceVolumeControlUpdater extends BluetoothDeviceUpdater
implements Preference.OnPreferenceClickListener {
private static final String TAG = "AudioSharingDeviceVolumeControlUpdater";
private static final String PREF_KEY = "audio_sharing_volume_control";
private final LocalBluetoothManager mLocalBtManager;
public AudioSharingDeviceVolumeControlUpdater(
Context context,
DevicePreferenceCallback devicePreferenceCallback,
int metricsCategory) {
super(context, devicePreferenceCallback, metricsCategory);
mLocalBtManager = Utils.getLocalBluetoothManager(context);
}
@Override
public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
boolean isFilterMatched = false;
if (isDeviceConnected(cachedDevice) && isDeviceInCachedDevicesList(cachedDevice)) {
// If device is LE audio device and in a sharing session on current sharing device,
// it would show in volume control group.
if (cachedDevice.isConnectedLeAudioDevice()
&& AudioSharingUtils.isBroadcasting(mLocalBtManager)
&& AudioSharingUtils.hasBroadcastSource(cachedDevice, mLocalBtManager)) {
isFilterMatched = true;
}
}
Log.d(
TAG,
"isFilterMatched() device : "
+ cachedDevice.getName()
+ ", isFilterMatched : "
+ isFilterMatched);
return isFilterMatched;
}
@Override
public boolean onPreferenceClick(Preference preference) {
return true;
}
@Override
protected void addPreference(CachedBluetoothDevice cachedDevice) {
if (cachedDevice == null) return;
final BluetoothDevice device = cachedDevice.getDevice();
if (!mPreferenceMap.containsKey(device)) {
SeekBar.OnSeekBarChangeListener listener =
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(
SeekBar seekBar, int progress, boolean fromUser) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (mLocalBtManager != null
&& mLocalBtManager.getProfileManager().getVolumeControlProfile()
!= null) {
mLocalBtManager
.getProfileManager()
.getVolumeControlProfile()
.setDeviceVolume(
cachedDevice.getDevice(),
seekBar.getProgress(),
/* isGroupOp= */ true);
}
}
};
AudioSharingDeviceVolumePreference vPreference =
new AudioSharingDeviceVolumePreference(mPrefContext, cachedDevice);
vPreference.initialize();
vPreference.setOnSeekBarChangeListener(listener);
vPreference.setKey(getPreferenceKey());
vPreference.setIcon(com.android.settingslib.R.drawable.ic_bt_untethered_earbuds);
vPreference.setTitle(cachedDevice.getName());
mPreferenceMap.put(device, vPreference);
mDevicePreferenceCallback.onDeviceAdded(vPreference);
}
}
@Override
protected String getPreferenceKey() {
return PREF_KEY;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected void update(CachedBluetoothDevice cachedBluetoothDevice) {
super.update(cachedBluetoothDevice);
Log.d(TAG, "Map : " + mPreferenceMap);
}
@Override
protected void addPreference(
CachedBluetoothDevice cachedDevice, @BluetoothDevicePreference.SortType int type) {}
@Override
protected void launchDeviceDetails(Preference preference) {}
@Override
public void refreshPreference() {}
}

View File

@@ -1,345 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.annotation.IntRange;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothVolumeControl;
import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.VolumeControlProfile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AudioSharingDeviceVolumeGroupController extends AudioSharingBasePreferenceController
implements DevicePreferenceCallback {
private static final String TAG = "AudioSharingDeviceVolumeGroupController";
private static final String KEY = "audio_sharing_device_volume_group";
private final LocalBluetoothManager mLocalBtManager;
private final LocalBluetoothLeBroadcastAssistant mAssistant;
private final Executor mExecutor;
private VolumeControlProfile mVolumeControl;
private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
private FragmentManager mFragmentManager;
private PreferenceGroup mPreferenceGroup;
private List<AudioSharingDeviceVolumePreference> mVolumePreferences = new ArrayList<>();
private Map<Integer, Integer> mValueMap = new HashMap<Integer, Integer>();
private BluetoothVolumeControl.Callback mVolumeControlCallback =
new BluetoothVolumeControl.Callback() {
@Override
public void onVolumeOffsetChanged(
@NonNull BluetoothDevice device, int volumeOffset) {}
@Override
public void onDeviceVolumeChanged(
@NonNull BluetoothDevice device,
@IntRange(from = -255, to = 255) int volume) {
CachedBluetoothDevice cachedDevice =
mLocalBtManager.getCachedDeviceManager().findDevice(device);
if (cachedDevice == null) return;
int groupId = AudioSharingUtils.getGroupId(cachedDevice);
mValueMap.put(groupId, volume);
for (AudioSharingDeviceVolumePreference preference : mVolumePreferences) {
if (preference.getCachedDevice() != null
&& AudioSharingUtils.getGroupId(preference.getCachedDevice())
== groupId) {
// If the callback return invalid volume, try to
// get the volume from AudioManager.STREAM_MUSIC
int finalVolume = getAudioVolumeIfNeeded(volume);
Log.d(
TAG,
"onDeviceVolumeChanged: set volume to "
+ finalVolume
+ " for "
+ device.getAnonymizedAddress());
mContext.getMainExecutor()
.execute(() -> preference.setProgress(finalVolume));
break;
}
}
}
};
private BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
new BluetoothLeBroadcastAssistant.Callback() {
@Override
public void onSearchStarted(int reason) {}
@Override
public void onSearchStartFailed(int reason) {}
@Override
public void onSearchStopped(int reason) {}
@Override
public void onSearchStopFailed(int reason) {}
@Override
public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {}
@Override
public void onSourceAdded(@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(
TAG,
"onSourceAdded(), sink = "
+ sink
+ ", sourceId = "
+ sourceId
+ ", reason = "
+ reason);
mBluetoothDeviceUpdater.forceUpdate();
}
@Override
public void onSourceAddFailed(
@NonNull BluetoothDevice sink,
@NonNull BluetoothLeBroadcastMetadata source,
int reason) {
Log.d(
TAG,
"onSourceAddFailed(), sink = "
+ sink
+ ", source = "
+ source
+ ", reason = "
+ reason);
}
@Override
public void onSourceModified(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceModifyFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceRemoved(
@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(
TAG,
"onSourceRemoved(), sink = "
+ sink
+ ", sourceId = "
+ sourceId
+ ", reason = "
+ reason);
mBluetoothDeviceUpdater.forceUpdate();
}
@Override
public void onSourceRemoveFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(
TAG,
"onSourceRemoveFailed(), sink = "
+ sink
+ ", sourceId = "
+ sourceId
+ ", reason = "
+ reason);
}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink,
int sourceId,
BluetoothLeBroadcastReceiveState state) {}
};
public AudioSharingDeviceVolumeGroupController(Context context) {
super(context, KEY);
mLocalBtManager = Utils.getLocalBtManager(mContext);
mAssistant = mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
mExecutor = Executors.newSingleThreadExecutor();
if (mLocalBtManager != null) {
mVolumeControl = mLocalBtManager.getProfileManager().getVolumeControlProfile();
}
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
super.onStart(owner);
if (mAssistant == null) {
Log.d(TAG, "onStart() Broadcast or assistant is not supported on this device");
return;
}
if (mBluetoothDeviceUpdater == null) {
Log.d(TAG, "onStart() Bluetooth device updater is not initialized");
return;
}
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
mBluetoothDeviceUpdater.registerCallback();
if (mVolumeControl != null) {
Log.d(TAG, "onStart() Registered volume control callback");
mVolumeControl.registerCallback(mExecutor, mVolumeControlCallback);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
super.onStop(owner);
if (mAssistant == null) {
Log.d(TAG, "onStop() Broadcast or assistant is not supported on this device");
return;
}
if (mBluetoothDeviceUpdater == null) {
Log.d(TAG, "onStop() Bluetooth device updater is not initialized");
return;
}
mAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
mBluetoothDeviceUpdater.unregisterCallback();
if (mVolumeControl != null) {
Log.d(TAG, "onStop() Unregistered volume control callback");
mVolumeControl.unregisterCallback(mVolumeControlCallback);
mValueMap.clear();
}
}
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
mVolumePreferences.clear();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreferenceGroup = screen.findPreference(KEY);
mPreferenceGroup.setVisible(false);
if (isAvailable() && mBluetoothDeviceUpdater != null) {
mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
mBluetoothDeviceUpdater.forceUpdate();
}
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public void onDeviceAdded(Preference preference) {
if (mPreferenceGroup.getPreferenceCount() == 0) {
mPreferenceGroup.setVisible(true);
}
mPreferenceGroup.addPreference(preference);
if (preference instanceof AudioSharingDeviceVolumePreference) {
var volumePref = (AudioSharingDeviceVolumePreference) preference;
mVolumePreferences.add(volumePref);
if (volumePref.getProgress() > 0) return;
CachedBluetoothDevice device = volumePref.getCachedDevice();
if (device == null) return;
int volume = mValueMap.getOrDefault(AudioSharingUtils.getGroupId(device), -1);
// If the volume is invalid, try to get the volume from AudioManager.STREAM_MUSIC
int finalVolume = getAudioVolumeIfNeeded(volume);
Log.d(
TAG,
"onDeviceAdded: set volume to "
+ finalVolume
+ " for "
+ device.getDevice().getAnonymizedAddress());
mContext.getMainExecutor().execute(() -> volumePref.setProgress(finalVolume));
}
}
@Override
public void onDeviceRemoved(Preference preference) {
mPreferenceGroup.removePreference(preference);
if (mPreferenceGroup.getPreferenceCount() == 0) {
mPreferenceGroup.setVisible(false);
}
if (preference instanceof AudioSharingDeviceVolumePreference) {
var volumePref = (AudioSharingDeviceVolumePreference) preference;
if (mVolumePreferences.contains(volumePref)) {
mVolumePreferences.remove(volumePref);
}
CachedBluetoothDevice device = volumePref.getCachedDevice();
Log.d(
TAG,
"onDeviceRemoved: "
+ (device == null
? "null"
: device.getDevice().getAnonymizedAddress()));
}
}
@Override
public void updateVisibility() {
if (mPreferenceGroup != null) {
mPreferenceGroup.setVisible(false);
if (mPreferenceGroup.getPreferenceCount() > 0) {
super.updateVisibility();
}
}
}
/**
* Initialize the controller.
*
* @param fragment The fragment to provide the context and metrics category for {@link
* AudioSharingBluetoothDeviceUpdater} and provide the host for dialogs.
*/
public void init(DashboardFragment fragment) {
mBluetoothDeviceUpdater =
new AudioSharingDeviceVolumeControlUpdater(
fragment.getContext(),
AudioSharingDeviceVolumeGroupController.this,
fragment.getMetricsCategory());
}
private int getAudioVolumeIfNeeded(int volume) {
if (volume >= 0) return volume;
try {
AudioManager audioManager = mContext.getSystemService(AudioManager.class);
int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int min = audioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC);
return Math.round(
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) * 255f / (max - min));
} catch (RuntimeException e) {
Log.e(TAG, "Fail to fetch current music stream volume, error = " + e);
return volume;
}
}
}

View File

@@ -1,56 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.content.Context;
import android.widget.SeekBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.settings.R;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
public class AudioSharingDeviceVolumePreference extends SeekBarPreference {
public static final int MIN_VOLUME = 0;
public static final int MAX_VOLUME = 255;
protected SeekBar mSeekBar;
private final CachedBluetoothDevice mCachedDevice;
public AudioSharingDeviceVolumePreference(
Context context, @NonNull CachedBluetoothDevice device) {
super(context);
setLayoutResource(R.layout.preference_volume_slider);
mCachedDevice = device;
}
@Nullable
public CachedBluetoothDevice getCachedDevice() {
return mCachedDevice;
}
/**
* Initialize {@link AudioSharingDeviceVolumePreference}.
* Need to be called after creating the preference.
*/
public void initialize() {
setMax(MAX_VOLUME);
setMin(MIN_VOLUME);
}
}

View File

@@ -1,154 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class AudioSharingDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "AudioSharingDialog";
private static final String BUNDLE_KEY_DEVICE_ITEMS = "bundle_key_device_items";
// The host creates an instance of this dialog fragment must implement this interface to receive
// event callbacks.
public interface DialogEventListener {
/**
* Called when users click the device item for sharing in the dialog.
*
* @param item The device item clicked.
*/
void onItemClick(AudioSharingDeviceItem item);
}
private static DialogEventListener sListener;
@Override
public int getMetricsCategory() {
return SettingsEnums.DIALOG_START_AUDIO_SHARING;
}
/**
* Display the {@link AudioSharingDialogFragment} dialog.
*
* @param host The Fragment this dialog will be hosted.
* @param deviceItems The connected device items eligible for audio sharing.
* @param listener The callback to handle the user action on this dialog.
*/
public static void show(
Fragment host,
ArrayList<AudioSharingDeviceItem> deviceItems,
DialogEventListener listener) {
if (!AudioSharingUtils.isFeatureEnabled()) return;
final FragmentManager manager = host.getChildFragmentManager();
sListener = listener;
if (manager.findFragmentByTag(TAG) == null) {
final Bundle bundle = new Bundle();
bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems);
AudioSharingDialogFragment dialog = new AudioSharingDialogFragment();
dialog.setArguments(bundle);
dialog.show(manager, TAG);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle arguments = requireArguments();
ArrayList<AudioSharingDeviceItem> deviceItems =
arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS);
final AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity()).setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(builder.getContext());
View customTitle = inflater.inflate(R.layout.dialog_custom_title_audio_sharing, null);
ImageView icon = customTitle.findViewById(R.id.title_icon);
icon.setImageResource(R.drawable.ic_bt_audio_sharing);
TextView title = customTitle.findViewById(R.id.title_text);
View rootView = inflater.inflate(R.layout.dialog_audio_sharing, /* parent= */ null);
TextView subTitle1 = rootView.findViewById(R.id.share_audio_subtitle1);
TextView subTitle2 = rootView.findViewById(R.id.share_audio_subtitle2);
RecyclerView recyclerView = rootView.findViewById(R.id.btn_list);
Button shareBtn = rootView.findViewById(R.id.share_btn);
Button cancelBtn = rootView.findViewById(R.id.cancel_btn);
if (deviceItems.isEmpty()) {
title.setText("Share your audio");
subTitle2.setText(
"To start sharing audio, "
+ "connect two pairs of headphones that support LE Audio");
ImageView image = rootView.findViewById(R.id.share_audio_guidance);
image.setVisibility(View.VISIBLE);
builder.setNegativeButton("Close", null);
} else if (deviceItems.size() == 1) {
title.setText("Share your audio");
subTitle1.setText(
deviceItems.stream()
.map(AudioSharingDeviceItem::getName)
.collect(Collectors.joining(" and ")));
subTitle2.setText(
"This device's music and videos will play on both pairs of headphones");
shareBtn.setText("Share audio");
shareBtn.setOnClickListener(
v -> {
sListener.onItemClick(Iterables.getOnlyElement(deviceItems));
dismiss();
});
cancelBtn.setOnClickListener(v -> dismiss());
subTitle1.setVisibility(View.VISIBLE);
shareBtn.setVisibility(View.VISIBLE);
cancelBtn.setVisibility(View.VISIBLE);
} else {
title.setText("Share audio with another device");
subTitle2.setText(
"This device's music and videos will play on the headphones you connect");
recyclerView.setAdapter(
new AudioSharingDeviceAdapter(
deviceItems,
(AudioSharingDeviceItem item) -> {
sListener.onItemClick(item);
dismiss();
},
"Connect "));
recyclerView.setLayoutManager(
new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
recyclerView.setVisibility(View.VISIBLE);
cancelBtn.setOnClickListener(v -> dismiss());
cancelBtn.setVisibility(View.VISIBLE);
}
AlertDialog dialog = builder.setCustomTitle(customTitle).setView(rootView).create();
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
}

View File

@@ -1,173 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import java.util.ArrayList;
import java.util.Locale;
public class AudioSharingDisconnectDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "AudioSharingDisconnectDialog";
private static final String BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS =
"bundle_key_device_to_disconnect_items";
private static final String BUNDLE_KEY_NEW_DEVICE_NAME = "bundle_key_new_device_name";
// The host creates an instance of this dialog fragment must implement this interface to receive
// event callbacks.
public interface DialogEventListener {
/**
* Called when users click the device item to disconnect from the audio sharing in the
* dialog.
*
* @param item The device item clicked.
*/
void onItemClick(AudioSharingDeviceItem item);
}
private static DialogEventListener sListener;
@Nullable private static CachedBluetoothDevice sNewDevice;
@Override
public int getMetricsCategory() {
return SettingsEnums.DIALOG_AUDIO_SHARING_SWITCH_DEVICE;
}
/**
* Display the {@link AudioSharingDisconnectDialogFragment} dialog.
*
* <p>If the dialog is showing for the same group, update the dialog event listener.
*
* @param host The Fragment this dialog will be hosted.
* @param deviceItems The existing connected device items in audio sharing session.
* @param newDevice The latest connected device triggered this dialog.
* @param listener The callback to handle the user action on this dialog.
*/
public static void show(
Fragment host,
ArrayList<AudioSharingDeviceItem> deviceItems,
CachedBluetoothDevice newDevice,
DialogEventListener listener) {
if (!AudioSharingUtils.isFeatureEnabled()) return;
final FragmentManager manager = host.getChildFragmentManager();
Fragment dialog = manager.findFragmentByTag(TAG);
if (dialog != null
&& ((DialogFragment) dialog).getDialog() != null
&& ((DialogFragment) dialog).getDialog().isShowing()) {
int newGroupId = AudioSharingUtils.getGroupId(newDevice);
if (sNewDevice != null && newGroupId == AudioSharingUtils.getGroupId(sNewDevice)) {
Log.d(
TAG,
String.format(
Locale.US,
"Dialog is showing for the same device group %d, "
+ "update the content.",
newGroupId));
sListener = listener;
sNewDevice = newDevice;
return;
} else {
Log.d(
TAG,
String.format(
Locale.US,
"Dialog is showing for new device group %d, "
+ "dismiss current dialog.",
newGroupId));
((DialogFragment) dialog).dismiss();
}
}
sListener = listener;
sNewDevice = newDevice;
Log.d(TAG, "Show up the dialog.");
final Bundle bundle = new Bundle();
bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS, deviceItems);
bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDevice.getName());
AudioSharingDisconnectDialogFragment dialogFrag =
new AudioSharingDisconnectDialogFragment();
dialogFrag.setArguments(bundle);
dialogFrag.show(manager, TAG);
}
/** Return the tag of {@link AudioSharingDisconnectDialogFragment} dialog. */
public static @NonNull String tag() {
return TAG;
}
/** Get the latest connected device which triggers the dialog. */
public @Nullable CachedBluetoothDevice getDevice() {
return sNewDevice;
}
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle arguments = requireArguments();
ArrayList<AudioSharingDeviceItem> deviceItems =
arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS);
final AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity()).setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(builder.getContext());
// Set custom title for the dialog.
View customTitle = inflater.inflate(R.layout.dialog_custom_title_audio_sharing, null);
ImageView icon = customTitle.findViewById(R.id.title_icon);
icon.setImageResource(R.drawable.ic_bt_audio_sharing);
TextView title = customTitle.findViewById(R.id.title_text);
title.setText("Choose a device to disconnect");
View rootView =
inflater.inflate(R.layout.dialog_audio_sharing_disconnect, /* parent= */ null);
TextView subTitle = rootView.findViewById(R.id.share_audio_disconnect_description);
subTitle.setText("Only 2 devices can share audio at a time");
RecyclerView recyclerView = rootView.findViewById(R.id.device_btn_list);
recyclerView.setAdapter(
new AudioSharingDeviceAdapter(
deviceItems,
(AudioSharingDeviceItem item) -> {
sListener.onItemClick(item);
dismiss();
},
"Disconnect "));
recyclerView.setLayoutManager(
new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
Button cancelBtn = rootView.findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener(v -> dismiss());
AlertDialog dialog = builder.setCustomTitle(customTitle).setView(rootView).create();
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
}

View File

@@ -1,175 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import java.util.ArrayList;
import java.util.Locale;
import java.util.stream.Collectors;
public class AudioSharingJoinDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "AudioSharingJoinDialog";
private static final String BUNDLE_KEY_DEVICE_ITEMS = "bundle_key_device_items";
private static final String BUNDLE_KEY_NEW_DEVICE_NAME = "bundle_key_new_device_name";
// The host creates an instance of this dialog fragment must implement this interface to receive
// event callbacks.
public interface DialogEventListener {
/** Called when users click the share audio button in the dialog. */
void onShareClick();
}
private static DialogEventListener sListener;
private static @Nullable CachedBluetoothDevice sNewDevice;
@Override
public int getMetricsCategory() {
return SettingsEnums.DIALOG_START_AUDIO_SHARING;
}
/**
* Display the {@link AudioSharingJoinDialogFragment} dialog.
*
* <p>If the dialog is showing, update the dialog message and event listener.
*
* @param host The Fragment this dialog will be hosted.
* @param deviceItems The existing connected device items eligible for audio sharing.
* @param newDevice The latest connected device triggered this dialog.
* @param listener The callback to handle the user action on this dialog.
*/
public static void show(
Fragment host,
ArrayList<AudioSharingDeviceItem> deviceItems,
CachedBluetoothDevice newDevice,
DialogEventListener listener) {
if (!AudioSharingUtils.isFeatureEnabled()) return;
final FragmentManager manager = host.getChildFragmentManager();
sListener = listener;
sNewDevice = newDevice;
Fragment dialog = manager.findFragmentByTag(TAG);
if (dialog != null
&& ((DialogFragment) dialog).getDialog() != null
&& ((DialogFragment) dialog).getDialog().isShowing()) {
Log.d(TAG, "Dialog is showing, update the content.");
updateDialog(
deviceItems,
newDevice.getName(),
(AlertDialog) ((DialogFragment) dialog).getDialog());
} else {
Log.d(TAG, "Show up the dialog.");
final Bundle bundle = new Bundle();
bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems);
bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDevice.getName());
final AudioSharingJoinDialogFragment dialogFrag = new AudioSharingJoinDialogFragment();
dialogFrag.setArguments(bundle);
dialogFrag.show(manager, TAG);
}
}
/** Return the tag of {@link AudioSharingJoinDialogFragment} dialog. */
public static @NonNull String tag() {
return TAG;
}
/** Get the latest connected device which triggers the dialog. */
public @Nullable CachedBluetoothDevice getDevice() {
return sNewDevice;
}
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle arguments = requireArguments();
ArrayList<AudioSharingDeviceItem> deviceItems =
arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS);
String newDeviceName = arguments.getString(BUNDLE_KEY_NEW_DEVICE_NAME);
final AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity()).setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(builder.getContext());
// Set custom title for the dialog.
View customTitle =
inflater.inflate(R.layout.dialog_custom_title_audio_sharing, /* parent= */ null);
ImageView icon = customTitle.findViewById(R.id.title_icon);
icon.setImageResource(R.drawable.ic_bt_audio_sharing);
TextView title = customTitle.findViewById(R.id.title_text);
title.setText("Share your audio");
View rootView = inflater.inflate(R.layout.dialog_audio_sharing_join, /* parent= */ null);
TextView subtitle = rootView.findViewById(R.id.share_audio_subtitle);
subtitle.setText("This device's music and videos will play on both pairs of headphones");
Button shareBtn = rootView.findViewById(R.id.share_btn);
Button cancelBtn = rootView.findViewById(R.id.cancel_btn);
shareBtn.setOnClickListener(
v -> {
sListener.onShareClick();
dismiss();
});
shareBtn.setText("Share audio");
cancelBtn.setOnClickListener(v -> dismiss());
AlertDialog dialog = builder.setCustomTitle(customTitle).setView(rootView).create();
dialog.setCanceledOnTouchOutside(false);
updateDialog(deviceItems, newDeviceName, dialog);
dialog.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
if (messageView != null) {
Typeface typeface = Typeface.create(Typeface.DEFAULT_FAMILY, Typeface.NORMAL);
messageView.setTypeface(typeface);
messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
messageView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
Log.w(TAG, "Fail to update message style: message view is null");
}
return dialog;
}
private static void updateDialog(
ArrayList<AudioSharingDeviceItem> deviceItems,
String newDeviceName,
@NonNull AlertDialog dialog) {
if (deviceItems.isEmpty()) {
dialog.setMessage(newDeviceName);
} else {
dialog.setMessage(
String.format(
Locale.US,
"%s and %s",
deviceItems.stream()
.map(AudioSharingDeviceItem::getName)
.collect(Collectors.joining(", ")),
newDeviceName));
}
}
}

View File

@@ -1,110 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsQrCodeFragment;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.widget.ValidatedEditTextPreference;
public class AudioSharingNamePreference extends ValidatedEditTextPreference {
private static final String TAG = "AudioSharingNamePreference";
private boolean mShowQrCodeIcon = false;
public AudioSharingNamePreference(
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize();
}
public AudioSharingNamePreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
public AudioSharingNamePreference(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public AudioSharingNamePreference(Context context) {
super(context);
initialize();
}
private void initialize() {
setLayoutResource(
com.android.settingslib.widget.preference.twotarget.R.layout.preference_two_target);
setWidgetLayoutResource(R.layout.preference_widget_qrcode);
}
void setShowQrCodeIcon(boolean show) {
mShowQrCodeIcon = show;
notifyChanged();
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
ImageButton shareButton = (ImageButton) holder.findViewById(R.id.button_icon);
View divider =
holder.findViewById(
com.android.settingslib.widget.preference.twotarget.R.id
.two_target_divider);
if (shareButton != null && divider != null) {
if (mShowQrCodeIcon) {
configureVisibleStateForQrCodeIcon(shareButton, divider);
} else {
configureInvisibleStateForQrCodeIcon(shareButton, divider);
}
} else {
Log.w(TAG, "onBindViewHolder() : shareButton or divider is null!");
}
}
private void configureVisibleStateForQrCodeIcon(ImageButton shareButton, View divider) {
divider.setVisibility(View.VISIBLE);
shareButton.setVisibility(View.VISIBLE);
shareButton.setImageDrawable(getContext().getDrawable(R.drawable.ic_qrcode_24dp));
shareButton.setOnClickListener(unused -> launchAudioSharingQrCodeFragment());
}
private void configureInvisibleStateForQrCodeIcon(ImageButton shareButton, View divider) {
divider.setVisibility(View.INVISIBLE);
shareButton.setVisibility(View.INVISIBLE);
shareButton.setOnClickListener(null);
}
private void launchAudioSharingQrCodeFragment() {
new SubSettingLauncher(getContext())
.setTitleText("Audio sharing QR code")
.setDestination(AudioStreamsQrCodeFragment.class.getName())
.setSourceMetricsCategory(SettingsEnums.AUDIO_SHARING_SETTINGS)
.launch();
}
}

View File

@@ -1,223 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.android.settings.connecteddevice.audiosharing.AudioSharingUtils.isBroadcasting;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.ValidatedEditTextPreference;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AudioSharingNamePreferenceController extends BasePreferenceController
implements ValidatedEditTextPreference.Validator,
Preference.OnPreferenceChangeListener,
DefaultLifecycleObserver {
private static final String TAG = "AudioSharingNamePreferenceController";
private static final boolean DEBUG = BluetoothUtils.D;
private static final String PREF_KEY = "audio_sharing_stream_name";
private final BluetoothLeBroadcast.Callback mBroadcastCallback =
new BluetoothLeBroadcast.Callback() {
@Override
public void onBroadcastMetadataChanged(
int broadcastId, BluetoothLeBroadcastMetadata metadata) {
if (DEBUG) {
Log.d(
TAG,
"onBroadcastMetadataChanged() broadcastId : "
+ broadcastId
+ " metadata: "
+ metadata);
}
updateQrCodeIcon(true);
}
@Override
public void onBroadcastStartFailed(int reason) {}
@Override
public void onBroadcastStarted(int reason, int broadcastId) {}
@Override
public void onBroadcastStopFailed(int reason) {}
@Override
public void onBroadcastStopped(int reason, int broadcastId) {
if (DEBUG) {
Log.d(
TAG,
"onBroadcastStopped() reason : "
+ reason
+ " broadcastId: "
+ broadcastId);
}
updateQrCodeIcon(false);
}
@Override
public void onBroadcastUpdateFailed(int reason, int broadcastId) {
Log.w(TAG, "onBroadcastUpdateFailed() reason : " + reason);
// Do nothing if update failed.
}
@Override
public void onBroadcastUpdated(int reason, int broadcastId) {
if (DEBUG) {
Log.d(TAG, "onBroadcastUpdated() reason : " + reason);
}
updateBroadcastName();
}
@Override
public void onPlaybackStarted(int reason, int broadcastId) {}
@Override
public void onPlaybackStopped(int reason, int broadcastId) {}
};
@Nullable private final LocalBluetoothManager mLocalBtManager;
@Nullable private final LocalBluetoothLeBroadcast mBroadcast;
private final Executor mExecutor;
private final AudioSharingNameTextValidator mAudioSharingNameTextValidator;
@Nullable private AudioSharingNamePreference mPreference;
public AudioSharingNamePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mLocalBtManager = Utils.getLocalBluetoothManager(context);
mBroadcast =
(mLocalBtManager != null)
? mLocalBtManager.getProfileManager().getLeAudioBroadcastProfile()
: null;
mAudioSharingNameTextValidator = new AudioSharingNameTextValidator();
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mBroadcast != null) {
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mBroadcast != null) {
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
}
}
@Override
public int getAvailabilityStatus() {
return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
if (mPreference != null) {
mPreference.setValidator(this);
updateBroadcastName();
updateQrCodeIcon(isBroadcasting(mLocalBtManager));
}
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mPreference != null
&& mPreference.getSummary() != null
&& ((String) newValue).contentEquals(mPreference.getSummary())) {
return false;
}
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
if (mBroadcast != null) {
mBroadcast.setProgramInfo((String) newValue);
if (isBroadcasting(mLocalBtManager)) {
// Update broadcast, UI update will be handled after callback
mBroadcast.updateBroadcast();
} else {
// Directly update UI if no ongoing broadcast
updateBroadcastName();
}
}
});
return true;
}
private void updateBroadcastName() {
if (mPreference != null) {
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
if (mBroadcast != null) {
String name = mBroadcast.getProgramInfo();
ThreadUtils.postOnMainThread(
() -> {
if (mPreference != null) {
mPreference.setText(name);
mPreference.setSummary(name);
}
});
}
});
}
}
private void updateQrCodeIcon(boolean show) {
if (mPreference != null) {
ThreadUtils.postOnMainThread(
() -> {
if (mPreference != null) {
mPreference.setShowQrCodeIcon(show);
}
});
}
}
@Override
public boolean isTextValid(String value) {
return mAudioSharingNameTextValidator.isTextValid(value);
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import com.android.settings.widget.ValidatedEditTextPreference;
import java.nio.charset.StandardCharsets;
/**
* Validator for Audio Sharing Name, which should be a UTF-8 encoded string containing a minimum of
* 4 characters and a maximum of 32 human-readable characters.
*/
public class AudioSharingNameTextValidator implements ValidatedEditTextPreference.Validator {
private static final int MIN_LENGTH = 4;
private static final int MAX_LENGTH = 32;
@Override
public boolean isTextValid(String value) {
if (value == null || value.length() < MIN_LENGTH || value.length() > MAX_LENGTH) {
return false;
}
return isValidUTF8(value);
}
private static boolean isValidUTF8(String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
String reconstructedString = new String(bytes, StandardCharsets.UTF_8);
return value.equals(reconstructedString);
}
}

View File

@@ -1,130 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.widget.ValidatedEditTextPreference;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AudioSharingPasswordPreferenceController extends BasePreferenceController
implements ValidatedEditTextPreference.Validator,
Preference.OnPreferenceChangeListener,
DefaultLifecycleObserver {
private static final String PREF_KEY = "audio_sharing_stream_password";
private final BluetoothLeBroadcast.Callback mBroadcastCallback =
new BluetoothLeBroadcast.Callback() {
@Override
public void onBroadcastMetadataChanged(
int broadcastId, BluetoothLeBroadcastMetadata metadata) {}
@Override
public void onBroadcastStartFailed(int reason) {}
@Override
public void onBroadcastStarted(int reason, int broadcastId) {}
@Override
public void onBroadcastStopFailed(int reason) {}
@Override
public void onBroadcastStopped(int reason, int broadcastId) {}
@Override
public void onBroadcastUpdateFailed(int reason, int broadcastId) {}
@Override
public void onBroadcastUpdated(int reason, int broadcastId) {}
@Override
public void onPlaybackStarted(int reason, int broadcastId) {}
@Override
public void onPlaybackStopped(int reason, int broadcastId) {}
};
@Nullable private final LocalBluetoothLeBroadcast mBroadcast;
private final Executor mExecutor;
private final AudioSharingPasswordValidator mAudioSharingPasswordValidator;
@Nullable private ValidatedEditTextPreference mPreference;
public AudioSharingPasswordPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mBroadcast =
Utils.getLocalBtManager(context).getProfileManager().getLeAudioBroadcastProfile();
mAudioSharingPasswordValidator = new AudioSharingPasswordValidator();
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mBroadcast != null) {
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mBroadcast != null) {
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
}
}
@Override
public int getAvailabilityStatus() {
return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
if (mPreference != null) {
mPreference.setValidator(this);
}
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO(chelseahao): implement
return true;
}
@Override
public boolean isTextValid(String value) {
return mAudioSharingPasswordValidator.isTextValid(value);
}
}

View File

@@ -1,51 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import com.android.settings.widget.ValidatedEditTextPreference;
import java.nio.charset.StandardCharsets;
/**
* Validator for Audio Sharing Password, which should be a UTF-8 string that has at least 4 octets
* and should not exceed 16 octets.
*/
public class AudioSharingPasswordValidator implements ValidatedEditTextPreference.Validator {
private static final int MIN_OCTETS = 4;
private static final int MAX_OCTETS = 16;
@Override
public boolean isTextValid(String value) {
if (value == null
|| getOctetsCount(value) < MIN_OCTETS
|| getOctetsCount(value) > MAX_OCTETS) {
return false;
}
return isValidUTF8(value);
}
private static int getOctetsCount(String value) {
return value.getBytes(StandardCharsets.UTF_8).length;
}
private static boolean isValidUTF8(String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
String reconstructedString = new String(bytes, StandardCharsets.UTF_8);
return value.equals(reconstructedString);
}
}

View File

@@ -1,98 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
public class AudioSharingPlaySoundPreferenceController
extends AudioSharingBasePreferenceController {
private static final String TAG = "AudioSharingPlaySoundPreferenceController";
private static final String PREF_KEY = "audio_sharing_play_sound";
private final Ringtone mRingtone;
public AudioSharingPlaySoundPreferenceController(Context context) {
super(context, PREF_KEY);
mRingtone = RingtoneManager.getRingtone(context, getMediaVolumeUri());
if (mRingtone != null) {
mRingtone.setStreamType(AudioManager.STREAM_MUSIC);
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference.setVisible(mRingtone != null);
mPreference.setOnPreferenceClickListener(
(v) -> {
if (mRingtone == null) {
Log.d(TAG, "Skip onClick due to ringtone is null");
return true;
}
try {
mRingtone.setAudioAttributes(
new AudioAttributes.Builder(mRingtone.getAudioAttributes())
.setFlags(AudioAttributes.FLAG_BYPASS_MUTE)
.addTag("VX_AOSP_SAMPLESOUND")
.build());
if (!mRingtone.isPlaying()) {
mRingtone.play();
}
} catch (Throwable e) {
Log.w(TAG, "Fail to play sample, error = " + e);
}
return true;
});
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
super.onStop(owner);
if (mRingtone != null && mRingtone.isPlaying()) {
mRingtone.stop();
}
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
}
private Uri getMediaVolumeUri() {
return Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://"
+ mContext.getPackageName()
+ "/"
+ R.raw.media_volume);
}
}

View File

@@ -1,138 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AudioSharingPreferenceController extends BasePreferenceController
implements DefaultLifecycleObserver, BluetoothCallback {
private static final String TAG = "AudioSharingPreferenceController";
private final LocalBluetoothManager mLocalBtManager;
private final Executor mExecutor;
@Nullable private LocalBluetoothLeBroadcast mBroadcast = null;
@Nullable private Preference mPreference;
private final BluetoothLeBroadcast.Callback mBroadcastCallback =
new BluetoothLeBroadcast.Callback() {
@Override
public void onBroadcastStarted(int reason, int broadcastId) {
if (mPreference != null) {
refreshSummary(mPreference);
}
}
@Override
public void onBroadcastStartFailed(int reason) {}
@Override
public void onBroadcastMetadataChanged(
int broadcastId, @NonNull BluetoothLeBroadcastMetadata metadata) {}
@Override
public void onBroadcastStopped(int reason, int broadcastId) {
if (mPreference != null) {
refreshSummary(mPreference);
}
}
@Override
public void onBroadcastStopFailed(int reason) {}
@Override
public void onBroadcastUpdated(int reason, int broadcastId) {}
@Override
public void onBroadcastUpdateFailed(int reason, int broadcastId) {}
@Override
public void onPlaybackStarted(int reason, int broadcastId) {}
@Override
public void onPlaybackStopped(int reason, int broadcastId) {}
};
public AudioSharingPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mLocalBtManager = Utils.getLocalBtManager(context);
if (mLocalBtManager != null) {
mBroadcast = mLocalBtManager.getProfileManager().getLeAudioBroadcastProfile();
}
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().registerCallback(this);
}
if (mBroadcast != null) {
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().unregisterCallback(this);
}
if (mBroadcast != null) {
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
}
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
@Override
public int getAvailabilityStatus() {
return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public CharSequence getSummary() {
return AudioSharingUtils.isBroadcasting(mLocalBtManager) ? "On" : "Off";
}
@Override
public void onBluetoothStateChanged(@AdapterState int bluetoothState) {
if (mPreference != null) {
refreshSummary(mPreference);
}
}
}

View File

@@ -1,138 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
public class AudioSharingStopDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "AudioSharingStopDialog";
private static final String BUNDLE_KEY_NEW_DEVICE_NAME = "bundle_key_new_device_name";
// The host creates an instance of this dialog fragment must implement this interface to receive
// event callbacks.
public interface DialogEventListener {
/** Called when users click the stop sharing button in the dialog. */
void onStopSharingClick();
}
private static DialogEventListener sListener;
private static @Nullable CachedBluetoothDevice sNewDevice;
@Override
public int getMetricsCategory() {
return SettingsEnums.DIALOG_STOP_AUDIO_SHARING;
}
/**
* Display the {@link AudioSharingStopDialogFragment} dialog.
*
* <p>If the dialog is showing, update the dialog message and event listener.
*
* @param host The Fragment this dialog will be hosted.
* @param newDevice The latest connected device triggered this dialog.
* @param listener The callback to handle the user action on this dialog.
*/
public static void show(
Fragment host, CachedBluetoothDevice newDevice, DialogEventListener listener) {
if (!AudioSharingUtils.isFeatureEnabled()) return;
final FragmentManager manager = host.getChildFragmentManager();
sListener = listener;
sNewDevice = newDevice;
Fragment dialog = manager.findFragmentByTag(TAG);
if (dialog != null
&& ((DialogFragment) dialog).getDialog() != null
&& ((DialogFragment) dialog).getDialog().isShowing()) {
Log.d(TAG, "Dialog is showing, update the content.");
updateDialog(newDevice.getName(), (AlertDialog) ((DialogFragment) dialog).getDialog());
} else {
Log.d(TAG, "Show up the dialog.");
final Bundle bundle = new Bundle();
bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDevice.getName());
AudioSharingStopDialogFragment dialogFrag = new AudioSharingStopDialogFragment();
dialogFrag.setArguments(bundle);
dialogFrag.show(manager, TAG);
}
}
/** Return the tag of {@link AudioSharingStopDialogFragment} dialog. */
public static @NonNull String tag() {
return TAG;
}
/** Get the latest connected device which triggers the dialog. */
public @Nullable CachedBluetoothDevice getDevice() {
return sNewDevice;
}
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle arguments = requireArguments();
String newDeviceName = arguments.getString(BUNDLE_KEY_NEW_DEVICE_NAME);
final AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity()).setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(builder.getContext());
// Set custom title for the dialog.
View customTitle =
inflater.inflate(R.layout.dialog_custom_title_audio_sharing, /* parent= */ null);
ImageView icon = customTitle.findViewById(R.id.title_icon);
icon.setImageResource(R.drawable.ic_warning_24dp);
TextView title = customTitle.findViewById(R.id.title_text);
title.setText("Stop sharing audio?");
builder.setPositiveButton(
"Stop sharing", (dialog, which) -> sListener.onStopSharingClick());
builder.setNegativeButton("Cancel", (dialog, which) -> dismiss());
AlertDialog dialog = builder.setCustomTitle(customTitle).create();
dialog.setCanceledOnTouchOutside(false);
updateDialog(newDeviceName, dialog);
dialog.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
if (messageView != null) {
Typeface typeface = Typeface.create(Typeface.DEFAULT_FAMILY, Typeface.NORMAL);
messageView.setTypeface(typeface);
messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
messageView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
Log.w(TAG, "sssFail to update dialog: message view is null");
}
return dialog;
}
private static void updateDialog(String newDeviceName, @NonNull AlertDialog dialog) {
dialog.setMessage(
newDeviceName + " wants to connect, headphones in audio sharing will disconnect.");
}
}

View File

@@ -1,395 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.SettingsMainSwitchBar;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class AudioSharingSwitchBarController extends BasePreferenceController
implements DefaultLifecycleObserver, OnCheckedChangeListener {
private static final String TAG = "AudioSharingSwitchBarCtl";
private static final String PREF_KEY = "audio_sharing_main_switch";
interface OnSwitchBarChangedListener {
void onSwitchBarChanged();
}
private final SettingsMainSwitchBar mSwitchBar;
private final BluetoothAdapter mBluetoothAdapter;
private final LocalBluetoothManager mBtManager;
private final LocalBluetoothLeBroadcast mBroadcast;
private final LocalBluetoothLeBroadcastAssistant mAssistant;
private final Executor mExecutor;
private final OnSwitchBarChangedListener mListener;
private DashboardFragment mFragment;
private Map<Integer, List<CachedBluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
private List<BluetoothDevice> mTargetActiveSinks = new ArrayList<>();
private ArrayList<AudioSharingDeviceItem> mDeviceItemsForSharing = new ArrayList<>();
@VisibleForTesting IntentFilter mIntentFilter;
@VisibleForTesting
BroadcastReceiver mReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) return;
int adapterState =
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
mSwitchBar.setChecked(isBroadcasting());
mSwitchBar.setEnabled(adapterState == BluetoothAdapter.STATE_ON);
mListener.onSwitchBarChanged();
}
};
private final BluetoothLeBroadcast.Callback mBroadcastCallback =
new BluetoothLeBroadcast.Callback() {
@Override
public void onBroadcastStarted(int reason, int broadcastId) {
Log.d(
TAG,
"onBroadcastStarted(), reason = "
+ reason
+ ", broadcastId = "
+ broadcastId);
updateSwitch();
}
@Override
public void onBroadcastStartFailed(int reason) {
Log.d(TAG, "onBroadcastStartFailed(), reason = " + reason);
// TODO: handle broadcast start fail
updateSwitch();
}
@Override
public void onBroadcastMetadataChanged(
int broadcastId, @NonNull BluetoothLeBroadcastMetadata metadata) {
Log.d(
TAG,
"onBroadcastMetadataChanged(), broadcastId = "
+ broadcastId
+ ", metadata = "
+ metadata.getBroadcastName());
addSourceToTargetSinks(mTargetActiveSinks);
if (mFragment == null) {
Log.w(TAG, "Dialog fail to show due to null fragment.");
return;
}
ThreadUtils.postOnMainThread(
() -> {
AudioSharingDialogFragment.show(
mFragment,
mDeviceItemsForSharing,
item -> {
addSourceToTargetSinks(
mGroupedConnectedDevices
.getOrDefault(
item.getGroupId(),
ImmutableList.of())
.stream()
.map(CachedBluetoothDevice::getDevice)
.collect(Collectors.toList()));
});
});
}
@Override
public void onBroadcastStopped(int reason, int broadcastId) {
Log.d(
TAG,
"onBroadcastStopped(), reason = "
+ reason
+ ", broadcastId = "
+ broadcastId);
updateSwitch();
}
@Override
public void onBroadcastStopFailed(int reason) {
Log.d(TAG, "onBroadcastStopFailed(), reason = " + reason);
// TODO: handle broadcast stop fail
updateSwitch();
}
@Override
public void onBroadcastUpdated(int reason, int broadcastId) {}
@Override
public void onBroadcastUpdateFailed(int reason, int broadcastId) {}
@Override
public void onPlaybackStarted(int reason, int broadcastId) {}
@Override
public void onPlaybackStopped(int reason, int broadcastId) {}
};
private BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
new BluetoothLeBroadcastAssistant.Callback() {
@Override
public void onSearchStarted(int reason) {}
@Override
public void onSearchStartFailed(int reason) {}
@Override
public void onSearchStopped(int reason) {}
@Override
public void onSearchStopFailed(int reason) {}
@Override
public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {}
@Override
public void onSourceAdded(@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(
TAG,
"onSourceAdded(), sink = "
+ sink
+ ", sourceId = "
+ sourceId
+ ", reason = "
+ reason);
}
@Override
public void onSourceAddFailed(
@NonNull BluetoothDevice sink,
@NonNull BluetoothLeBroadcastMetadata source,
int reason) {
Log.d(
TAG,
"onSourceAddFailed(), sink = "
+ sink
+ ", source = "
+ source
+ ", reason = "
+ reason);
AudioSharingUtils.toastMessage(
mContext,
String.format(
Locale.US,
"Fail to add source to %s reason %d",
sink.getAddress(),
reason));
}
@Override
public void onSourceModified(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceModifyFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceRemoved(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceRemoveFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink,
int sourceId,
BluetoothLeBroadcastReceiveState state) {}
};
AudioSharingSwitchBarController(
Context context, SettingsMainSwitchBar switchBar, OnSwitchBarChangedListener listener) {
super(context, PREF_KEY);
mSwitchBar = switchBar;
mListener = listener;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
mBtManager = Utils.getLocalBtManager(context);
mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile();
mAssistant = mBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
mSwitchBar.addOnSwitchChangeListener(this);
mContext.registerReceiver(mReceiver, mIntentFilter, Context.RECEIVER_EXPORTED_UNAUDITED);
if (mBroadcast != null) {
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
}
if (mAssistant != null) {
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
}
if (isAvailable()) {
mSwitchBar.setChecked(isBroadcasting());
mSwitchBar.setEnabled(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled());
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
mSwitchBar.removeOnSwitchChangeListener(this);
mContext.unregisterReceiver(mReceiver);
if (mBroadcast != null) {
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
}
if (mAssistant != null) {
mAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Filter out unnecessary callbacks when switch is disabled.
if (!buttonView.isEnabled()) return;
if (isChecked) {
startAudioSharing();
} else {
stopAudioSharing();
}
}
@Override
public int getAvailabilityStatus() {
return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
/**
* Initialize the controller.
*
* @param fragment The fragment to host the {@link AudioSharingSwitchBarController} dialog.
*/
public void init(DashboardFragment fragment) {
this.mFragment = fragment;
}
private void startAudioSharing() {
mSwitchBar.setEnabled(false);
if (mBroadcast == null || isBroadcasting()) {
Log.d(TAG, "Already in broadcasting or broadcast not support, ignore!");
mSwitchBar.setEnabled(true);
return;
}
mGroupedConnectedDevices = AudioSharingUtils.fetchConnectedDevicesByGroupId(mBtManager);
ArrayList<AudioSharingDeviceItem> deviceItems =
AudioSharingUtils.buildOrderedConnectedLeadAudioSharingDeviceItem(
mBtManager, mGroupedConnectedDevices, /* filterByInSharing= */ false);
// deviceItems is ordered. The active device is the first place if exits.
mDeviceItemsForSharing = new ArrayList<>(deviceItems);
if (!deviceItems.isEmpty() && deviceItems.get(0).isActive()) {
for (CachedBluetoothDevice device :
mGroupedConnectedDevices.getOrDefault(
deviceItems.get(0).getGroupId(), ImmutableList.of())) {
// If active device exists for audio sharing, share to it
// automatically once the broadcast is started.
mTargetActiveSinks.add(device.getDevice());
}
mDeviceItemsForSharing.remove(0);
}
mBroadcast.startPrivateBroadcast();
}
private void stopAudioSharing() {
mSwitchBar.setEnabled(false);
if (mBroadcast == null || !isBroadcasting()) {
Log.d(TAG, "Already not broadcasting or broadcast not support, ignore!");
mSwitchBar.setEnabled(true);
return;
}
mBroadcast.stopBroadcast(mBroadcast.getLatestBroadcastId());
}
private void updateSwitch() {
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
boolean isBroadcasting = isBroadcasting();
ThreadUtils.postOnMainThread(
() -> {
if (mSwitchBar.isChecked() != isBroadcasting) {
mSwitchBar.setChecked(isBroadcasting);
}
mSwitchBar.setEnabled(true);
mListener.onSwitchBarChanged();
});
});
}
private boolean isBroadcasting() {
return mBroadcast != null && mBroadcast.isEnabled(null);
}
private void addSourceToTargetSinks(List<BluetoothDevice> sinks) {
if (sinks.isEmpty() || mBroadcast == null || mAssistant == null) {
Log.d(TAG, "Skip adding source to target.");
return;
}
BluetoothLeBroadcastMetadata broadcastMetadata =
mBroadcast.getLatestBluetoothLeBroadcastMetadata();
if (broadcastMetadata == null) {
Log.e(TAG, "Error: There is no broadcastMetadata.");
return;
}
for (BluetoothDevice sink : sinks) {
Log.d(
TAG,
"Add broadcast with broadcastId: "
+ broadcastMetadata.getBroadcastId()
+ "to the device: "
+ sink.getAnonymizedAddress());
mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
}
}
}

View File

@@ -1,408 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.android.settings.flags.Flags;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
public class AudioSharingUtils {
private static final String TAG = "AudioSharingUtils";
private static final boolean DEBUG = BluetoothUtils.D;
/**
* Fetch {@link CachedBluetoothDevice}s connected to the broadcast assistant. The devices are
* grouped by CSIP group id.
*
* @param localBtManager The BT manager to provide BT functions.
* @return A map of connected devices grouped by CSIP group id.
*/
public static Map<Integer, List<CachedBluetoothDevice>> fetchConnectedDevicesByGroupId(
LocalBluetoothManager localBtManager) {
Map<Integer, List<CachedBluetoothDevice>> groupedDevices = new HashMap<>();
if (localBtManager == null) {
Log.d(TAG, "Skip fetchConnectedDevicesByGroupId due to bt manager is null");
return groupedDevices;
}
LocalBluetoothLeBroadcastAssistant assistant =
localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
if (assistant == null) {
Log.d(TAG, "Skip fetchConnectedDevicesByGroupId due to assistant profile is null");
return groupedDevices;
}
List<BluetoothDevice> connectedDevices = assistant.getConnectedDevices();
CachedBluetoothDeviceManager cacheManager = localBtManager.getCachedDeviceManager();
for (BluetoothDevice device : connectedDevices) {
CachedBluetoothDevice cachedDevice = cacheManager.findDevice(device);
if (cachedDevice == null) {
Log.d(TAG, "Skip device due to not being cached: " + device.getAnonymizedAddress());
continue;
}
int groupId = getGroupId(cachedDevice);
if (groupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
Log.d(
TAG,
"Skip device due to no valid group id: " + device.getAnonymizedAddress());
continue;
}
if (!groupedDevices.containsKey(groupId)) {
groupedDevices.put(groupId, new ArrayList<>());
}
groupedDevices.get(groupId).add(cachedDevice);
}
if (DEBUG) {
Log.d(TAG, "fetchConnectedDevicesByGroupId: " + groupedDevices);
}
return groupedDevices;
}
/**
* Fetch a list of ordered connected lead {@link CachedBluetoothDevice}s eligible for audio
* sharing. The active device is placed in the first place if it exists. The devices can be
* filtered by whether it is already in the audio sharing session.
*
* @param localBtManager The BT manager to provide BT functions. *
* @param groupedConnectedDevices devices connected to broadcast assistant grouped by CSIP group
* id.
* @param filterByInSharing Whether to filter the device by if is already in the sharing
* session.
* @return A list of ordered connected devices eligible for the audio sharing. The active device
* is placed in the first place if it exists.
*/
public static List<CachedBluetoothDevice> buildOrderedConnectedLeadDevices(
LocalBluetoothManager localBtManager,
Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices,
boolean filterByInSharing) {
List<CachedBluetoothDevice> orderedDevices = new ArrayList<>();
for (List<CachedBluetoothDevice> devices : groupedConnectedDevices.values()) {
@Nullable CachedBluetoothDevice leadDevice = getLeadDevice(devices);
if (leadDevice == null) {
Log.d(TAG, "Skip due to no lead device");
continue;
}
if (filterByInSharing && !hasBroadcastSource(leadDevice, localBtManager)) {
Log.d(
TAG,
"Filtered the device due to not in sharing session: "
+ leadDevice.getDevice().getAnonymizedAddress());
continue;
}
orderedDevices.add(leadDevice);
}
orderedDevices.sort(
(CachedBluetoothDevice d1, CachedBluetoothDevice d2) -> {
// Active above not inactive
int comparison =
(isActiveLeAudioDevice(d2) ? 1 : 0)
- (isActiveLeAudioDevice(d1) ? 1 : 0);
if (comparison != 0) return comparison;
// Bonded above not bonded
comparison =
(d2.getBondState() == BluetoothDevice.BOND_BONDED ? 1 : 0)
- (d1.getBondState() == BluetoothDevice.BOND_BONDED ? 1 : 0);
if (comparison != 0) return comparison;
// Bond timestamp available above unavailable
comparison =
(d2.getBondTimestamp() != null ? 1 : 0)
- (d1.getBondTimestamp() != null ? 1 : 0);
if (comparison != 0) return comparison;
// Order by bond timestamp if it is available
// Otherwise order by device name
return d1.getBondTimestamp() != null
? d1.getBondTimestamp().compareTo(d2.getBondTimestamp())
: d1.getName().compareTo(d2.getName());
});
return orderedDevices;
}
/**
* Get the lead device from a list of devices with same group id.
*
* @param devices A list of devices with same group id.
* @return The lead device
*/
@Nullable
public static CachedBluetoothDevice getLeadDevice(
@NonNull List<CachedBluetoothDevice> devices) {
if (devices.isEmpty()) return null;
for (CachedBluetoothDevice device : devices) {
if (!device.getMemberDevice().isEmpty()) {
return device;
}
}
CachedBluetoothDevice leadDevice = devices.get(0);
Log.d(
TAG,
"No lead device in the group, pick arbitrary device as the lead: "
+ leadDevice.getDevice().getAnonymizedAddress());
return leadDevice;
}
/**
* Fetch a list of ordered connected lead {@link AudioSharingDeviceItem}s eligible for audio
* sharing. The active device is placed in the first place if it exists. The devices can be
* filtered by whether it is already in the audio sharing session.
*
* @param localBtManager The BT manager to provide BT functions. *
* @param groupedConnectedDevices devices connected to broadcast assistant grouped by CSIP group
* id.
* @param filterByInSharing Whether to filter the device by if is already in the sharing
* session.
* @return A list of ordered connected devices eligible for the audio sharing. The active device
* is placed in the first place if it exists.
*/
public static ArrayList<AudioSharingDeviceItem> buildOrderedConnectedLeadAudioSharingDeviceItem(
LocalBluetoothManager localBtManager,
Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices,
boolean filterByInSharing) {
return buildOrderedConnectedLeadDevices(
localBtManager, groupedConnectedDevices, filterByInSharing)
.stream()
.map(device -> buildAudioSharingDeviceItem(device))
.collect(Collectors.toCollection(ArrayList::new));
}
/** Build {@link AudioSharingDeviceItem} from {@link CachedBluetoothDevice}. */
public static AudioSharingDeviceItem buildAudioSharingDeviceItem(
CachedBluetoothDevice cachedDevice) {
return new AudioSharingDeviceItem(
cachedDevice.getName(),
getGroupId(cachedDevice),
isActiveLeAudioDevice(cachedDevice));
}
/**
* Check if {@link CachedBluetoothDevice} is in an audio sharing session.
*
* @param cachedDevice The cached bluetooth device to check.
* @param localBtManager The BT manager to provide BT functions.
* @return Whether the device is in an audio sharing session.
*/
public static boolean hasBroadcastSource(
CachedBluetoothDevice cachedDevice, LocalBluetoothManager localBtManager) {
if (localBtManager == null) {
Log.d(TAG, "Skip check hasBroadcastSource due to bt manager is null");
return false;
}
LocalBluetoothLeBroadcastAssistant assistant =
localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
if (assistant == null) {
Log.d(TAG, "Skip check hasBroadcastSource due to assistant profile is null");
return false;
}
List<BluetoothLeBroadcastReceiveState> sourceList =
assistant.getAllSources(cachedDevice.getDevice());
if (!sourceList.isEmpty()) {
Log.d(
TAG,
"Lead device has broadcast source, device = "
+ cachedDevice.getDevice().getAnonymizedAddress());
return true;
}
// Return true if member device is in broadcast.
for (CachedBluetoothDevice device : cachedDevice.getMemberDevice()) {
List<BluetoothLeBroadcastReceiveState> list =
assistant.getAllSources(device.getDevice());
if (!list.isEmpty()) {
Log.d(
TAG,
"Member device has broadcast source, device = "
+ device.getDevice().getAnonymizedAddress());
return true;
}
}
return false;
}
/**
* Check if {@link CachedBluetoothDevice} is an active le audio device.
*
* @param cachedDevice The cached bluetooth device to check.
* @return Whether the device is an active le audio device.
*/
public static boolean isActiveLeAudioDevice(CachedBluetoothDevice cachedDevice) {
return BluetoothUtils.isActiveLeAudioDevice(cachedDevice);
}
/**
* Retrieves the one and only active Bluetooth LE Audio sink device, regardless if the device is
* currently in an audio sharing session.
*
* @param manager The LocalBluetoothManager instance used to fetch connected devices.
* @return An Optional containing the active LE Audio device, or an empty Optional if not found.
*/
public static Optional<CachedBluetoothDevice> getActiveSinkOnAssistant(
@Nullable LocalBluetoothManager manager) {
if (manager == null) {
Log.w(TAG, "getActiveSinksOnAssistant(): LocalBluetoothManager is null!");
return Optional.empty();
}
var groupedDevices = fetchConnectedDevicesByGroupId(manager);
var leadDevices = buildOrderedConnectedLeadDevices(manager, groupedDevices, false);
if (!leadDevices.isEmpty() && isActiveLeAudioDevice(leadDevices.get(0))) {
return Optional.of(leadDevices.get(0));
} else {
Log.w(TAG, "getActiveSinksOnAssistant(): No active lead device!");
}
return Optional.empty();
}
/** Toast message on main thread. */
public static void toastMessage(Context context, String message) {
context.getMainExecutor()
.execute(() -> Toast.makeText(context, message, Toast.LENGTH_LONG).show());
}
/** Returns if the le audio sharing is enabled. */
public static boolean isFeatureEnabled() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
return Flags.enableLeAudioSharing()
&& adapter.isLeAudioBroadcastSourceSupported()
== BluetoothStatusCodes.FEATURE_SUPPORTED
&& adapter.isLeAudioBroadcastAssistantSupported()
== BluetoothStatusCodes.FEATURE_SUPPORTED;
}
/** Automatically update active device if needed. */
public static void updateActiveDeviceIfNeeded(LocalBluetoothManager localBtManager) {
if (localBtManager == null) {
Log.d(TAG, "Skip updateActiveDeviceIfNeeded due to bt manager is null");
return;
}
Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices =
fetchConnectedDevicesByGroupId(localBtManager);
List<CachedBluetoothDevice> devicesInSharing =
buildOrderedConnectedLeadDevices(
localBtManager, groupedConnectedDevices, /* filterByInSharing= */ true);
if (devicesInSharing.isEmpty()) return;
List<BluetoothDevice> devices =
BluetoothAdapter.getDefaultAdapter().getMostRecentlyConnectedDevices();
CachedBluetoothDevice targetDevice = null;
// Find the earliest connected device in sharing session.
int targetDeviceIdx = -1;
for (CachedBluetoothDevice device : devicesInSharing) {
if (devices.contains(device.getDevice())) {
int idx = devices.indexOf(device.getDevice());
if (idx > targetDeviceIdx) {
targetDeviceIdx = idx;
targetDevice = device;
}
}
}
if (targetDevice != null && !isActiveLeAudioDevice(targetDevice)) {
Log.d(
TAG,
"updateActiveDeviceIfNeeded, set active device: "
+ targetDevice.getDevice().getAnonymizedAddress());
targetDevice.setActive();
} else {
Log.d(
TAG,
"updateActiveDeviceIfNeeded, skip set active device: "
+ (targetDevice == null
? "null"
: (targetDevice.getDevice().getAnonymizedAddress()
+ " is already active")));
}
}
/** Returns if the broadcast is on-going. */
public static boolean isBroadcasting(@Nullable LocalBluetoothManager manager) {
if (manager == null) return false;
LocalBluetoothLeBroadcast broadcast =
manager.getProfileManager().getLeAudioBroadcastProfile();
return broadcast != null && broadcast.isEnabled(null);
}
/** Stops the latest broadcast. */
public static void stopBroadcasting(LocalBluetoothManager manager) {
if (manager == null) {
Log.d(TAG, "Skip stop broadcasting due to bt manager is null");
return;
}
LocalBluetoothLeBroadcast broadcast =
manager.getProfileManager().getLeAudioBroadcastProfile();
if (broadcast == null) {
Log.d(TAG, "Skip stop broadcasting due to broadcast profile is null");
}
broadcast.stopBroadcast(broadcast.getLatestBroadcastId());
}
/**
* Get CSIP group id for {@link CachedBluetoothDevice}.
*
* <p>If CachedBluetoothDevice#getGroupId is invalid, fetch group id from
* LeAudioProfile#getGroupId.
*/
public static int getGroupId(CachedBluetoothDevice cachedDevice) {
int groupId = cachedDevice.getGroupId();
String anonymizedAddress = cachedDevice.getDevice().getAnonymizedAddress();
if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
Log.d(TAG, "getGroupId by CSIP profile for device: " + anonymizedAddress);
return groupId;
}
for (LocalBluetoothProfile profile : cachedDevice.getProfiles()) {
if (profile instanceof LeAudioProfile) {
Log.d(TAG, "getGroupId by LEA profile for device: " + anonymizedAddress);
return ((LeAudioProfile) profile).getGroupId(cachedDevice.getDevice());
}
}
Log.d(TAG, "getGroupId return invalid id for device: " + anonymizedAddress);
return BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
}
/** Get the fallback active group id from SettingsProvider. */
public static int getFallbackActiveGroupId(@NonNull Context context) {
return Settings.Secure.getInt(
context.getContentResolver(),
"bluetooth_le_broadcast_fallback_active_group_id",
BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
}
/** Post the runnable to main thread. */
public static void postOnMainThread(@NonNull Context context, @NonNull Runnable runnable) {
context.getMainExecutor().execute(runnable);
}
}

View File

@@ -1,103 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import java.util.ArrayList;
/** Provides a dialog to choose the active device for calls and alarms. */
public class CallsAndAlarmsDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "CallsAndAlarmsDialog";
private static final String BUNDLE_KEY_DEVICE_ITEMS = "bundle_key_device_items";
// The host creates an instance of this dialog fragment must implement this interface to receive
// event callbacks.
public interface DialogEventListener {
/**
* Called when users click the device item to set active for calls and alarms in the dialog.
*
* @param item The device item clicked.
*/
void onItemClick(AudioSharingDeviceItem item);
}
private static DialogEventListener sListener;
@Override
public int getMetricsCategory() {
return SettingsEnums.DIALOG_AUDIO_SHARING_SWITCH_ACTIVE;
}
/**
* Display the {@link CallsAndAlarmsDialogFragment} dialog.
*
* @param host The Fragment this dialog will be hosted.
* @param deviceItems The connected device items in audio sharing session.
* @param listener The callback to handle the user action on this dialog.
*/
public static void show(
Fragment host,
ArrayList<AudioSharingDeviceItem> deviceItems,
DialogEventListener listener) {
if (!AudioSharingUtils.isFeatureEnabled()) return;
final FragmentManager manager = host.getChildFragmentManager();
sListener = listener;
if (manager.findFragmentByTag(TAG) == null) {
final Bundle bundle = new Bundle();
bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems);
final CallsAndAlarmsDialogFragment dialog = new CallsAndAlarmsDialogFragment();
dialog.setArguments(bundle);
dialog.show(manager, TAG);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle arguments = requireArguments();
ArrayList<AudioSharingDeviceItem> deviceItems =
arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS);
int checkedItem = -1;
for (AudioSharingDeviceItem item : deviceItems) {
int fallbackActiveGroupId = AudioSharingUtils.getFallbackActiveGroupId(getContext());
if (item.getGroupId() == fallbackActiveGroupId) {
checkedItem = deviceItems.indexOf(item);
}
}
String[] choices =
deviceItems.stream().map(AudioSharingDeviceItem::getName).toArray(String[]::new);
AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity())
.setTitle(R.string.calls_and_alarms_device_title)
.setSingleChoiceItems(
choices,
checkedItem,
(dialog, which) -> {
sListener.onItemClick(deviceItems.get(which));
});
return builder.create();
}
}

View File

@@ -1,261 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/** PreferenceController to control the dialog to choose the active device for calls and alarms */
public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferenceController
implements BluetoothCallback {
private static final String TAG = "CallsAndAlarmsPreferenceController";
private static final String PREF_KEY = "calls_and_alarms";
private final LocalBluetoothManager mLocalBtManager;
private final Executor mExecutor;
@Nullable private LocalBluetoothLeBroadcastAssistant mAssistant = null;
private DashboardFragment mFragment;
Map<Integer, List<CachedBluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
private ArrayList<AudioSharingDeviceItem> mDeviceItemsInSharingSession = new ArrayList<>();
private BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
new BluetoothLeBroadcastAssistant.Callback() {
@Override
public void onSearchStarted(int reason) {}
@Override
public void onSearchStartFailed(int reason) {}
@Override
public void onSearchStopped(int reason) {}
@Override
public void onSearchStopFailed(int reason) {}
@Override
public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {}
@Override
public void onSourceAdded(@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(TAG, "onSourceAdded");
updatePreference();
}
@Override
public void onSourceAddFailed(
@NonNull BluetoothDevice sink,
@NonNull BluetoothLeBroadcastMetadata source,
int reason) {}
@Override
public void onSourceModified(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceModifyFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceRemoved(
@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(TAG, "onSourceRemoved");
updatePreference();
}
@Override
public void onSourceRemoveFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink,
int sourceId,
BluetoothLeBroadcastReceiveState state) {}
};
public CallsAndAlarmsPreferenceController(Context context) {
super(context, PREF_KEY);
mLocalBtManager = Utils.getLocalBtManager(mContext);
if (mLocalBtManager != null) {
mAssistant = mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
}
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
mPreference.setOnPreferenceClickListener(
preference -> {
if (mFragment == null) {
Log.w(TAG, "Dialog fail to show due to null host.");
return true;
}
updateDeviceItemsInSharingSession();
if (mDeviceItemsInSharingSession.size() >= 1) {
CallsAndAlarmsDialogFragment.show(
mFragment,
mDeviceItemsInSharingSession,
(AudioSharingDeviceItem item) -> {
if (!mGroupedConnectedDevices.containsKey(item.getGroupId())) {
return;
}
List<CachedBluetoothDevice> devices =
mGroupedConnectedDevices.get(item.getGroupId());
@Nullable
CachedBluetoothDevice lead =
AudioSharingUtils.getLeadDevice(devices);
if (lead != null) {
Log.d(
TAG,
"Set fallback active device: "
+ lead.getDevice().getAnonymizedAddress());
lead.setActive();
updatePreference();
} else {
Log.w(
TAG,
"Fail to set fallback active device: no lead"
+ " device");
}
});
}
return true;
});
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
super.onStart(owner);
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().registerCallback(this);
}
if (mAssistant != null) {
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
super.onStop(owner);
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().unregisterCallback(this);
}
if (mAssistant != null) {
mAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
}
}
@Override
public void updateVisibility() {
if (mPreference == null) return;
var unused = ThreadUtils.postOnBackgroundThread(() -> updatePreference());
}
private void updatePreference() {
boolean isVisible = isBroadcasting() && isBluetoothStateOn();
if (!isVisible) {
AudioSharingUtils.postOnMainThread(mContext, () -> mPreference.setVisible(false));
return;
}
updateDeviceItemsInSharingSession();
int fallbackActiveGroupId = AudioSharingUtils.getFallbackActiveGroupId(mContext);
Log.d(TAG, "updatePreference: get fallback active group " + fallbackActiveGroupId);
if (fallbackActiveGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
for (AudioSharingDeviceItem item : mDeviceItemsInSharingSession) {
if (item.getGroupId() == fallbackActiveGroupId) {
AudioSharingUtils.postOnMainThread(
mContext,
() -> {
mPreference.setSummary(item.getName());
mPreference.setVisible(true);
});
return;
}
}
}
AudioSharingUtils.postOnMainThread(
mContext,
() -> {
mPreference.setSummary("No active device in sharing");
mPreference.setVisible(true);
});
}
@Override
public void onProfileConnectionStateChanged(
@NonNull CachedBluetoothDevice cachedDevice,
@ConnectionState int state,
int bluetoothProfile) {
if (state == BluetoothAdapter.STATE_DISCONNECTED
&& bluetoothProfile == BluetoothProfile.LE_AUDIO) {
// The fallback active device could be updated if the previous fallback device is
// disconnected.
updatePreference();
}
}
/**
* Initialize the controller.
*
* @param fragment The fragment to host the {@link CallsAndAlarmsDialogFragment} dialog.
*/
public void init(DashboardFragment fragment) {
this.mFragment = fragment;
}
private void updateDeviceItemsInSharingSession() {
mGroupedConnectedDevices =
AudioSharingUtils.fetchConnectedDevicesByGroupId(mLocalBtManager);
mDeviceItemsInSharingSession =
AudioSharingUtils.buildOrderedConnectedLeadAudioSharingDeviceItem(
mLocalBtManager, mGroupedConnectedDevices, /* filterByInSharing= */ true);
}
}

View File

@@ -1,98 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
public class StreamSettingsCategoryController extends BasePreferenceController
implements DefaultLifecycleObserver {
private static final String TAG = "StreamSettingsCategoryController";
private final BluetoothAdapter mBluetoothAdapter;
private final IntentFilter mIntentFilter;
private @Nullable Preference mPreference;
private BroadcastReceiver mReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) return;
int adapterState =
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
mContext.getMainExecutor()
.execute(
() -> {
if (mPreference == null) {
Log.w(
TAG,
"Skip BT state change due to mPreference "
+ "is null");
} else {
mPreference.setVisible(
adapterState == BluetoothAdapter.STATE_ON);
}
});
}
};
public StreamSettingsCategoryController(Context context, String key) {
super(context, key);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
mContext.registerReceiver(mReceiver, mIntentFilter, Context.RECEIVER_EXPORTED_UNAUDITED);
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
mContext.unregisterReceiver(mReceiver);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
if (mPreference != null) {
mPreference.setVisible(isBluetoothStateOn());
}
}
@Override
public int getAvailabilityStatus() {
return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
private boolean isBluetoothStateOn() {
return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
}
}

View File

@@ -1,197 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.content.Context;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.utils.ThreadUtils;
import com.android.settingslib.widget.ActionButtonsPreference;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AudioStreamButtonController extends BasePreferenceController
implements DefaultLifecycleObserver {
private static final String TAG = "AudioStreamButtonController";
private static final String KEY = "audio_stream_button";
private final BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
new AudioStreamsBroadcastAssistantCallback() {
@Override
public void onSourceRemoved(BluetoothDevice sink, int sourceId, int reason) {
super.onSourceRemoved(sink, sourceId, reason);
updateButton();
}
@Override
public void onSourceRemoveFailed(BluetoothDevice sink, int sourceId, int reason) {
super.onSourceRemoveFailed(sink, sourceId, reason);
updateButton();
}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink,
int sourceId,
BluetoothLeBroadcastReceiveState state) {
super.onReceiveStateChanged(sink, sourceId, state);
if (mAudioStreamsHelper.isConnected(state)) {
updateButton();
}
}
@Override
public void onSourceAddFailed(
BluetoothDevice sink, BluetoothLeBroadcastMetadata source, int reason) {
super.onSourceAddFailed(sink, source, reason);
updateButton();
}
@Override
public void onSourceLost(int broadcastId) {
super.onSourceLost(broadcastId);
updateButton();
}
};
private final AudioStreamsRepository mAudioStreamsRepository =
AudioStreamsRepository.getInstance();
private final Executor mExecutor;
private final AudioStreamsHelper mAudioStreamsHelper;
private final @Nullable LocalBluetoothLeBroadcastAssistant mLeBroadcastAssistant;
private @Nullable ActionButtonsPreference mPreference;
private int mBroadcastId = -1;
public AudioStreamButtonController(Context context, String preferenceKey) {
super(context, preferenceKey);
mExecutor = Executors.newSingleThreadExecutor();
mAudioStreamsHelper = new AudioStreamsHelper(Utils.getLocalBtManager(context));
mLeBroadcastAssistant = mAudioStreamsHelper.getLeBroadcastAssistant();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "onStart(): LeBroadcastAssistant is null!");
return;
}
mLeBroadcastAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "onStop(): LeBroadcastAssistant is null!");
return;
}
mLeBroadcastAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
}
@Override
public final void displayPreference(PreferenceScreen screen) {
mPreference = screen.findPreference(getPreferenceKey());
updateButton();
super.displayPreference(screen);
}
private void updateButton() {
if (mPreference != null) {
if (mAudioStreamsHelper.getAllConnectedSources().stream()
.map(BluetoothLeBroadcastReceiveState::getBroadcastId)
.anyMatch(connectedBroadcastId -> connectedBroadcastId == mBroadcastId)) {
ThreadUtils.postOnMainThread(
() -> {
if (mPreference != null) {
mPreference.setButton1Enabled(true);
mPreference
.setButton1Text(
R.string.bluetooth_device_context_disconnect)
.setButton1Icon(R.drawable.ic_settings_close)
.setButton1OnClickListener(
unused -> {
if (mPreference != null) {
mPreference.setButton1Enabled(false);
}
mAudioStreamsHelper.removeSource(mBroadcastId);
});
}
});
} else {
View.OnClickListener clickToRejoin =
unused ->
ThreadUtils.postOnBackgroundThread(
() -> {
var metadata =
mAudioStreamsRepository.getSavedMetadata(
mContext, mBroadcastId);
if (metadata != null) {
mAudioStreamsHelper.addSource(metadata);
ThreadUtils.postOnMainThread(
() -> {
if (mPreference != null) {
mPreference.setButton1Enabled(
false);
}
});
}
});
ThreadUtils.postOnMainThread(
() -> {
if (mPreference != null) {
mPreference.setButton1Enabled(true);
mPreference
.setButton1Text(R.string.bluetooth_device_context_connect)
.setButton1Icon(R.drawable.ic_add_24dp)
.setButton1OnClickListener(clickToRejoin);
}
});
}
} else {
Log.w(TAG, "updateButton(): preference is null!");
}
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public String getPreferenceKey() {
return KEY;
}
/** Initialize with broadcast id */
void init(int broadcastId) {
mBroadcastId = broadcastId;
}
}

View File

@@ -1,128 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.app.Activity;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.os.Bundle;
import android.util.Log;
import com.android.settings.R;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode.QrCodeScanModeFragment;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.BluetoothLeBroadcastMetadataExt;
import com.google.common.base.Strings;
public class AudioStreamConfirmDialog extends InstrumentedDialogFragment {
public static final String KEY_BROADCAST_METADATA = "key_broadcast_metadata";
private static final String TAG = "AudioStreamConfirmDialog";
private Activity mActivity;
private String mBroadcastMetadataStr;
private BluetoothLeBroadcastMetadata mBroadcastMetadata;
private boolean mIsRequestValid = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setShowsDialog(true);
mActivity = getActivity();
if (mActivity == null) {
Log.w(TAG, "onCreate() mActivity is null!");
return;
}
mBroadcastMetadataStr =
mActivity.getIntent().getStringExtra(QrCodeScanModeFragment.KEY_BROADCAST_METADATA);
if (Strings.isNullOrEmpty(mBroadcastMetadataStr)) {
Log.w(TAG, "onCreate() mBroadcastMetadataStr is null or empty!");
return;
}
mBroadcastMetadata =
BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata(
mBroadcastMetadataStr);
if (mBroadcastMetadata == null) {
Log.w(TAG, "onCreate() mBroadcastMetadata is null!");
} else {
// Warm up LE_AUDIO_BROADCAST_ASSISTANT service
Utils.getLocalBluetoothManager(mActivity);
mIsRequestValid = true;
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return mIsRequestValid ? getConfirmDialog() : getErrorDialog();
}
@Override
public int getMetricsCategory() {
// TODO(chelseahao): update metrics id
return 0;
}
private Dialog getConfirmDialog() {
return new AudioStreamsDialogFragment.DialogBuilder(mActivity)
.setTitle("Listen to audio stream")
.setSubTitle1(mBroadcastMetadata.getBroadcastName())
.setSubTitle2(
"The audio stream will play on the active LE audio device. Use this device"
+ " to control the volume.")
.setLeftButtonText("Cancel")
.setLeftButtonOnClickListener(
unused -> {
dismiss();
mActivity.finish();
})
.setRightButtonText("Listen")
.setRightButtonOnClickListener(
unused -> {
launchAudioStreamsActivity();
dismiss();
mActivity.finish();
})
.build();
}
private Dialog getErrorDialog() {
return new AudioStreamsDialogFragment.DialogBuilder(mActivity)
.setTitle("Can't listen to audio stream")
.setSubTitle2("Can't play this audio stream. Learn more")
.setRightButtonText("Close")
.setRightButtonOnClickListener(
unused -> {
dismiss();
mActivity.finish();
})
.build();
}
private void launchAudioStreamsActivity() {
Bundle bundle = new Bundle();
bundle.putString(KEY_BROADCAST_METADATA, mBroadcastMetadataStr);
new SubSettingLauncher(mActivity)
.setTitleRes(R.string.bluetooth_find_broadcast_title)
.setDestination(AudioStreamsDashboardFragment.class.getName())
.setArguments(bundle)
.setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN)
.launch();
}
}

View File

@@ -1,59 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.content.Context;
import android.os.Bundle;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
public class AudioStreamDetailsFragment extends DashboardFragment {
static final String BROADCAST_NAME_ARG = "broadcast_name";
static final String BROADCAST_ID_ARG = "broadcast_id";
private static final String TAG = "AudioStreamDetailsFragment";
@Override
public void onAttach(Context context) {
super.onAttach(context);
Bundle arguments = getArguments();
if (arguments != null) {
use(AudioStreamHeaderController.class)
.init(
this,
arguments.getString(BROADCAST_NAME_ARG),
arguments.getInt(BROADCAST_ID_ARG));
use(AudioStreamButtonController.class).init(arguments.getInt(BROADCAST_ID_ARG));
}
}
@Override
public int getMetricsCategory() {
// TODO(chelseahao): update metrics id
return 0;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.audio_stream_details_fragment;
}
@Override
protected String getLogTag() {
return TAG;
}
}

View File

@@ -1,171 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.utils.ThreadUtils;
import com.android.settingslib.widget.LayoutPreference;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.annotation.Nullable;
public class AudioStreamHeaderController extends BasePreferenceController
implements DefaultLifecycleObserver {
private static final String TAG = "AudioStreamHeaderController";
private static final String KEY = "audio_stream_header";
private final Executor mExecutor;
private final AudioStreamsHelper mAudioStreamsHelper;
@Nullable private final LocalBluetoothLeBroadcastAssistant mLeBroadcastAssistant;
private final BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
new AudioStreamsBroadcastAssistantCallback() {
@Override
public void onSourceRemoved(BluetoothDevice sink, int sourceId, int reason) {
super.onSourceRemoved(sink, sourceId, reason);
updateSummary();
}
@Override
public void onSourceLost(int broadcastId) {
super.onSourceLost(broadcastId);
updateSummary();
}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink,
int sourceId,
BluetoothLeBroadcastReceiveState state) {
super.onReceiveStateChanged(sink, sourceId, state);
if (mAudioStreamsHelper.isConnected(state)) {
updateSummary();
}
}
};
private @Nullable EntityHeaderController mHeaderController;
private @Nullable DashboardFragment mFragment;
private String mBroadcastName = "";
private int mBroadcastId = -1;
public AudioStreamHeaderController(Context context, String preferenceKey) {
super(context, preferenceKey);
mExecutor = Executors.newSingleThreadExecutor();
mAudioStreamsHelper = new AudioStreamsHelper(Utils.getLocalBtManager(context));
mLeBroadcastAssistant = mAudioStreamsHelper.getLeBroadcastAssistant();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "onStart(): LeBroadcastAssistant is null!");
return;
}
mLeBroadcastAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "onStop(): LeBroadcastAssistant is null!");
return;
}
mLeBroadcastAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
}
@Override
public final void displayPreference(PreferenceScreen screen) {
LayoutPreference headerPreference = screen.findPreference(KEY);
if (headerPreference != null && mFragment != null) {
mHeaderController =
EntityHeaderController.newInstance(
mFragment.getActivity(),
mFragment,
headerPreference.findViewById(R.id.entity_header));
if (mBroadcastName != null) {
mHeaderController.setLabel(mBroadcastName);
}
mHeaderController.setIcon(
screen.getContext().getDrawable(R.drawable.ic_bt_audio_sharing));
screen.addPreference(headerPreference);
updateSummary();
}
super.displayPreference(screen);
}
private void updateSummary() {
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
var latestSummary =
mAudioStreamsHelper.getAllConnectedSources().stream()
.map(
BluetoothLeBroadcastReceiveState
::getBroadcastId)
.anyMatch(
connectedBroadcastId ->
connectedBroadcastId
== mBroadcastId)
? "Listening now"
: "";
ThreadUtils.postOnMainThread(
() -> {
if (mHeaderController != null) {
mHeaderController.setSummary(latestSummary);
mHeaderController.done(true);
}
});
});
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public String getPreferenceKey() {
return KEY;
}
/** Initialize with {@link AudioStreamDetailsFragment} and broadcast name and id */
void init(
AudioStreamDetailsFragment audioStreamDetailsFragment,
String broadcastName,
int broadcastId) {
mFragment = audioStreamDetailsFragment;
mBroadcastName = broadcastName;
mBroadcastId = broadcastId;
}
}

View File

@@ -1,195 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothLeAudioContentMetadata;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settingslib.widget.TwoTargetPreference;
import com.google.common.base.Strings;
/**
* Custom preference class for managing audio stream preferences with an optional lock icon. Extends
* {@link TwoTargetPreference}.
*/
class AudioStreamPreference extends TwoTargetPreference {
private boolean mIsConnected = false;
private AudioStream mAudioStream;
/**
* Update preference UI based on connection status
*
* @param isConnected Is this stream connected
* @param summary Summary text
* @param onPreferenceClickListener Click listener for the preference
*/
void setIsConnected(
boolean isConnected,
String summary,
@Nullable OnPreferenceClickListener onPreferenceClickListener) {
if (mIsConnected == isConnected
&& getSummary() == summary
&& getOnPreferenceClickListener() == onPreferenceClickListener) {
// Nothing to update.
return;
}
mIsConnected = isConnected;
setSummary(summary);
setOnPreferenceClickListener(onPreferenceClickListener);
notifyChanged();
}
AudioStreamPreference(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setIcon(R.drawable.ic_bt_audio_sharing);
}
void setAudioStreamState(AudioStreamsProgressCategoryController.AudioStreamState state) {
mAudioStream.setState(state);
}
void setAudioStreamMetadata(BluetoothLeBroadcastMetadata metadata) {
mAudioStream.setMetadata(metadata);
}
int getAudioStreamBroadcastId() {
return mAudioStream.getBroadcastId();
}
int getAudioStreamRssi() {
return mAudioStream.getRssi();
}
@Nullable
BluetoothLeBroadcastMetadata getAudioStreamMetadata() {
return mAudioStream.getMetadata();
}
AudioStreamsProgressCategoryController.AudioStreamState getAudioStreamState() {
return mAudioStream.getState();
}
@Override
protected boolean shouldHideSecondTarget() {
return mIsConnected;
}
@Override
protected int getSecondTargetResId() {
return R.layout.preference_widget_lock;
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
View divider =
holder.findViewById(
com.android.settingslib.widget.preference.twotarget.R.id
.two_target_divider);
if (divider != null) {
divider.setVisibility(View.GONE);
}
}
static AudioStreamPreference fromMetadata(
Context context, BluetoothLeBroadcastMetadata source) {
AudioStreamPreference preference = new AudioStreamPreference(context, /* attrs= */ null);
preference.setTitle(getBroadcastName(source));
preference.setAudioStream(new AudioStream(source));
return preference;
}
static AudioStreamPreference fromReceiveState(
Context context, BluetoothLeBroadcastReceiveState receiveState) {
AudioStreamPreference preference = new AudioStreamPreference(context, /* attrs= */ null);
preference.setTitle(getBroadcastName(receiveState));
preference.setAudioStream(new AudioStream(receiveState));
return preference;
}
private void setAudioStream(AudioStream audioStream) {
mAudioStream = audioStream;
}
private static String getBroadcastName(BluetoothLeBroadcastMetadata source) {
return source.getSubgroups().stream()
.map(s -> s.getContentMetadata().getProgramInfo())
.filter(i -> !Strings.isNullOrEmpty(i))
.findFirst()
.orElse("Broadcast Id: " + source.getBroadcastId());
}
private static String getBroadcastName(BluetoothLeBroadcastReceiveState state) {
return state.getSubgroupMetadata().stream()
.map(BluetoothLeAudioContentMetadata::getProgramInfo)
.filter(i -> !Strings.isNullOrEmpty(i))
.findFirst()
.orElse("Broadcast Id: " + state.getBroadcastId());
}
private static final class AudioStream {
private static final int UNAVAILABLE = -1;
@Nullable private BluetoothLeBroadcastMetadata mMetadata;
@Nullable private BluetoothLeBroadcastReceiveState mReceiveState;
private AudioStreamsProgressCategoryController.AudioStreamState mState =
AudioStreamsProgressCategoryController.AudioStreamState.UNKNOWN;
private AudioStream(BluetoothLeBroadcastMetadata metadata) {
mMetadata = metadata;
}
private AudioStream(BluetoothLeBroadcastReceiveState receiveState) {
mReceiveState = receiveState;
}
private int getBroadcastId() {
return mMetadata != null
? mMetadata.getBroadcastId()
: mReceiveState != null ? mReceiveState.getBroadcastId() : UNAVAILABLE;
}
private int getRssi() {
return mMetadata != null ? mMetadata.getRssi() : Integer.MAX_VALUE;
}
private AudioStreamsProgressCategoryController.AudioStreamState getState() {
return mState;
}
@Nullable
private BluetoothLeBroadcastMetadata getMetadata() {
return mMetadata;
}
private void setState(AudioStreamsProgressCategoryController.AudioStreamState state) {
mState = state;
}
private void setMetadata(BluetoothLeBroadcastMetadata metadata) {
mMetadata = metadata;
}
}
}

View File

@@ -1,67 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
public class AudioStreamsActiveDeviceController extends BasePreferenceController
implements AudioStreamsActiveDeviceSummaryUpdater.OnSummaryChangeListener,
DefaultLifecycleObserver {
public static final String KEY = "audio_streams_active_device";
private final AudioStreamsActiveDeviceSummaryUpdater mSummaryHelper;
private Preference mPreference;
public AudioStreamsActiveDeviceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mSummaryHelper = new AudioStreamsActiveDeviceSummaryUpdater(mContext, this);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(KEY);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void onSummaryChanged(String summary) {
mPreference.setSummary(summary);
}
@Override
public void onResume(@NonNull LifecycleOwner owner) {
mSummaryHelper.register(true);
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
mSummaryHelper.register(false);
}
}

View File

@@ -1,100 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
public class AudioStreamsActiveDeviceSummaryUpdater implements BluetoothCallback {
private static final String TAG = "AudioStreamsActiveDeviceSummaryUpdater";
private static final boolean DEBUG = BluetoothUtils.D;
private final LocalBluetoothManager mBluetoothManager;
private String mSummary;
private OnSummaryChangeListener mListener;
public AudioStreamsActiveDeviceSummaryUpdater(
Context context, OnSummaryChangeListener listener) {
mBluetoothManager = Utils.getLocalBluetoothManager(context);
mListener = listener;
}
@Override
public void onActiveDeviceChanged(
@Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
if (DEBUG) {
Log.d(
TAG,
"onActiveDeviceChanged() with activeDevice : "
+ (activeDevice == null ? "null" : activeDevice.getAddress())
+ " on profile : "
+ bluetoothProfile);
}
if (bluetoothProfile == BluetoothProfile.LE_AUDIO) {
notifyChangeIfNeeded();
}
}
void register(boolean register) {
if (register) {
notifyChangeIfNeeded();
mBluetoothManager.getEventManager().registerCallback(this);
} else {
mBluetoothManager.getEventManager().unregisterCallback(this);
}
}
private void notifyChangeIfNeeded() {
ThreadUtils.postOnBackgroundThread(
() -> {
String summary = getSummary();
if (!TextUtils.equals(mSummary, summary)) {
mSummary = summary;
ThreadUtils.postOnMainThread(() -> mListener.onSummaryChanged(summary));
}
});
}
private String getSummary() {
var activeSink = AudioSharingUtils.getActiveSinkOnAssistant(mBluetoothManager);
if (activeSink.isEmpty()) {
return "No active LE Audio device";
}
return activeSink.get().getName();
}
/** Interface definition for a callback to be invoked when the summary has been changed. */
interface OnSummaryChangeListener {
/**
* Called when summary has changed.
*
* @param summary The new summary.
*/
void onSummaryChanged(String summary);
}
}

View File

@@ -1,132 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.util.Log;
import com.android.settingslib.bluetooth.BluetoothUtils;
public class AudioStreamsBroadcastAssistantCallback
implements BluetoothLeBroadcastAssistant.Callback {
private static final String TAG = "AudioStreamsBroadcastAssistantCallback";
private static final boolean DEBUG = BluetoothUtils.D;
@Override
public void onReceiveStateChanged(
BluetoothDevice sink, int sourceId, BluetoothLeBroadcastReceiveState state) {
if (DEBUG) {
Log.d(
TAG,
"onReceiveStateChanged() sink : "
+ sink.getAddress()
+ " sourceId: "
+ sourceId
+ " state: "
+ state);
}
}
@Override
public void onSearchStartFailed(int reason) {
Log.w(TAG, "onSearchStartFailed() reason : " + reason);
}
@Override
public void onSearchStarted(int reason) {
if (DEBUG) {
Log.d(TAG, "onSearchStarted() reason : " + reason);
}
}
@Override
public void onSearchStopFailed(int reason) {
Log.w(TAG, "onSearchStopFailed() reason : " + reason);
}
@Override
public void onSearchStopped(int reason) {
if (DEBUG) {
Log.d(TAG, "onSearchStopped() reason : " + reason);
}
}
@Override
public void onSourceAddFailed(
BluetoothDevice sink, BluetoothLeBroadcastMetadata source, int reason) {
if (DEBUG) {
Log.d(
TAG,
"onSourceAddFailed() sink : "
+ sink.getAddress()
+ " source: "
+ source
+ " reason: "
+ reason);
}
}
@Override
public void onSourceAdded(BluetoothDevice sink, int sourceId, int reason) {
if (DEBUG) {
Log.d(
TAG,
"onSourceAdded() sink : "
+ sink.getAddress()
+ " sourceId: "
+ sourceId
+ " reason: "
+ reason);
}
}
@Override
public void onSourceFound(BluetoothLeBroadcastMetadata source) {
if (DEBUG) {
Log.d(TAG, "onSourceFound() broadcastId : " + source.getBroadcastId());
}
}
@Override
public void onSourceLost(int broadcastId) {
if (DEBUG) {
Log.d(TAG, "onSourceLost() broadcastId : " + broadcastId);
}
}
@Override
public void onSourceModified(BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceModifyFailed(BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceRemoveFailed(BluetoothDevice sink, int sourceId, int reason) {
Log.w(TAG, "onSourceRemoveFailed() sourceId : " + sourceId + " reason : " + reason);
}
@Override
public void onSourceRemoved(BluetoothDevice sink, int sourceId, int reason) {
if (DEBUG) {
Log.d(TAG, "onSourceRemoved() sourceId : " + sourceId + " reason : " + reason);
}
}
}

View File

@@ -1,110 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.audiosharing.AudioSharingBasePreferenceController;
import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
import com.android.settings.flags.Flags;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AudioStreamsCategoryController extends AudioSharingBasePreferenceController {
private static final String TAG = "AudioStreamsCategoryController";
private static final boolean DEBUG = BluetoothUtils.D;
private final LocalBluetoothManager mLocalBtManager;
private final Executor mExecutor;
private final BluetoothCallback mBluetoothCallback =
new BluetoothCallback() {
@Override
public void onActiveDeviceChanged(
@Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
if (bluetoothProfile == BluetoothProfile.LE_AUDIO) {
updateVisibility();
}
}
};
public AudioStreamsCategoryController(Context context, String key) {
super(context, key);
mLocalBtManager = Utils.getLocalBtManager(mContext);
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
super.onStart(owner);
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().registerCallback(mBluetoothCallback);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
super.onStop(owner);
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().unregisterCallback(mBluetoothCallback);
}
}
@Override
public int getAvailabilityStatus() {
return Flags.enableLeAudioQrCodePrivateBroadcastSharing()
? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public void updateVisibility() {
if (mPreference == null) return;
mExecutor.execute(
() -> {
boolean hasActiveLe =
AudioSharingUtils.getActiveSinkOnAssistant(mLocalBtManager).isPresent();
boolean isBroadcasting = isBroadcasting();
boolean isBluetoothOn = isBluetoothStateOn();
if (DEBUG) {
Log.d(
TAG,
"updateVisibility() isBroadcasting : "
+ isBroadcasting
+ " hasActiveLe : "
+ hasActiveLe
+ " is BT on : "
+ isBluetoothOn);
}
ThreadUtils.postOnMainThread(
() ->
mPreference.setVisible(
isBluetoothOn && hasActiveLe && !isBroadcasting));
});
}
}

View File

@@ -1,134 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import static com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsScanQrCodeController.REQUEST_SCAN_BT_BROADCAST_QR_CODE;
import android.app.Activity;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.android.settings.R;
import com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode.QrCodeScanModeFragment;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.BluetoothLeBroadcastMetadataExt;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.google.common.base.Strings;
public class AudioStreamsDashboardFragment extends DashboardFragment {
private static final String TAG = "AudioStreamsDashboardFrag";
private static final boolean DEBUG = BluetoothUtils.D;
private AudioStreamsProgressCategoryController mAudioStreamsProgressCategoryController;
public AudioStreamsDashboardFragment() {
super();
}
@Override
public int getMetricsCategory() {
// TODO: update category id.
return 0;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
public int getHelpResource() {
return R.string.help_url_audio_sharing;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.bluetooth_audio_streams;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(AudioStreamsScanQrCodeController.class).setFragment(this);
mAudioStreamsProgressCategoryController = use(AudioStreamsProgressCategoryController.class);
mAudioStreamsProgressCategoryController.setFragment(this);
if (getArguments() != null) {
String broadcastMetadataStr =
getArguments().getString(AudioStreamConfirmDialog.KEY_BROADCAST_METADATA);
if (!Strings.isNullOrEmpty(broadcastMetadataStr)) {
BluetoothLeBroadcastMetadata broadcastMetadata =
BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata(
broadcastMetadataStr);
if (broadcastMetadata == null) {
Log.w(TAG, "onAttach() broadcastMetadata is null!");
} else {
mAudioStreamsProgressCategoryController.setSourceFromQrCode(broadcastMetadata);
}
}
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (DEBUG) {
Log.d(
TAG,
"onActivityResult() requestCode : "
+ requestCode
+ " resultCode : "
+ resultCode);
}
if (requestCode == REQUEST_SCAN_BT_BROADCAST_QR_CODE) {
if (resultCode == Activity.RESULT_OK) {
String broadcastMetadata =
data.getStringExtra(QrCodeScanModeFragment.KEY_BROADCAST_METADATA);
BluetoothLeBroadcastMetadata source =
BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata(
broadcastMetadata);
if (source == null) {
Log.w(TAG, "onActivityResult() source is null!");
return;
}
if (DEBUG) {
Log.d(TAG, "onActivityResult() broadcastId : " + source.getBroadcastId());
}
if (mAudioStreamsProgressCategoryController == null) {
Log.w(
TAG,
"onActivityResult() AudioStreamsProgressCategoryController is null!");
return;
}
mAudioStreamsProgressCategoryController.setSourceFromQrCode(source);
}
}
}
}

View File

@@ -1,151 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.google.common.base.Strings;
import java.util.function.Consumer;
public class AudioStreamsDialogFragment extends InstrumentedDialogFragment {
private static final String TAG = "AudioStreamsDialogFragment";
private final DialogBuilder mDialogBuilder;
AudioStreamsDialogFragment(DialogBuilder dialogBuilder) {
mDialogBuilder = dialogBuilder;
}
@Override
public int getMetricsCategory() {
// TODO(chelseahao): update metrics id
return 0;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return mDialogBuilder.build();
}
static void show(Fragment host, DialogBuilder dialogBuilder) {
FragmentManager manager = host.getChildFragmentManager();
(new AudioStreamsDialogFragment(dialogBuilder)).show(manager, TAG);
}
static class DialogBuilder {
private final Context mContext;
private final AlertDialog.Builder mBuilder;
private String mTitle;
private String mSubTitle1;
private String mSubTitle2;
private String mLeftButtonText;
private String mRightButtonText;
private Consumer<AlertDialog> mLeftButtonOnClickListener;
private Consumer<AlertDialog> mRightButtonOnClickListener;
DialogBuilder(Context context) {
mContext = context;
mBuilder = new AlertDialog.Builder(context);
}
DialogBuilder setTitle(String title) {
mTitle = title;
return this;
}
DialogBuilder setSubTitle1(String subTitle1) {
mSubTitle1 = subTitle1;
return this;
}
DialogBuilder setSubTitle2(String subTitle2) {
mSubTitle2 = subTitle2;
return this;
}
DialogBuilder setLeftButtonText(String text) {
mLeftButtonText = text;
return this;
}
DialogBuilder setLeftButtonOnClickListener(Consumer<AlertDialog> listener) {
mLeftButtonOnClickListener = listener;
return this;
}
DialogBuilder setRightButtonText(String text) {
mRightButtonText = text;
return this;
}
DialogBuilder setRightButtonOnClickListener(Consumer<AlertDialog> listener) {
mRightButtonOnClickListener = listener;
return this;
}
AlertDialog build() {
View rootView =
LayoutInflater.from(mContext)
.inflate(R.xml.bluetooth_audio_streams_dialog, /* parent= */ null);
AlertDialog dialog = mBuilder.setView(rootView).setCancelable(false).create();
dialog.setCanceledOnTouchOutside(false);
TextView title = rootView.requireViewById(R.id.dialog_title);
title.setText(mTitle);
if (!Strings.isNullOrEmpty(mSubTitle1)) {
TextView subTitle1 = rootView.requireViewById(R.id.dialog_subtitle);
subTitle1.setText(mSubTitle1);
subTitle1.setVisibility(View.VISIBLE);
}
if (!Strings.isNullOrEmpty(mSubTitle2)) {
TextView subTitle2 = rootView.requireViewById(R.id.dialog_subtitle_2);
subTitle2.setText(mSubTitle2);
subTitle2.setVisibility(View.VISIBLE);
}
if (!Strings.isNullOrEmpty(mLeftButtonText)) {
Button leftButton = rootView.requireViewById(R.id.left_button);
leftButton.setText(mLeftButtonText);
leftButton.setVisibility(View.VISIBLE);
leftButton.setOnClickListener(unused -> mLeftButtonOnClickListener.accept(dialog));
}
if (!Strings.isNullOrEmpty(mRightButtonText)) {
Button rightButton = rootView.requireViewById(R.id.right_button);
rightButton.setText(mRightButtonText);
rightButton.setVisibility(View.VISIBLE);
rightButton.setOnClickListener(
unused -> mRightButtonOnClickListener.accept(dialog));
}
return dialog;
}
}
}

View File

@@ -1,166 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import static java.util.Collections.emptyList;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.util.Log;
import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.utils.ThreadUtils;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nullable;
/**
* A helper class that adds, removes and retrieves LE broadcast sources for all active sink devices.
*/
class AudioStreamsHelper {
private static final String TAG = "AudioStreamsHelper";
private static final boolean DEBUG = BluetoothUtils.D;
private final @Nullable LocalBluetoothManager mBluetoothManager;
private final @Nullable LocalBluetoothLeBroadcastAssistant mLeBroadcastAssistant;
AudioStreamsHelper(@Nullable LocalBluetoothManager bluetoothManager) {
mBluetoothManager = bluetoothManager;
mLeBroadcastAssistant = getLeBroadcastAssistant(mBluetoothManager);
}
/**
* Adds the specified LE broadcast source to all active sinks.
*
* @param source The LE broadcast metadata representing the audio source.
*/
void addSource(BluetoothLeBroadcastMetadata source) {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "addSource(): LeBroadcastAssistant is null!");
return;
}
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
for (var sink : getActiveSinksOnAssistant(mBluetoothManager)) {
if (DEBUG) {
Log.d(
TAG,
"addSource(): join broadcast broadcastId"
+ " : "
+ source.getBroadcastId()
+ " sink : "
+ sink.getAddress());
}
mLeBroadcastAssistant.addSource(sink, source, false);
}
});
}
/** Removes sources from LE broadcasts associated for all active sinks based on broadcast Id. */
void removeSource(int broadcastId) {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "removeSource(): LeBroadcastAssistant is null!");
return;
}
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
for (var sink : getActiveSinksOnAssistant(mBluetoothManager)) {
if (DEBUG) {
Log.d(
TAG,
"removeSource(): remove all sources with broadcast id :"
+ broadcastId
+ " from sink : "
+ sink.getAddress());
}
mLeBroadcastAssistant.getAllSources(sink).stream()
.filter(state -> state.getBroadcastId() == broadcastId)
.forEach(
state ->
mLeBroadcastAssistant.removeSource(
sink, state.getSourceId()));
}
});
}
/** Retrieves a list of all LE broadcast receive states from active sinks. */
List<BluetoothLeBroadcastReceiveState> getAllConnectedSources() {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "getAllSources(): LeBroadcastAssistant is null!");
return emptyList();
}
return getActiveSinksOnAssistant(mBluetoothManager).stream()
.flatMap(sink -> mLeBroadcastAssistant.getAllSources(sink).stream())
.filter(this::isConnected)
.toList();
}
@Nullable
LocalBluetoothLeBroadcastAssistant getLeBroadcastAssistant() {
return mLeBroadcastAssistant;
}
boolean isConnected(BluetoothLeBroadcastReceiveState state) {
return state.getPaSyncState() == BluetoothLeBroadcastReceiveState.PA_SYNC_STATE_SYNCHRONIZED
&& state.getBigEncryptionState()
== BluetoothLeBroadcastReceiveState.BIG_ENCRYPTION_STATE_DECRYPTING;
}
private static List<BluetoothDevice> getActiveSinksOnAssistant(
@Nullable LocalBluetoothManager manager) {
if (manager == null) {
Log.w(TAG, "getActiveSinksOnAssistant(): LocalBluetoothManager is null!");
return emptyList();
}
return AudioSharingUtils.getActiveSinkOnAssistant(manager)
.map(
cachedBluetoothDevice ->
Stream.concat(
Stream.of(cachedBluetoothDevice.getDevice()),
cachedBluetoothDevice.getMemberDevice().stream()
.map(CachedBluetoothDevice::getDevice))
.toList())
.orElse(emptyList());
}
private static @Nullable LocalBluetoothLeBroadcastAssistant getLeBroadcastAssistant(
@Nullable LocalBluetoothManager manager) {
if (manager == null) {
Log.w(TAG, "getLeBroadcastAssistant(): LocalBluetoothManager is null!");
return null;
}
LocalBluetoothProfileManager profileManager = manager.getProfileManager();
if (profileManager == null) {
Log.w(TAG, "getLeBroadcastAssistant(): LocalBluetoothProfileManager is null!");
return null;
}
return profileManager.getLeAudioBroadcastAssistantProfile();
}
}

View File

@@ -1,117 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.util.Log;
import java.util.Locale;
public class AudioStreamsProgressCategoryCallback extends AudioStreamsBroadcastAssistantCallback {
private static final String TAG = "AudioStreamsProgressCategoryCallback";
private final AudioStreamsProgressCategoryController mCategoryController;
public AudioStreamsProgressCategoryCallback(
AudioStreamsProgressCategoryController audioStreamsProgressCategoryController) {
mCategoryController = audioStreamsProgressCategoryController;
}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink, int sourceId, BluetoothLeBroadcastReceiveState state) {
super.onReceiveStateChanged(sink, sourceId, state);
mCategoryController.handleSourceConnected(state);
}
@Override
public void onSearchStartFailed(int reason) {
super.onSearchStartFailed(reason);
mCategoryController.showToast(
String.format(Locale.US, "Failed to start scanning, reason %d", reason));
}
@Override
public void onSearchStarted(int reason) {
super.onSearchStarted(reason);
if (mCategoryController == null) {
Log.w(TAG, "onSearchStarted() : mCategoryController is null!");
return;
}
mCategoryController.setScanning(true);
}
@Override
public void onSearchStopFailed(int reason) {
super.onSearchStopFailed(reason);
mCategoryController.showToast(
String.format(Locale.US, "Failed to stop scanning, reason %d", reason));
}
@Override
public void onSearchStopped(int reason) {
super.onSearchStopped(reason);
if (mCategoryController == null) {
Log.w(TAG, "onSearchStopped() : mCategoryController is null!");
return;
}
mCategoryController.setScanning(false);
}
@Override
public void onSourceAddFailed(
BluetoothDevice sink, BluetoothLeBroadcastMetadata source, int reason) {
super.onSourceAddFailed(sink, source, reason);
mCategoryController.showToast(
String.format(Locale.US, "Failed to join broadcast, reason %d", reason));
}
@Override
public void onSourceFound(BluetoothLeBroadcastMetadata source) {
super.onSourceFound(source);
if (mCategoryController == null) {
Log.w(TAG, "onSourceFound() : mCategoryController is null!");
return;
}
mCategoryController.handleSourceFound(source);
}
@Override
public void onSourceLost(int broadcastId) {
super.onSourceLost(broadcastId);
mCategoryController.handleSourceLost(broadcastId);
}
@Override
public void onSourceRemoveFailed(BluetoothDevice sink, int sourceId, int reason) {
super.onSourceRemoveFailed(sink, sourceId, reason);
mCategoryController.showToast(
String.format(
Locale.US,
"Failed to remove source %d for sink %s",
sourceId,
sink.getAddress()));
}
@Override
public void onSourceRemoved(BluetoothDevice sink, int sourceId, int reason) {
super.onSourceRemoved(sink, sourceId, reason);
mCategoryController.handleSourceRemoved();
}
}

View File

@@ -1,641 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import static com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsScanQrCodeController.REQUEST_SCAN_BT_BROADCAST_QR_CODE;
import static java.util.Collections.emptyList;
import android.app.AlertDialog;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
import com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode.QrCodeScanModeActivity;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.SubSettingLauncher;
import com.android.settingslib.bluetooth.BluetoothBroadcastUtils;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.annotation.Nullable;
public class AudioStreamsProgressCategoryController extends BasePreferenceController
implements DefaultLifecycleObserver {
private static final String TAG = "AudioStreamsProgressCategoryController";
private static final boolean DEBUG = BluetoothUtils.D;
private final BluetoothCallback mBluetoothCallback =
new BluetoothCallback() {
@Override
public void onActiveDeviceChanged(
@Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
if (bluetoothProfile == BluetoothProfile.LE_AUDIO) {
mExecutor.execute(() -> init(activeDevice != null));
}
}
};
private final Preference.OnPreferenceClickListener mAddSourceOrShowDialog =
preference -> {
var p = (AudioStreamPreference) preference;
if (DEBUG) {
Log.d(
TAG,
"preferenceClicked(): attempt to join broadcast id : "
+ p.getAudioStreamBroadcastId());
}
var source = p.getAudioStreamMetadata();
if (source != null) {
if (source.isEncrypted()) {
ThreadUtils.postOnMainThread(() -> launchPasswordDialog(source, p));
} else {
moveToState(p, AudioStreamState.ADD_SOURCE_WAIT_FOR_RESPONSE);
}
}
return true;
};
private final Preference.OnPreferenceClickListener mLaunchDetailFragment =
preference -> {
var p = (AudioStreamPreference) preference;
Bundle broadcast = new Bundle();
broadcast.putString(
AudioStreamDetailsFragment.BROADCAST_NAME_ARG, (String) p.getTitle());
broadcast.putInt(
AudioStreamDetailsFragment.BROADCAST_ID_ARG, p.getAudioStreamBroadcastId());
new SubSettingLauncher(mContext)
.setTitleText("Audio stream details")
.setDestination(AudioStreamDetailsFragment.class.getName())
// TODO(chelseahao): Add logging enum
.setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN)
.setArguments(broadcast)
.launch();
return true;
};
private final AudioStreamsRepository mAudioStreamsRepository =
AudioStreamsRepository.getInstance();
enum AudioStreamState {
UNKNOWN,
// When mTimedSourceFromQrCode is present and this source has not been synced.
WAIT_FOR_SYNC,
// When source has been synced but not added to any sink.
SYNCED,
// When addSource is called for this source and waiting for response.
ADD_SOURCE_WAIT_FOR_RESPONSE,
// Source is added to active sink.
SOURCE_ADDED,
}
private final Comparator<AudioStreamPreference> mComparator =
Comparator.<AudioStreamPreference, Boolean>comparing(
p ->
p.getAudioStreamState()
== AudioStreamsProgressCategoryController
.AudioStreamState.SOURCE_ADDED)
.thenComparingInt(AudioStreamPreference::getAudioStreamRssi)
.reversed();
private final Executor mExecutor;
private final AudioStreamsProgressCategoryCallback mBroadcastAssistantCallback;
private final AudioStreamsHelper mAudioStreamsHelper;
private final @Nullable LocalBluetoothLeBroadcastAssistant mLeBroadcastAssistant;
private final @Nullable LocalBluetoothManager mBluetoothManager;
private final ConcurrentHashMap<Integer, AudioStreamPreference> mBroadcastIdToPreferenceMap =
new ConcurrentHashMap<>();
private @Nullable TimedSourceFromQrCode mTimedSourceFromQrCode;
private AudioStreamsProgressCategoryPreference mCategoryPreference;
private AudioStreamsDashboardFragment mFragment;
public AudioStreamsProgressCategoryController(Context context, String preferenceKey) {
super(context, preferenceKey);
mExecutor = Executors.newSingleThreadExecutor();
mBluetoothManager = Utils.getLocalBtManager(mContext);
mAudioStreamsHelper = new AudioStreamsHelper(mBluetoothManager);
mLeBroadcastAssistant = mAudioStreamsHelper.getLeBroadcastAssistant();
mBroadcastAssistantCallback = new AudioStreamsProgressCategoryCallback(this);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mCategoryPreference = screen.findPreference(getPreferenceKey());
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mBluetoothManager != null) {
mBluetoothManager.getEventManager().registerCallback(mBluetoothCallback);
}
mExecutor.execute(
() -> {
boolean hasActive =
AudioSharingUtils.getActiveSinkOnAssistant(mBluetoothManager)
.isPresent();
init(hasActive);
});
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mBluetoothManager != null) {
mBluetoothManager.getEventManager().unregisterCallback(mBluetoothCallback);
}
mExecutor.execute(this::stopScanning);
}
void setFragment(AudioStreamsDashboardFragment fragment) {
mFragment = fragment;
}
void setSourceFromQrCode(BluetoothLeBroadcastMetadata source) {
mTimedSourceFromQrCode =
new TimedSourceFromQrCode(source, () -> handleSourceLost(source.getBroadcastId()));
}
void setScanning(boolean isScanning) {
ThreadUtils.postOnMainThread(
() -> {
if (mCategoryPreference != null) mCategoryPreference.setProgress(isScanning);
});
}
void handleSourceFound(BluetoothLeBroadcastMetadata source) {
var broadcastIdFound = source.getBroadcastId();
mBroadcastIdToPreferenceMap.compute(
broadcastIdFound,
(k, v) -> {
if (v == null) {
// No existing preference for this source founded, add one and set initial
// state to SYNCED.
return addNewPreference(source, AudioStreamState.SYNCED);
}
var fromState = v.getAudioStreamState();
if (fromState == AudioStreamState.WAIT_FOR_SYNC
&& mTimedSourceFromQrCode != null) {
var pendingSource = mTimedSourceFromQrCode.get();
if (pendingSource == null) {
Log.w(
TAG,
"handleSourceFound(): unexpected state with null pendingSource:"
+ fromState
+ " for broadcastId : "
+ broadcastIdFound);
v.setAudioStreamMetadata(source);
moveToState(v, AudioStreamState.SYNCED);
return v;
}
// A preference with source founded is existed from a QR code scan. As the
// source is now synced, we update the preference with pendingSource from QR
// code scan and add source with it (since it has the password).
v.setAudioStreamMetadata(pendingSource);
moveToState(v, AudioStreamState.ADD_SOURCE_WAIT_FOR_RESPONSE);
} else {
// A preference with source founded existed either because it's already
// connected (SOURCE_ADDED), or other unexpected reason. We update the
// preference with this source and won't change it's state.
v.setAudioStreamMetadata(source);
if (fromState != AudioStreamState.SOURCE_ADDED) {
Log.w(
TAG,
"handleSourceFound(): unexpected state : "
+ fromState
+ " for broadcastId : "
+ broadcastIdFound);
}
}
return v;
});
}
private void handleSourceFromQrCodeIfExists() {
if (mTimedSourceFromQrCode == null || mTimedSourceFromQrCode.get() == null) {
return;
}
var metadataFromQrCode = mTimedSourceFromQrCode.get();
mBroadcastIdToPreferenceMap.compute(
metadataFromQrCode.getBroadcastId(),
(k, v) -> {
if (v == null) {
// No existing preference for this source from the QR code scan, add one and
// set initial state to WAIT_FOR_SYNC.
return addNewPreference(metadataFromQrCode, AudioStreamState.WAIT_FOR_SYNC);
}
var fromState = v.getAudioStreamState();
if (fromState == AudioStreamState.SYNCED) {
// A preference with source from the QR code is existed because it has been
// founded during scanning, now we have the password, we can add source.
v.setAudioStreamMetadata(metadataFromQrCode);
moveToState(v, AudioStreamState.ADD_SOURCE_WAIT_FOR_RESPONSE);
} else {
v.setAudioStreamMetadata(metadataFromQrCode);
Log.w(
TAG,
"handleSourceFromQrCode(): unexpected state : "
+ fromState
+ " for broadcastId : "
+ metadataFromQrCode.getBroadcastId());
}
return v;
});
}
void handleSourceLost(int broadcastId) {
var toRemove = mBroadcastIdToPreferenceMap.remove(broadcastId);
if (toRemove != null) {
ThreadUtils.postOnMainThread(
() -> {
if (mCategoryPreference != null) {
mCategoryPreference.removePreference(toRemove);
}
});
}
mAudioStreamsHelper.removeSource(broadcastId);
}
void handleSourceRemoved() {
for (var entry : mBroadcastIdToPreferenceMap.entrySet()) {
var preference = entry.getValue();
// Look for preference has SOURCE_ADDED state, re-check if they are still connected. If
// not, means the source is removed from the sink, we move back the preference to SYNCED
// state.
if (preference.getAudioStreamState() == AudioStreamState.SOURCE_ADDED
&& mAudioStreamsHelper.getAllConnectedSources().stream()
.noneMatch(
connected ->
connected.getBroadcastId()
== preference.getAudioStreamBroadcastId())) {
ThreadUtils.postOnMainThread(
() -> {
var metadata = preference.getAudioStreamMetadata();
if (metadata != null) {
moveToState(preference, AudioStreamState.SYNCED);
} else {
handleSourceLost(preference.getAudioStreamBroadcastId());
}
});
return;
}
}
}
void handleSourceConnected(BluetoothLeBroadcastReceiveState receiveState) {
if (!mAudioStreamsHelper.isConnected(receiveState)) {
return;
}
var broadcastIdConnected = receiveState.getBroadcastId();
mBroadcastIdToPreferenceMap.compute(
broadcastIdConnected,
(k, v) -> {
if (v == null) {
// No existing preference for this source even if it's already connected,
// add one and set initial state to SOURCE_ADDED. This could happen because
// we retrieves the connected source during onStart() from
// AudioStreamsHelper#getAllConnectedSources() even before the source is
// founded by scanning.
return addNewPreference(receiveState, AudioStreamState.SOURCE_ADDED);
}
var fromState = v.getAudioStreamState();
if (fromState == AudioStreamState.ADD_SOURCE_WAIT_FOR_RESPONSE
|| fromState == AudioStreamState.SYNCED
|| fromState == AudioStreamState.WAIT_FOR_SYNC
|| fromState == AudioStreamState.SOURCE_ADDED) {
// Expected state, do nothing
} else {
Log.w(
TAG,
"handleSourceConnected(): unexpected state : "
+ fromState
+ " for broadcastId : "
+ broadcastIdConnected);
}
moveToState(v, AudioStreamState.SOURCE_ADDED);
return v;
});
}
void showToast(String msg) {
AudioSharingUtils.toastMessage(mContext, msg);
}
private void init(boolean hasActive) {
mBroadcastIdToPreferenceMap.clear();
ThreadUtils.postOnMainThread(
() -> {
if (mCategoryPreference != null) {
mCategoryPreference.removeAudioStreamPreferences();
mCategoryPreference.setVisible(hasActive);
}
});
if (hasActive) {
startScanning();
} else {
stopScanning();
ThreadUtils.postOnMainThread(
() -> AudioStreamsDialogFragment.show(mFragment, getNoLeDeviceDialog()));
}
}
private void startScanning() {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "startScanning(): LeBroadcastAssistant is null!");
return;
}
if (mLeBroadcastAssistant.isSearchInProgress()) {
showToast("Failed to start scanning, please try again.");
return;
}
if (DEBUG) {
Log.d(TAG, "startScanning()");
}
mLeBroadcastAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
// Handle QR code scan and display currently connected streams
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
handleSourceFromQrCodeIfExists();
mAudioStreamsHelper
.getAllConnectedSources()
.forEach(this::handleSourceConnected);
mLeBroadcastAssistant.startSearchingForSources(emptyList());
});
}
private void stopScanning() {
if (mLeBroadcastAssistant == null) {
Log.w(TAG, "stopScanning(): LeBroadcastAssistant is null!");
return;
}
if (mLeBroadcastAssistant.isSearchInProgress()) {
if (DEBUG) {
Log.d(TAG, "stopScanning()");
}
mLeBroadcastAssistant.stopSearchingForSources();
}
mLeBroadcastAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
if (mTimedSourceFromQrCode != null) {
mTimedSourceFromQrCode.cleanup();
mTimedSourceFromQrCode = null;
}
}
private AudioStreamPreference addNewPreference(
BluetoothLeBroadcastReceiveState receiveState, AudioStreamState state) {
var preference = AudioStreamPreference.fromReceiveState(mContext, receiveState);
moveToState(preference, state);
return preference;
}
private AudioStreamPreference addNewPreference(
BluetoothLeBroadcastMetadata metadata, AudioStreamState state) {
var preference = AudioStreamPreference.fromMetadata(mContext, metadata);
moveToState(preference, state);
return preference;
}
private void moveToState(AudioStreamPreference preference, AudioStreamState state) {
if (preference.getAudioStreamState() == state) {
return;
}
preference.setAudioStreamState(state);
// Perform action according to the new state
if (state == AudioStreamState.ADD_SOURCE_WAIT_FOR_RESPONSE) {
if (mTimedSourceFromQrCode != null) {
mTimedSourceFromQrCode.consumed(preference.getAudioStreamBroadcastId());
}
var metadata = preference.getAudioStreamMetadata();
if (metadata != null) {
mAudioStreamsHelper.addSource(metadata);
// Cache the metadata that used for add source, if source is added successfully, we
// will save it persistently.
mAudioStreamsRepository.cacheMetadata(metadata);
}
} else if (state == AudioStreamState.SOURCE_ADDED) {
if (mTimedSourceFromQrCode != null) {
mTimedSourceFromQrCode.consumed(preference.getAudioStreamBroadcastId());
}
// Saved connected metadata for user to re-join this broadcast later.
var cached =
mAudioStreamsRepository.getCachedMetadata(
preference.getAudioStreamBroadcastId());
if (cached != null) {
mAudioStreamsRepository.saveMetadata(mContext, cached);
}
} else if (state == AudioStreamState.WAIT_FOR_SYNC) {
if (mTimedSourceFromQrCode != null) {
mTimedSourceFromQrCode.waitForConsume();
}
}
// Get preference click listener according to the new state
Preference.OnPreferenceClickListener listener;
if (state == AudioStreamState.SYNCED) {
listener = mAddSourceOrShowDialog;
} else if (state == AudioStreamState.SOURCE_ADDED) {
listener = mLaunchDetailFragment;
} else {
listener = null;
}
// Get preference summary according to the new state
String summary;
if (state == AudioStreamState.WAIT_FOR_SYNC) {
summary = "Scanning...";
} else if (state == AudioStreamState.ADD_SOURCE_WAIT_FOR_RESPONSE) {
summary = "Connecting...";
} else if (state == AudioStreamState.SOURCE_ADDED) {
summary = "Listening now";
} else {
summary = "";
}
// Update UI
ThreadUtils.postOnMainThread(
() -> {
preference.setIsConnected(
state == AudioStreamState.SOURCE_ADDED, summary, listener);
if (mCategoryPreference != null) {
mCategoryPreference.addAudioStreamPreference(preference, mComparator);
}
});
}
private void launchPasswordDialog(
BluetoothLeBroadcastMetadata source, AudioStreamPreference preference) {
View layout =
LayoutInflater.from(mContext)
.inflate(R.layout.bluetooth_find_broadcast_password_dialog, null);
((TextView) layout.requireViewById(R.id.broadcast_name_text))
.setText(preference.getTitle());
AlertDialog alertDialog =
new AlertDialog.Builder(mContext)
.setTitle(R.string.find_broadcast_password_dialog_title)
.setView(layout)
.setNeutralButton(android.R.string.cancel, null)
.setPositiveButton(
R.string.bluetooth_connect_access_dialog_positive,
(dialog, which) -> {
var code =
((EditText)
layout.requireViewById(
R.id.broadcast_edit_text))
.getText()
.toString();
var metadata =
new BluetoothLeBroadcastMetadata.Builder(source)
.setBroadcastCode(
code.getBytes(StandardCharsets.UTF_8))
.build();
// Update the metadata after user entered the password
preference.setAudioStreamMetadata(metadata);
moveToState(
preference,
AudioStreamState.ADD_SOURCE_WAIT_FOR_RESPONSE);
})
.create();
alertDialog.show();
}
private AudioStreamsDialogFragment.DialogBuilder getNoLeDeviceDialog() {
return new AudioStreamsDialogFragment.DialogBuilder(mContext)
.setTitle("Connect compatible headphones")
.setSubTitle2(
"To listen to an audio stream, first connect headphones that support LE"
+ " Audio to this device. Learn more")
.setLeftButtonText("Close")
.setLeftButtonOnClickListener(AlertDialog::dismiss)
.setRightButtonText("Connect a device")
.setRightButtonOnClickListener(
dialog -> {
mContext.startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
dialog.dismiss();
});
}
private AudioStreamsDialogFragment.DialogBuilder getBroadcastUnavailableDialog(
String broadcastName) {
return new AudioStreamsDialogFragment.DialogBuilder(mContext)
.setTitle("Audio stream isn't available")
.setSubTitle1(broadcastName)
.setSubTitle2("This audio stream isn't playing anything right now")
.setLeftButtonText("Close")
.setLeftButtonOnClickListener(AlertDialog::dismiss)
.setRightButtonText("Retry")
.setRightButtonOnClickListener(
dialog -> {
if (mFragment != null) {
Intent intent = new Intent(mContext, QrCodeScanModeActivity.class);
intent.setAction(
BluetoothBroadcastUtils
.ACTION_BLUETOOTH_LE_AUDIO_QR_CODE_SCANNER);
mFragment.startActivityForResult(
intent, REQUEST_SCAN_BT_BROADCAST_QR_CODE);
dialog.dismiss();
}
});
}
private class TimedSourceFromQrCode {
private static final int WAIT_FOR_SYNC_TIMEOUT_MILLIS = 15000;
private final CountDownTimer mTimer;
private BluetoothLeBroadcastMetadata mSourceFromQrCode;
private TimedSourceFromQrCode(
BluetoothLeBroadcastMetadata sourceFromQrCode, Runnable timeoutAction) {
mSourceFromQrCode = sourceFromQrCode;
mTimer =
new CountDownTimer(WAIT_FOR_SYNC_TIMEOUT_MILLIS, 1000) {
@Override
public void onTick(long millisUntilFinished) {}
@Override
public void onFinish() {
timeoutAction.run();
ThreadUtils.postOnMainThread(
() ->
AudioStreamsDialogFragment.show(
mFragment,
getBroadcastUnavailableDialog(
sourceFromQrCode.getBroadcastName())));
}
};
}
private void waitForConsume() {
mTimer.start();
}
private void cleanup() {
mTimer.cancel();
mSourceFromQrCode = null;
}
private void consumed(int broadcastId) {
if (mSourceFromQrCode == null || broadcastId != mSourceFromQrCode.getBroadcastId()) {
return;
}
cleanup();
}
private BluetoothLeBroadcastMetadata get() {
return mSourceFromQrCode;
}
}
}

View File

@@ -1,89 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import com.android.settings.ProgressCategory;
import com.android.settings.R;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class AudioStreamsProgressCategoryPreference extends ProgressCategory {
public AudioStreamsProgressCategoryPreference(Context context) {
super(context);
init();
}
public AudioStreamsProgressCategoryPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public AudioStreamsProgressCategoryPreference(
Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public AudioStreamsProgressCategoryPreference(
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
void addAudioStreamPreference(
@NonNull AudioStreamPreference preference,
Comparator<AudioStreamPreference> comparator) {
super.addPreference(preference);
List<AudioStreamPreference> preferences = getAllAudioStreamPreferences();
preferences.sort(comparator);
for (int i = 0; i < preferences.size(); i++) {
// setOrder to i + 1, since the order 0 preference should always be the
// "audio_streams_scan_qr_code"
preferences.get(i).setOrder(i + 1);
}
}
void removeAudioStreamPreferences() {
List<AudioStreamPreference> streams = getAllAudioStreamPreferences();
for (var toRemove : streams) {
removePreference(toRemove);
}
}
private List<AudioStreamPreference> getAllAudioStreamPreferences() {
List<AudioStreamPreference> streams = new ArrayList<>();
for (int i = 0; i < getPreferenceCount(); i++) {
if (getPreference(i) instanceof AudioStreamPreference) {
streams.add((AudioStreamPreference) getPreference(i));
}
}
return streams;
}
private void init() {
setEmptyTextRes(R.string.audio_streams_empty);
}
}

View File

@@ -1,124 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.android.settings.R;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.InstrumentedFragment;
import com.android.settingslib.bluetooth.BluetoothLeBroadcastMetadataExt;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.qrcode.QrCodeGenerator;
import com.google.zxing.WriterException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
public class AudioStreamsQrCodeFragment extends InstrumentedFragment {
private static final String TAG = "AudioStreamsQrCodeFragment";
@Override
public int getMetricsCategory() {
// TODO(chelseahao): update metrics id
return 0;
}
@Override
public final View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.xml.bluetooth_audio_streams_qr_code, container, false);
BluetoothLeBroadcastMetadata broadcastMetadata = getBroadcastMetadata();
if (broadcastMetadata != null) {
getQrCodeBitmap(broadcastMetadata)
.ifPresent(
bm -> {
((ImageView) view.requireViewById(R.id.qrcode_view))
.setImageBitmap(bm);
((TextView) view.requireViewById(R.id.password))
.setText(
"Password: "
+ new String(
broadcastMetadata
.getBroadcastCode(),
StandardCharsets.UTF_8));
});
}
return view;
}
private Optional<Bitmap> getQrCodeBitmap(@Nullable BluetoothLeBroadcastMetadata metadata) {
if (metadata == null) {
Log.d(TAG, "onCreateView: broadcastMetadata is empty!");
return Optional.empty();
}
String metadataStr = BluetoothLeBroadcastMetadataExt.INSTANCE.toQrCodeString(metadata);
if (metadataStr.isEmpty()) {
Log.d(TAG, "onCreateView: metadataStr is empty!");
return Optional.empty();
}
Log.d("chelsea", metadataStr);
try {
int qrcodeSize = getContext().getResources().getDimensionPixelSize(R.dimen.qrcode_size);
Bitmap bitmap = QrCodeGenerator.encodeQrCode(metadataStr, qrcodeSize);
return Optional.of(bitmap);
} catch (WriterException e) {
Log.d(
TAG,
"onCreateView: broadcastMetadata "
+ metadata
+ " qrCode generation exception "
+ e);
}
return Optional.empty();
}
@Nullable
private BluetoothLeBroadcastMetadata getBroadcastMetadata() {
LocalBluetoothLeBroadcast localBluetoothLeBroadcast =
Utils.getLocalBtManager(getActivity())
.getProfileManager()
.getLeAudioBroadcastProfile();
if (localBluetoothLeBroadcast == null) {
Log.d(TAG, "getBroadcastMetadataQrCode: localBluetoothLeBroadcast is null!");
return null;
}
BluetoothLeBroadcastMetadata metadata =
localBluetoothLeBroadcast.getLatestBluetoothLeBroadcastMetadata();
if (metadata == null) {
Log.d(TAG, "getBroadcastMetadataQrCode: metadata is null!");
return null;
}
return metadata;
}
}

View File

@@ -1,160 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.android.settingslib.bluetooth.BluetoothLeBroadcastMetadataExt;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.utils.ThreadUtils;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;
/** Manages the caching and storage of Bluetooth audio stream metadata. */
public class AudioStreamsRepository {
private static final String TAG = "AudioStreamsRepository";
private static final boolean DEBUG = BluetoothUtils.D;
private static final String PREF_KEY = "bluetooth_audio_stream_pref";
private static final String METADATA_KEY = "bluetooth_audio_stream_metadata";
@Nullable
private static AudioStreamsRepository sInstance = null;
private AudioStreamsRepository() {}
/**
* Gets the single instance of AudioStreamsRepository.
*
* @return The AudioStreamsRepository instance.
*/
public static synchronized AudioStreamsRepository getInstance() {
if (sInstance == null) {
sInstance = new AudioStreamsRepository();
}
return sInstance;
}
private final ConcurrentHashMap<Integer, BluetoothLeBroadcastMetadata>
mBroadcastIdToMetadataCacheMap = new ConcurrentHashMap<>();
/**
* Caches BluetoothLeBroadcastMetadata in a local cache.
*
* @param metadata The BluetoothLeBroadcastMetadata to be cached.
*/
void cacheMetadata(BluetoothLeBroadcastMetadata metadata) {
if (DEBUG) {
Log.d(
TAG,
"cacheMetadata(): broadcastId "
+ metadata.getBroadcastId()
+ " saved in local cache.");
}
mBroadcastIdToMetadataCacheMap.put(metadata.getBroadcastId(), metadata);
}
/**
* Gets cached BluetoothLeBroadcastMetadata by broadcastId.
*
* @param broadcastId The broadcastId to look up in the cache.
* @return The cached BluetoothLeBroadcastMetadata or null if not found.
*/
@Nullable
BluetoothLeBroadcastMetadata getCachedMetadata(int broadcastId) {
var metadata = mBroadcastIdToMetadataCacheMap.get(broadcastId);
if (metadata == null) {
Log.w(
TAG,
"getCachedMetadata(): broadcastId not found in"
+ " mBroadcastIdToMetadataCacheMap.");
return null;
}
return metadata;
}
/**
* Saves metadata to SharedPreferences asynchronously.
*
* @param context The context.
* @param metadata The BluetoothLeBroadcastMetadata to be saved.
*/
void saveMetadata(Context context, BluetoothLeBroadcastMetadata metadata) {
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
SharedPreferences sharedPref =
context.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE);
if (sharedPref != null) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(
METADATA_KEY,
BluetoothLeBroadcastMetadataExt.INSTANCE.toQrCodeString(
metadata));
editor.apply();
if (DEBUG) {
Log.d(
TAG,
"saveMetadata(): broadcastId "
+ metadata.getBroadcastId()
+ " metadata saved in storage.");
}
}
});
}
/**
* Gets saved metadata from SharedPreferences.
*
* @param context The context.
* @param broadcastId The broadcastId to retrieve metadata for.
* @return The saved BluetoothLeBroadcastMetadata or null if not found.
*/
@Nullable
BluetoothLeBroadcastMetadata getSavedMetadata(Context context, int broadcastId) {
SharedPreferences sharedPref = context.getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE);
if (sharedPref != null) {
String savedMetadataStr = sharedPref.getString(METADATA_KEY, null);
if (savedMetadataStr == null) {
Log.w(TAG, "getSavedMetadata(): savedMetadataStr is null");
return null;
}
var savedMetadata =
BluetoothLeBroadcastMetadataExt.INSTANCE.convertToBroadcastMetadata(
savedMetadataStr);
if (savedMetadata == null || savedMetadata.getBroadcastId() != broadcastId) {
Log.w(TAG, "getSavedMetadata(): savedMetadata doesn't match broadcast Id.");
return null;
}
if (DEBUG) {
Log.d(
TAG,
"getSavedMetadata(): broadcastId "
+ savedMetadata.getBroadcastId()
+ " metadata found in storage.");
}
return savedMetadata;
}
return null;
}
}

View File

@@ -1,132 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
import com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode.QrCodeScanModeActivity;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.bluetooth.BluetoothBroadcastUtils;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
public class AudioStreamsScanQrCodeController extends BasePreferenceController
implements DefaultLifecycleObserver {
static final int REQUEST_SCAN_BT_BROADCAST_QR_CODE = 0;
private static final String TAG = "AudioStreamsProgressCategoryController";
private static final boolean DEBUG = BluetoothUtils.D;
private static final String KEY = "audio_streams_scan_qr_code";
private final BluetoothCallback mBluetoothCallback =
new BluetoothCallback() {
@Override
public void onActiveDeviceChanged(
@Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
if (bluetoothProfile == BluetoothProfile.LE_AUDIO) {
updateVisibility();
}
}
};
private final LocalBluetoothManager mLocalBtManager;
private AudioStreamsDashboardFragment mFragment;
private Preference mPreference;
public AudioStreamsScanQrCodeController(Context context, String preferenceKey) {
super(context, preferenceKey);
mLocalBtManager = Utils.getLocalBtManager(mContext);
}
public void setFragment(AudioStreamsDashboardFragment fragment) {
mFragment = fragment;
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().registerCallback(mBluetoothCallback);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().unregisterCallback(mBluetoothCallback);
}
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
if (mPreference == null) {
Log.w(TAG, "displayPreference() mPreference is null!");
return;
}
mPreference.setOnPreferenceClickListener(
preference -> {
if (mFragment == null) {
Log.w(TAG, "displayPreference() mFragment is null!");
return false;
}
if (preference.getKey().equals(KEY)) {
Intent intent = new Intent(mContext, QrCodeScanModeActivity.class);
intent.setAction(
BluetoothBroadcastUtils.ACTION_BLUETOOTH_LE_AUDIO_QR_CODE_SCANNER);
mFragment.startActivityForResult(intent, REQUEST_SCAN_BT_BROADCAST_QR_CODE);
if (DEBUG) {
Log.w(TAG, "displayPreference() sent intent : " + intent);
}
return true;
}
return false;
});
}
private void updateVisibility() {
ThreadUtils.postOnBackgroundThread(
() -> {
boolean hasActiveLe =
AudioSharingUtils.getActiveSinkOnAssistant(mLocalBtManager).isPresent();
ThreadUtils.postOnMainThread(() -> mPreference.setVisible(hasActiveLe));
});
}
}

View File

@@ -1,113 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.fragment.app.FragmentTransaction;
import com.android.settings.R;
import com.android.settingslib.bluetooth.BluetoothBroadcastUtils;
import com.android.settingslib.bluetooth.BluetoothUtils;
/**
* Finding a broadcast through QR code.
*
* <p>To use intent action {@link
* BluetoothBroadcastUtils#ACTION_BLUETOOTH_LE_AUDIO_QR_CODE_SCANNER}, specify the bluetooth device
* sink of the broadcast to be provisioned in {@link
* BluetoothBroadcastUtils#EXTRA_BLUETOOTH_DEVICE_SINK} and check the operation for all coordinated
* set members throughout one session or not by {@link
* BluetoothBroadcastUtils#EXTRA_BLUETOOTH_SINK_IS_GROUP}.
*/
public class QrCodeScanModeActivity extends QrCodeScanModeBaseActivity {
private static final boolean DEBUG = BluetoothUtils.D;
private static final String TAG = "QrCodeScanModeActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void handleIntent(Intent intent) {
String action = intent != null ? intent.getAction() : null;
if (DEBUG) {
Log.d(TAG, "handleIntent(), action = " + action);
}
if (action == null) {
finish();
return;
}
switch (action) {
case BluetoothBroadcastUtils.ACTION_BLUETOOTH_LE_AUDIO_QR_CODE_SCANNER:
showQrCodeScannerFragment(intent);
break;
default:
if (DEBUG) {
Log.e(TAG, "Launch with an invalid action");
}
finish();
}
}
protected void showQrCodeScannerFragment(Intent intent) {
if (intent == null) {
if (DEBUG) {
Log.d(TAG, "intent is null, can not get bluetooth information from intent.");
}
return;
}
if (DEBUG) {
Log.d(TAG, "showQrCodeScannerFragment");
}
if (DEBUG) {
Log.d(TAG, "get extra from intent");
}
QrCodeScanModeFragment fragment =
(QrCodeScanModeFragment)
mFragmentManager.findFragmentByTag(
BluetoothBroadcastUtils.TAG_FRAGMENT_QR_CODE_SCANNER);
if (fragment == null) {
fragment = new QrCodeScanModeFragment();
} else {
if (fragment.isVisible()) {
return;
}
// When the fragment in back stack but not on top of the stack, we can simply pop
// stack because current fragment transactions are arranged in an order
mFragmentManager.popBackStackImmediate();
return;
}
final FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(
R.id.fragment_container,
fragment,
BluetoothBroadcastUtils.TAG_FRAGMENT_QR_CODE_SCANNER);
fragmentTransaction.commit();
}
}

View File

@@ -1,64 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemProperties;
import androidx.fragment.app.FragmentManager;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.ObservableActivity;
import com.google.android.setupdesign.util.ThemeHelper;
import com.google.android.setupdesign.util.ThemeResolver;
public abstract class QrCodeScanModeBaseActivity extends ObservableActivity {
private static final String THEME_KEY = "setupwizard.theme";
private static final String THEME_DEFAULT_VALUE = "SudThemeGlifV3_DayNight";
protected FragmentManager mFragmentManager;
protected abstract void handleIntent(Intent intent);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int defaultTheme =
ThemeHelper.isSetupWizardDayNightEnabled(this)
? com.google.android.setupdesign.R.style.SudThemeGlifV3_DayNight
: com.google.android.setupdesign.R.style.SudThemeGlifV3_Light;
ThemeResolver themeResolver =
new ThemeResolver.Builder(ThemeResolver.getDefault())
.setDefaultTheme(defaultTheme)
.setUseDayNight(true)
.build();
setTheme(
themeResolver.resolve(
SystemProperties.get(THEME_KEY, THEME_DEFAULT_VALUE),
/* suppressDayNight= */ !ThemeHelper.isSetupWizardDayNightEnabled(this)));
setContentView(R.layout.qrcode_scan_mode_activity);
mFragmentManager = getSupportFragmentManager();
if (savedInstanceState == null) {
handleIntent(getIntent());
}
}
}

View File

@@ -1,269 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing.audiostreams.qrcode;
import android.app.Activity;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.graphics.Matrix;
import android.graphics.Outline;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;
import android.util.Size;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.accessibility.AccessibilityEvent;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import com.android.settings.R;
import com.android.settings.core.InstrumentedFragment;
import com.android.settingslib.bluetooth.BluetoothBroadcastUtils;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.qrcode.QrCamera;
import java.time.Duration;
public class QrCodeScanModeFragment extends InstrumentedFragment
implements TextureView.SurfaceTextureListener, QrCamera.ScannerCallback {
private static final boolean DEBUG = BluetoothUtils.D;
private static final String TAG = "QrCodeScanModeFragment";
/** Message sent to hide error message */
private static final int MESSAGE_HIDE_ERROR_MESSAGE = 1;
/** Message sent to show error message */
private static final int MESSAGE_SHOW_ERROR_MESSAGE = 2;
/** Message sent to broadcast QR code */
private static final int MESSAGE_SCAN_BROADCAST_SUCCESS = 3;
private static final long SHOW_ERROR_MESSAGE_INTERVAL = 10000;
private static final long SHOW_SUCCESS_SQUARE_INTERVAL = 1000;
private static final Duration VIBRATE_DURATION_QR_CODE_RECOGNITION = Duration.ofMillis(3);
public static final String KEY_BROADCAST_METADATA = "key_broadcast_metadata";
private int mCornerRadius;
private String mBroadcastMetadata;
private Context mContext;
private QrCamera mCamera;
private TextureView mTextureView;
private TextView mSummary;
private TextView mErrorMessage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getContext();
}
@Override
public final View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(
R.layout.qrcode_scanner_fragment, container, /* attachToRoot */ false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mTextureView = view.findViewById(R.id.preview_view);
mCornerRadius =
mContext.getResources().getDimensionPixelSize(R.dimen.qrcode_preview_radius);
mTextureView.setSurfaceTextureListener(this);
mTextureView.setOutlineProvider(
new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(
0, 0, view.getWidth(), view.getHeight(), mCornerRadius);
}
});
mTextureView.setClipToOutline(true);
mErrorMessage = view.findViewById(R.id.error_message);
}
private void initCamera(SurfaceTexture surface) {
// Check if the camera has already created.
if (mCamera == null) {
mCamera = new QrCamera(mContext, this);
mCamera.start(surface);
}
}
private void destroyCamera() {
if (mCamera != null) {
mCamera.stop();
mCamera = null;
}
}
@Override
public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
initCamera(surface);
}
@Override
public void onSurfaceTextureSizeChanged(
@NonNull SurfaceTexture surface, int width, int height) {}
@Override
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
destroyCamera();
return true;
}
@Override
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {}
@Override
public void handleSuccessfulResult(String qrCode) {
if (DEBUG) {
Log.d(TAG, "handleSuccessfulResult(), get the qr code string.");
}
mBroadcastMetadata = qrCode;
handleBtLeAudioScanner();
}
@Override
public void handleCameraFailure() {
destroyCamera();
}
@Override
public Size getViewSize() {
return new Size(mTextureView.getWidth(), mTextureView.getHeight());
}
@Override
public Rect getFramePosition(Size previewSize, int cameraOrientation) {
return new Rect(0, 0, previewSize.getHeight(), previewSize.getHeight());
}
@Override
public void setTransform(Matrix transform) {
mTextureView.setTransform(transform);
}
@Override
public boolean isValid(String qrCode) {
if (qrCode.startsWith(BluetoothBroadcastUtils.SCHEME_BT_BROADCAST_METADATA)) {
return true;
} else {
showErrorMessage(R.string.bt_le_audio_qr_code_is_not_valid_format);
return false;
}
}
protected boolean isDecodeTaskAlive() {
return mCamera != null && mCamera.isDecodeTaskAlive();
}
private final Handler mHandler =
new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_HIDE_ERROR_MESSAGE:
mErrorMessage.setVisibility(View.INVISIBLE);
break;
case MESSAGE_SHOW_ERROR_MESSAGE:
final String errorMessage = (String) msg.obj;
mErrorMessage.setVisibility(View.VISIBLE);
mErrorMessage.setText(errorMessage);
mErrorMessage.sendAccessibilityEvent(
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
// Cancel any pending messages to hide error view and requeue the
// message so
// user has time to see error
removeMessages(MESSAGE_HIDE_ERROR_MESSAGE);
sendEmptyMessageDelayed(
MESSAGE_HIDE_ERROR_MESSAGE, SHOW_ERROR_MESSAGE_INTERVAL);
break;
case MESSAGE_SCAN_BROADCAST_SUCCESS:
Log.d(TAG, "scan success");
final Intent resultIntent = new Intent();
resultIntent.putExtra(KEY_BROADCAST_METADATA, mBroadcastMetadata);
getActivity().setResult(Activity.RESULT_OK, resultIntent);
notifyUserForQrCodeRecognition();
break;
default:
}
}
};
private void notifyUserForQrCodeRecognition() {
if (mCamera != null) {
mCamera.stop();
}
mErrorMessage.setVisibility(View.INVISIBLE);
mTextureView.setVisibility(View.INVISIBLE);
triggerVibrationForQrCodeRecognition(getContext());
getActivity().finish();
}
private static void triggerVibrationForQrCodeRecognition(Context context) {
Vibrator vibrator = context.getSystemService(Vibrator.class);
if (vibrator == null) {
return;
}
vibrator.vibrate(
VibrationEffect.createOneShot(
VIBRATE_DURATION_QR_CODE_RECOGNITION.toMillis(),
VibrationEffect.DEFAULT_AMPLITUDE));
}
private void showErrorMessage(@StringRes int messageResId) {
final Message message =
mHandler.obtainMessage(MESSAGE_SHOW_ERROR_MESSAGE, getString(messageResId));
message.sendToTarget();
}
private void handleBtLeAudioScanner() {
Message message = mHandler.obtainMessage(MESSAGE_SCAN_BROADCAST_SUCCESS);
mHandler.sendMessageDelayed(message, SHOW_SUCCESS_SQUARE_INTERVAL);
}
private void updateSummary() {
mSummary.setText(getString(R.string.bt_le_audio_scan_qr_code_scanner));
}
@Override
public int getMetricsCategory() {
return SettingsEnums.LE_AUDIO_BROADCAST_SCAN_QR_CODE;
}
}

View File

@@ -86,7 +86,6 @@ import com.android.settings.connecteddevice.AdvancedConnectedDeviceDashboardFrag
import com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment; import com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment;
import com.android.settings.connecteddevice.NfcAndPaymentFragment; import com.android.settings.connecteddevice.NfcAndPaymentFragment;
import com.android.settings.connecteddevice.PreviouslyConnectedDeviceDashboardFragment; import com.android.settings.connecteddevice.PreviouslyConnectedDeviceDashboardFragment;
import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamConfirmDialog;
import com.android.settings.connecteddevice.stylus.StylusUsiDetailsFragment; import com.android.settings.connecteddevice.stylus.StylusUsiDetailsFragment;
import com.android.settings.connecteddevice.usb.UsbDetailsFragment; import com.android.settings.connecteddevice.usb.UsbDetailsFragment;
import com.android.settings.datausage.DataSaverSummary; import com.android.settings.datausage.DataSaverSummary;
@@ -359,7 +358,6 @@ public class SettingsGateway {
DataUsageList.class.getName(), DataUsageList.class.getName(),
ToggleBackupSettingFragment.class.getName(), ToggleBackupSettingFragment.class.getName(),
PreviouslyConnectedDeviceDashboardFragment.class.getName(), PreviouslyConnectedDeviceDashboardFragment.class.getName(),
AudioStreamConfirmDialog.class.getName(),
BatterySaverScheduleSettings.class.getName(), BatterySaverScheduleSettings.class.getName(),
MobileNetworkListFragment.class.getName(), MobileNetworkListFragment.class.getName(),
PowerMenuSettings.class.getName(), PowerMenuSettings.class.getName(),

View File

@@ -73,9 +73,6 @@ public class ConnectedDeviceDashboardFragmentTest {
private static final String KEY_SAVED_DEVICE_SEE_ALL = "previously_connected_devices_see_all"; private static final String KEY_SAVED_DEVICE_SEE_ALL = "previously_connected_devices_see_all";
private static final String KEY_FAST_PAIR_DEVICE_SEE_ALL = "fast_pair_devices_see_all"; private static final String KEY_FAST_PAIR_DEVICE_SEE_ALL = "fast_pair_devices_see_all";
private static final String KEY_ADD_BT_DEVICES = "add_bt_devices"; private static final String KEY_ADD_BT_DEVICES = "add_bt_devices";
private static final String KEY_AUDIO_SHARING_DEVICE_LIST = "audio_sharing_device_list";
private static final String KEY_AUDIO_SHARING_SETTINGS =
"connected_device_audio_sharing_settings";
private static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
private static final String SYSTEMUI_PACKAGE_NAME = "com.android.systemui"; private static final String SYSTEMUI_PACKAGE_NAME = "com.android.systemui";
private static final String SLICE_ACTION = "com.android.settings.SEARCH_RESULT_TRAMPOLINE"; private static final String SLICE_ACTION = "com.android.settings.SEARCH_RESULT_TRAMPOLINE";
@@ -138,9 +135,7 @@ public class ConnectedDeviceDashboardFragmentTest {
KEY_NEARBY_DEVICES, KEY_NEARBY_DEVICES,
KEY_DISCOVERABLE_FOOTER, KEY_DISCOVERABLE_FOOTER,
KEY_SAVED_DEVICE_SEE_ALL, KEY_SAVED_DEVICE_SEE_ALL,
KEY_FAST_PAIR_DEVICE_SEE_ALL, KEY_FAST_PAIR_DEVICE_SEE_ALL);
KEY_AUDIO_SHARING_DEVICE_LIST,
KEY_AUDIO_SHARING_SETTINGS);
} }
@Test @Test

View File

@@ -1,232 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothStatusCodes;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
@RunWith(RobolectricTestRunner.class)
@Config(
shadows = {
ShadowAlertDialogCompat.class,
ShadowBluetoothAdapter.class,
})
public class AudioSharingDialogFragmentTest {
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2";
private static final String TEST_DEVICE_NAME3 = "test3";
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM1 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME1, /* groupId= */ 1, /* isActive= */ false);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM2 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME2, /* groupId= */ 2, /* isActive= */ false);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM3 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME3, /* groupId= */ 3, /* isActive= */ false);
private Fragment mParent;
private AudioSharingDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before
public void setUp() {
ShadowAlertDialogCompat.reset();
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mFragment = new AudioSharingDialogFragment();
mParent = new Fragment();
FragmentController.setupFragment(
mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null);
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() {
mFragment.show(mParent, new ArrayList<>(), (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_noConnectedDevice() {
mFragment.show(mParent, new ArrayList<>(), (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1);
ImageView guidance = rootView.findViewById(R.id.share_audio_guidance);
Button shareBtn = rootView.findViewById(R.id.share_btn);
assertThat(dialog.isShowing()).isTrue();
assertThat(subtitle1.getVisibility()).isEqualTo(View.GONE);
assertThat(guidance.getVisibility()).isEqualTo(View.VISIBLE);
assertThat(shareBtn.getVisibility()).isEqualTo(View.GONE);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_noConnectedDevice_dialogDismiss() {
mFragment.show(mParent, new ArrayList<>(), (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
dialog.findViewById(android.R.id.button2).performClick();
shadowMainLooper().idle();
assertThat(dialog.isShowing()).isFalse();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_singleConnectedDevice() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
mFragment.show(mParent, list, (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1);
ImageView guidance = rootView.findViewById(R.id.share_audio_guidance);
Button shareBtn = rootView.findViewById(R.id.share_btn);
assertThat(dialog.isShowing()).isTrue();
assertThat(subtitle1.getVisibility()).isEqualTo(View.VISIBLE);
assertThat(subtitle1.getText().toString()).isEqualTo(TEST_DEVICE_NAME1);
assertThat(guidance.getVisibility()).isEqualTo(View.GONE);
assertThat(shareBtn.getVisibility()).isEqualTo(View.VISIBLE);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_singleConnectedDevice_dialogDismiss() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
mFragment.show(mParent, list, (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
rootView.findViewById(R.id.cancel_btn).performClick();
assertThat(dialog.isShowing()).isFalse();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_singleConnectedDevice_shareClicked() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
AtomicBoolean isShareBtnClicked = new AtomicBoolean(false);
mFragment.show(
mParent,
list,
(item) -> {
isShareBtnClicked.set(true);
});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
rootView.findViewById(R.id.share_btn).performClick();
assertThat(dialog.isShowing()).isFalse();
assertThat(isShareBtnClicked.get()).isTrue();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_multipleConnectedDevice() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
list.add(TEST_DEVICE_ITEM2);
list.add(TEST_DEVICE_ITEM3);
mFragment.show(mParent, list, (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
TextView subtitle1 = rootView.findViewById(R.id.share_audio_subtitle1);
ImageView guidance = rootView.findViewById(R.id.share_audio_guidance);
Button shareBtn = rootView.findViewById(R.id.share_btn);
RecyclerView recyclerView = rootView.findViewById(R.id.btn_list);
assertThat(dialog.isShowing()).isTrue();
assertThat(subtitle1.getVisibility()).isEqualTo(View.GONE);
assertThat(guidance.getVisibility()).isEqualTo(View.GONE);
assertThat(shareBtn.getVisibility()).isEqualTo(View.GONE);
assertThat(recyclerView.getVisibility()).isEqualTo(View.VISIBLE);
assertThat(recyclerView.getAdapter().getItemCount()).isEqualTo(3);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_multipleConnectedDevice_dialogDismiss() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
list.add(TEST_DEVICE_ITEM2);
list.add(TEST_DEVICE_ITEM3);
mFragment.show(mParent, list, (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
rootView.findViewById(R.id.cancel_btn).performClick();
assertThat(dialog.isShowing()).isFalse();
}
}

View File

@@ -1,224 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothStatusCodes;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
@RunWith(RobolectricTestRunner.class)
@Config(
shadows = {
ShadowAlertDialogCompat.class,
ShadowBluetoothAdapter.class,
})
public class AudioSharingDisconnectDialogFragmentTest {
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2";
private static final String TEST_DEVICE_NAME3 = "test3";
private static final int TEST_GROUP_ID1 = 1;
private static final int TEST_GROUP_ID2 = 2;
private static final int TEST_GROUP_ID3 = 3;
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM1 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME1, TEST_GROUP_ID1, /* isActive= */ true);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM2 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME2, TEST_GROUP_ID2, /* isActive= */ false);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM3 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME3, TEST_GROUP_ID3, /* isActive= */ false);
@Mock private BluetoothDevice mDevice1;
@Mock private BluetoothDevice mDevice2;
@Mock private BluetoothDevice mDevice3;
@Mock private CachedBluetoothDevice mCachedDevice1;
@Mock private CachedBluetoothDevice mCachedDevice2;
@Mock private CachedBluetoothDevice mCachedDevice3;
private Fragment mParent;
private AudioSharingDisconnectDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private ArrayList<AudioSharingDeviceItem> mDeviceItems = new ArrayList<>();
@Before
public void setUp() {
AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
if (latestAlertDialog != null) {
latestAlertDialog.dismiss();
ShadowAlertDialogCompat.reset();
}
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
when(mCachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME1);
when(mCachedDevice1.getDevice()).thenReturn(mDevice1);
when(mCachedDevice1.getGroupId()).thenReturn(TEST_GROUP_ID1);
when(mCachedDevice2.getName()).thenReturn(TEST_DEVICE_NAME2);
when(mCachedDevice2.getDevice()).thenReturn(mDevice2);
when(mCachedDevice2.getGroupId()).thenReturn(TEST_GROUP_ID2);
when(mCachedDevice3.getName()).thenReturn(TEST_DEVICE_NAME3);
when(mCachedDevice3.getDevice()).thenReturn(mDevice3);
when(mCachedDevice3.getGroupId()).thenReturn(TEST_GROUP_ID3);
mFragment = new AudioSharingDisconnectDialogFragment();
mParent = new Fragment();
FragmentController.setupFragment(
mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null);
mDeviceItems.add(TEST_DEVICE_ITEM1);
mDeviceItems.add(TEST_DEVICE_ITEM2);
mFragment.show(mParent, mDeviceItems, mCachedDevice3, (item) -> {});
shadowMainLooper().idle();
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
list.add(TEST_DEVICE_ITEM2);
mFragment.show(mParent, list, mCachedDevice3, (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_dialogShowBtnForTwoDevices() {
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
RecyclerView view = rootView.findViewById(R.id.device_btn_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(2);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_dialogIsShowingForSameGroup_updateDialog() {
String prefix = "Disconnect ";
AtomicBoolean isItemBtnClicked = new AtomicBoolean(false);
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
RecyclerView view = rootView.findViewById(R.id.device_btn_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(2);
Button btn1 =
view.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.device_button);
assertThat(btn1.getText().toString()).isEqualTo(prefix + TEST_DEVICE_NAME1);
Button btn2 =
view.findViewHolderForAdapterPosition(1).itemView.findViewById(R.id.device_button);
assertThat(btn2.getText().toString()).isEqualTo(prefix + TEST_DEVICE_NAME2);
btn1.performClick();
assertThat(isItemBtnClicked.get()).isFalse();
// Update dialog content with same group
mFragment.show(mParent, mDeviceItems, mCachedDevice3, (item) -> isItemBtnClicked.set(true));
shadowMainLooper().idle();
dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
btn1 = view.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.device_button);
btn1.performClick();
assertThat(isItemBtnClicked.get()).isTrue();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_dialogIsShowingForNewGroup_updateDialog() {
String prefix = "Disconnect ";
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
RecyclerView view = rootView.findViewById(R.id.device_btn_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(2);
// Update dialog content with new group
ArrayList<AudioSharingDeviceItem> newDeviceItems = new ArrayList<>();
newDeviceItems.add(TEST_DEVICE_ITEM2);
newDeviceItems.add(TEST_DEVICE_ITEM3);
mFragment.show(mParent, newDeviceItems, mCachedDevice1, (item) -> {});
shadowMainLooper().idle();
dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
rootView = shadowDialog.getView();
view = rootView.findViewById(R.id.device_btn_list);
assertThat(view.getAdapter().getItemCount()).isEqualTo(2);
Button btn1 =
view.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.device_button);
assertThat(btn1.getText().toString()).isEqualTo(prefix + TEST_DEVICE_NAME2);
Button btn2 =
view.findViewHolderForAdapterPosition(1).itemView.findViewById(R.id.device_button);
assertThat(btn2.getText().toString()).isEqualTo(prefix + TEST_DEVICE_NAME3);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickCancel_dialogDismiss() {
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
rootView.findViewById(R.id.cancel_btn).performClick();
assertThat(dialog.isShowing()).isFalse();
}
}

View File

@@ -1,190 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothStatusCodes;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.view.View;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.R;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
@RunWith(RobolectricTestRunner.class)
@Config(
shadows = {
ShadowAlertDialogCompat.class,
ShadowBluetoothAdapter.class,
})
public class AudioSharingJoinDialogFragmentTest {
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2";
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM1 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME1, /* groupId= */ 1, /* isActive= */ true);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM2 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME2, /* groupId= */ 2, /* isActive= */ false);
@Mock private CachedBluetoothDevice mCachedDevice1;
@Mock private CachedBluetoothDevice mCachedDevice2;
private Fragment mParent;
private AudioSharingJoinDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before
public void setUp() {
AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
if (latestAlertDialog != null) {
latestAlertDialog.dismiss();
ShadowAlertDialogCompat.reset();
}
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
when(mCachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME1);
when(mCachedDevice2.getName()).thenReturn(TEST_DEVICE_NAME2);
mFragment = new AudioSharingJoinDialogFragment();
mParent = new Fragment();
FragmentController.setupFragment(
mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null);
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() {
mFragment.show(mParent, new ArrayList<>(), mCachedDevice2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_dialogShowTextForSingleDevice() {
mFragment.show(mParent, new ArrayList<>(), mCachedDevice2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowDialog.getMessage().toString()).isEqualTo(TEST_DEVICE_NAME2);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOn_dialogShowTextForTwoDevice() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
mFragment.show(mParent, list, mCachedDevice2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowDialog.getMessage().toString())
.isEqualTo(TEST_DEVICE_NAME1 + " and " + TEST_DEVICE_NAME2);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_dialogIsShowing_updateDialog() {
ArrayList<AudioSharingDeviceItem> list = new ArrayList<>();
list.add(TEST_DEVICE_ITEM1);
mFragment.show(mParent, list, mCachedDevice2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
// Update the content
ArrayList<AudioSharingDeviceItem> list2 = new ArrayList<>();
list2.add(TEST_DEVICE_ITEM2);
mFragment.show(mParent, list2, mCachedDevice1, () -> {});
shadowMainLooper().idle();
dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowDialog.getMessage().toString())
.isEqualTo(TEST_DEVICE_NAME2 + " and " + TEST_DEVICE_NAME1);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickCancel_dialogDismiss() {
mFragment.show(mParent, new ArrayList<>(), mCachedDevice2, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
rootView.findViewById(R.id.cancel_btn).performClick();
assertThat(dialog.isShowing()).isFalse();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickShare_callbackTriggered() {
AtomicBoolean isShareBtnClicked = new AtomicBoolean(false);
mFragment.show(
mParent, new ArrayList<>(), mCachedDevice2, () -> isShareBtnClicked.set(true));
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
View rootView = shadowDialog.getView();
rootView.findViewById(R.id.share_btn).performClick();
assertThat(dialog.isShowing()).isFalse();
assertThat(isShareBtnClicked.get()).isTrue();
}
}

View File

@@ -1,160 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static android.bluetooth.BluetoothAdapter.STATE_OFF;
import static android.bluetooth.BluetoothAdapter.STATE_ON;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.bluetooth.Utils;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothUtils.class})
public class AudioSharingPreferenceControllerTest {
private static final String PREF_KEY = "audio_sharing_settings";
private static final String SUMMARY_ON = "On";
private static final String SUMMARY_OFF = "Off";
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
@Spy Context mContext = ApplicationProvider.getApplicationContext();
@Mock private PreferenceScreen mScreen;
@Mock private LocalBluetoothManager mLocalBtManager;
@Mock private BluetoothEventManager mBtEventManager;
@Mock private LocalBluetoothProfileManager mLocalBtProfileManager;
@Mock private LocalBluetoothLeBroadcast mBroadcast;
private AudioSharingPreferenceController mController;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private LocalBluetoothManager mLocalBluetoothManager;
private Lifecycle mLifecycle;
private LifecycleOwner mLifecycleOwner;
private Preference mPreference;
@Before
public void setUp() {
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
when(mLocalBluetoothManager.getEventManager()).thenReturn(mBtEventManager);
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBtProfileManager);
when(mLocalBtProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
mController = new AudioSharingPreferenceController(mContext, PREF_KEY);
mPreference = new Preference(mContext);
when(mScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
}
@Test
public void onStart_registerCallback() {
mController.onStart(mLifecycleOwner);
verify(mBtEventManager).registerCallback(mController);
verify(mBroadcast).registerServiceCallBack(any(), any(BluetoothLeBroadcast.Callback.class));
}
@Test
public void onStop_unregisterCallback() {
mController.onStop(mLifecycleOwner);
verify(mBtEventManager).unregisterCallback(mController);
verify(mBroadcast).unregisterServiceCallBack(any(BluetoothLeBroadcast.Callback.class));
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void getAvailabilityStatus_flagOn() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void getAvailabilityStatus_flagOff() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getSummary_broadcastOn() {
when(mBroadcast.isEnabled(any())).thenReturn(true);
assertThat(mController.getSummary().toString()).isEqualTo(SUMMARY_ON);
}
@Test
public void getSummary_broadcastOff() {
when(mBroadcast.isEnabled(any())).thenReturn(false);
assertThat(mController.getSummary().toString()).isEqualTo(SUMMARY_OFF);
}
@Test
public void onBluetoothStateChanged_refreshSummary() {
mController.displayPreference(mScreen);
when(mBroadcast.isEnabled(any())).thenReturn(true);
mController.onBluetoothStateChanged(STATE_ON);
assertThat(mPreference.getSummary().toString()).isEqualTo(SUMMARY_ON);
when(mBroadcast.isEnabled(any())).thenReturn(false);
mController.onBluetoothStateChanged(STATE_OFF);
assertThat(mPreference.getSummary().toString()).isEqualTo(SUMMARY_OFF);
}
}

View File

@@ -1,151 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothStatusCodes;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.concurrent.atomic.AtomicBoolean;
@RunWith(RobolectricTestRunner.class)
@Config(
shadows = {
ShadowAlertDialogCompat.class,
ShadowBluetoothAdapter.class,
})
public class AudioSharingStopDialogFragmentTest {
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2";
@Mock private CachedBluetoothDevice mCachedDevice1;
@Mock private CachedBluetoothDevice mCachedDevice2;
private Fragment mParent;
private AudioSharingStopDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before
public void setUp() {
AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog();
if (latestAlertDialog != null) {
latestAlertDialog.dismiss();
ShadowAlertDialogCompat.reset();
}
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
when(mCachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME1);
when(mCachedDevice2.getName()).thenReturn(TEST_DEVICE_NAME2);
mFragment = new AudioSharingStopDialogFragment();
mParent = new Fragment();
FragmentController.setupFragment(
mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null);
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() {
mFragment.show(mParent, mCachedDevice1, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_dialogIsShowing_updateDialog() {
String postMessage = " wants to connect, headphones in audio sharing will disconnect.";
mFragment.show(mParent, mCachedDevice1, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowDialog.getMessage().toString()).isEqualTo(TEST_DEVICE_NAME1 + postMessage);
// Update the content
mFragment.show(mParent, mCachedDevice2, () -> {});
shadowMainLooper().idle();
dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
assertThat(dialog.isShowing()).isTrue();
shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowDialog.getMessage().toString()).isEqualTo(TEST_DEVICE_NAME2 + postMessage);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickCancel_dialogDismiss() {
mFragment.show(mParent, mCachedDevice1, () -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
dialog.findViewById(android.R.id.button2).performClick();
shadowMainLooper().idle();
assertThat(dialog.isShowing()).isFalse();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_clickShare_callbackTriggered() {
AtomicBoolean isStopBtnClicked = new AtomicBoolean(false);
mFragment.show(mParent, mCachedDevice1, () -> isStopBtnClicked.set(true));
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
dialog.findViewById(android.R.id.button1).performClick();
shadowMainLooper().idle();
assertThat(dialog.isShowing()).isFalse();
assertThat(isStopBtnClicked.get()).isTrue();
}
}

View File

@@ -1,85 +0,0 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.widget.Switch;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.flags.Flags;
import com.android.settings.widget.SettingsMainSwitchBar;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class AudioSharingSwitchBarControllerTest {
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
@Spy Context mContext = ApplicationProvider.getApplicationContext();
@Mock private Switch mSwitch;
private SettingsMainSwitchBar mSwitchBar;
private AudioSharingSwitchBarController mController;
private AudioSharingSwitchBarController.OnSwitchBarChangedListener mListener;
private boolean mOnSwitchBarChanged;
@Before
public void setUp() {
mSwitchBar = new SettingsMainSwitchBar(mContext);
mOnSwitchBarChanged = false;
mListener = () -> mOnSwitchBarChanged = true;
mController = new AudioSharingSwitchBarController(mContext, mSwitchBar, mListener);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void bluetoothOff_switchDisabled() {
assertThat(mSwitchBar.isEnabled()).isTrue();
mContext.registerReceiver(
mController.mReceiver,
mController.mIntentFilter,
Context.RECEIVER_EXPORTED_UNAUDITED);
Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
mContext.sendBroadcast(intent);
shadowOf(Looper.getMainLooper()).idle();
assertThat(mSwitchBar.isEnabled()).isFalse();
assertThat(mOnSwitchBarChanged).isTrue();
}
}

View File

@@ -1,110 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothStatusCodes;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.androidx.fragment.FragmentController;
import java.util.ArrayList;
@RunWith(RobolectricTestRunner.class)
@Config(
shadows = {
ShadowAlertDialogCompat.class,
ShadowBluetoothAdapter.class,
})
public class CallsAndAlarmsDialogFragmentTest {
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2";
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM1 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME1, /* groupId= */ 1, /* isActive= */ true);
private static final AudioSharingDeviceItem TEST_DEVICE_ITEM2 =
new AudioSharingDeviceItem(TEST_DEVICE_NAME2, /* groupId= */ 1, /* isActive= */ true);
private Fragment mParent;
private CallsAndAlarmsDialogFragment mFragment;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before
public void setUp() {
ShadowAlertDialogCompat.reset();
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mFragment = new CallsAndAlarmsDialogFragment();
mParent = new Fragment();
FragmentController.setupFragment(
mParent, FragmentActivity.class, /* containerViewId= */ 0, /* bundle= */ null);
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_flagOff_dialogNotExist() {
mFragment.show(mParent, new ArrayList<>(), (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNull();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onCreateDialog_showCorrectItems() {
ArrayList<AudioSharingDeviceItem> deviceItemList = new ArrayList<>();
deviceItemList.add(TEST_DEVICE_ITEM1);
deviceItemList.add(TEST_DEVICE_ITEM2);
mFragment.show(mParent, deviceItemList, (item) -> {});
shadowMainLooper().idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.getListView().getCount()).isEqualTo(2);
}
}

View File

@@ -1,248 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.os.Looper;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.bluetooth.Utils;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import java.util.ArrayList;
@RunWith(RobolectricTestRunner.class)
@Config(
shadows = {
ShadowBluetoothAdapter.class,
ShadowBluetoothUtils.class,
})
public class CallsAndAlarmsPreferenceControllerTest {
private static final String PREF_KEY = "calls_and_alarms";
private static final String SUMMARY_EMPTY = "No active device in sharing";
private static final String TEST_DEVICE_NAME1 = "test1";
private static final String TEST_DEVICE_NAME2 = "test2";
private static final String TEST_SETTINGS_KEY =
"bluetooth_le_broadcast_fallback_active_group_id";
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
@Spy Context mContext = ApplicationProvider.getApplicationContext();
@Mock private PreferenceScreen mScreen;
@Mock private LocalBluetoothManager mLocalBtManager;
@Mock private BluetoothEventManager mBtEventManager;
@Mock private LocalBluetoothProfileManager mLocalBtProfileManager;
@Mock private CachedBluetoothDeviceManager mCacheManager;
@Mock private LocalBluetoothLeBroadcast mBroadcast;
@Mock private LocalBluetoothLeBroadcastAssistant mAssistant;
@Mock private BluetoothDevice mDevice1;
@Mock private BluetoothDevice mDevice2;
@Mock private BluetoothDevice mDevice3;
@Mock private CachedBluetoothDevice mCachedDevice1;
@Mock private CachedBluetoothDevice mCachedDevice2;
@Mock private CachedBluetoothDevice mCachedDevice3;
@Mock private BluetoothLeBroadcastReceiveState mState;
private CallsAndAlarmsPreferenceController mController;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private LocalBluetoothManager mLocalBluetoothManager;
private Lifecycle mLifecycle;
private LifecycleOwner mLifecycleOwner;
private Preference mPreference;
@Before
public void setUp() {
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
when(mLocalBluetoothManager.getEventManager()).thenReturn(mBtEventManager);
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBtProfileManager);
when(mLocalBtProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
when(mLocalBtProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(mAssistant);
mController = new CallsAndAlarmsPreferenceController(mContext);
mPreference = new Preference(mContext);
when(mScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
}
@Test
public void onStart_registerCallback() {
mController.onStart(mLifecycleOwner);
verify(mBtEventManager).registerCallback(mController);
verify(mAssistant)
.registerServiceCallBack(any(), any(BluetoothLeBroadcastAssistant.Callback.class));
}
@Test
public void onStop_unregisterCallback() {
mController.onStop(mLifecycleOwner);
verify(mBtEventManager).unregisterCallback(mController);
verify(mAssistant)
.unregisterServiceCallBack(any(BluetoothLeBroadcastAssistant.Callback.class));
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void getAvailabilityStatus_flagOn() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void getAvailabilityStatus_flagOff() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void updateVisibility_broadcastOffBluetoothOff() {
when(mBroadcast.isEnabled(any())).thenReturn(false);
mShadowBluetoothAdapter.setEnabled(false);
mController.displayPreference(mScreen);
mController.updateVisibility();
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isFalse();
}
@Test
public void updateVisibility_broadcastOnBluetoothOff() {
when(mBroadcast.isEnabled(any())).thenReturn(true);
mShadowBluetoothAdapter.setEnabled(false);
mController.displayPreference(mScreen);
mController.updateVisibility();
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isFalse();
}
@Test
public void updateVisibility_broadcastOffBluetoothOn() {
when(mBroadcast.isEnabled(any())).thenReturn(false);
mController.displayPreference(mScreen);
mController.updateVisibility();
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isFalse();
}
@Test
public void updateVisibility_broadcastOnBluetoothOn() {
when(mBroadcast.isEnabled(any())).thenReturn(true);
when(mAssistant.getConnectedDevices()).thenReturn(new ArrayList<BluetoothDevice>());
mController.displayPreference(mScreen);
mController.updateVisibility();
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary().toString()).isEqualTo(SUMMARY_EMPTY);
}
@Test
public void onProfileConnectionStateChanged_updatePreference() {
when(mBroadcast.isEnabled(any())).thenReturn(true);
when(mAssistant.getConnectedDevices()).thenReturn(new ArrayList<BluetoothDevice>());
mController.displayPreference(mScreen);
mController.onProfileConnectionStateChanged(
mCachedDevice1, BluetoothAdapter.STATE_DISCONNECTED, BluetoothProfile.LE_AUDIO);
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary().toString()).isEqualTo(SUMMARY_EMPTY);
}
@Test
public void updatePreference_showCorrectSummary() {
final int groupId1 = 1;
final int groupId2 = 2;
Settings.Secure.putInt(mContext.getContentResolver(), TEST_SETTINGS_KEY, groupId1);
when(mCachedDevice1.getGroupId()).thenReturn(groupId1);
when(mCachedDevice1.getDevice()).thenReturn(mDevice1);
when(mCachedDevice2.getGroupId()).thenReturn(groupId1);
when(mCachedDevice2.getDevice()).thenReturn(mDevice2);
when(mCachedDevice1.getMemberDevice()).thenReturn(ImmutableSet.of(mCachedDevice2));
when(mCachedDevice1.getName()).thenReturn(TEST_DEVICE_NAME1);
when(mCachedDevice3.getGroupId()).thenReturn(groupId2);
when(mCachedDevice3.getDevice()).thenReturn(mDevice3);
when(mCachedDevice3.getName()).thenReturn(TEST_DEVICE_NAME2);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCacheManager);
when(mCacheManager.findDevice(mDevice1)).thenReturn(mCachedDevice1);
when(mCacheManager.findDevice(mDevice2)).thenReturn(mCachedDevice2);
when(mCacheManager.findDevice(mDevice3)).thenReturn(mCachedDevice3);
when(mBroadcast.isEnabled(any())).thenReturn(true);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1, mDevice2, mDevice3);
when(mAssistant.getConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState));
mController.displayPreference(mScreen);
mController.updateVisibility();
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary().toString()).isEqualTo(TEST_DEVICE_NAME1);
}
}