Don't show key missing dialog for exclusively managed device

Test: local tested
Flag: EXEMPT minor fix
Bug: 404890083
Change-Id: I5ec0541b55af81bd3424e3b1af329cecd3a480ad
This commit is contained in:
Haijie Hong
2025-03-20 13:39:36 +08:00
parent afe39e66e9
commit d22895a781
2 changed files with 28 additions and 0 deletions

View File

@@ -59,6 +59,10 @@ public final class BluetoothKeyMissingReceiver extends BroadcastReceiver {
if (device == null) {
return;
}
if (BluetoothUtils.isExclusivelyManagedBluetoothDevice(context, device)) {
Log.d(TAG, "Exclusively managed device " + device + ", skip");
return;
}
PowerManager powerManager = context.getSystemService(PowerManager.class);
if (TextUtils.equals(action, BluetoothDevice.ACTION_KEY_MISSING)) {
Log.d(TAG, "Receive ACTION_KEY_MISSING");

View File

@@ -33,6 +33,8 @@ import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
@@ -68,9 +70,12 @@ public class BluetoothKeyMissingReceiverTest {
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private static final String TEST_EXCLUSIVE_MANAGER = "com.test.manager";
private Context mContext;
private ShadowApplication mShadowApplication;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Mock private PackageManager mPackageManager;
@Mock private LocalBluetoothManager mLocalBtManager;
@Mock private NotificationManager mNm;
@Mock private BluetoothDevice mBluetoothDevice;
@@ -78,6 +83,7 @@ public class BluetoothKeyMissingReceiverTest {
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.getApplication());
when(mContext.getPackageManager()).thenReturn(mPackageManager);
mShadowApplication = Shadow.extract(mContext);
mShadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
@@ -121,6 +127,24 @@ public class BluetoothKeyMissingReceiverTest {
verifyNoInteractions(mNm);
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_BLUETOOTH_KEY_MISSING_DIALOG)
public void broadcastReceiver_exclusiveManaged_skip() throws Exception {
Intent intent = spy(new Intent(BluetoothDevice.ACTION_KEY_MISSING));
when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(mBluetoothDevice);
when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
TEST_EXCLUSIVE_MANAGER.getBytes());
when(mPackageManager.getApplicationInfo(
TEST_EXCLUSIVE_MANAGER, 0)).thenReturn(new ApplicationInfo());
BluetoothKeyMissingReceiver bluetoothKeyMissingReceiver = getReceiver(intent);
bluetoothKeyMissingReceiver.onReceive(mContext, intent);
verifyNoInteractions(mNm);
verify(mContext, never()).startActivityAsUser(any(), any());
}
@Test
@Ignore("Cannot test reflection")
@EnableFlags(Flags.FLAG_ENABLE_BLUETOOTH_KEY_MISSING_DIALOG)