1 /** 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.bluetooth; 18 19 import static com.android.settingslib.bluetooth.BluetoothBroadcastUtils.EXTRA_BLUETOOTH_DEVICE_SINK; 20 import static com.android.settingslib.bluetooth.BluetoothBroadcastUtils.EXTRA_BLUETOOTH_SINK_IS_GROUP; 21 22 import android.bluetooth.BluetoothDevice; 23 import android.content.Intent; 24 import android.os.Bundle; 25 import android.util.Log; 26 27 import androidx.fragment.app.FragmentTransaction; 28 29 import com.android.settingslib.R; 30 import com.android.settingslib.bluetooth.BluetoothBroadcastUtils; 31 import com.android.settingslib.bluetooth.BluetoothUtils; 32 33 /** 34 * Finding a broadcast through QR code. 35 * 36 * To use intent action {@link BluetoothBroadcastUtils#ACTION_BLUETOOTH_LE_AUDIO_QR_CODE_SCANNER}, 37 * specify the bluetooth device sink of the broadcast to be provisioned in 38 * {@link BluetoothBroadcastUtils#EXTRA_BLUETOOTH_DEVICE_SINK} and check the operation for all 39 * coordinated set members throughout one session or not by 40 * {@link BluetoothBroadcastUtils#EXTRA_BLUETOOTH_SINK_IS_GROUP}. 41 */ 42 public class QrCodeScanModeActivity extends QrCodeScanModeBaseActivity { 43 private static final boolean DEBUG = BluetoothUtils.D; 44 private static final String TAG = "QrCodeScanModeActivity"; 45 46 private boolean mIsGroupOp; 47 private BluetoothDevice mSink; 48 49 @Override onCreate(Bundle savedInstanceState)50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 } 53 54 @Override handleIntent(Intent intent)55 protected void handleIntent(Intent intent) { 56 String action = intent != null ? intent.getAction() : null; 57 if (DEBUG) { 58 Log.d(TAG, "handleIntent(), action = " + action); 59 } 60 61 if (action == null) { 62 finish(); 63 return; 64 } 65 66 switch (action) { 67 case BluetoothBroadcastUtils.ACTION_BLUETOOTH_LE_AUDIO_QR_CODE_SCANNER: 68 showQrCodeScannerFragment(intent); 69 break; 70 default: 71 if (DEBUG) { 72 Log.e(TAG, "Launch with an invalid action"); 73 } 74 finish(); 75 } 76 } 77 showQrCodeScannerFragment(Intent intent)78 protected void showQrCodeScannerFragment(Intent intent) { 79 if (intent == null) { 80 if (DEBUG) { 81 Log.d(TAG, "intent is null, can not get bluetooth information from intent."); 82 } 83 return; 84 } 85 86 if (DEBUG) { 87 Log.d(TAG, "showQrCodeScannerFragment"); 88 } 89 90 mSink = intent.getParcelableExtra(EXTRA_BLUETOOTH_DEVICE_SINK); 91 mIsGroupOp = intent.getBooleanExtra(EXTRA_BLUETOOTH_SINK_IS_GROUP, false); 92 if (DEBUG) { 93 Log.d(TAG, "get extra from intent"); 94 } 95 96 QrCodeScanModeFragment fragment = 97 (QrCodeScanModeFragment) mFragmentManager.findFragmentByTag( 98 BluetoothBroadcastUtils.TAG_FRAGMENT_QR_CODE_SCANNER); 99 100 if (fragment == null) { 101 fragment = new QrCodeScanModeFragment(mIsGroupOp, mSink); 102 } else { 103 if (fragment.isVisible()) { 104 return; 105 } 106 107 // When the fragment in back stack but not on top of the stack, we can simply pop 108 // stack because current fragment transactions are arranged in an order 109 mFragmentManager.popBackStackImmediate(); 110 return; 111 } 112 final FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 113 114 fragmentTransaction.replace(R.id.fragment_container, fragment, 115 BluetoothBroadcastUtils.TAG_FRAGMENT_QR_CODE_SCANNER); 116 fragmentTransaction.commit(); 117 } 118 } 119 120