Enable configurable bind service flags
Currently, the ServiceWatcherImpl uses default flags when binding its
service. This change enables the client to configure these flags.
Bug: 375236794
Test: verified with ag/30025549
Flag: EXEMPT does not impact current usage
Change-Id: I9b03011b4f3a4767a81a4650d04cc69f172f0845
diff --git a/core/java/com/android/server/servicewatcher/ServiceWatcher.java b/core/java/com/android/server/servicewatcher/ServiceWatcher.java
index f3d13d3..831ff67 100644
--- a/core/java/com/android/server/servicewatcher/ServiceWatcher.java
+++ b/core/java/com/android/server/servicewatcher/ServiceWatcher.java
@@ -16,6 +16,10 @@
package com.android.server.servicewatcher;
+import static android.content.Context.BIND_AUTO_CREATE;
+import static android.content.Context.BIND_NOT_FOREGROUND;
+import static android.content.Context.BIND_NOT_VISIBLE;
+
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.content.ComponentName;
@@ -146,6 +150,10 @@
protected final @Nullable String mAction;
protected final int mUid;
protected final ComponentName mComponentName;
+ private final int mFlags;
+
+ private static final int DEFAULT_FLAGS =
+ BIND_AUTO_CREATE | BIND_NOT_FOREGROUND | BIND_NOT_VISIBLE;
protected BoundServiceInfo(String action, ResolveInfo resolveInfo) {
this(action, resolveInfo.serviceInfo.applicationInfo.uid,
@@ -153,9 +161,14 @@
}
protected BoundServiceInfo(String action, int uid, ComponentName componentName) {
+ this(action, uid, componentName, DEFAULT_FLAGS);
+ }
+
+ protected BoundServiceInfo(String action, int uid, ComponentName componentName, int flags) {
mAction = action;
mUid = uid;
mComponentName = Objects.requireNonNull(componentName);
+ mFlags = flags;
}
/** Returns the action associated with this bound service. */
@@ -173,6 +186,11 @@
return UserHandle.getUserId(mUid);
}
+ /** Returns flags used when binding the service. */
+ public int getFlags() {
+ return mFlags;
+ }
+
@Override
public final boolean equals(Object o) {
if (this == o) {
@@ -185,12 +203,13 @@
BoundServiceInfo that = (BoundServiceInfo) o;
return mUid == that.mUid
&& Objects.equals(mAction, that.mAction)
- && mComponentName.equals(that.mComponentName);
+ && mComponentName.equals(that.mComponentName)
+ && mFlags == that.mFlags;
}
@Override
public final int hashCode() {
- return Objects.hash(mAction, mUid, mComponentName);
+ return Objects.hash(mAction, mUid, mComponentName, mFlags);
}
@Override
diff --git a/core/java/com/android/server/servicewatcher/ServiceWatcherImpl.java b/core/java/com/android/server/servicewatcher/ServiceWatcherImpl.java
index be0cd37..ccbab9f 100644
--- a/core/java/com/android/server/servicewatcher/ServiceWatcherImpl.java
+++ b/core/java/com/android/server/servicewatcher/ServiceWatcherImpl.java
@@ -16,10 +16,6 @@
package com.android.server.servicewatcher;
-import static android.content.Context.BIND_AUTO_CREATE;
-import static android.content.Context.BIND_NOT_FOREGROUND;
-import static android.content.Context.BIND_NOT_VISIBLE;
-
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
@@ -214,7 +210,7 @@
mBoundServiceInfo.getComponentName());
try {
if (!mContext.bindServiceAsUser(bindIntent, this,
- BIND_AUTO_CREATE | BIND_NOT_FOREGROUND | BIND_NOT_VISIBLE,
+ mBoundServiceInfo.getFlags(),
mHandler, UserHandle.of(mBoundServiceInfo.getUserId()))) {
Log.e(TAG, "[" + mTag + "] unexpected bind failure - retrying later");
mRebinder = this::bind;