Show a dialog if bluetooth key is missing when reconnecting

Previous change is reverted due to test failure in b/362901443.

BUG: 360031750
Test: atest BluetoothKeyMissingDialogTest
Flag: com.android.settings.flags.enable_bluetooth_key_missing_dialog
Change-Id: I05b940e8aac26c14f93baa19c224ad98c291b891
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 7b79611..13aafc9 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3230,6 +3230,19 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".bluetooth.BluetoothKeyMissingDialog"
+                  android:permission="android.permission.BLUETOOTH_PRIVILEGED"
+                  android:excludeFromRecents="true"
+                  android:windowSoftInputMode="stateVisible|adjustResize"
+                  android:theme="@style/Theme.AlertDialog"
+                  android:exported="false"
+                  android:taskAffinity=".bluetooth.BluetoothKeyMissingDialog">
+            <intent-filter android:priority="1">
+                <action android:name="android.bluetooth.device.action.KEY_MISSING" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".bluetooth.RequestPermissionActivity"
                   android:excludeFromRecents="true"
                   android:permission="android.permission.BLUETOOTH_CONNECT"
@@ -3284,6 +3297,13 @@
             </intent-filter>
         </receiver>
 
+        <receiver android:name=".bluetooth.BluetoothKeyMissingReceiver"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.bluetooth.device.action.KEY_MISSING" />
+            </intent-filter>
+        </receiver>
+
         <receiver android:name=".bluetooth.BluetoothPermissionRequest"
                   android:exported="true"
                   android:permission="android.permission.BLUETOOTH_CONNECT">
diff --git a/aconfig/settings_bluetooth_declarations.aconfig b/aconfig/settings_bluetooth_declarations.aconfig
index 3d14288..0c423b5 100644
--- a/aconfig/settings_bluetooth_declarations.aconfig
+++ b/aconfig/settings_bluetooth_declarations.aconfig
@@ -34,3 +34,13 @@
       purpose: PURPOSE_BUGFIX
   }
 }
+
+flag {
+  name: "enable_bluetooth_key_missing_dialog"
+  namespace: "cross_device_experiences"
+  description: "Show a dialog if the bluetooth key is missing when reconnecting"
+  bug: "360031750"
+  metadata {
+      purpose: PURPOSE_BUGFIX
+  }
+}
diff --git a/res/layout/bluetooth_key_missing.xml b/res/layout/bluetooth_key_missing.xml
new file mode 100644
index 0000000..b9f8d86
--- /dev/null
+++ b/res/layout/bluetooth_key_missing.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+~ Copyright (C) 2024 The Android Open Source Project
+~
+~ Licensed under the Apache License, Version 2.0 (the "License");
+~ you may not use this file except in compliance with the License.
+~ You may obtain a copy of the License at
+~
+~      http://www.apache.org/licenses/LICENSE-2.0
+~
+~ Unless required by applicable law or agreed to in writing, software
+~ distributed under the License is distributed on an "AS IS" BASIS,
+~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+~ See the License for the specific language governing permissions and
+~ limitations under the License.
+-->
+
+<ScrollView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:paddingTop="@dimen/bluetooth_dialog_padding_top">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical"
+        android:paddingStart="24dp"
+        android:paddingEnd="24dp">
+
+        <ImageView
+            android:id="@id/preview_placeholder"
+            android:layout_width="32dp"
+            android:layout_height="32dp"
+            android:layout_gravity="center"
+            android:importantForAccessibility="no"
+            android:src="@drawable/ic_warning_24dp"
+            android:tint="@color/settingslib_materialColorOutline" />
+
+        <TextView
+            android:id="@+id/bluetooth_key_missing_title"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="20dp"
+            android:gravity="center"
+            android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Title" />
+
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="20dp"
+            android:text="@string/bluetooth_key_missing_message"
+            android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Body1" />
+
+    </LinearLayout>
+
+
+</ScrollView>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0fcb0d6..17a9a08 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1860,6 +1860,15 @@
     <!-- Bluetooth settings. Link text to bring the user to "scanning settings" screen. [CHAR LIMIT=NONE]-->
     <string name="bluetooth_scan_change">Change</string>
 
+    <!-- Dialog title when key is missing in a Bluetooth device -->
+    <string name="bluetooth_key_missing_title"><xliff:g id="device_name">%1$s</xliff:g> not connected</string>
+    <!-- Dialog content when key is missing in a Bluetooth device -->
+    <string name="bluetooth_key_missing_message">For your security, forget this device, then pair it again</string>
+    <!-- Button text to forget device when bluetooth key is missing -->
+    <string name="bluetooth_key_missing_forget">Forget device</string>
+    <!-- Button text to cancel when bluetooth key is missing-->
+    <string name="bluetooth_key_missing_cancel">Cancel</string>
+
     <!-- Title of device details screen [CHAR LIMIT=28]-->
     <string name="device_details_title">Device details</string>
     <!-- Title for keyboard settings preferences. [CHAR LIMIT=50] -->
diff --git a/src/com/android/settings/bluetooth/BluetoothKeyMissingDialog.java b/src/com/android/settings/bluetooth/BluetoothKeyMissingDialog.java
new file mode 100644
index 0000000..46975f7
--- /dev/null
+++ b/src/com/android/settings/bluetooth/BluetoothKeyMissingDialog.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.bluetooth;
+
+import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
+
+import android.bluetooth.BluetoothDevice;
+import android.content.Intent;
+import android.os.Bundle;
+
+import androidx.annotation.Nullable;
+import androidx.fragment.app.FragmentActivity;
+
+/** A dialog to ask the user to forget a bluetooth device when the key is missing. */
+public class BluetoothKeyMissingDialog extends FragmentActivity {
+    public static final String FRAGMENT_TAG = "BtKeyMissingFrg";
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
+        Intent intent = getIntent();
+        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+        if (device == null) {
+            finish();
+            return;
+        }
+        BluetoothKeyMissingDialogFragment fragment = new BluetoothKeyMissingDialogFragment(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
new file mode 100644
index 0000000..a8e3aae
--- /dev/null
+++ b/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogFragment.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settings.bluetooth;
+
+import android.app.Dialog;
+import android.app.settings.SettingsEnums;
+import android.bluetooth.BluetoothDevice;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.TextView;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
+
+import com.android.settings.R;
+import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
+
+/**
+ * A dialogFragment used by {@link BluetoothKeyMissingDialog} to create a dialog for the
+ * bluetooth device.
+ */
+public class BluetoothKeyMissingDialogFragment extends InstrumentedDialogFragment
+        implements OnClickListener {
+
+    private static final String TAG = "BTKeyMissingDialogFragment";
+
+    private BluetoothDevice mBluetoothDevice;
+
+    public BluetoothKeyMissingDialogFragment(@NonNull BluetoothDevice bluetoothDevice) {
+        mBluetoothDevice = bluetoothDevice;
+    }
+
+    @NonNull
+    @Override
+    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
+        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);
+        keyMissingTitle.setText(
+                getString(R.string.bluetooth_key_missing_title, mBluetoothDevice.getName()));
+        builder.setView(view);
+        builder.setPositiveButton(getString(R.string.bluetooth_key_missing_forget), this);
+        builder.setNegativeButton(getString(R.string.bluetooth_key_missing_cancel), this);
+        AlertDialog dialog = builder.create();
+        dialog.setCanceledOnTouchOutside(false);
+        return dialog;
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        if (!getActivity().isFinishing()) {
+            getActivity().finish();
+        }
+    }
+
+    @Override
+    public void onClick(DialogInterface dialog, int which) {
+        if (which == DialogInterface.BUTTON_POSITIVE) {
+            Log.i(
+                    TAG,
+                    "Positive button clicked, remove bond for "
+                            + mBluetoothDevice.getAnonymizedAddress());
+            mBluetoothDevice.removeBond();
+        } else if (which == DialogInterface.BUTTON_NEGATIVE) {
+            Log.i(TAG, "Negative button clicked for " + mBluetoothDevice.getAnonymizedAddress());
+        }
+        if (!getActivity().isFinishing()) {
+            getActivity().finish();
+        }
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return SettingsEnums.BLUETOOTH_KEY_MISSING_DIALOG_FRAGMENT;
+    }
+}
diff --git a/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiver.java b/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiver.java
new file mode 100644
index 0000000..d7a5343
--- /dev/null
+++ b/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiver.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.bluetooth;
+
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.PowerManager;
+import android.os.UserHandle;
+import android.text.TextUtils;
+import android.util.Log;
+
+import androidx.core.app.NotificationCompat;
+
+import com.android.settings.R;
+import com.android.settings.flags.Flags;
+
+/**
+ * BluetoothKeyMissingReceiver is a receiver for Bluetooth key missing error when reconnecting to a
+ * bonded bluetooth device.
+ */
+public final class BluetoothKeyMissingReceiver extends BroadcastReceiver {
+    private static final String TAG = "BtKeyMissingReceiver";
+    private static final String CHANNEL_ID = "bluetooth_notification_channel";
+    private static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (!Flags.enableBluetoothKeyMissingDialog()) {
+            return;
+        }
+        String action = intent.getAction();
+        if (action == null) {
+            return;
+        }
+
+        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+        PowerManager powerManager = context.getSystemService(PowerManager.class);
+        if (TextUtils.equals(action, BluetoothDevice.ACTION_KEY_MISSING)) {
+            Log.d(TAG, "Receive ACTION_KEY_MISSING");
+            if (shouldShowDialog(context, device, powerManager)) {
+                Intent pairingIntent = getKeyMissingDialogIntent(context, device);
+                Log.d(TAG, "Show key missing dialog:" + device);
+                context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);
+            } else {
+                Log.d(TAG, "Show key missing notification: " + device);
+                showNotification(context, device);
+            }
+        }
+    }
+
+    private Intent getKeyMissingDialogIntent(Context context, BluetoothDevice device) {
+        Intent pairingIntent = new Intent();
+        pairingIntent.setClass(context, BluetoothKeyMissingDialog.class);
+        pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
+        pairingIntent.setAction(BluetoothDevice.ACTION_KEY_MISSING);
+        pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        return pairingIntent;
+    }
+
+    private boolean shouldShowDialog(
+            Context context, BluetoothDevice device, PowerManager powerManager) {
+        return LocalBluetoothPreferences.shouldShowDialogInForeground(context, device)
+                && powerManager.isInteractive();
+    }
+
+    private void showNotification(Context context, BluetoothDevice bluetoothDevice) {
+        NotificationManager nm = context.getSystemService(NotificationManager.class);
+        NotificationChannel notificationChannel =
+                new NotificationChannel(
+                        CHANNEL_ID,
+                        context.getString(R.string.bluetooth),
+                        NotificationManager.IMPORTANCE_HIGH);
+        nm.createNotificationChannel(notificationChannel);
+
+        PendingIntent pairIntent =
+                PendingIntent.getActivity(
+                        context,
+                        0,
+                        getKeyMissingDialogIntent(context, bluetoothDevice),
+                        PendingIntent.FLAG_ONE_SHOT
+                                | PendingIntent.FLAG_UPDATE_CURRENT
+                                | PendingIntent.FLAG_IMMUTABLE);
+
+        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
+                CHANNEL_ID)
+                .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
+                .setTicker(context.getString(R.string.bluetooth_notif_ticker))
+                .setLocalOnly(true);
+        builder.setContentTitle(
+                        context.getString(
+                                R.string.bluetooth_key_missing_title, bluetoothDevice.getName()))
+                .setContentText(context.getString(R.string.bluetooth_key_missing_message))
+                .setContentIntent(pairIntent)
+                .setAutoCancel(true)
+                .setDefaults(Notification.DEFAULT_SOUND)
+                .setColor(
+                        context.getColor(
+                                com.android.internal.R.color.system_notification_accent_color));
+
+        nm.notify(NOTIFICATION_ID, builder.build());
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogTest.java
new file mode 100644
index 0000000..a47101e
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingDialogTest.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settings.bluetooth;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
+
+import android.bluetooth.BluetoothDevice;
+
+import androidx.appcompat.app.AlertDialog;
+import androidx.fragment.app.FragmentActivity;
+
+import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(shadows = ShadowAlertDialogCompat.class)
+public class BluetoothKeyMissingDialogTest {
+    @Mock private BluetoothDevice mBluetoothDevice;
+
+    private BluetoothKeyMissingDialogFragment mFragment = null;
+    private FragmentActivity mActivity = null;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mActivity = Robolectric.setupActivity(FragmentActivity.class);
+        mFragment = new BluetoothKeyMissingDialogFragment(mBluetoothDevice);
+        mActivity
+                .getSupportFragmentManager()
+                .beginTransaction()
+                .add(mFragment, null)
+                .commit();
+        shadowMainLooper().idle();
+    }
+
+    @Test
+    public void clickForgetDevice_removeBond() {
+        mFragment.onClick(mFragment.getDialog(), AlertDialog.BUTTON_POSITIVE);
+
+        verify(mBluetoothDevice).removeBond();
+        assertThat(mActivity.isFinishing()).isTrue();
+    }
+
+    @Test
+    public void clickCancel_notRemoveBond() {
+        mFragment.onClick(mFragment.getDialog(), AlertDialog.BUTTON_NEGATIVE);
+
+        verify(mBluetoothDevice, never()).removeBond();
+        assertThat(mActivity.isFinishing()).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiverTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiverTest.java
new file mode 100644
index 0000000..c764ed6
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiverTest.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.bluetooth;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.UserHandle;
+import android.platform.test.annotations.DisableFlags;
+import android.platform.test.annotations.EnableFlags;
+import android.platform.test.flag.junit.SetFlagsRule;
+
+import com.android.settings.flags.Flags;
+import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
+import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowApplication;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothUtils.class})
+public class BluetoothKeyMissingReceiverTest {
+    @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
+
+    private Context mContext;
+    private ShadowApplication mShadowApplication;
+    private ShadowBluetoothAdapter mShadowBluetoothAdapter;
+    @Mock private LocalBluetoothManager mLocalBtManager;
+    @Mock private NotificationManager mNm;
+    @Mock private BluetoothDevice mBluetoothDevice;
+
+    @Before
+    public void setUp() {
+        mContext = spy(RuntimeEnvironment.getApplication());
+        mShadowApplication = Shadow.extract(mContext);
+        mShadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
+        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
+        mShadowBluetoothAdapter.setEnabled(true);
+        ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
+    }
+
+    @After
+    public void tearDown() {
+        ShadowBluetoothUtils.reset();
+    }
+
+    @Test
+    public void broadcastReceiver_isRegistered() {
+        List<ShadowApplication.Wrapper> registeredReceivers =
+                mShadowApplication.getRegisteredReceivers();
+
+        int matchedCount =
+                registeredReceivers.stream()
+                        .filter(
+                                receiver ->
+                                        BluetoothKeyMissingReceiver.class
+                                                .getSimpleName()
+                                                .equals(
+                                                        receiver.broadcastReceiver
+                                                                .getClass()
+                                                                .getSimpleName()))
+                        .collect(Collectors.toList())
+                        .size();
+        assertThat(matchedCount).isEqualTo(1);
+    }
+
+    @Test
+    @DisableFlags(Flags.FLAG_ENABLE_BLUETOOTH_KEY_MISSING_DIALOG)
+    public void broadcastReceiver_receiveKeyMissingIntentFlagOff_doNothing() {
+        Intent intent = spy(new Intent(BluetoothDevice.ACTION_KEY_MISSING));
+        when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(mBluetoothDevice);
+        BluetoothKeyMissingReceiver bluetoothKeyMissingReceiver = getReceiver(intent);
+        bluetoothKeyMissingReceiver.onReceive(mContext, intent);
+
+        verifyNoInteractions(mNm);
+    }
+
+    @Test
+    @EnableFlags(Flags.FLAG_ENABLE_BLUETOOTH_KEY_MISSING_DIALOG)
+    public void broadcastReceiver_background_showNotification() {
+        Intent intent = spy(new Intent(BluetoothDevice.ACTION_KEY_MISSING));
+        when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(mBluetoothDevice);
+        BluetoothKeyMissingReceiver bluetoothKeyMissingReceiver = getReceiver(intent);
+        bluetoothKeyMissingReceiver.onReceive(mContext, intent);
+
+        verify(mNm).notify(eq(android.R.drawable.stat_sys_data_bluetooth), any(Notification.class));
+        verify(mContext, never()).startActivityAsUser(any(), any());
+    }
+
+    @Test
+    @EnableFlags(Flags.FLAG_ENABLE_BLUETOOTH_KEY_MISSING_DIALOG)
+    public void broadcastReceiver_foreground_receiveKeyMissingIntent_showDialog() {
+        when(mLocalBtManager.isForegroundActivity()).thenReturn(true);
+        Intent intent = spy(new Intent(BluetoothDevice.ACTION_KEY_MISSING));
+        when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(mBluetoothDevice);
+        BluetoothKeyMissingReceiver bluetoothKeyMissingReceiver = getReceiver(intent);
+        bluetoothKeyMissingReceiver.onReceive(mContext, intent);
+
+        verifyNoInteractions(mNm);
+        ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
+        verify(mContext).startActivityAsUser(captor.capture(), eq(UserHandle.CURRENT));
+        assertThat(captor.getValue().getComponent().getClassName())
+                .isEqualTo(BluetoothKeyMissingDialog.class.getName());
+    }
+
+    private BluetoothKeyMissingReceiver getReceiver(Intent intent) {
+        assertThat(mShadowApplication.hasReceiverForIntent(intent)).isTrue();
+        List<BroadcastReceiver> receiversForIntent =
+                mShadowApplication.getReceiversForIntent(intent);
+        assertThat(receiversForIntent).hasSize(1);
+        BroadcastReceiver broadcastReceiver = receiversForIntent.get(0);
+        assertThat(broadcastReceiver).isInstanceOf(BluetoothKeyMissingReceiver.class);
+        return (BluetoothKeyMissingReceiver) broadcastReceiver;
+    }
+}