From 1ab367d3a00e9c435d1a34e59f94834166d25691 Mon Sep 17 00:00:00 2001 From: Haijie Hong Date: Mon, 15 Apr 2024 13:29:42 +0800 Subject: [PATCH] Exit bluetooth device details page when bluetooth is off Bug: 333836213 Test: atest BluetoothDeviceDetailsFragmentTest Change-Id: I4632c10a8f6f828885825e1ee46df6537a0c050c --- .../BluetoothDeviceDetailsFragment.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java index 87b2c6b65d0..e0171439198 100644 --- a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java +++ b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java @@ -19,7 +19,9 @@ package com.android.settings.bluetooth; import static android.bluetooth.BluetoothDevice.BOND_NONE; import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; +import android.app.Activity; import android.app.settings.SettingsEnums; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.content.Intent; @@ -51,6 +53,7 @@ import com.android.settings.dashboard.RestrictedDashboardFragment; import com.android.settings.inputmethod.KeyboardSettingsPreferenceController; import com.android.settings.overlay.FeatureFactory; import com.android.settings.slices.SlicePreferenceController; +import com.android.settingslib.bluetooth.BluetoothCallback; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.core.AbstractPreferenceController; @@ -98,6 +101,20 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment private UserManager mUserManager; + private final BluetoothCallback mBluetoothCallback = + new BluetoothCallback() { + @Override + public void onBluetoothStateChanged(int bluetoothState) { + if (bluetoothState == BluetoothAdapter.STATE_OFF) { + Log.i(TAG, "Bluetooth is off, exit activity."); + Activity activity = getActivity(); + if (activity != null) { + activity.finish(); + } + } + } + }; + public BluetoothDeviceDetailsFragment() { super(DISALLOW_CONFIG_BLUETOOTH); } @@ -183,6 +200,14 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment use(BlockingPrefWithSliceController.class).setSliceUri(sliceEnabled ? featureProvider.getBluetoothDeviceSettingsUri(mCachedDevice.getDevice()) : null); + + mManager.getEventManager().registerCallback(mBluetoothCallback); + } + + @Override + public void onDetach() { + super.onDetach(); + mManager.getEventManager().unregisterCallback(mBluetoothCallback); } private void updateExtraControlUri(int viewWidth) {