Extend bindService flags from 32 bits to 64 bits.

bindService flags used to a be a 32 bits integer type, we are running
out of 32 bits flags. Now use an object BindServiceFlags which is a 64
bits long type (can be changed to other data structure/type in the
future) for the bindService flags.

Bug: 191785864
Test: atest cts/tests/app/src/android/app/cts/ServiceTest.java#testBindServiceFlags
atest cts/tests/app/src/android/app/cts/ServiceTest.java
atest cts/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java
atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java
Change-Id: Ib120869c5d90ff37f551d760205a7b65f8b56020

Change-Id: Ib5bec28b7c9aa55874f46210fdbe24596bab40d6
diff --git a/services/api/current.txt b/services/api/current.txt
index 5c7b947..329dbdf 100644
--- a/services/api/current.txt
+++ b/services/api/current.txt
@@ -39,6 +39,7 @@
 
   public interface ActivityManagerLocal {
     method public boolean bindSdkSandboxService(@NonNull android.content.Intent, @NonNull android.content.ServiceConnection, int, @NonNull android.os.IBinder, @NonNull String, @NonNull String, int) throws android.os.RemoteException;
+    method public boolean bindSdkSandboxService(@NonNull android.content.Intent, @NonNull android.content.ServiceConnection, int, @NonNull android.os.IBinder, @NonNull String, @NonNull String, @NonNull android.content.Context.BindServiceFlags) throws android.os.RemoteException;
     method @Deprecated public boolean bindSdkSandboxService(@NonNull android.content.Intent, @NonNull android.content.ServiceConnection, int, @NonNull String, @NonNull String, int) throws android.os.RemoteException;
     method public boolean canStartForegroundService(int, int, @NonNull String);
     method public void killSdkSandboxClientAppProcess(@NonNull android.os.IBinder);
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index 24ce684..638179d 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -32,6 +32,7 @@
 import static android.app.ForegroundServiceTypePolicy.FGS_TYPE_POLICY_CHECK_PERMISSION_DENIED_ENFORCED;
 import static android.app.ForegroundServiceTypePolicy.FGS_TYPE_POLICY_CHECK_PERMISSION_DENIED_PERMISSIVE;
 import static android.app.ForegroundServiceTypePolicy.FGS_TYPE_POLICY_CHECK_UNKNOWN;
+import static android.content.Context.BIND_ALLOW_WHITELIST_MANAGEMENT;
 import static android.content.pm.PackageManager.PERMISSION_DENIED;
 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
 import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST;
@@ -3248,14 +3249,14 @@
     }
 
     int bindServiceLocked(IApplicationThread caller, IBinder token, Intent service,
-            String resolvedType, final IServiceConnection connection, int flags,
+            String resolvedType, final IServiceConnection connection, long flags,
             String instanceName, boolean isSdkSandboxService, int sdkSandboxClientAppUid,
             String sdkSandboxClientAppPackage, IApplicationThread sdkSandboxClientApplicationThread,
             String callingPackage, final int userId)
             throws TransactionTooLargeException {
         if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "bindService: " + service
                 + " type=" + resolvedType + " conn=" + connection.asBinder()
-                + " flags=0x" + Integer.toHexString(flags));
+                + " flags=0x" + Long.toHexString(flags));
         final int callingPid = Binder.getCallingPid();
         final int callingUid = Binder.getCallingUid();
         final ProcessRecord callerApp = mAm.getRecordForAppLOSP(caller);
@@ -3306,7 +3307,7 @@
                     + ") set BIND_SCHEDULE_LIKE_TOP_APP when binding service " + service);
         }
 
-        if ((flags & Context.BIND_ALLOW_WHITELIST_MANAGEMENT) != 0 && !isCallerSystem) {
+        if ((flags & BIND_ALLOW_WHITELIST_MANAGEMENT) != 0 && !isCallerSystem) {
             throw new SecurityException(
                     "Non-system caller " + caller + " (pid=" + callingPid
                     + ") set BIND_ALLOW_WHITELIST_MANAGEMENT when binding service " + service);
@@ -3337,7 +3338,9 @@
 
         final boolean callerFg = callerApp.mState.getSetSchedGroup()
                 != ProcessList.SCHED_GROUP_BACKGROUND;
-        final boolean isBindExternal = (flags & Context.BIND_EXTERNAL_SERVICE) != 0;
+        final boolean isBindExternal =
+                (flags & Integer.toUnsignedLong(Context.BIND_EXTERNAL_SERVICE)) != 0
+                || (flags & Context.BIND_EXTERNAL_SERVICE_LONG) != 0;
         final boolean allowInstant = (flags & Context.BIND_ALLOW_INSTANT) != 0;
         final boolean inSharedIsolatedProcess = (flags & Context.BIND_SHARED_ISOLATED_PROCESS) != 0;
 
@@ -3428,23 +3431,23 @@
             }
             clientPsr.addConnection(c);
             c.startAssociationIfNeeded();
-            if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) {
+            if (c.hasFlag(Context.BIND_ABOVE_CLIENT)) {
                 clientPsr.setHasAboveClient(true);
             }
-            if ((c.flags&Context.BIND_ALLOW_WHITELIST_MANAGEMENT) != 0) {
+            if (c.hasFlag(BIND_ALLOW_WHITELIST_MANAGEMENT)) {
                 s.allowlistManager = true;
             }
-            if ((flags & Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS) != 0) {
+            if (c.hasFlag(Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS)) {
                 s.setAllowedBgActivityStartsByBinding(true);
             }
 
-            if ((flags & Context.BIND_NOT_APP_COMPONENT_USAGE) != 0) {
+            if (c.hasFlag(Context.BIND_NOT_APP_COMPONENT_USAGE)) {
                 s.isNotAppComponentUsage = true;
             }
 
             if (s.app != null && s.app.mState != null
                     && s.app.mState.getCurProcState() <= PROCESS_STATE_TOP
-                    && (flags & Context.BIND_ALMOST_PERCEPTIBLE) != 0) {
+                    && c.hasFlag(Context.BIND_ALMOST_PERCEPTIBLE)) {
                 s.lastTopAlmostPerceptibleBindRequestUptimeMs = SystemClock.uptimeMillis();
             }
 
@@ -3459,7 +3462,7 @@
             clist.add(c);
 
             boolean needOomAdj = false;
-            if ((flags&Context.BIND_AUTO_CREATE) != 0) {
+            if (c.hasFlag(Context.BIND_AUTO_CREATE)) {
                 s.lastActivity = SystemClock.uptimeMillis();
                 needOomAdj = true;
                 if (bringUpServiceLocked(s, service.getFlags(), callerFg, false,
@@ -3473,7 +3476,7 @@
 
             if (s.app != null) {
                 ProcessServiceRecord servicePsr = s.app.mServices;
-                if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
+                if (c.hasFlag(Context.BIND_TREAT_LIKE_ACTIVITY)) {
                     servicePsr.setTreatLikeActivity(true);
                 }
                 if (s.allowlistManager) {
@@ -3483,7 +3486,7 @@
                 mAm.updateLruProcessLocked(s.app, (callerApp.hasActivitiesOrRecentTasks()
                             && servicePsr.hasClientActivities())
                         || (callerApp.mState.getCurProcState() <= PROCESS_STATE_TOP
-                            && (flags & Context.BIND_TREAT_LIKE_ACTIVITY) != 0),
+                            && c.hasFlag(Context.BIND_TREAT_LIKE_ACTIVITY)),
                         b.client);
                 needOomAdj = true;
                 mAm.enqueueOomAdjTargetLocked(s.app);
@@ -3698,7 +3701,7 @@
                         updateAllowlistManagerLocked(psr);
                     }
                     // This could have made the service less important.
-                    if ((r.flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
+                    if (r.hasFlag(Context.BIND_TREAT_LIKE_ACTIVITY)) {
                         psr.setTreatLikeActivity(true);
                         mAm.updateLruProcessLocked(app, true, null);
                     }
@@ -5542,23 +5545,23 @@
         if (b.client != skipApp) {
             final ProcessServiceRecord psr = b.client.mServices;
             psr.removeConnection(c);
-            if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) {
+            if (c.hasFlag(Context.BIND_ABOVE_CLIENT)) {
                 psr.updateHasAboveClientLocked();
             }
             // If this connection requested allowlist management, see if we should
             // now clear that state.
-            if ((c.flags&Context.BIND_ALLOW_WHITELIST_MANAGEMENT) != 0) {
+            if (c.hasFlag(BIND_ALLOW_WHITELIST_MANAGEMENT)) {
                 s.updateAllowlistManager();
                 if (!s.allowlistManager && s.app != null) {
                     updateAllowlistManagerLocked(s.app.mServices);
                 }
             }
             // And do the same for bg activity starts ability.
-            if ((c.flags & Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS) != 0) {
+            if (c.hasFlag(Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS)) {
                 s.updateIsAllowedBgActivityStartsByBinding();
             }
             // And for almost perceptible exceptions.
-            if ((c.flags & Context.BIND_ALMOST_PERCEPTIBLE) != 0) {
+            if (c.hasFlag(Context.BIND_ALMOST_PERCEPTIBLE)) {
                 psr.updateHasTopStartedAlmostPerceptibleServices();
             }
             if (s.app != null) {
@@ -5588,7 +5591,7 @@
                 try {
                     bumpServiceExecutingLocked(s, false, "unbind",
                             OomAdjuster.OOM_ADJ_REASON_UNBIND_SERVICE);
-                    if (b.client != s.app && (c.flags&Context.BIND_WAIVE_PRIORITY) == 0
+                    if (b.client != s.app && c.notHasFlag(Context.BIND_WAIVE_PRIORITY)
                             && s.app.mState.getSetProcState() <= PROCESS_STATE_HEAVY_WEIGHT) {
                         // If this service's process is not already in the cached list,
                         // then update it in the LRU list here because this may be causing
@@ -5613,7 +5616,7 @@
                 mPendingBringups.remove(s);
             }
 
-            if ((c.flags&Context.BIND_AUTO_CREATE) != 0) {
+            if (c.hasFlag(Context.BIND_AUTO_CREATE)) {
                 boolean hasAutoCreate = s.hasAutoCreateConnections();
                 if (!hasAutoCreate) {
                     if (s.tracker != null) {
@@ -6183,8 +6186,9 @@
                     boolean hasCreate = false;
                     for (int conni = abind.connections.size() - 1; conni >= 0; conni--) {
                         ConnectionRecord conn = abind.connections.valueAt(conni);
-                        if ((conn.flags&(Context.BIND_AUTO_CREATE|Context.BIND_ALLOW_OOM_MANAGEMENT
-                                |Context.BIND_WAIVE_PRIORITY)) == Context.BIND_AUTO_CREATE) {
+                        if (conn.hasFlag(Context.BIND_AUTO_CREATE)
+                                && conn.notHasFlag(Context.BIND_ALLOW_OOM_MANAGEMENT
+                                |Context.BIND_WAIVE_PRIORITY)) {
                             hasCreate = true;
                             break;
                         }
diff --git a/services/core/java/com/android/server/am/ActivityManagerLocal.java b/services/core/java/com/android/server/am/ActivityManagerLocal.java
index 31ea092..3f06990 100644
--- a/services/core/java/com/android/server/am/ActivityManagerLocal.java
+++ b/services/core/java/com/android/server/am/ActivityManagerLocal.java
@@ -20,6 +20,8 @@
 import android.annotation.SuppressLint;
 import android.annotation.SystemApi;
 import android.content.Context;
+import android.content.Context.BindServiceFlags;
+import android.content.Context.BindServiceFlagsBits;
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.os.IBinder;
@@ -95,19 +97,31 @@
     boolean bindSdkSandboxService(@NonNull Intent service, @NonNull ServiceConnection conn,
             int clientAppUid, @NonNull IBinder clientAppProcessToken,
             @NonNull String clientAppPackage, @NonNull String processName,
-            @Context.BindServiceFlags int flags)
+            @BindServiceFlagsBits int flags)
+            throws RemoteException;
+
+    /**
+     * See {@link #bindSdkSandboxService(Intent, ServiceConnection, int, IBinder, String, String,
+     *       int)}
+     */
+    @SuppressLint("RethrowRemoteException")
+    boolean bindSdkSandboxService(@NonNull Intent service, @NonNull ServiceConnection conn,
+            int clientAppUid, @NonNull IBinder clientAppProcessToken,
+            @NonNull String clientAppPackage, @NonNull String processName,
+            @NonNull BindServiceFlags flags)
             throws RemoteException;
 
     /**
      * @deprecated Please use
-     * {@link #bindSdkSandboxService(Intent, ServiceConnection, int, IBinder, String, String, int)}
+     * {@link #bindSdkSandboxService(Intent, ServiceConnection, int, IBinder, String, String,
+     *       BindServiceFlags)}
      *
      * This API can't be deleted yet because it can be used by early AdService module versions.
      */
     @SuppressLint("RethrowRemoteException")
     boolean bindSdkSandboxService(@NonNull Intent service, @NonNull ServiceConnection conn,
             int clientAppUid, @NonNull String clientAppPackage, @NonNull String processName,
-            @Context.BindServiceFlags int flags)
+            @BindServiceFlagsBits int flags)
             throws RemoteException;
 
     /**
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 9085e2a..8076682 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -239,6 +239,7 @@
 import android.content.ContentProvider;
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.Context.BindServiceFlags;
 import android.content.IIntentReceiver;
 import android.content.IIntentSender;
 import android.content.Intent;
@@ -13254,7 +13255,7 @@
     }
 
     public int bindService(IApplicationThread caller, IBinder token, Intent service,
-            String resolvedType, IServiceConnection connection, int flags,
+            String resolvedType, IServiceConnection connection, long flags,
             String callingPackage, int userId) throws TransactionTooLargeException {
         return bindServiceInstance(caller, token, service, resolvedType, connection, flags,
                 null, callingPackage, userId);
@@ -13265,14 +13266,14 @@
      * If the instanceName field is not supplied, binding to the service occurs as usual.
      */
     public int bindServiceInstance(IApplicationThread caller, IBinder token, Intent service,
-            String resolvedType, IServiceConnection connection, int flags, String instanceName,
+            String resolvedType, IServiceConnection connection, long flags, String instanceName,
             String callingPackage, int userId) throws TransactionTooLargeException {
         return bindServiceInstance(caller, token, service, resolvedType, connection, flags,
                 instanceName, false, INVALID_UID, null, null, callingPackage, userId);
     }
 
     private int bindServiceInstance(IApplicationThread caller, IBinder token, Intent service,
-            String resolvedType, IServiceConnection connection, int flags, String instanceName,
+            String resolvedType, IServiceConnection connection, long flags, String instanceName,
             boolean isSdkSandboxService, int sdkSandboxClientAppUid,
             String sdkSandboxClientAppPackage,
             IApplicationThread sdkSandboxClientApplicationThread,
@@ -17165,6 +17166,23 @@
                 int clientAppUid, IBinder clientApplicationThread, String clientAppPackage,
                 String processName, int flags)
                 throws RemoteException {
+            return bindSdkSandboxServiceInternal(service, conn, clientAppUid,
+                    clientApplicationThread, clientAppPackage, processName,
+                    Integer.toUnsignedLong(flags));
+        }
+
+        @Override
+        public boolean bindSdkSandboxService(Intent service, ServiceConnection conn,
+                int clientAppUid, IBinder clientApplicationThread, String clientAppPackage,
+                String processName, BindServiceFlags flags) throws RemoteException {
+            return bindSdkSandboxServiceInternal(service, conn, clientAppUid,
+                    clientApplicationThread, clientAppPackage, processName, flags.getValue());
+        }
+
+        private boolean bindSdkSandboxServiceInternal(Intent service, ServiceConnection conn,
+                int clientAppUid, IBinder clientApplicationThread, String clientAppPackage,
+                String processName, long flags)
+                throws RemoteException {
             if (service == null) {
                 throw new IllegalArgumentException("intent is null");
             }
@@ -17206,11 +17224,13 @@
                     clientApplicationThreadVerified = rec.getThread();
                 }
             }
-            final IServiceConnection sd = mContext.getServiceDispatcher(conn, handler, flags);
+            final IServiceConnection sd = mContext.getServiceDispatcher(conn, handler,
+                    flags);
             service.prepareToLeaveProcess(mContext);
             return ActivityManagerService.this.bindServiceInstance(
                     mContext.getIApplicationThread(), mContext.getActivityToken(), service,
-                    service.resolveTypeIfNeeded(mContext.getContentResolver()), sd, flags,
+                    service.resolveTypeIfNeeded(mContext.getContentResolver()), sd,
+                    flags,
                     processName, /*isSdkSandboxService*/ true, clientAppUid, clientAppPackage,
                     clientApplicationThreadVerified, mContext.getOpPackageName(),
                     UserHandle.getUserId(clientAppUid)) != 0;
diff --git a/services/core/java/com/android/server/am/ConnectionRecord.java b/services/core/java/com/android/server/am/ConnectionRecord.java
index f9c0c49..4ef31bf 100644
--- a/services/core/java/com/android/server/am/ConnectionRecord.java
+++ b/services/core/java/com/android/server/am/ConnectionRecord.java
@@ -41,7 +41,7 @@
     final AppBindRecord binding;    // The application/service binding.
     final ActivityServiceConnectionsHolder<ConnectionRecord> activity;  // If non-null, the owning activity.
     final IServiceConnection conn;  // The client connection.
-    final int flags;                // Binding options.
+    private final long flags;                // Binding options.
     final int clientLabel;          // String resource labeling this client.
     final PendingIntent clientIntent; // How to launch the client.
     final int clientUid;            // The identity of this connection's client
@@ -106,12 +106,12 @@
             activity.dump(pw, prefix);
         }
         pw.println(prefix + "conn=" + conn.asBinder()
-                + " flags=0x" + Integer.toHexString(flags));
+                + " flags=0x" + Long.toHexString(flags));
     }
 
     ConnectionRecord(AppBindRecord _binding,
             ActivityServiceConnectionsHolder<ConnectionRecord> _activity,
-            IServiceConnection _conn, int _flags,
+            IServiceConnection _conn, long _flags,
             int _clientLabel, PendingIntent _clientIntent,
             int _clientUid, String _clientProcessName, String _clientPackageName,
             ComponentName _aliasComponent) {
@@ -127,12 +127,24 @@
         aliasComponent = _aliasComponent;
     }
 
+    public long getFlags() {
+        return flags;
+    }
+
     public boolean hasFlag(final int flag) {
+        return (flags & Integer.toUnsignedLong(flag)) != 0;
+    }
+
+    public boolean hasFlag(final long flag) {
         return (flags & flag) != 0;
     }
 
     public boolean notHasFlag(final int flag) {
-        return (flags & flag) == 0;
+        return !hasFlag(flag);
+    }
+
+    public boolean notHasFlag(final long flag) {
+        return !hasFlag(flag);
     }
 
     public void startAssociationIfNeeded() {
@@ -188,61 +200,61 @@
         sb.append(" u");
         sb.append(binding.client.userId);
         sb.append(' ');
-        if ((flags&Context.BIND_AUTO_CREATE) != 0) {
+        if (hasFlag(Context.BIND_AUTO_CREATE)) {
             sb.append("CR ");
         }
-        if ((flags&Context.BIND_DEBUG_UNBIND) != 0) {
+        if (hasFlag(Context.BIND_DEBUG_UNBIND)) {
             sb.append("DBG ");
         }
-        if ((flags&Context.BIND_NOT_FOREGROUND) != 0) {
+        if (hasFlag(Context.BIND_NOT_FOREGROUND)) {
             sb.append("!FG ");
         }
-        if ((flags&Context.BIND_IMPORTANT_BACKGROUND) != 0) {
+        if (hasFlag(Context.BIND_IMPORTANT_BACKGROUND)) {
             sb.append("IMPB ");
         }
-        if ((flags&Context.BIND_ABOVE_CLIENT) != 0) {
+        if (hasFlag(Context.BIND_ABOVE_CLIENT)) {
             sb.append("ABCLT ");
         }
-        if ((flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) {
+        if (hasFlag(Context.BIND_ALLOW_OOM_MANAGEMENT)) {
             sb.append("OOM ");
         }
-        if ((flags&Context.BIND_WAIVE_PRIORITY) != 0) {
+        if (hasFlag(Context.BIND_WAIVE_PRIORITY)) {
             sb.append("WPRI ");
         }
-        if ((flags&Context.BIND_IMPORTANT) != 0) {
+        if (hasFlag(Context.BIND_IMPORTANT)) {
             sb.append("IMP ");
         }
-        if ((flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) {
+        if (hasFlag(Context.BIND_ADJUST_WITH_ACTIVITY)) {
             sb.append("WACT ");
         }
-        if ((flags&Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE) != 0) {
+        if (hasFlag(Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE)) {
             sb.append("FGSA ");
         }
-        if ((flags&Context.BIND_FOREGROUND_SERVICE) != 0) {
+        if (hasFlag(Context.BIND_FOREGROUND_SERVICE)) {
             sb.append("FGS ");
         }
-        if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
+        if (hasFlag(Context.BIND_TREAT_LIKE_ACTIVITY)) {
             sb.append("LACT ");
         }
-        if ((flags & Context.BIND_SCHEDULE_LIKE_TOP_APP) != 0) {
+        if (hasFlag(Context.BIND_SCHEDULE_LIKE_TOP_APP)) {
             sb.append("SLTA ");
         }
-        if ((flags & Context.BIND_TREAT_LIKE_VISIBLE_FOREGROUND_SERVICE) != 0) {
+        if (hasFlag(Context.BIND_TREAT_LIKE_VISIBLE_FOREGROUND_SERVICE)) {
             sb.append("VFGS ");
         }
-        if ((flags&Context.BIND_SHOWING_UI) != 0) {
+        if (hasFlag(Context.BIND_SHOWING_UI)) {
             sb.append("UI ");
         }
-        if ((flags&Context.BIND_NOT_VISIBLE) != 0) {
+        if (hasFlag(Context.BIND_NOT_VISIBLE)) {
             sb.append("!VIS ");
         }
-        if ((flags & Context.BIND_NOT_PERCEPTIBLE) != 0) {
+        if (hasFlag(Context.BIND_NOT_PERCEPTIBLE)) {
             sb.append("!PRCP ");
         }
-        if ((flags & Context.BIND_ALLOW_ACTIVITY_STARTS) != 0) {
+        if (hasFlag(Context.BIND_ALLOW_ACTIVITY_STARTS)) {
             sb.append("BALF ");
         }
-        if ((flags & Context.BIND_INCLUDE_CAPABILITIES) != 0) {
+        if (hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
             sb.append("CAPS ");
         }
         if (serviceDead) {
@@ -251,6 +263,7 @@
         sb.append(binding.service.shortInstanceName);
         sb.append(":@");
         sb.append(Integer.toHexString(System.identityHashCode(conn.asBinder())));
+        sb.append(" flags=0x" + Long.toHexString(flags));
         sb.append('}');
         return stringName = sb.toString();
     }
diff --git a/services/core/java/com/android/server/am/IntentBindRecord.java b/services/core/java/com/android/server/am/IntentBindRecord.java
index e622013..abc7ab1 100644
--- a/services/core/java/com/android/server/am/IntentBindRecord.java
+++ b/services/core/java/com/android/server/am/IntentBindRecord.java
@@ -77,12 +77,12 @@
         intent = _intent;
     }
 
-    int collectFlags() {
-        int flags = 0;
+    long collectFlags() {
+        long flags = 0;
         for (int i=apps.size()-1; i>=0; i--) {
             final ArraySet<ConnectionRecord> connections = apps.valueAt(i).connections;
             for (int j=connections.size()-1; j>=0; j--) {
-                flags |= connections.valueAt(j).flags;
+                flags |= connections.valueAt(j).getFlags();
             }
         }
         return flags;
diff --git a/services/core/java/com/android/server/am/OomAdjuster.java b/services/core/java/com/android/server/am/OomAdjuster.java
index f11a3be..32db33d 100644
--- a/services/core/java/com/android/server/am/OomAdjuster.java
+++ b/services/core/java/com/android/server/am/OomAdjuster.java
@@ -723,7 +723,7 @@
             final ProcessServiceRecord psr = pr.mServices;
             for (int i = psr.numberOfConnections() - 1; i >= 0; i--) {
                 ConnectionRecord cr = psr.getConnectionAt(i);
-                ProcessRecord service = (cr.flags & ServiceInfo.FLAG_ISOLATED_PROCESS) != 0
+                ProcessRecord service = cr.hasFlag(ServiceInfo.FLAG_ISOLATED_PROCESS)
                         ? cr.binding.service.isolationHostProc : cr.binding.service.app;
                 if (service == null || service == pr
                         || ((service.mState.getMaxAdj() >= ProcessList.SYSTEM_ADJ)
@@ -734,10 +734,9 @@
                 if (service.mState.isReachable()) {
                     continue;
                 }
-                if ((cr.flags & (Context.BIND_WAIVE_PRIORITY
-                        | Context.BIND_TREAT_LIKE_ACTIVITY
-                        | Context.BIND_ADJUST_WITH_ACTIVITY))
-                        == Context.BIND_WAIVE_PRIORITY) {
+                if (cr.hasFlag(Context.BIND_WAIVE_PRIORITY)
+                        && cr.notHasFlag(Context.BIND_TREAT_LIKE_ACTIVITY
+                        | Context.BIND_ADJUST_WITH_ACTIVITY)) {
                     continue;
                 }
                 queue.offer(service);
@@ -2218,7 +2217,7 @@
                     // we check the final procstate, and remove it if the procsate is below BFGS.
                     capability |= getBfslCapabilityFromClient(client);
 
-                    if ((cr.flags & Context.BIND_WAIVE_PRIORITY) == 0) {
+                    if (cr.notHasFlag(Context.BIND_WAIVE_PRIORITY)) {
                         if (cr.hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
                             capability |= cstate.getCurCapability();
                         }
@@ -2231,8 +2230,7 @@
                         if ((cstate.getCurCapability() & PROCESS_CAPABILITY_NETWORK) != 0) {
                             if (clientProcState <= PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
                                 // This is used to grant network access to Expedited Jobs.
-                                if ((cr.flags & Context.BIND_BYPASS_POWER_NETWORK_RESTRICTIONS)
-                                        != 0) {
+                                if (cr.hasFlag(Context.BIND_BYPASS_POWER_NETWORK_RESTRICTIONS)) {
                                     capability |= PROCESS_CAPABILITY_NETWORK;
                                 }
                             } else {
@@ -2251,7 +2249,7 @@
                             clientProcState = PROCESS_STATE_CACHED_EMPTY;
                         }
                         String adjType = null;
-                        if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) {
+                        if (cr.hasFlag(Context.BIND_ALLOW_OOM_MANAGEMENT)) {
                             // Similar to BIND_WAIVE_PRIORITY, keep it unfrozen.
                             if (clientAdj < CACHED_APP_MIN_ADJ) {
                                 app.mOptRecord.setShouldNotFreeze(true);
@@ -2300,8 +2298,8 @@
                             } else {
                                 int newAdj;
                                 int lbAdj = VISIBLE_APP_ADJ; // lower bound of adj.
-                                if ((cr.flags&(Context.BIND_ABOVE_CLIENT
-                                        |Context.BIND_IMPORTANT)) != 0) {
+                                if (cr.hasFlag(Context.BIND_ABOVE_CLIENT
+                                        | Context.BIND_IMPORTANT)) {
                                     if (clientAdj >= PERSISTENT_SERVICE_ADJ) {
                                         newAdj = clientAdj;
                                     } else {
@@ -2312,26 +2310,26 @@
                                         cr.trackProcState(procState, mAdjSeq);
                                         trackedProcState = true;
                                     }
-                                } else if ((cr.flags & Context.BIND_NOT_PERCEPTIBLE) != 0
+                                } else if (cr.hasFlag(Context.BIND_NOT_PERCEPTIBLE)
                                         && clientAdj <= PERCEPTIBLE_APP_ADJ
                                         && adj >= (lbAdj = PERCEPTIBLE_LOW_APP_ADJ)) {
                                     newAdj = PERCEPTIBLE_LOW_APP_ADJ;
-                                } else if ((cr.flags & Context.BIND_ALMOST_PERCEPTIBLE) != 0
-                                        && (cr.flags & Context.BIND_NOT_FOREGROUND) == 0
+                                } else if (cr.hasFlag(Context.BIND_ALMOST_PERCEPTIBLE)
+                                        && cr.notHasFlag(Context.BIND_NOT_FOREGROUND)
                                         && clientAdj < PERCEPTIBLE_APP_ADJ
                                         && adj >= (lbAdj = PERCEPTIBLE_APP_ADJ)) {
                                     // This is for user-initiated jobs.
                                     // We use APP_ADJ + 1 here, so we can tell them apart from FGS.
                                     newAdj = PERCEPTIBLE_APP_ADJ + 1;
-                                } else if ((cr.flags & Context.BIND_ALMOST_PERCEPTIBLE) != 0
-                                        && (cr.flags & Context.BIND_NOT_FOREGROUND) != 0
+                                } else if (cr.hasFlag(Context.BIND_ALMOST_PERCEPTIBLE)
+                                        && cr.hasFlag(Context.BIND_NOT_FOREGROUND)
                                         && clientAdj < PERCEPTIBLE_APP_ADJ
                                         && adj >= (lbAdj = (PERCEPTIBLE_MEDIUM_APP_ADJ + 2))) {
                                     // This is for expedited jobs.
                                     // We use MEDIUM_APP_ADJ + 2 here, so we can tell apart
                                     // EJ and short-FGS.
                                     newAdj = PERCEPTIBLE_MEDIUM_APP_ADJ + 2;
-                                } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0
+                                } else if (cr.hasFlag(Context.BIND_NOT_VISIBLE)
                                         && clientAdj < PERCEPTIBLE_APP_ADJ
                                         && adj >= (lbAdj = PERCEPTIBLE_APP_ADJ)) {
                                     newAdj = PERCEPTIBLE_APP_ADJ;
@@ -2359,14 +2357,14 @@
                                 }
                             }
                         }
-                        if ((cr.flags & (Context.BIND_NOT_FOREGROUND
-                                | Context.BIND_IMPORTANT_BACKGROUND)) == 0) {
+                        if (cr.notHasFlag(Context.BIND_NOT_FOREGROUND
+                                | Context.BIND_IMPORTANT_BACKGROUND)) {
                             // This will treat important bound services identically to
                             // the top app, which may behave differently than generic
                             // foreground work.
                             final int curSchedGroup = cstate.getCurrentSchedulingGroup();
                             if (curSchedGroup > schedGroup) {
-                                if ((cr.flags&Context.BIND_IMPORTANT) != 0) {
+                                if (cr.hasFlag(Context.BIND_IMPORTANT)) {
                                     schedGroup = curSchedGroup;
                                 } else {
                                     schedGroup = SCHED_GROUP_DEFAULT;
@@ -2383,8 +2381,8 @@
                                     clientProcState = PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
                                 } else if (mService.mWakefulness.get()
                                         == PowerManagerInternal.WAKEFULNESS_AWAKE
-                                        && (cr.flags & Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE)
-                                                != 0) {
+                                        && cr.hasFlag(Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE))
+                                {
                                     clientProcState = PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
                                 } else {
                                     clientProcState =
@@ -2408,7 +2406,7 @@
                                     capability |= cstate.getCurCapability();
                                 }
                             }
-                        } else if ((cr.flags & Context.BIND_IMPORTANT_BACKGROUND) == 0) {
+                        } else if (cr.notHasFlag(Context.BIND_IMPORTANT_BACKGROUND)) {
                             if (clientProcState <
                                     PROCESS_STATE_TRANSIENT_BACKGROUND) {
                                 clientProcState =
@@ -2423,7 +2421,7 @@
                         }
 
                         if (schedGroup < SCHED_GROUP_TOP_APP
-                                && (cr.flags & Context.BIND_SCHEDULE_LIKE_TOP_APP) != 0
+                                && cr.hasFlag(Context.BIND_SCHEDULE_LIKE_TOP_APP)
                                 && clientIsSystem) {
                             schedGroup = SCHED_GROUP_TOP_APP;
                             scheduleLikeTopApp = true;
@@ -2441,7 +2439,7 @@
                             }
                         }
                         if (procState < PROCESS_STATE_IMPORTANT_BACKGROUND
-                                && (cr.flags & Context.BIND_SHOWING_UI) != 0) {
+                                && cr.hasFlag(Context.BIND_SHOWING_UI)) {
                             app.setPendingUiClean(true);
                         }
                         if (adjType != null) {
@@ -2472,17 +2470,17 @@
                             app.mOptRecord.setShouldNotFreeze(true);
                         }
                     }
-                    if ((cr.flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
+                    if (cr.hasFlag(Context.BIND_TREAT_LIKE_ACTIVITY)) {
                         psr.setTreatLikeActivity(true);
                     }
                     final ActivityServiceConnectionsHolder a = cr.activity;
-                    if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) {
+                    if (cr.hasFlag(Context.BIND_ADJUST_WITH_ACTIVITY)) {
                         if (a != null && adj > FOREGROUND_APP_ADJ
                                 && a.isActivityVisible()) {
                             adj = FOREGROUND_APP_ADJ;
                             state.setCurRawAdj(adj);
-                            if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
-                                if ((cr.flags&Context.BIND_IMPORTANT) != 0) {
+                            if (cr.notHasFlag(Context.BIND_NOT_FOREGROUND)) {
+                                if (cr.hasFlag(Context.BIND_IMPORTANT)) {
                                     schedGroup = SCHED_GROUP_TOP_APP_BOUND;
                                 } else {
                                     schedGroup = SCHED_GROUP_DEFAULT;
diff --git a/services/core/java/com/android/server/am/ProcessList.java b/services/core/java/com/android/server/am/ProcessList.java
index ee315bd..f4e2b0f 100644
--- a/services/core/java/com/android/server/am/ProcessList.java
+++ b/services/core/java/com/android/server/am/ProcessList.java
@@ -3733,7 +3733,7 @@
             if (cr.binding != null && !cr.serviceDead && cr.binding.service != null
                     && cr.binding.service.app != null
                     && cr.binding.service.app.getLruSeq() != mLruSeq
-                    && (cr.flags & Context.BIND_REDUCTION_FLAGS) == 0
+                    && cr.notHasFlag(Context.BIND_REDUCTION_FLAGS)
                     && !cr.binding.service.app.isPersistent()) {
                 if (cr.binding.service.app.mServices.hasClientActivities()) {
                     if (nextActivityIndex >= 0) {
diff --git a/services/core/java/com/android/server/am/ProcessServiceRecord.java b/services/core/java/com/android/server/am/ProcessServiceRecord.java
index bd7f96a..53fa4f1 100644
--- a/services/core/java/com/android/server/am/ProcessServiceRecord.java
+++ b/services/core/java/com/android/server/am/ProcessServiceRecord.java
@@ -298,7 +298,7 @@
 
             for (int c = clist.size() - 1; c >= 0; --c) {
                 final ConnectionRecord cr = clist.get(c);
-                if ((cr.flags & Context.BIND_ALMOST_PERCEPTIBLE) != 0) {
+                if (cr.hasFlag(Context.BIND_ALMOST_PERCEPTIBLE)) {
                     return true;
                 }
             }
@@ -341,7 +341,7 @@
         mHasAboveClient = false;
         for (int i = mConnections.size() - 1; i >= 0; i--) {
             ConnectionRecord cr = mConnections.valueAt(i);
-            if ((cr.flags & Context.BIND_ABOVE_CLIENT) != 0) {
+            if (cr.hasFlag(Context.BIND_ABOVE_CLIENT)) {
                 mHasAboveClient = true;
                 break;
             }
@@ -507,7 +507,7 @@
         return mConnections.size();
     }
 
-    void addBoundClientUid(int clientUid, String clientPackageName, int bindFlags) {
+    void addBoundClientUid(int clientUid, String clientPackageName, long bindFlags) {
         mBoundClientUids.add(clientUid);
         mApp.getWindowProcessController()
                 .addBoundClientUid(clientUid, clientPackageName, bindFlags);
@@ -531,7 +531,7 @@
                 for (int i = 0; i < c.size(); i++) {
                     ConnectionRecord cr = c.get(i);
                     boundClientUids.add(cr.clientUid);
-                    controller.addBoundClientUid(cr.clientUid, cr.clientPackageName, cr.flags);
+                    controller.addBoundClientUid(cr.clientUid, cr.clientPackageName, cr.getFlags());
                 }
             }
         }
@@ -549,7 +549,7 @@
                 ConnectionRecord cr = c.get(i);
                 mBoundClientUids.add(cr.clientUid);
                 mApp.getWindowProcessController()
-                        .addBoundClientUid(cr.clientUid, cr.clientPackageName, cr.flags);
+                        .addBoundClientUid(cr.clientUid, cr.clientPackageName, cr.getFlags());
 
             }
         }
diff --git a/services/core/java/com/android/server/am/ServiceRecord.java b/services/core/java/com/android/server/am/ServiceRecord.java
index 2a7f181..ae8ceab 100644
--- a/services/core/java/com/android/server/am/ServiceRecord.java
+++ b/services/core/java/com/android/server/am/ServiceRecord.java
@@ -894,7 +894,7 @@
 
         // if we have a process attached, add bound client uid of this connection to it
         if (app != null) {
-            app.mServices.addBoundClientUid(c.clientUid, c.clientPackageName, c.flags);
+            app.mServices.addBoundClientUid(c.clientUid, c.clientPackageName, c.getFlags());
             app.mProfile.addHostingComponentType(HOSTING_COMPONENT_TYPE_BOUND_SERVICE);
         }
     }
@@ -924,7 +924,7 @@
         for (int conni = connections.size() - 1; conni >= 0; conni--) {
             ArrayList<ConnectionRecord> cr = connections.valueAt(conni);
             for (int i = 0; i < cr.size(); i++) {
-                if ((cr.get(i).flags & Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS) != 0) {
+                if (cr.get(i).hasFlag(Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS)) {
                     isAllowedByBinding = true;
                     break;
                 }
@@ -1093,7 +1093,7 @@
         for (int conni=connections.size()-1; conni>=0; conni--) {
             ArrayList<ConnectionRecord> cr = connections.valueAt(conni);
             for (int i=0; i<cr.size(); i++) {
-                if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) {
+                if (cr.get(i).hasFlag(Context.BIND_AUTO_CREATE)) {
                     return true;
                 }
             }
@@ -1106,7 +1106,7 @@
         for (int conni=connections.size()-1; conni>=0; conni--) {
             ArrayList<ConnectionRecord> cr = connections.valueAt(conni);
             for (int i=0; i<cr.size(); i++) {
-                if ((cr.get(i).flags&Context.BIND_ALLOW_WHITELIST_MANAGEMENT) != 0) {
+                if (cr.get(i).hasFlag(Context.BIND_ALLOW_WHITELIST_MANAGEMENT)) {
                     allowlistManager = true;
                     return;
                 }
diff --git a/services/core/java/com/android/server/textclassifier/TextClassificationManagerService.java b/services/core/java/com/android/server/textclassifier/TextClassificationManagerService.java
index fe74167..984cb19 100644
--- a/services/core/java/com/android/server/textclassifier/TextClassificationManagerService.java
+++ b/services/core/java/com/android/server/textclassifier/TextClassificationManagerService.java
@@ -888,7 +888,7 @@
         @NonNull
         final TextClassifierServiceConnection mConnection;
         final boolean mIsTrusted;
-        @Context.BindServiceFlags
+        @Context.BindServiceFlagsBits
         final int mBindServiceFlags;
         @NonNull
         @GuardedBy("mLock")
@@ -925,7 +925,7 @@
             mEnabled = isServiceEnabledForUser();
         }
 
-        @Context.BindServiceFlags
+        @Context.BindServiceFlagsBits
         private int createBindServiceFlags(@NonNull String packageName) {
             int flags = Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE;
             if (!packageName.equals(mDefaultTextClassifierPackage)) {
diff --git a/services/tests/mockingservicestests/src/com/android/server/am/MockingOomAdjusterTests.java b/services/tests/mockingservicestests/src/com/android/server/am/MockingOomAdjusterTests.java
index fb05699..dc7f3cd 100644
--- a/services/tests/mockingservicestests/src/com/android/server/am/MockingOomAdjusterTests.java
+++ b/services/tests/mockingservicestests/src/com/android/server/am/MockingOomAdjusterTests.java
@@ -2600,7 +2600,7 @@
     }
 
     private ServiceRecord bindService(ProcessRecord service, ProcessRecord client,
-            ServiceRecord record, int bindFlags, IBinder binder) {
+            ServiceRecord record, long bindFlags, IBinder binder) {
         if (record == null) {
             record = makeServiceRecord(service);
         }