[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:
chelseahao
2024-06-14 16:24:28 +08:00
committed by Chelsea Hao
parent 24d8411915
commit 16f9205fb6
9 changed files with 629 additions and 45 deletions

View File

@@ -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);
}
}