Fix foreground Service background launch restriction

Remove setForeground() in BluetoothPairingService. Refactor
the behavior to not holding service in the background.

Bug: 184901840
Test: make RunSettingsRoboTests -j56
Change-Id: Iaeb96943971f55dbeb3dba4b06b28abe770ba3c2
This commit is contained in:
Hugh Chen
2021-05-14 15:49:51 +08:00
parent d31c3649ba
commit 3eb374adb4
3 changed files with 236 additions and 74 deletions

View File

@@ -31,38 +31,39 @@ import android.os.UserHandle;
*/
public final class BluetoothPairingRequest extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null || !action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
return;
}
PowerManager powerManager = context.getSystemService(PowerManager.class);
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
BluetoothDevice.ERROR);
String deviceAddress = device != null ? device.getAddress() : null;
String deviceName = device != null ? device.getName() : null;
boolean shouldShowDialog = LocalBluetoothPreferences.shouldShowDialogInForeground(
context, deviceAddress, deviceName);
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null || !action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
return;
}
// Skips consent pairing dialog if the device was recently associated with CDM
if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_CONSENT
&& device.canBondWithoutDialog()) {
device.setPairingConfirmation(true);
} else if (powerManager.isInteractive() && shouldShowDialog) {
// Since the screen is on and the BT-related activity is in the foreground,
// just open the dialog
// convert broadcast intent into activity intent (same action string)
Intent pairingIntent = BluetoothPairingService.getPairingDialogIntent(context, intent,
BluetoothDevice.EXTRA_PAIRING_INITIATOR_FOREGROUND);
PowerManager powerManager = context.getSystemService(PowerManager.class);
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
BluetoothDevice.ERROR);
String deviceAddress = device != null ? device.getAddress() : null;
String deviceName = device != null ? device.getName() : null;
boolean shouldShowDialog = LocalBluetoothPreferences.shouldShowDialogInForeground(
context, deviceAddress, deviceName);
context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);
} else {
// Put up a notification that leads to the dialog
intent.setClass(context, BluetoothPairingService.class);
context.startServiceAsUser(intent, UserHandle.CURRENT);
// Skips consent pairing dialog if the device was recently associated with CDM
if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_CONSENT
&& device.canBondWithoutDialog()) {
device.setPairingConfirmation(true);
} else if (powerManager.isInteractive() && shouldShowDialog) {
// Since the screen is on and the BT-related activity is in the foreground,
// just open the dialog
// convert broadcast intent into activity intent (same action string)
Intent pairingIntent = BluetoothPairingService.getPairingDialogIntent(context, intent,
BluetoothDevice.EXTRA_PAIRING_INITIATOR_FOREGROUND);
context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);
} else {
// Put up a notification that leads to the dialog
intent.setClass(context, BluetoothPairingService.class);
intent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
context.startServiceAsUser(intent, UserHandle.CURRENT);
}
}
}
}