Require apps to be in forground to be able to scan
After seeing the history of this change and debugging it, now OKG
doesn't initiate a scanning request and the latest documention for media
router recommends removing to remove the MediaRouter Callback in onStop
method.
- By this change we are limiting scanning to apps in the foreground
while screen is on.
- Wrap the change inside of a feature flag to help toggling it off
later if problems started to be reported.
Bug: 254634785
Test: manually && atest MediaRouter2Test MediaRouterManagerTest
Change-Id: I2d56cf3b2775f55e0e6b110c145b77d1a436a1ed
diff --git a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java
index 1998af6..15586c4 100644
--- a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java
+++ b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java
@@ -29,6 +29,7 @@
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityManager;
+import android.app.ActivityThread;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -51,6 +52,7 @@
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.UserHandle;
+import android.provider.DeviceConfig;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
@@ -89,10 +91,19 @@
// TODO: (In Android S or later) if we add callback methods for generic failures
// in MediaRouter2, remove this constant and replace the usages with the real request IDs.
private static final long DUMMY_REQUEST_ID = -1;
- private static final int PACKAGE_IMPORTANCE_FOR_DISCOVERY = IMPORTANCE_FOREGROUND_SERVICE;
private static final int DUMP_EVENTS_MAX_COUNT = 70;
+ private static final String MEDIA_BETTER_TOGETHER_NAMESPACE = "media_better_together";
+
+ private static final String KEY_SCANNING_PACKAGE_MINIMUM_IMPORTANCE =
+ "scanning_package_minimum_importance";
+
+ private static int sPackageImportanceForScanning = DeviceConfig.getInt(
+ MEDIA_BETTER_TOGETHER_NAMESPACE,
+ /* name */ KEY_SCANNING_PACKAGE_MINIMUM_IMPORTANCE,
+ /* defaultValue */ IMPORTANCE_FOREGROUND_SERVICE);
+
private final Context mContext;
private final UserManagerInternal mUserManagerInternal;
private final Object mLock = new Object();
@@ -140,7 +151,7 @@
mContext = context;
mActivityManager = mContext.getSystemService(ActivityManager.class);
mActivityManager.addOnUidImportanceListener(mOnUidImportanceListener,
- PACKAGE_IMPORTANCE_FOR_DISCOVERY);
+ sPackageImportanceForScanning);
mPowerManager = mContext.getSystemService(PowerManager.class);
mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
@@ -149,6 +160,10 @@
screenOnOffIntentFilter.addAction(ACTION_SCREEN_OFF);
mContext.registerReceiver(mScreenOnOffReceiver, screenOnOffIntentFilter);
+
+ DeviceConfig.addOnPropertiesChangedListener(MEDIA_BETTER_TOGETHER_NAMESPACE,
+ ActivityThread.currentApplication().getMainExecutor(),
+ this::onDeviceConfigChange);
}
// Start of methods that implement MediaRouter2 operations.
@@ -1386,6 +1401,12 @@
// End of locked methods that are used by both MediaRouter2 and MediaRouter2Manager.
+ private void onDeviceConfigChange(@NonNull DeviceConfig.Properties properties) {
+ sPackageImportanceForScanning = properties.getInt(
+ /* name */ KEY_SCANNING_PACKAGE_MINIMUM_IMPORTANCE,
+ /* defaultValue */ IMPORTANCE_FOREGROUND_SERVICE);
+ }
+
static long toUniqueRequestId(int requesterId, int originalRequestId) {
return ((long) requesterId << 32) | originalRequestId;
}
@@ -2563,7 +2584,7 @@
isManagerScanning = managerRecords.stream().anyMatch(manager ->
manager.mIsScanning && service.mActivityManager
.getPackageImportance(manager.mPackageName)
- <= PACKAGE_IMPORTANCE_FOR_DISCOVERY);
+ <= sPackageImportanceForScanning);
if (isManagerScanning) {
discoveryPreferences = routerRecords.stream()
@@ -2572,7 +2593,7 @@
} else {
discoveryPreferences = routerRecords.stream().filter(record ->
service.mActivityManager.getPackageImportance(record.mPackageName)
- <= PACKAGE_IMPORTANCE_FOR_DISCOVERY)
+ <= sPackageImportanceForScanning)
.map(record -> record.mDiscoveryPreference)
.collect(Collectors.toList());
}