[Audiosharing] Add tests for qrcode related classes.
Test: atest -c com.android.settings.connecteddevice.audiosharing.audiostreams Bug: 308368124 Change-Id: I74caacf43a23bbd0a6da44af24a1be4dd9544a5d
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2023 The Android Open Source Project
|
||||
* 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.
|
||||
|
||||
@@ -27,6 +27,7 @@ import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.android.settings.R;
|
||||
@@ -35,10 +36,12 @@ 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.android.settingslib.utils.ThreadUtils;
|
||||
|
||||
import com.google.zxing.WriterException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class AudioStreamsQrCodeFragment extends InstrumentedFragment {
|
||||
@@ -52,57 +55,69 @@ public class AudioStreamsQrCodeFragment extends InstrumentedFragment {
|
||||
@Override
|
||||
public final View onCreateView(
|
||||
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.xml.bluetooth_audio_streams_qr_code, container, false);
|
||||
return inflater.inflate(R.xml.bluetooth_audio_streams_qr_code, container, false);
|
||||
}
|
||||
|
||||
BluetoothLeBroadcastMetadata broadcastMetadata = getBroadcastMetadata();
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
var unused = ThreadUtils.postOnBackgroundThread(
|
||||
() -> {
|
||||
BluetoothLeBroadcastMetadata broadcastMetadata = getBroadcastMetadata();
|
||||
if (broadcastMetadata == null) {
|
||||
return;
|
||||
}
|
||||
Bitmap bm = getQrCodeBitmap(broadcastMetadata).orElse(null);
|
||||
if (bm == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (broadcastMetadata != null) {
|
||||
Optional<Bitmap> bm = getQrCodeBitmap(broadcastMetadata);
|
||||
if (bm.isEmpty()) {
|
||||
return view;
|
||||
}
|
||||
((ImageView) view.requireViewById(R.id.qrcode_view)).setImageBitmap(bm.get());
|
||||
if (broadcastMetadata.getBroadcastCode() != null) {
|
||||
String password =
|
||||
new String(broadcastMetadata.getBroadcastCode(), StandardCharsets.UTF_8);
|
||||
String passwordText =
|
||||
getContext()
|
||||
.getString(R.string.audio_streams_qr_code_page_password, password);
|
||||
((TextView) view.requireViewById(R.id.password)).setText(passwordText);
|
||||
}
|
||||
TextView summaryView = view.requireViewById(android.R.id.summary);
|
||||
String summary =
|
||||
view.getContext()
|
||||
.getString(
|
||||
R.string.audio_streams_qr_code_page_description,
|
||||
broadcastMetadata.getBroadcastName());
|
||||
summaryView.setText(summary);
|
||||
}
|
||||
return view;
|
||||
ThreadUtils.postOnMainThread(
|
||||
() -> {
|
||||
((ImageView) view.requireViewById(R.id.qrcode_view))
|
||||
.setImageBitmap(bm);
|
||||
if (broadcastMetadata.getBroadcastCode() != null) {
|
||||
String password =
|
||||
new String(
|
||||
broadcastMetadata.getBroadcastCode(),
|
||||
StandardCharsets.UTF_8);
|
||||
String passwordText =
|
||||
getString(
|
||||
R.string.audio_streams_qr_code_page_password,
|
||||
password);
|
||||
((TextView) view.requireViewById(R.id.password))
|
||||
.setText(passwordText);
|
||||
}
|
||||
TextView summaryView = view.requireViewById(android.R.id.summary);
|
||||
String summary =
|
||||
getString(
|
||||
R.string.audio_streams_qr_code_page_description,
|
||||
broadcastMetadata.getBroadcastName());
|
||||
summaryView.setText(summary);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private Optional<Bitmap> getQrCodeBitmap(@Nullable BluetoothLeBroadcastMetadata metadata) {
|
||||
if (metadata == null) {
|
||||
Log.d(TAG, "onCreateView: broadcastMetadata is empty!");
|
||||
Log.d(TAG, "getQrCodeBitmap: broadcastMetadata is empty!");
|
||||
return Optional.empty();
|
||||
}
|
||||
String metadataStr = BluetoothLeBroadcastMetadataExt.INSTANCE.toQrCodeString(metadata);
|
||||
if (metadataStr.isEmpty()) {
|
||||
Log.d(TAG, "onCreateView: metadataStr is empty!");
|
||||
Log.d(TAG, "getQrCodeBitmap: metadataStr is empty!");
|
||||
return Optional.empty();
|
||||
}
|
||||
Log.i(TAG, "onCreateView: metadataStr : " + metadataStr);
|
||||
Log.d(TAG, "getQrCodeBitmap: metadata : " + metadata);
|
||||
try {
|
||||
int qrcodeSize =
|
||||
getContext()
|
||||
.getResources()
|
||||
.getDimensionPixelSize(R.dimen.audio_streams_qrcode_size);
|
||||
getResources().getDimensionPixelSize(R.dimen.audio_streams_qrcode_size);
|
||||
Bitmap bitmap = QrCodeGenerator.encodeQrCode(metadataStr, qrcodeSize);
|
||||
return Optional.of(bitmap);
|
||||
} catch (WriterException e) {
|
||||
Log.d(
|
||||
TAG,
|
||||
"onCreateView: broadcastMetadata "
|
||||
"getQrCodeBitmap: broadcastMetadata "
|
||||
+ metadata
|
||||
+ " qrCode generation exception "
|
||||
+ e);
|
||||
@@ -122,13 +137,13 @@ public class AudioStreamsQrCodeFragment extends InstrumentedFragment {
|
||||
return null;
|
||||
}
|
||||
|
||||
BluetoothLeBroadcastMetadata metadata =
|
||||
localBluetoothLeBroadcast.getLatestBluetoothLeBroadcastMetadata();
|
||||
if (metadata == null) {
|
||||
List<BluetoothLeBroadcastMetadata> metadata =
|
||||
localBluetoothLeBroadcast.getAllBroadcastMetadata();
|
||||
if (metadata.isEmpty()) {
|
||||
Log.d(TAG, "getBroadcastMetadataQrCode: metadata is null!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return metadata;
|
||||
return metadata.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.bluetooth.Utils;
|
||||
@@ -62,8 +63,8 @@ public class AudioStreamsQrCodeScanFragment extends InstrumentedFragment
|
||||
private static final int MESSAGE_HIDE_ERROR_MESSAGE = 1;
|
||||
private static final int MESSAGE_SHOW_ERROR_MESSAGE = 2;
|
||||
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;
|
||||
@VisibleForTesting static final long SHOW_ERROR_MESSAGE_INTERVAL = 10000;
|
||||
@VisibleForTesting static final long SHOW_SUCCESS_SQUARE_INTERVAL = 1000;
|
||||
private static final Duration VIBRATE_DURATION_QR_CODE_RECOGNITION = Duration.ofMillis(3);
|
||||
private final Handler mHandler =
|
||||
new Handler(Looper.getMainLooper()) {
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.preference.Preference;
|
||||
@@ -32,7 +33,6 @@ import com.android.settings.bluetooth.Utils;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
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;
|
||||
@@ -41,9 +41,10 @@ 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 =
|
||||
@VisibleForTesting static final String KEY = "audio_streams_scan_qr_code";
|
||||
|
||||
@VisibleForTesting
|
||||
final BluetoothCallback mBluetoothCallback =
|
||||
new BluetoothCallback() {
|
||||
@Override
|
||||
public void onActiveDeviceChanged(
|
||||
|
||||
Reference in New Issue
Block a user