Show a notification when a BG-launch FGS is restricted.

Previously each BG-launch FGS that is restricted has a logcat message, now we
also display a notification that has:
Title: "Foreground Service BG-Launch Restricted"
Content: "App restricted:" + app package name
Detail: intent of the FGS.

Use following command to turn on the FGS BG-launch restriction feature:
 adb shell device_config put activity_manager default_fgs_starts_restriction_enabled true
 adb shell device_config get activity_manager default_fgs_starts_restriction_enabled

Bug: 157473819
Test: Observe the notification when FGS BG-launch is restricted.
atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java#testFgsStartFromBG

Change-Id: Ia1ce1c7e103fbfa0a8487b0a3009b2d2ca61ba74
This commit is contained in:
Hui Yu
2020-09-30 14:00:48 -07:00
parent 282c68d3b3
commit 12378c5223
2 changed files with 36 additions and 1 deletions

View File

@@ -252,6 +252,10 @@ message SystemMessage {
// Package: android
NOTE_ID_WIFI_SIM_REQUIRED = 60;
// TODO: remove this notification after feature development is done
// Inform the user a foreground service is restricted from BG-launch.
NOTE_FOREGROUND_SERVICE_BG_LAUNCH = 61;
// Display the Android Debug Protocol status
// Package: android
NOTE_ADB_WIFI_ACTIVE = 62;

View File

@@ -25,6 +25,7 @@ import static android.os.Process.SHELL_UID;
import static android.os.Process.SYSTEM_UID;
import static android.os.Process.ZYGOTE_POLICY_FLAG_EMPTY;
import static com.android.internal.messages.nano.SystemMessageProto.SystemMessage.NOTE_FOREGROUND_SERVICE_BG_LAUNCH;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_BACKGROUND_CHECK;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOREGROUND_SERVICE;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_MU;
@@ -119,6 +120,7 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@@ -228,6 +230,10 @@ public final class ActiveServices {
// white listed packageName.
ArraySet<String> mWhiteListAllowWhileInUsePermissionInFgs = new ArraySet<>();
// TODO: remove this after feature development is done
private static final SimpleDateFormat DATE_FORMATTER =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Runnable mLastAnrDumpClearer = new Runnable() {
@Override public void run() {
synchronized (mAm) {
@@ -553,8 +559,9 @@ public final class ActiveServices {
if (r.mAllowStartForeground == FGS_FEATURE_DENIED
&& mAm.mConstants.mFlagFgsStartRestrictionEnabled) {
Slog.w(TAG, "startForegroundService() not allowed due to "
+ " mAllowStartForeground false: service "
+ "mAllowStartForeground false: service "
+ r.shortInstanceName);
showFgsBgRestrictedNotificationLocked(r);
forcedStandby = true;
}
}
@@ -1459,6 +1466,7 @@ public final class ActiveServices {
"Service.startForeground() not allowed due to "
+ "mAllowStartForeground false: service "
+ r.shortInstanceName);
showFgsBgRestrictedNotificationLocked(r);
updateServiceForegroundLocked(r.app, true);
ignoreForeground = true;
}
@@ -5056,4 +5064,27 @@ public final class ActiveServices {
&& code != FGS_FEATURE_ALLOWED_BY_UID_VISIBLE;
}
// TODO: remove this notification after feature development is done
private void showFgsBgRestrictedNotificationLocked(ServiceRecord r) {
final Context context = mAm.mContext;
final String title = "Foreground Service BG-Launch Restricted";
final String content = "App restricted: " + r.mRecentCallingPackage;
final long now = System.currentTimeMillis();
final String bigText = DATE_FORMATTER.format(now) + " " + r.mInfoAllowStartForeground;
final String groupKey = "com.android.fgs-bg-restricted";
final Notification.Builder n =
new Notification.Builder(context,
SystemNotificationChannels.ALERTS)
.setGroup(groupKey)
.setSmallIcon(R.drawable.stat_sys_vitals)
.setWhen(0)
.setColor(context.getColor(
com.android.internal.R.color.system_notification_accent_color))
.setTicker(title)
.setContentTitle(title)
.setContentText(content)
.setStyle(new Notification.BigTextStyle().bigText(bigText));
context.getSystemService(NotificationManager.class).notifyAsUser(Long.toString(now),
NOTE_FOREGROUND_SERVICE_BG_LAUNCH, n.build(), UserHandle.ALL);
}
}