camera: Add AIDL interface for CameraServiceProxy.

- Adds an AIDL interface to allow the proxy camera service
  running in system server to accept RPCs from the camera
  service running in mediaserver.
- Request an update to the valid user set from the proxy
  camera service when mediaserver restarts to initialize
  properly + avoid DOS after a crash.

Bug: 21267484
Change-Id: Ib821582794ddd1e3574b5dc6c79f7cb197b57f10
diff --git a/Android.mk b/Android.mk
index eb6e7b5..a6ae613 100644
--- a/Android.mk
+++ b/Android.mk
@@ -145,6 +145,7 @@
 	core/java/android/database/IContentObserver.aidl \
 	core/java/android/hardware/ICameraService.aidl \
 	core/java/android/hardware/ICameraServiceListener.aidl \
+	core/java/android/hardware/ICameraServiceProxy.aidl \
 	core/java/android/hardware/ICamera.aidl \
 	core/java/android/hardware/ICameraClient.aidl \
 	core/java/android/hardware/IConsumerIrService.aidl \
diff --git a/core/java/android/hardware/ICameraService.aidl b/core/java/android/hardware/ICameraService.aidl
index 9201b614..c933f92 100644
--- a/core/java/android/hardware/ICameraService.aidl
+++ b/core/java/android/hardware/ICameraService.aidl
@@ -25,7 +25,11 @@
 import android.hardware.ICameraServiceListener;
 import android.hardware.CameraInfo;
 
-/** @hide */
+/**
+ * Binder interface for the native camera service running in mediaserver.
+ *
+ * @hide
+ */
 interface ICameraService
 {
     /**
diff --git a/core/java/android/hardware/ICameraServiceProxy.aidl b/core/java/android/hardware/ICameraServiceProxy.aidl
new file mode 100644
index 0000000..0bb24bc
--- /dev/null
+++ b/core/java/android/hardware/ICameraServiceProxy.aidl
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2015 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 android.hardware;
+
+/**
+ * Binder interface for the camera service proxy running in system_server.
+ *
+ * @hide
+ */
+interface ICameraServiceProxy
+{
+    /**
+     * Ping the service proxy to update the valid users for the camera service.
+     */
+    oneway void pingForUserUpdate();
+}
diff --git a/services/core/java/com/android/server/camera/CameraService.java b/services/core/java/com/android/server/camera/CameraService.java
index 1d77bc2..777a9dd 100644
--- a/services/core/java/com/android/server/camera/CameraService.java
+++ b/services/core/java/com/android/server/camera/CameraService.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.content.pm.UserInfo;
 import android.hardware.ICameraService;
+import android.hardware.ICameraServiceProxy;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.UserManager;
@@ -42,14 +43,30 @@
      */
     private static final String CAMERA_SERVICE_BINDER_NAME = "media.camera";
 
+    public static final String CAMERA_SERVICE_PROXY_BINDER_NAME = "media.camera.proxy";
+
     // Event arguments to use with the camera service notifySystemEvent call:
     public static final int NO_EVENT = 0; // NOOP
     public static final int USER_SWITCHED = 1; // User changed, argument is the new user handle
 
     private final Context mContext;
     private UserManager mUserManager;
+
+    private final Object mLock = new Object();
     private Set<Integer> mEnabledCameraUsers;
 
+    private final ICameraServiceProxy.Stub mCameraServiceProxy = new ICameraServiceProxy.Stub() {
+        @Override
+        public void pingForUserUpdate() {
+            // Binder call
+            synchronized(mLock) {
+                if (mEnabledCameraUsers != null) {
+                    notifyMediaserver(USER_SWITCHED, mEnabledCameraUsers);
+                }
+            }
+        }
+    };
+
     public CameraService(Context context) {
         super(context);
         mContext = context;
@@ -62,18 +79,27 @@
             // Should never see this unless someone messes up the SystemServer service boot order.
             throw new IllegalStateException("UserManagerService must start before CameraService!");
         }
+        publishBinderService(CAMERA_SERVICE_PROXY_BINDER_NAME, mCameraServiceProxy);
     }
 
     @Override
     public void onStartUser(int userHandle) {
-        if (mEnabledCameraUsers == null) {
-            // Initialize mediaserver, or update mediaserver if we are recovering from a crash.
-            onSwitchUser(userHandle);
+        synchronized(mLock) {
+            if (mEnabledCameraUsers == null) {
+                // Initialize mediaserver, or update mediaserver if we are recovering from a crash.
+                switchUserLocked(userHandle);
+            }
         }
     }
 
     @Override
     public void onSwitchUser(int userHandle) {
+        synchronized(mLock) {
+            switchUserLocked(userHandle);
+        }
+    }
+
+    private void switchUserLocked(int userHandle) {
         Set<Integer> currentUserHandles = getEnabledUserHandles(userHandle);
         if (mEnabledCameraUsers == null || !mEnabledCameraUsers.equals(currentUserHandles)) {
             // Some user handles have been added or removed, update mediaserver.
@@ -82,7 +108,6 @@
         }
     }
 
-
     private Set<Integer> getEnabledUserHandles(int currentUserHandle) {
         List<UserInfo> userProfiles = mUserManager.getEnabledProfiles(currentUserHandle);
         Set<Integer> handles = new HashSet<>(userProfiles.size());