1 /* 2 * Copyright 2018 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.car.settings.bluetooth; 18 19 import android.bluetooth.BluetoothAdapter; 20 import android.bluetooth.BluetoothDevice; 21 import android.content.Context; 22 23 import androidx.annotation.VisibleForTesting; 24 25 import com.android.car.settings.R; 26 import com.android.car.settings.common.SettingsFragment; 27 import com.android.settingslib.bluetooth.BluetoothCallback; 28 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 29 import com.android.settingslib.bluetooth.LocalBluetoothManager; 30 31 /** 32 * Page which scans for Bluetooth devices so that a user can select a new device to pair. When a 33 * device pairs, this page will finish. 34 */ 35 public class BluetoothPairingSelectionFragment extends SettingsFragment { 36 37 @VisibleForTesting 38 final BluetoothCallback mCallback = new BluetoothCallback() { 39 @Override 40 public void onBluetoothStateChanged(int bluetoothState) { 41 // If bluetooth is turned off, go back since pairing is no longer possible 42 if (bluetoothState == BluetoothAdapter.STATE_OFF) { 43 goBack(); 44 } 45 } 46 47 @Override 48 public void onScanningStateChanged(boolean started) { 49 } 50 51 @Override 52 public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) { 53 if (bondState == BluetoothDevice.BOND_BONDED) { 54 goBack(); 55 } 56 } 57 }; 58 59 private LocalBluetoothManager mManager; 60 61 @Override getPreferenceScreenResId()62 protected int getPreferenceScreenResId() { 63 return R.xml.bluetooth_pairing_selection_fragment; 64 } 65 66 @Override onAttach(Context context)67 public void onAttach(Context context) { 68 super.onAttach(context); 69 mManager = BluetoothUtils.getLocalBtManager(context); 70 if (mManager == null) { 71 goBack(); 72 } 73 } 74 75 @Override onStart()76 public void onStart() { 77 super.onStart(); 78 mManager.setForegroundActivity(requireActivity()); 79 mManager.getEventManager().registerCallback(mCallback); 80 getToolbar().getProgressBar().setVisible(true); 81 } 82 83 @Override onStop()84 public void onStop() { 85 super.onStop(); 86 mManager.setForegroundActivity(null); 87 mManager.getEventManager().unregisterCallback(mCallback); 88 getToolbar().getProgressBar().setVisible(false); 89 } 90 } 91