Merge "Fix foreground Service background launch restriction" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-05-14 09:54:12 +00:00
committed by Android (Google) Code Review
3 changed files with 236 additions and 74 deletions

View File

@@ -37,9 +37,9 @@ public final class BluetoothPairingRequest extends BroadcastReceiver {
if (action == null || !action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) { if (action == null || !action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
return; return;
} }
PowerManager powerManager = context.getSystemService(PowerManager.class); PowerManager powerManager = context.getSystemService(PowerManager.class);
BluetoothDevice device = BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
BluetoothDevice.ERROR); BluetoothDevice.ERROR);
String deviceAddress = device != null ? device.getAddress() : null; String deviceAddress = device != null ? device.getAddress() : null;
@@ -62,6 +62,7 @@ public final class BluetoothPairingRequest extends BroadcastReceiver {
} else { } else {
// Put up a notification that leads to the dialog // Put up a notification that leads to the dialog
intent.setClass(context, BluetoothPairingService.class); intent.setClass(context, BluetoothPairingService.class);
intent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
context.startServiceAsUser(intent, UserHandle.CURRENT); context.startServiceAsUser(intent, UserHandle.CURRENT);
} }
} }

View File

@@ -31,6 +31,9 @@ import android.os.IBinder;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.core.app.NotificationCompat;
import com.android.settings.R; import com.android.settings.R;
/** /**
@@ -39,10 +42,14 @@ import com.android.settings.R;
*/ */
public final class BluetoothPairingService extends Service { public final class BluetoothPairingService extends Service {
private static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth; @VisibleForTesting
static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
private static final String ACTION_DISMISS_PAIRING = @VisibleForTesting
static final String ACTION_DISMISS_PAIRING =
"com.android.settings.bluetooth.ACTION_DISMISS_PAIRING"; "com.android.settings.bluetooth.ACTION_DISMISS_PAIRING";
@VisibleForTesting
static final String ACTION_PAIRING_DIALOG =
"com.android.settings.bluetooth.ACTION_PAIRING_DIALOG";
private static final String BLUETOOTH_NOTIFICATION_CHANNEL = private static final String BLUETOOTH_NOTIFICATION_CHANNEL =
"bluetooth_notification_channel"; "bluetooth_notification_channel";
@@ -51,6 +58,9 @@ public final class BluetoothPairingService extends Service {
private BluetoothDevice mDevice; private BluetoothDevice mDevice;
@VisibleForTesting
NotificationManager mNm;
public static Intent getPairingDialogIntent(Context context, Intent intent, int initiator) { public static Intent getPairingDialogIntent(Context context, Intent intent, int initiator) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
@@ -80,33 +90,35 @@ public final class BluetoothPairingService extends Service {
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) { if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
BluetoothDevice.ERROR); BluetoothDevice.ERROR);
Log.d(TAG, "onReceive() Bond state change : " + bondState + ", device name : "
+ mDevice.getName());
if ((bondState != BluetoothDevice.BOND_NONE) && (bondState != BluetoothDevice.BOND_BONDED)) { if ((bondState != BluetoothDevice.BOND_NONE) && (bondState != BluetoothDevice.BOND_BONDED)) {
return; return;
} }
} else if (action.equals(ACTION_DISMISS_PAIRING)) { } else if (action.equals(ACTION_DISMISS_PAIRING)) {
Log.d(TAG, "Notification cancel " + mDevice.getAddress() + " (" + Log.d(TAG, "Notification cancel " + " (" +
mDevice.getName() + ")"); mDevice.getName() + ")");
mDevice.cancelPairing(); mDevice.cancelPairing();
} else { } else {
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
BluetoothDevice.ERROR); BluetoothDevice.ERROR);
Log.d(TAG, "Dismiss pairing for " + mDevice.getAddress() + " (" + Log.d(TAG, "Dismiss pairing for " + " (" +
mDevice.getName() + "), BondState: " + bondState); mDevice.getName() + "), BondState: " + bondState);
} }
stopForeground(true);
mNm.cancel(NOTIFICATION_ID);
stopSelf(); stopSelf();
} }
}; };
@Override @Override
public void onCreate() { public void onCreate() {
NotificationManager mgr = (NotificationManager)this mNm = getSystemService(NotificationManager.class);
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel( NotificationChannel notificationChannel = new NotificationChannel(
BLUETOOTH_NOTIFICATION_CHANNEL, BLUETOOTH_NOTIFICATION_CHANNEL,
this.getString(R.string.bluetooth), this.getString(R.string.bluetooth),
NotificationManager.IMPORTANCE_HIGH); NotificationManager.IMPORTANCE_HIGH);
mgr.createNotificationChannel(notificationChannel); mNm.createNotificationChannel(notificationChannel);
} }
@Override @Override
@@ -116,23 +128,8 @@ public final class BluetoothPairingService extends Service {
stopSelf(); stopSelf();
return START_NOT_STICKY; return START_NOT_STICKY;
} }
String action = intent.getAction();
Resources res = getResources(); Log.d(TAG, "onStartCommand() action : " + action);
Notification.Builder builder = new Notification.Builder(this,
BLUETOOTH_NOTIFICATION_CHANNEL)
.setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
.setTicker(res.getString(R.string.bluetooth_notif_ticker))
.setLocalOnly(true);
PendingIntent pairIntent = PendingIntent.getActivity(this, 0,
getPairingDialogIntent(this, intent,
BluetoothDevice.EXTRA_PAIRING_INITIATOR_BACKGROUND),
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_IMMUTABLE);
PendingIntent dismissIntent = PendingIntent.getBroadcast(this, 0,
new Intent(ACTION_DISMISS_PAIRING), PendingIntent.FLAG_ONE_SHOT
| PendingIntent.FLAG_IMMUTABLE);
mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
@@ -142,26 +139,16 @@ public final class BluetoothPairingService extends Service {
return START_NOT_STICKY; return START_NOT_STICKY;
} }
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); if (TextUtils.equals(action, BluetoothDevice.ACTION_PAIRING_REQUEST)) {
if (TextUtils.isEmpty(name)) { createPairingNotification(intent);
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); } else if (TextUtils.equals(action, ACTION_DISMISS_PAIRING)) {
name = device != null ? device.getAlias() : res.getString(android.R.string.unknownName); Log.d(TAG, "Notification cancel " + " (" + mDevice.getName() + ")");
} mDevice.cancelPairing();
mNm.cancel(NOTIFICATION_ID);
Log.d(TAG, "Show pairing notification for " + mDevice.getAddress() + " (" + name + ")"); stopSelf();
} else if (TextUtils.equals(action, ACTION_PAIRING_DIALOG)) {
Notification.Action pairAction = new Notification.Action.Builder(0, Intent pairingDialogIntent = getPairingDialogIntent(this, intent,
res.getString(R.string.bluetooth_device_context_pair_connect), pairIntent).build(); BluetoothDevice.EXTRA_PAIRING_INITIATOR_BACKGROUND);
Notification.Action dismissAction = new Notification.Action.Builder(0,
res.getString(android.R.string.cancel), dismissIntent).build();
builder.setContentTitle(res.getString(R.string.bluetooth_notif_title))
.setContentText(res.getString(R.string.bluetooth_notif_message, name))
.setContentIntent(pairIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setColor(getColor(com.android.internal.R.color.system_notification_accent_color))
.addAction(pairAction)
.addAction(dismissAction);
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
@@ -170,8 +157,58 @@ public final class BluetoothPairingService extends Service {
registerReceiver(mCancelReceiver, filter); registerReceiver(mCancelReceiver, filter);
mRegistered = true; mRegistered = true;
startForeground(NOTIFICATION_ID, builder.getNotification()); startActivity(pairingDialogIntent);
return START_REDELIVER_INTENT; }
return START_STICKY;
}
private void createPairingNotification(Intent intent) {
Resources res = getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
BLUETOOTH_NOTIFICATION_CHANNEL)
.setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
.setTicker(res.getString(R.string.bluetooth_notif_ticker))
.setLocalOnly(true);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
BluetoothDevice.ERROR);
Intent pairingDialogIntent = new Intent(ACTION_PAIRING_DIALOG);
pairingDialogIntent.setClass(this, BluetoothPairingService.class);
pairingDialogIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
pairingDialogIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, type);
PendingIntent pairIntent = PendingIntent.getService(this, 0, pairingDialogIntent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
Intent serviceIntent = new Intent(ACTION_DISMISS_PAIRING);
serviceIntent.setClass(this, BluetoothPairingService.class);
serviceIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
PendingIntent dismissIntent = PendingIntent.getService(this, 0,
serviceIntent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
if (TextUtils.isEmpty(name)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
name = device != null ? device.getAlias() : res.getString(android.R.string.unknownName);
}
Log.d(TAG, "Show pairing notification for " + " (" + name + ")");
NotificationCompat.Action pairAction = new NotificationCompat.Action.Builder(0,
res.getString(R.string.bluetooth_device_context_pair_connect), pairIntent).build();
NotificationCompat.Action dismissAction = new NotificationCompat.Action.Builder(0,
res.getString(android.R.string.cancel), dismissIntent).build();
builder.setContentTitle(res.getString(R.string.bluetooth_notif_title))
.setContentText(res.getString(R.string.bluetooth_notif_message, name))
.setContentIntent(pairIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setOngoing(true)
.setColor(getColor(com.android.internal.R.color.system_notification_accent_color))
.addAction(pairAction)
.addAction(dismissAction);
mNm.notify(NOTIFICATION_ID, builder.build());
} }
@Override @Override
@@ -180,7 +217,6 @@ public final class BluetoothPairingService extends Service {
unregisterReceiver(mCancelReceiver); unregisterReceiver(mCancelReceiver);
mRegistered = false; mRegistered = false;
} }
stopForeground(true);
} }
@Override @Override

View File

@@ -0,0 +1,125 @@
/*
* Copyright (C) 2021 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.android.settings.bluetooth.BluetoothPairingService.ACTION_DISMISS_PAIRING;
import static com.android.settings.bluetooth.BluetoothPairingService.ACTION_PAIRING_DIALOG;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Application;
import android.app.NotificationManager;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class BluetoothPairingServiceTest {
private final String mFakeTicker = "fake_ticker";
@Mock
private NotificationManager mNm;
@Mock
private BluetoothDevice mDevice;
@Mock
private Context mContext;
@Mock
private Resources mResources;
@Mock
private PackageManager mPackageManager;
@Mock
private DisplayMetrics mDisplayMetrics;
private BluetoothPairingService mBluetoothPairingService;
private Application mApplication;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mBluetoothPairingService = new BluetoothPairingService();
mBluetoothPairingService.mNm = mNm;
mApplication = RuntimeEnvironment.application;
ReflectionHelpers.setField(mBluetoothPairingService, "mBase", mContext);
when(mContext.getResources()).thenReturn(mResources);
when(mResources.getString(R.string.bluetooth_notif_ticker)).thenReturn(mFakeTicker);
when(mContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mResources.getDisplayMetrics()).thenReturn(mDisplayMetrics);
mDisplayMetrics.density = 1.5f;
}
@Test
public void receivePairingRequestAction_notificationShown() {
Intent intent = new Intent();
intent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
intent.putExtra(BluetoothDevice.EXTRA_NAME, "fake_name");
when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING);
mBluetoothPairingService.onStartCommand(intent, /* flags */ 0, /* startId */ 0);
verify(mNm).notify(eq(mBluetoothPairingService.NOTIFICATION_ID), any());
}
@Test
public void receiveDismissPairingAction_cancelPairing() {
Intent intent = new Intent();
intent.setAction(ACTION_DISMISS_PAIRING);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
intent.putExtra(BluetoothDevice.EXTRA_NAME, "fake_name");
when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING);
mBluetoothPairingService.onStartCommand(intent, /* flags */ 0, /* startId */ 0);
verify(mDevice).cancelPairing();
verify(mNm).cancel(mBluetoothPairingService.NOTIFICATION_ID);
}
@Test
public void receivePairingDialogAction_startActivity() {
Intent intent = new Intent();
intent.setAction(ACTION_PAIRING_DIALOG);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
intent.putExtra(BluetoothDevice.EXTRA_NAME, "fake_name");
when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING);
mBluetoothPairingService.onStartCommand(intent, /* flags */ 0, /* startId */ 0);
verify(mContext).startActivity(any());
}
}