From f761a300403b27fcd555e5b90b702428279d1589 Mon Sep 17 00:00:00 2001 From: Haijie Hong Date: Mon, 13 Jan 2025 16:17:09 +0800 Subject: [PATCH] Fix exception in key missing dialog when rotating screen BUG: 387915075 Test: atest BluetoothKeyMissingDialogTest Flag: com.android.settings.flags.enable_bluetooth_key_missing_dialog Change-Id: I966954f27d074a5ca0dc329cb142c1ab66b3b013 --- .../bluetooth/BluetoothKeyMissingDialog.java | 3 ++- .../BluetoothKeyMissingDialogFragment.java | 17 ++++++++++++++--- .../BluetoothKeyMissingDialogTest.java | 16 ++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothKeyMissingDialog.java b/src/com/android/settings/bluetooth/BluetoothKeyMissingDialog.java index 46975f77726..fa43c24ad14 100644 --- a/src/com/android/settings/bluetooth/BluetoothKeyMissingDialog.java +++ b/src/com/android/settings/bluetooth/BluetoothKeyMissingDialog.java @@ -40,7 +40,8 @@ public class BluetoothKeyMissingDialog extends FragmentActivity { finish(); return; } - BluetoothKeyMissingDialogFragment fragment = new BluetoothKeyMissingDialogFragment(device); + BluetoothKeyMissingDialogFragment fragment = + BluetoothKeyMissingDialogFragment.newInstance(device); fragment.show(getSupportFragmentManager(), FRAGMENT_TAG); closeSystemDialogs(); } diff --git a/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogFragment.java b/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogFragment.java index a8e3aae175a..342af34338e 100644 --- a/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogFragment.java +++ b/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogFragment.java @@ -31,6 +31,7 @@ import androidx.appcompat.app.AlertDialog; import com.android.settings.R; import com.android.settings.core.instrumentation.InstrumentedDialogFragment; +import com.android.settingslib.bluetooth.LocalBluetoothManager; /** * A dialogFragment used by {@link BluetoothKeyMissingDialog} to create a dialog for the @@ -40,16 +41,26 @@ public class BluetoothKeyMissingDialogFragment extends InstrumentedDialogFragmen implements OnClickListener { private static final String TAG = "BTKeyMissingDialogFragment"; + private static final String KEY_CACHED_DEVICE_ADDRESS = "cached_device"; private BluetoothDevice mBluetoothDevice; - public BluetoothKeyMissingDialogFragment(@NonNull BluetoothDevice bluetoothDevice) { - mBluetoothDevice = bluetoothDevice; + /** Creates a new instant of the fragment. */ + public static BluetoothKeyMissingDialogFragment newInstance(BluetoothDevice device) { + Bundle args = new Bundle(1); + args.putString(KEY_CACHED_DEVICE_ADDRESS, device.getAddress()); + BluetoothKeyMissingDialogFragment fragment = new BluetoothKeyMissingDialogFragment(); + fragment.setArguments(args); + return fragment; } @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + String deviceAddress = getArguments().getString(KEY_CACHED_DEVICE_ADDRESS); + LocalBluetoothManager manager = Utils.getLocalBtManager(getContext()); + mBluetoothDevice = manager.getBluetoothAdapter().getRemoteDevice(deviceAddress); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); View view = getActivity().getLayoutInflater().inflate(R.layout.bluetooth_key_missing, null); TextView keyMissingTitle = view.findViewById(R.id.bluetooth_key_missing_title); @@ -66,7 +77,7 @@ public class BluetoothKeyMissingDialogFragment extends InstrumentedDialogFragmen @Override public void onDestroy() { super.onDestroy(); - if (!getActivity().isFinishing()) { + if (!getActivity().isChangingConfigurations() && !getActivity().isFinishing()) { getActivity().finish(); } } diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogTest.java index a47101e7b79..14e263c0fb2 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.robolectric.shadows.ShadowLooper.shadowMainLooper; import android.bluetooth.BluetoothDevice; @@ -27,10 +28,13 @@ import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.FragmentActivity; import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; +import com.android.settings.testutils.shadow.ShadowBluetoothUtils; +import com.android.settingslib.bluetooth.LocalBluetoothManager; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Answers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.Robolectric; @@ -38,18 +42,26 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) -@Config(shadows = ShadowAlertDialogCompat.class) +@Config(shadows = {ShadowAlertDialogCompat.class, ShadowBluetoothUtils.class}) public class BluetoothKeyMissingDialogTest { @Mock private BluetoothDevice mBluetoothDevice; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private LocalBluetoothManager mLocalBtManager; private BluetoothKeyMissingDialogFragment mFragment = null; private FragmentActivity mActivity = null; + private static final String MAC_ADDRESS = "12:34:56:78:90:12"; + @Before public void setUp() { MockitoAnnotations.initMocks(this); + when(mBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); + when(mLocalBtManager.getBluetoothAdapter().getRemoteDevice(MAC_ADDRESS)) + .thenReturn(mBluetoothDevice); + ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager; mActivity = Robolectric.setupActivity(FragmentActivity.class); - mFragment = new BluetoothKeyMissingDialogFragment(mBluetoothDevice); + mFragment = BluetoothKeyMissingDialogFragment.newInstance(mBluetoothDevice); mActivity .getSupportFragmentManager() .beginTransaction()