Merge "[BT LE broadcast sink] Initialize the sourceId" into tm-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
eb254aa0c3
@@ -16,7 +16,9 @@
|
|||||||
|
|
||||||
package com.android.settings.bluetooth;
|
package com.android.settings.bluetooth;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothLeAudioContentMetadata;
|
||||||
import android.bluetooth.BluetoothLeBroadcastMetadata;
|
import android.bluetooth.BluetoothLeBroadcastMetadata;
|
||||||
|
import android.bluetooth.BluetoothLeBroadcastReceiveState;
|
||||||
import android.bluetooth.BluetoothLeBroadcastSubgroup;
|
import android.bluetooth.BluetoothLeBroadcastSubgroup;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
@@ -43,16 +45,15 @@ class BluetoothBroadcastSourcePreference extends Preference {
|
|||||||
private static final int RESOURCE_ID_ICON = R.drawable.settings_input_antenna;
|
private static final int RESOURCE_ID_ICON = R.drawable.settings_input_antenna;
|
||||||
|
|
||||||
private BluetoothLeBroadcastMetadata mBluetoothLeBroadcastMetadata;
|
private BluetoothLeBroadcastMetadata mBluetoothLeBroadcastMetadata;
|
||||||
|
private BluetoothLeBroadcastReceiveState mBluetoothLeBroadcastReceiveState;
|
||||||
private ImageView mFrictionImageView;
|
private ImageView mFrictionImageView;
|
||||||
private String mTitle;
|
private String mTitle;
|
||||||
private boolean mStatus;
|
private boolean mStatus;
|
||||||
private boolean mIsEncrypted;
|
private boolean mIsEncrypted;
|
||||||
|
|
||||||
BluetoothBroadcastSourcePreference(@NonNull Context context,
|
BluetoothBroadcastSourcePreference(@NonNull Context context) {
|
||||||
@NonNull BluetoothLeBroadcastMetadata source) {
|
|
||||||
super(context);
|
super(context);
|
||||||
initUi();
|
initUi();
|
||||||
updateMetadataAndRefreshUi(source, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -68,7 +69,7 @@ class BluetoothBroadcastSourcePreference extends Preference {
|
|||||||
private void initUi() {
|
private void initUi() {
|
||||||
setLayoutResource(R.layout.preference_access_point);
|
setLayoutResource(R.layout.preference_access_point);
|
||||||
setWidgetLayoutResource(R.layout.access_point_friction_widget);
|
setWidgetLayoutResource(R.layout.access_point_friction_widget);
|
||||||
|
mTitle = getContext().getString(RESOURCE_ID_UNKNOWN_PROGRAM_INFO);
|
||||||
mStatus = false;
|
mStatus = false;
|
||||||
final Drawable drawable = getContext().getDrawable(RESOURCE_ID_ICON);
|
final Drawable drawable = getContext().getDrawable(RESOURCE_ID_ICON);
|
||||||
if (drawable != null) {
|
if (drawable != null) {
|
||||||
@@ -105,9 +106,20 @@ class BluetoothBroadcastSourcePreference extends Preference {
|
|||||||
*/
|
*/
|
||||||
public void updateMetadataAndRefreshUi(BluetoothLeBroadcastMetadata source, boolean status) {
|
public void updateMetadataAndRefreshUi(BluetoothLeBroadcastMetadata source, boolean status) {
|
||||||
mBluetoothLeBroadcastMetadata = source;
|
mBluetoothLeBroadcastMetadata = source;
|
||||||
mTitle = getBroadcastMetadataProgramInfo();
|
mTitle = getProgramInfo();
|
||||||
mIsEncrypted = mBluetoothLeBroadcastMetadata.isEncrypted();
|
mIsEncrypted = mBluetoothLeBroadcastMetadata.isEncrypted();
|
||||||
mStatus = status;
|
mStatus = status || mBluetoothLeBroadcastReceiveState != null;
|
||||||
|
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the title and status from BluetoothLeBroadcastReceiveState.
|
||||||
|
*/
|
||||||
|
public void updateReceiveStateAndRefreshUi(BluetoothLeBroadcastReceiveState receiveState) {
|
||||||
|
mBluetoothLeBroadcastReceiveState = receiveState;
|
||||||
|
mTitle = getProgramInfo();
|
||||||
|
mStatus = true;
|
||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
@@ -124,7 +136,17 @@ class BluetoothBroadcastSourcePreference extends Preference {
|
|||||||
updateStatusButton();
|
updateStatusButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getBroadcastMetadataProgramInfo() {
|
private String getProgramInfo() {
|
||||||
|
if (mBluetoothLeBroadcastReceiveState != null) {
|
||||||
|
List<BluetoothLeAudioContentMetadata> bluetoothLeAudioContentMetadata =
|
||||||
|
mBluetoothLeBroadcastReceiveState.getSubgroupMetadata();
|
||||||
|
if (!bluetoothLeAudioContentMetadata.isEmpty()) {
|
||||||
|
return bluetoothLeAudioContentMetadata.stream()
|
||||||
|
.map(i -> i.getProgramInfo())
|
||||||
|
.findFirst().orElse(
|
||||||
|
getContext().getString(RESOURCE_ID_UNKNOWN_PROGRAM_INFO));
|
||||||
|
}
|
||||||
|
}
|
||||||
if (mBluetoothLeBroadcastMetadata == null) {
|
if (mBluetoothLeBroadcastMetadata == null) {
|
||||||
return getContext().getString(RESOURCE_ID_UNKNOWN_PROGRAM_INFO);
|
return getContext().getString(RESOURCE_ID_UNKNOWN_PROGRAM_INFO);
|
||||||
}
|
}
|
||||||
@@ -138,4 +160,24 @@ class BluetoothBroadcastSourcePreference extends Preference {
|
|||||||
.filter(i -> !TextUtils.isEmpty(i))
|
.filter(i -> !TextUtils.isEmpty(i))
|
||||||
.findFirst().orElse(getContext().getString(RESOURCE_ID_UNKNOWN_PROGRAM_INFO));
|
.findFirst().orElse(getContext().getString(RESOURCE_ID_UNKNOWN_PROGRAM_INFO));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the broadcast source is encrypted or not.
|
||||||
|
* @return If true, the broadcast source needs the broadcast code. If false, the broadcast
|
||||||
|
* source does not need the broadcast code.
|
||||||
|
*/
|
||||||
|
public boolean isEncrypted() {
|
||||||
|
return mIsEncrypted;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the BluetoothLeBroadcastReceiveState and reset the state when the user clicks the
|
||||||
|
* "leave broadcast" button.
|
||||||
|
*/
|
||||||
|
public void clearReceiveState() {
|
||||||
|
mBluetoothLeBroadcastReceiveState = null;
|
||||||
|
mTitle = getProgramInfo();
|
||||||
|
mStatus = false;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -86,9 +86,7 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onSearchStarted(int reason) {
|
public void onSearchStarted(int reason) {
|
||||||
Log.d(TAG, "onSearchStarted: " + reason);
|
Log.d(TAG, "onSearchStarted: " + reason);
|
||||||
|
getActivity().runOnUiThread(() -> handleSearchStarted());
|
||||||
getActivity().runOnUiThread(
|
|
||||||
() -> cacheRemoveAllPrefs(mBroadcastSourceListCategory));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -109,7 +107,8 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {
|
public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {
|
||||||
Log.d(TAG, "onSourceFound:");
|
Log.d(TAG, "onSourceFound:");
|
||||||
getActivity().runOnUiThread(() -> updateListCategory(source, false));
|
getActivity().runOnUiThread(
|
||||||
|
() -> updateListCategoryFromBroadcastMetadata(source, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -119,7 +118,7 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
Log.w(TAG, "onSourceAdded: mSelectedPreference == null!");
|
Log.w(TAG, "onSourceAdded: mSelectedPreference == null!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
getActivity().runOnUiThread(() -> updateListCategory(
|
getActivity().runOnUiThread(() -> updateListCategoryFromBroadcastMetadata(
|
||||||
mSelectedPreference.getBluetoothLeBroadcastMetadata(), true));
|
mSelectedPreference.getBluetoothLeBroadcastMetadata(), true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,6 +143,7 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
public void onSourceRemoved(@NonNull BluetoothDevice sink, int sourceId,
|
public void onSourceRemoved(@NonNull BluetoothDevice sink, int sourceId,
|
||||||
int reason) {
|
int reason) {
|
||||||
Log.d(TAG, "onSourceRemoved:");
|
Log.d(TAG, "onSourceRemoved:");
|
||||||
|
getActivity().runOnUiThread(() -> handleSourceRemoved());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -215,6 +215,8 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
//check assistant status. Start searching...
|
//check assistant status. Start searching...
|
||||||
if (mLeBroadcastAssistant != null && !mLeBroadcastAssistant.isSearchInProgress()) {
|
if (mLeBroadcastAssistant != null && !mLeBroadcastAssistant.isSearchInProgress()) {
|
||||||
mLeBroadcastAssistant.startSearchingForSources(getScanFilter());
|
mLeBroadcastAssistant.startSearchingForSources(getScanFilter());
|
||||||
|
} else {
|
||||||
|
addConnectedSourcePreference();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -310,11 +312,13 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateListCategory(BluetoothLeBroadcastMetadata source, boolean isConnected) {
|
private void updateListCategoryFromBroadcastMetadata(BluetoothLeBroadcastMetadata source,
|
||||||
|
boolean isConnected) {
|
||||||
BluetoothBroadcastSourcePreference item = mBroadcastSourceListCategory.findPreference(
|
BluetoothBroadcastSourcePreference item = mBroadcastSourceListCategory.findPreference(
|
||||||
Integer.toString(source.getBroadcastId()));
|
Integer.toString(source.getBroadcastId()));
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
item = createBluetoothBroadcastSourcePreference(source);
|
item = createBluetoothBroadcastSourcePreference();
|
||||||
|
item.setKey(Integer.toString(source.getBroadcastId()));
|
||||||
mBroadcastSourceListCategory.addPreference(item);
|
mBroadcastSourceListCategory.addPreference(item);
|
||||||
}
|
}
|
||||||
item.updateMetadataAndRefreshUi(source, isConnected);
|
item.updateMetadataAndRefreshUi(source, isConnected);
|
||||||
@@ -326,13 +330,36 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BluetoothBroadcastSourcePreference createBluetoothBroadcastSourcePreference(
|
private void updateListCategoryFromBroadcastReceiveState(
|
||||||
BluetoothLeBroadcastMetadata source) {
|
BluetoothLeBroadcastReceiveState receiveState) {
|
||||||
|
BluetoothBroadcastSourcePreference item = mBroadcastSourceListCategory.findPreference(
|
||||||
|
Integer.toString(receiveState.getBroadcastId()));
|
||||||
|
if (item == null) {
|
||||||
|
item = createBluetoothBroadcastSourcePreference();
|
||||||
|
item.setKey(Integer.toString(receiveState.getBroadcastId()));
|
||||||
|
mBroadcastSourceListCategory.addPreference(item);
|
||||||
|
}
|
||||||
|
item.updateReceiveStateAndRefreshUi(receiveState);
|
||||||
|
item.setOrder(0);
|
||||||
|
|
||||||
|
setSourceId(receiveState.getSourceId());
|
||||||
|
mSelectedPreference = item;
|
||||||
|
|
||||||
|
//refresh the header
|
||||||
|
if (mBluetoothFindBroadcastsHeaderController != null) {
|
||||||
|
mBluetoothFindBroadcastsHeaderController.refreshUi();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private BluetoothBroadcastSourcePreference createBluetoothBroadcastSourcePreference() {
|
||||||
BluetoothBroadcastSourcePreference pref = new BluetoothBroadcastSourcePreference(
|
BluetoothBroadcastSourcePreference pref = new BluetoothBroadcastSourcePreference(
|
||||||
getContext(), source);
|
getContext());
|
||||||
pref.setKey(Integer.toString(source.getBroadcastId()));
|
|
||||||
pref.setOnPreferenceClickListener(preference -> {
|
pref.setOnPreferenceClickListener(preference -> {
|
||||||
if (source.isEncrypted()) {
|
if (pref.getBluetoothLeBroadcastMetadata() == null) {
|
||||||
|
Log.d(TAG, "BluetoothLeBroadcastMetadata is null, do nothing.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (pref.isEncrypted()) {
|
||||||
launchBroadcastCodeDialog(pref);
|
launchBroadcastCodeDialog(pref);
|
||||||
} else {
|
} else {
|
||||||
addSource(pref);
|
addSource(pref);
|
||||||
@@ -383,6 +410,10 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
.setPositiveButton(R.string.bluetooth_connect_access_dialog_positive,
|
.setPositiveButton(R.string.bluetooth_connect_access_dialog_positive,
|
||||||
(d, w) -> {
|
(d, w) -> {
|
||||||
Log.d(TAG, "setPositiveButton: clicked");
|
Log.d(TAG, "setPositiveButton: clicked");
|
||||||
|
if (pref.getBluetoothLeBroadcastMetadata() == null) {
|
||||||
|
Log.d(TAG, "BluetoothLeBroadcastMetadata is null, do nothing.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
addBroadcastCodeIntoPreference(pref, editText.getText().toString());
|
addBroadcastCodeIntoPreference(pref, editText.getText().toString());
|
||||||
addSource(pref);
|
addSource(pref);
|
||||||
})
|
})
|
||||||
@@ -392,6 +423,30 @@ public class BluetoothFindBroadcastsFragment extends RestrictedDashboardFragment
|
|||||||
alertDialog.show();
|
alertDialog.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleSearchStarted() {
|
||||||
|
cacheRemoveAllPrefs(mBroadcastSourceListCategory);
|
||||||
|
addConnectedSourcePreference();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleSourceRemoved() {
|
||||||
|
if (mSelectedPreference != null) {
|
||||||
|
if (mSelectedPreference.getBluetoothLeBroadcastMetadata() == null) {
|
||||||
|
mBroadcastSourceListCategory.removePreference(mSelectedPreference);
|
||||||
|
} else {
|
||||||
|
mSelectedPreference.clearReceiveState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mSelectedPreference = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addConnectedSourcePreference() {
|
||||||
|
List<BluetoothLeBroadcastReceiveState> receiveStateList =
|
||||||
|
mLeBroadcastAssistant.getAllSources(mCachedDevice.getDevice());
|
||||||
|
if (!receiveStateList.isEmpty()) {
|
||||||
|
updateListCategoryFromBroadcastReceiveState(receiveStateList.get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getSourceId() {
|
public int getSourceId() {
|
||||||
return mSourceId;
|
return mSourceId;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user