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