[Audiosharing] Some UI tweaks (e.g, sort by RSSI)
Bug: 308368124 Test: manual Change-Id: Ie066077f6ef47a57b9fb1c85bc7200498dcae093
This commit is contained in:
@@ -104,7 +104,7 @@ public class AudioStreamConfirmDialog extends InstrumentedDialogFragment {
|
||||
private Dialog getErrorDialog() {
|
||||
return new AudioStreamsDialogFragment.DialogBuilder(mActivity)
|
||||
.setTitle("Can't listen to audio stream")
|
||||
.setSubTitle1("Can't play this audio stream. Learn more")
|
||||
.setSubTitle2("Can't play this audio stream. Learn more")
|
||||
.setRightButtonText("Close")
|
||||
.setRightButtonOnClickListener(
|
||||
unused -> {
|
||||
|
@@ -21,8 +21,10 @@ 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;
|
||||
@@ -56,7 +58,6 @@ class AudioStreamPreference extends TwoTargetPreference {
|
||||
}
|
||||
mIsConnected = isConnected;
|
||||
setSummary(summary);
|
||||
setOrder(isConnected ? 0 : 1);
|
||||
setOnPreferenceClickListener(onPreferenceClickListener);
|
||||
notifyChanged();
|
||||
}
|
||||
@@ -70,6 +71,10 @@ class AudioStreamPreference extends TwoTargetPreference {
|
||||
mAudioStream.setState(state);
|
||||
}
|
||||
|
||||
int getAudioStreamRssi() {
|
||||
return mAudioStream.getRssi();
|
||||
}
|
||||
|
||||
AudioStreamsProgressCategoryController.AudioStreamState getAudioStreamState() {
|
||||
return mAudioStream.getState();
|
||||
}
|
||||
@@ -84,13 +89,26 @@ class AudioStreamPreference extends TwoTargetPreference {
|
||||
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,
|
||||
AudioStreamsProgressCategoryController.AudioStreamState streamState) {
|
||||
AudioStreamPreference preference = new AudioStreamPreference(context, /* attrs= */ null);
|
||||
preference.setTitle(getBroadcastName(source));
|
||||
preference.setAudioStream(new AudioStream(source.getBroadcastId(), streamState));
|
||||
preference.setAudioStream(
|
||||
new AudioStream(source.getBroadcastId(), streamState, source.getRssi()));
|
||||
return preference;
|
||||
}
|
||||
|
||||
@@ -129,12 +147,16 @@ class AudioStreamPreference extends TwoTargetPreference {
|
||||
private static final class AudioStream {
|
||||
private int mSourceId;
|
||||
private int mBroadcastId;
|
||||
private int mRssi = Integer.MIN_VALUE;
|
||||
private AudioStreamsProgressCategoryController.AudioStreamState mState;
|
||||
|
||||
private AudioStream(
|
||||
int broadcastId, AudioStreamsProgressCategoryController.AudioStreamState state) {
|
||||
int broadcastId,
|
||||
AudioStreamsProgressCategoryController.AudioStreamState state,
|
||||
int rssi) {
|
||||
mBroadcastId = broadcastId;
|
||||
mState = state;
|
||||
mRssi = rssi;
|
||||
}
|
||||
|
||||
private AudioStream(
|
||||
@@ -156,6 +178,10 @@ class AudioStreamPreference extends TwoTargetPreference {
|
||||
return mBroadcastId;
|
||||
}
|
||||
|
||||
private int getRssi() {
|
||||
return mRssi;
|
||||
}
|
||||
|
||||
private AudioStreamsProgressCategoryController.AudioStreamState getState() {
|
||||
return mState;
|
||||
}
|
||||
|
@@ -53,6 +53,7 @@ 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;
|
||||
@@ -88,6 +89,15 @@ public class AudioStreamsProgressCategoryController extends BasePreferenceContro
|
||||
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;
|
||||
@@ -338,7 +348,7 @@ public class AudioStreamsProgressCategoryController extends BasePreferenceContro
|
||||
ThreadUtils.postOnMainThread(
|
||||
() -> {
|
||||
if (mCategoryPreference != null) {
|
||||
mCategoryPreference.removeAll();
|
||||
mCategoryPreference.removeAudioStreamPreferences();
|
||||
mCategoryPreference.setVisible(hasActive);
|
||||
}
|
||||
});
|
||||
@@ -423,7 +433,7 @@ public class AudioStreamsProgressCategoryController extends BasePreferenceContro
|
||||
getPreferenceSummary(state),
|
||||
onClickListener);
|
||||
if (mCategoryPreference != null) {
|
||||
mCategoryPreference.addPreference(preference);
|
||||
mCategoryPreference.addAudioStreamPreference(preference, mComparator);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -492,7 +502,7 @@ public class AudioStreamsProgressCategoryController extends BasePreferenceContro
|
||||
private AudioStreamsDialogFragment.DialogBuilder getNoLeDeviceDialog() {
|
||||
return new AudioStreamsDialogFragment.DialogBuilder(mContext)
|
||||
.setTitle("Connect compatible headphones")
|
||||
.setSubTitle1(
|
||||
.setSubTitle2(
|
||||
"To listen to an audio stream, first connect headphones that support LE"
|
||||
+ " Audio to this device. Learn more")
|
||||
.setLeftButtonText("Close")
|
||||
|
@@ -19,9 +19,15 @@ 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) {
|
||||
@@ -46,6 +52,37 @@ public class AudioStreamsProgressCategoryPreference extends ProgressCategory {
|
||||
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);
|
||||
}
|
||||
|
@@ -24,6 +24,9 @@ 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;
|
||||
@@ -34,6 +37,7 @@ 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 {
|
||||
@@ -49,30 +53,47 @@ public class AudioStreamsQrCodeFragment extends InstrumentedFragment {
|
||||
public final View onCreateView(
|
||||
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.xml.bluetooth_audio_streams_qr_code, container, false);
|
||||
getQrCodeBitmap()
|
||||
.ifPresent(
|
||||
bm ->
|
||||
|
||||
BluetoothLeBroadcastMetadata broadcastMetadata = getBroadcastMetadata();
|
||||
|
||||
if (broadcastMetadata != null) {
|
||||
getQrCodeBitmap(broadcastMetadata)
|
||||
.ifPresent(
|
||||
bm -> {
|
||||
((ImageView) view.requireViewById(R.id.qrcode_view))
|
||||
.setImageBitmap(bm));
|
||||
.setImageBitmap(bm);
|
||||
((TextView) view.requireViewById(R.id.password))
|
||||
.setText(
|
||||
"Password: "
|
||||
+ new String(
|
||||
broadcastMetadata
|
||||
.getBroadcastCode(),
|
||||
StandardCharsets.UTF_8));
|
||||
});
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
private Optional<Bitmap> getQrCodeBitmap() {
|
||||
String broadcastMetadata = getBroadcastMetadataQrCode();
|
||||
if (broadcastMetadata.isEmpty()) {
|
||||
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(broadcastMetadata, qrcodeSize);
|
||||
Bitmap bitmap = QrCodeGenerator.encodeQrCode(metadataStr, qrcodeSize);
|
||||
return Optional.of(bitmap);
|
||||
} catch (WriterException e) {
|
||||
Log.d(
|
||||
TAG,
|
||||
"onCreateView: broadcastMetadata "
|
||||
+ broadcastMetadata
|
||||
+ metadata
|
||||
+ " qrCode generation exception "
|
||||
+ e);
|
||||
}
|
||||
@@ -80,23 +101,24 @@ public class AudioStreamsQrCodeFragment extends InstrumentedFragment {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private String getBroadcastMetadataQrCode() {
|
||||
@Nullable
|
||||
private BluetoothLeBroadcastMetadata getBroadcastMetadata() {
|
||||
LocalBluetoothLeBroadcast localBluetoothLeBroadcast =
|
||||
Utils.getLocalBtManager(getActivity())
|
||||
.getProfileManager()
|
||||
.getLeAudioBroadcastProfile();
|
||||
if (localBluetoothLeBroadcast == null) {
|
||||
Log.d(TAG, "getBroadcastMetadataQrCode: localBluetoothLeBroadcast is null!");
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
BluetoothLeBroadcastMetadata metadata =
|
||||
localBluetoothLeBroadcast.getLatestBluetoothLeBroadcastMetadata();
|
||||
if (metadata == null) {
|
||||
Log.d(TAG, "getBroadcastMetadataQrCode: metadata is null!");
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
return BluetoothLeBroadcastMetadataExt.INSTANCE.toQrCodeString(metadata);
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
|
@@ -229,6 +229,7 @@ public class QrCodeScanModeFragment extends InstrumentedFragment
|
||||
}
|
||||
|
||||
mErrorMessage.setVisibility(View.INVISIBLE);
|
||||
mTextureView.setVisibility(View.INVISIBLE);
|
||||
|
||||
triggerVibrationForQrCodeRecognition(getContext());
|
||||
|
||||
|
Reference in New Issue
Block a user