Add stub for VirtualDeviceManagerInternal

isAppOwnerOfAnyVirtualDevice is implemented but
isAppRunningOnAnyVirtualDevice is currently a stub and should be
implemented once we have virtual display support

Bug: 194949534
Test: Manual
Change-Id: I83a62b4f4666d69f6a0a89b81a9e93aec655de00
diff --git a/services/companion/java/com/android/server/companion/virtual/VirtualDeviceManagerInternal.java b/services/companion/java/com/android/server/companion/virtual/VirtualDeviceManagerInternal.java
new file mode 100644
index 0000000..ee09832
--- /dev/null
+++ b/services/companion/java/com/android/server/companion/virtual/VirtualDeviceManagerInternal.java
@@ -0,0 +1,36 @@
+/*
+ * 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.server.companion.virtual;
+
+/**
+ * Virtual device manager local service interface.
+ */
+public abstract class VirtualDeviceManagerInternal {
+
+    /**
+     * Returns true if the given {@code uid} is the owner of any virtual devices that are
+     * currently active.
+     */
+    public abstract boolean isAppOwnerOfAnyVirtualDevice(int uid);
+
+    /**
+     * Returns true if the given {@code uid} is currently running on any virtual devices. This is
+     * determined by whether the app has any activities in the task stack on a virtual-device-owned
+     * display.
+     */
+    public abstract boolean isAppRunningOnAnyVirtualDevice(int uid);
+}
diff --git a/services/companion/java/com/android/server/companion/virtual/VirtualDeviceManagerService.java b/services/companion/java/com/android/server/companion/virtual/VirtualDeviceManagerService.java
index 58d0801..8592c05 100644
--- a/services/companion/java/com/android/server/companion/virtual/VirtualDeviceManagerService.java
+++ b/services/companion/java/com/android/server/companion/virtual/VirtualDeviceManagerService.java
@@ -81,6 +81,7 @@
     @Override
     public void onStart() {
         publishBinderService(Context.VIRTUAL_DEVICE_SERVICE, mImpl);
+        publishLocalService(VirtualDeviceManagerInternal.class, new LocalService());
     }
 
     @Override
@@ -119,8 +120,10 @@
     private class VirtualDeviceImpl extends IVirtualDevice.Stub implements IBinder.DeathRecipient {
 
         private final AssociationInfo mAssociationInfo;
+        private final int mOwnerUid;
 
-        private VirtualDeviceImpl(IBinder token, AssociationInfo associationInfo) {
+        private VirtualDeviceImpl(int ownerUid, IBinder token, AssociationInfo associationInfo) {
+            mOwnerUid = ownerUid;
             mAssociationInfo = associationInfo;
             try {
                 token.linkToDeath(this, 0);
@@ -156,10 +159,11 @@
             getContext().enforceCallingOrSelfPermission(
                     android.Manifest.permission.CREATE_VIRTUAL_DEVICE,
                     "createVirtualDevice");
-            if (!PermissionUtils.validatePackageName(getContext(), packageName, getCallingUid())) {
+            final int callingUid = getCallingUid();
+            if (!PermissionUtils.validatePackageName(getContext(), packageName, callingUid)) {
                 throw new SecurityException(
                         "Package name " + packageName + " does not belong to calling uid "
-                                + getCallingUid());
+                                + callingUid);
             }
             AssociationInfo associationInfo = getAssociationInfo(packageName, associationId);
             if (associationInfo == null) {
@@ -171,7 +175,7 @@
                             "Virtual device for association ID " + associationId
                                     + " already exists");
                 }
-                return new VirtualDeviceImpl(token, associationInfo);
+                return new VirtualDeviceImpl(callingUid, token, associationInfo);
             }
         }
 
@@ -222,4 +226,26 @@
             }
         }
     }
+
+    private final class LocalService extends VirtualDeviceManagerInternal {
+
+        @Override
+        public boolean isAppOwnerOfAnyVirtualDevice(int uid) {
+            synchronized (mVirtualDeviceManagerLock) {
+                int size = mVirtualDevices.size();
+                for (int i = 0; i < size; i++) {
+                    if (mVirtualDevices.valueAt(i).mOwnerUid == uid) {
+                        return true;
+                    }
+                }
+                return false;
+            }
+        }
+
+        @Override
+        public boolean isAppRunningOnAnyVirtualDevice(int uid) {
+            // TODO(yukl): Implement this using DWPC.onRunningAppsChanged
+            return false;
+        }
+    }
 }