Camera moduel API 2.4 and torch mode support

Minimal camera module API 2.4 support. Mainly adding set_torch_mode
support.

It also addresses an issue where cameraservice assumes all HAL3
devices should support torch mode.

Bug: 36233279
Bug: 68802239
Test: Pass android.hardware.camera2.cts.FlashlightTest
Change-Id: Idd6c80b503850e0ff711c1c827685128b5086ab7
(cherry picked from commit 172fcb0b38fa802c690aa4367d6e91ab209eb1e7)
diff --git a/guest/hals/camera/EmulatedBaseCamera.cpp b/guest/hals/camera/EmulatedBaseCamera.cpp
index b488088..a61d722 100644
--- a/guest/hals/camera/EmulatedBaseCamera.cpp
+++ b/guest/hals/camera/EmulatedBaseCamera.cpp
@@ -85,6 +85,10 @@
 }
 #endif
 
+status_t EmulatedBaseCamera::setTorchMode(bool /* enabled */) {
+    ALOGE("%s: not supported", __FUNCTION__);
+    return INVALID_OPERATION;
+}
 
 
 } /* namespace android */
diff --git a/guest/hals/camera/EmulatedBaseCamera.h b/guest/hals/camera/EmulatedBaseCamera.h
index d9b0500..b57624b 100644
--- a/guest/hals/camera/EmulatedBaseCamera.h
+++ b/guest/hals/camera/EmulatedBaseCamera.h
@@ -104,6 +104,12 @@
      */
     virtual status_t getImageMetadata(struct ImageMetadata* meta) = 0;
 
+    /* Set torch mode.
+     * This method is called in response to camera_module_t::set_torch_mode
+     * callback.
+     */
+    virtual status_t setTorchMode(bool enabled);
+
     /****************************************************************************
      * Data members
      ***************************************************************************/
diff --git a/guest/hals/camera/EmulatedCameraFactory.cpp b/guest/hals/camera/EmulatedCameraFactory.cpp
index ef9c112..a59224a 100755
--- a/guest/hals/camera/EmulatedCameraFactory.cpp
+++ b/guest/hals/camera/EmulatedCameraFactory.cpp
@@ -194,6 +194,15 @@
 }
 #endif
 
+int EmulatedCameraFactory::setTorchMode(const char* camera_id, bool enabled) {
+    ALOGV("%s: camera_id = %s, enabled =%d", __FUNCTION__, camera_id, enabled);
+
+    EmulatedBaseCamera* camera = getOrCreateFakeCamera(atoi(camera_id));
+    if (camera == NULL) return -EINVAL;
+
+    return camera->setTorchMode(enabled);
+}
+
 /****************************************************************************
  * Camera HAL API callbacks.
  ***************************************************************************/
@@ -250,6 +259,10 @@
     return -ENOSYS;
 }
 
+int EmulatedCameraFactory::set_torch_mode(const char* camera_id, bool enabled) {
+    return EmulatedCameraFactory::Instance().setTorchMode(camera_id, enabled);
+}
+
 /********************************************************************************
  * Internal API
  *******************************************************************************/
@@ -287,6 +300,24 @@
 
 }
 
+void EmulatedCameraFactory::onTorchModeStatusChanged(int cameraId, int newStatus) {
+    EmulatedBaseCamera *cam = getOrCreateFakeCamera(cameraId);
+    if (!cam) {
+        ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
+        return;
+    }
+
+#if VSOC_PLATFORM_SDK_AFTER(L_MR1)
+    const camera_module_callbacks_t* cb = mCallbacks;
+    if (cb != NULL && cb->torch_mode_status_change != NULL) {
+        char id[10];
+        sprintf(id, "%d", cameraId);
+        cb->torch_mode_status_change(cb, id, newStatus);
+    }
+#endif
+
+}
+
 /********************************************************************************
  * Initializer for the static member structure.
  *******************************************************************************/
diff --git a/guest/hals/camera/EmulatedCameraFactory.h b/guest/hals/camera/EmulatedCameraFactory.h
index cbf41b2..4e92ffc 100755
--- a/guest/hals/camera/EmulatedCameraFactory.h
+++ b/guest/hals/camera/EmulatedCameraFactory.h
@@ -95,6 +95,8 @@
     void getVendorTagOps(vendor_tag_ops_t* ops);
 #endif
 
+    int setTorchMode(const char* camera_id, bool enabled);
+
     /****************************************************************************
      * Camera HAL API callbacks.
      ***************************************************************************/
@@ -118,6 +120,8 @@
     static int open_legacy(const struct hw_module_t* module, const char* id,
             uint32_t halVersion, struct hw_device_t** device);
 
+    static int set_torch_mode(const char* camera_id, bool enabled);
+
 private:
     /* hw_module_methods_t::open callback entry point. */
     static int device_open(const hw_module_t* module,
@@ -144,6 +148,8 @@
 
     void onStatusChanged(int cameraId, int newStatus);
 
+    void onTorchModeStatusChanged(int cameraId, int newStatus);
+
     /****************************************************************************
      * Private API
      ***************************************************************************/
diff --git a/guest/hals/camera/EmulatedCameraHal.cpp b/guest/hals/camera/EmulatedCameraHal.cpp
index e71874c..24c7c6d 100755
--- a/guest/hals/camera/EmulatedCameraHal.cpp
+++ b/guest/hals/camera/EmulatedCameraHal.cpp
@@ -32,7 +32,9 @@
 camera_module_t HAL_MODULE_INFO_SYM = {
   VSOC_STATIC_INITIALIZER(common) {
          VSOC_STATIC_INITIALIZER(tag)                HARDWARE_MODULE_TAG,
-#if VSOC_PLATFORM_SDK_AFTER(K)
+#if VSOC_PLATFORM_SDK_AFTER(L_MR1)
+         VSOC_STATIC_INITIALIZER(module_api_version) CAMERA_MODULE_API_VERSION_2_4,
+#elif VSOC_PLATFORM_SDK_AFTER(K)
          VSOC_STATIC_INITIALIZER(module_api_version) CAMERA_MODULE_API_VERSION_2_3,
 #elif VSOC_PLATFORM_SDK_AFTER(J_MR2)
          VSOC_STATIC_INITIALIZER(module_api_version) CAMERA_MODULE_API_VERSION_2_2,
@@ -54,6 +56,9 @@
     VSOC_STATIC_INITIALIZER(get_vendor_tag_ops)     android::EmulatedCameraFactory::get_vendor_tag_ops,
 #endif
 #if VSOC_PLATFORM_SDK_AFTER(K)
-    VSOC_STATIC_INITIALIZER(open_legacy)            android::EmulatedCameraFactory::open_legacy
+    VSOC_STATIC_INITIALIZER(open_legacy)            android::EmulatedCameraFactory::open_legacy,
+#endif
+#if VSOC_PLATFORM_SDK_AFTER(L_MR1)
+    set_torch_mode:         android::EmulatedCameraFactory::set_torch_mode,
 #endif
 };
diff --git a/guest/hals/camera/EmulatedFakeCamera3.cpp b/guest/hals/camera/EmulatedFakeCamera3.cpp
index d45f673..238a6fb 100644
--- a/guest/hals/camera/EmulatedFakeCamera3.cpp
+++ b/guest/hals/camera/EmulatedFakeCamera3.cpp
@@ -209,9 +209,26 @@
 status_t EmulatedFakeCamera3::getCameraInfo(struct camera_info *info) {
     info->facing = mFacingBack ? CAMERA_FACING_BACK : CAMERA_FACING_FRONT;
     info->orientation = EmulatedCameraFactory::Instance().getFakeCameraOrientation();
+#if VSOC_PLATFORM_SDK_AFTER(L_MR1)
+    info->resource_cost = 100;
+    info->conflicting_devices = NULL;
+    info->conflicting_devices_length = 0;
+#endif
     return EmulatedCamera3::getCameraInfo(info);
 }
 
+status_t EmulatedFakeCamera3::setTorchMode(bool enabled) {
+    if (!mFacingBack) {
+        ALOGE("%s: Front camera does not have flash unit", __FUNCTION__);
+        return INVALID_OPERATION;
+    }
+    EmulatedCameraFactory::Instance().onTorchModeStatusChanged(
+        mCameraID, enabled ?
+        TORCH_MODE_STATUS_AVAILABLE_ON :
+        TORCH_MODE_STATUS_AVAILABLE_OFF);
+    return NO_ERROR;
+}
+
 /**
  * Camera3 interface methods
  */
diff --git a/guest/hals/camera/EmulatedFakeCamera3.h b/guest/hals/camera/EmulatedFakeCamera3.h
index 37670f0..9fa56c9 100644
--- a/guest/hals/camera/EmulatedFakeCamera3.h
+++ b/guest/hals/camera/EmulatedFakeCamera3.h
@@ -71,6 +71,8 @@
 
     virtual status_t getCameraInfo(struct camera_info *info);
 
+    virtual status_t setTorchMode(bool enabled);
+
     /****************************************************************************
      * EmulatedCamera3 abstract API implementation
      ***************************************************************************/