Exit bluetooth device details page when bluetooth is off

Bug: 333836213
Test: atest BluetoothDeviceDetailsFragmentTest
Change-Id: I4632c10a8f6f828885825e1ee46df6537a0c050c
This commit is contained in:
Haijie Hong
2024-04-15 13:29:42 +08:00
parent 860bd52451
commit 1ab367d3a0

View File

@@ -19,7 +19,9 @@ package com.android.settings.bluetooth;
import static android.bluetooth.BluetoothDevice.BOND_NONE; import static android.bluetooth.BluetoothDevice.BOND_NONE;
import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
import android.app.Activity;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -51,6 +53,7 @@ import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.inputmethod.KeyboardSettingsPreferenceController; import com.android.settings.inputmethod.KeyboardSettingsPreferenceController;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settings.slices.SlicePreferenceController; import com.android.settings.slices.SlicePreferenceController;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
@@ -98,6 +101,20 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
private UserManager mUserManager; 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() { public BluetoothDeviceDetailsFragment() {
super(DISALLOW_CONFIG_BLUETOOTH); super(DISALLOW_CONFIG_BLUETOOTH);
} }
@@ -183,6 +200,14 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
use(BlockingPrefWithSliceController.class).setSliceUri(sliceEnabled use(BlockingPrefWithSliceController.class).setSliceUri(sliceEnabled
? featureProvider.getBluetoothDeviceSettingsUri(mCachedDevice.getDevice()) ? featureProvider.getBluetoothDeviceSettingsUri(mCachedDevice.getDevice())
: null); : null);
mManager.getEventManager().registerCallback(mBluetoothCallback);
}
@Override
public void onDetach() {
super.onDetach();
mManager.getEventManager().unregisterCallback(mBluetoothCallback);
} }
private void updateExtraControlUri(int viewWidth) { private void updateExtraControlUri(int viewWidth) {