OSDN Git Service

Updating light service for hidl.
authorSteven Moreland <smoreland@google.com>
Wed, 5 Oct 2016 00:25:52 +0000 (17:25 -0700)
committerSteven Moreland <smoreland@google.com>
Wed, 12 Oct 2016 23:14:42 +0000 (16:14 -0700)
Bug: 32022100
Test: end to end
Change-Id: I3c35551ffbbd272df4291f74455ba9562373f517

services/core/Android.mk
services/core/java/com/android/server/lights/Light.java
services/core/java/com/android/server/lights/LightsManager.java
services/core/java/com/android/server/lights/LightsService.java
services/core/jni/Android.mk
services/core/jni/com_android_server_lights_LightsService.cpp

index fedce0c..9efafb7 100644 (file)
@@ -17,8 +17,12 @@ LOCAL_SRC_FILES += \
 LOCAL_AIDL_INCLUDES += \
     system/netd/server/binder
 
-LOCAL_JAVA_LIBRARIES := services.net telephony-common \
-    android.hardware.power@1.0-java
+LOCAL_JAVA_LIBRARIES := \
+    services.net \
+    telephony-common \
+    android.hardware.power@1.0-java \
+    android.hardware.light@2.0-java
+
 LOCAL_STATIC_JAVA_LIBRARIES := tzdata_update
 LOCAL_PROTOC_OPTIMIZE_TYPE := nano
 
index b18a181..0bab86b 100644 (file)
 
 package com.android.server.lights;
 
+import android.hardware.light.V2_0.Flash;
+import android.hardware.light.V2_0.Brightness;
+
 public abstract class Light {
-    public static final int LIGHT_FLASH_NONE = 0;
-    public static final int LIGHT_FLASH_TIMED = 1;
-    public static final int LIGHT_FLASH_HARDWARE = 2;
+    public static final int LIGHT_FLASH_NONE = Flash.NONE;
+    public static final int LIGHT_FLASH_TIMED = Flash.TIMED;
+    public static final int LIGHT_FLASH_HARDWARE = Flash.HARDWARE;
 
     /**
      * Light brightness is managed by a user setting.
      */
-    public static final int BRIGHTNESS_MODE_USER = 0;
+    public static final int BRIGHTNESS_MODE_USER = Brightness.USER;
 
     /**
      * Light brightness is managed by a light sensor.
      */
-    public static final int BRIGHTNESS_MODE_SENSOR = 1;
+    public static final int BRIGHTNESS_MODE_SENSOR = Brightness.SENSOR;
 
     /**
      * Low-persistence light mode.
      */
-    public static final int BRIGHTNESS_MODE_LOW_PERSISTENCE = 2;
+    public static final int BRIGHTNESS_MODE_LOW_PERSISTENCE = Brightness.LOW_PERSISTENCE;
 
     public abstract void setBrightness(int brightness);
     public abstract void setBrightness(int brightness, int brightnessMode);
index 2f20509..be20a44 100644 (file)
 
 package com.android.server.lights;
 
+import android.hardware.light.V2_0.Type;
+
 public abstract class LightsManager {
-    public static final int LIGHT_ID_BACKLIGHT = 0;
-    public static final int LIGHT_ID_KEYBOARD = 1;
-    public static final int LIGHT_ID_BUTTONS = 2;
-    public static final int LIGHT_ID_BATTERY = 3;
-    public static final int LIGHT_ID_NOTIFICATIONS = 4;
-    public static final int LIGHT_ID_ATTENTION = 5;
-    public static final int LIGHT_ID_BLUETOOTH = 6;
-    public static final int LIGHT_ID_WIFI = 7;
-    public static final int LIGHT_ID_COUNT = 8;
+    public static final int LIGHT_ID_BACKLIGHT = Type.BACKLIGHT;
+    public static final int LIGHT_ID_KEYBOARD = Type.KEYBOARD;
+    public static final int LIGHT_ID_BUTTONS = Type.BUTTONS;
+    public static final int LIGHT_ID_BATTERY = Type.BATTERY;
+    public static final int LIGHT_ID_NOTIFICATIONS = Type.NOTIFICATIONS;
+    public static final int LIGHT_ID_ATTENTION = Type.ATTENTION;
+    public static final int LIGHT_ID_BLUETOOTH = Type.BLUETOOTH;
+    public static final int LIGHT_ID_WIFI = Type.WIFI;
+    public static final int LIGHT_ID_COUNT = Type.COUNT;
 
     public abstract Light getLight(int id);
 }
index ca64817..bba0a50 100644 (file)
@@ -133,7 +133,7 @@ public class LightsService extends SystemService {
                 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x"
                         + Integer.toHexString(color) + ")");
                 try {
-                    setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode);
+                    setLight_native(mId, color, mode, onMS, offMS, brightnessMode);
                 } finally {
                     Trace.traceEnd(Trace.TRACE_TAG_POWER);
                 }
@@ -155,8 +155,6 @@ public class LightsService extends SystemService {
     public LightsService(Context context) {
         super(context);
 
-        mNativePointer = init_native();
-
         for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) {
             mLights[i] = new LightImpl(i);
         }
@@ -217,7 +215,7 @@ public class LightsService extends SystemService {
     private final LightsManager mService = new LightsManager() {
         @Override
         public Light getLight(int id) {
-            if (id < LIGHT_ID_COUNT) {
+            if (0 <= id && id < LIGHT_ID_COUNT) {
                 return mLights[id];
             } else {
                 return null;
@@ -225,12 +223,6 @@ public class LightsService extends SystemService {
         }
     };
 
-    @Override
-    protected void finalize() throws Throwable {
-        finalize_native(mNativePointer);
-        super.finalize();
-    }
-
     private Handler mH = new Handler() {
         @Override
         public void handleMessage(Message msg) {
@@ -239,11 +231,6 @@ public class LightsService extends SystemService {
         }
     };
 
-    private static native long init_native();
-    private static native void finalize_native(long ptr);
-
-    static native void setLight_native(long ptr, int light, int color, int mode,
+    static native void setLight_native(int light, int color, int mode,
             int onMS, int offMS, int brightnessMode);
-
-    private long mNativePointer;
 }
index b3bfc16..d328ade 100644 (file)
@@ -68,7 +68,5 @@ LOCAL_SHARED_LIBRARIES += \
     libhwbinder \
     libutils \
     android.hardware.power@1.0 \
-
-LOCAL_SHARED_LIBRARIES += \
-    libhidl libhwbinder android.hardware.vibrator@1.0
-
+    android.hardware.vibrator@1.0 \
+    android.hardware.light@2.0 \
index c8e3946..e6072bb 100644 (file)
 #include "JNIHelp.h"
 #include "android_runtime/AndroidRuntime.h"
 
+#include <android/hardware/light/2.0/ILight.h>
+#include <android/hardware/light/2.0/types.h>
 #include <utils/misc.h>
 #include <utils/Log.h>
-#include <hardware/hardware.h>
-#include <hardware/lights.h>
-
+#include <map>
 #include <stdio.h>
 
-namespace android
-{
-
-// These values must correspond with the LIGHT_ID constants in
-// LightsService.java
-enum {
-    LIGHT_INDEX_BACKLIGHT = 0,
-    LIGHT_INDEX_KEYBOARD = 1,
-    LIGHT_INDEX_BUTTONS = 2,
-    LIGHT_INDEX_BATTERY = 3,
-    LIGHT_INDEX_NOTIFICATIONS = 4,
-    LIGHT_INDEX_ATTENTION = 5,
-    LIGHT_INDEX_BLUETOOTH = 6,
-    LIGHT_INDEX_WIFI = 7,
-    LIGHT_COUNT
-};
+namespace android {
 
-struct Devices {
-    light_device_t* lights[LIGHT_COUNT];
-};
+using ILight     = ::android::hardware::light::V2_0::ILight;
+using Brightness = ::android::hardware::light::V2_0::Brightness;
+using Flash      = ::android::hardware::light::V2_0::Flash;
+using Type       = ::android::hardware::light::V2_0::Type;
+using LightState = ::android::hardware::light::V2_0::LightState;
 
-static light_device_t* get_device(hw_module_t* module, char const* name)
-{
-    int err;
-    hw_device_t* device;
-    err = module->methods->open(module, name, &device);
-    if (err == 0) {
-        return (light_device_t*)device;
-    } else {
-        return NULL;
+static sp<ILight> gLight;
+
+static bool validate(jint light, jint flash, jint brightness) {
+    bool valid = true;
+
+    if (light < 0 || light >= static_cast<int>(Type::COUNT)) {
+        ALOGE("Invalid light parameter %d.", light);
+        valid = false;
     }
-}
 
-static jlong init_native(JNIEnv* /* env */, jobject /* clazz */)
-{
-    int err;
-    hw_module_t* module;
-    Devices* devices;
-    
-    devices = (Devices*)malloc(sizeof(Devices));
-
-    err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
-    if (err == 0) {
-        devices->lights[LIGHT_INDEX_BACKLIGHT]
-                = get_device(module, LIGHT_ID_BACKLIGHT);
-        devices->lights[LIGHT_INDEX_KEYBOARD]
-                = get_device(module, LIGHT_ID_KEYBOARD);
-        devices->lights[LIGHT_INDEX_BUTTONS]
-                = get_device(module, LIGHT_ID_BUTTONS);
-        devices->lights[LIGHT_INDEX_BATTERY]
-                = get_device(module, LIGHT_ID_BATTERY);
-        devices->lights[LIGHT_INDEX_NOTIFICATIONS]
-                = get_device(module, LIGHT_ID_NOTIFICATIONS);
-        devices->lights[LIGHT_INDEX_ATTENTION]
-                = get_device(module, LIGHT_ID_ATTENTION);
-        devices->lights[LIGHT_INDEX_BLUETOOTH]
-                = get_device(module, LIGHT_ID_BLUETOOTH);
-        devices->lights[LIGHT_INDEX_WIFI]
-                = get_device(module, LIGHT_ID_WIFI);
-    } else {
-        memset(devices, 0, sizeof(Devices));
+    if (flash != static_cast<int>(Flash::NONE) &&
+        flash != static_cast<int>(Flash::TIMED) &&
+        flash != static_cast<int>(Flash::HARDWARE)) {
+        ALOGE("Invalid flash parameter %d.", flash);
+        valid = false;
     }
 
-    return (jlong)devices;
+    if (brightness != static_cast<int>(Brightness::USER) &&
+        brightness != static_cast<int>(Brightness::SENSOR) &&
+        brightness != static_cast<int>(Brightness::LOW_PERSISTENCE)) {
+        ALOGE("Invalid brightness parameter %d.", brightness);
+        valid = false;
+    }
+
+    return valid;
 }
 
-static void finalize_native(JNIEnv* /* env */, jobject /* clazz */, jlong ptr)
-{
-    Devices* devices = (Devices*)ptr;
-    if (devices == NULL) {
+static void setLight_native(
+        JNIEnv* /* env */,
+        jobject /* clazz */,
+        jint light,
+        jint colorARGB,
+        jint flashMode,
+        jint onMS,
+        jint offMS,
+        jint brightnessMode) {
+
+    if (!validate(light, flashMode, brightnessMode)) {
         return;
     }
 
-    free(devices);
-}
-
-static void setLight_native(JNIEnv* /* env */, jobject /* clazz */, jlong ptr,
-        jint light, jint colorARGB, jint flashMode, jint onMS, jint offMS, jint brightnessMode)
-{
-    Devices* devices = (Devices*)ptr;
-    light_state_t state;
+    // TODO(b/31632518)
+    if (gLight == nullptr) {
+        gLight = ILight::getService("light");
+    }
 
-    if (light < 0 || light >= LIGHT_COUNT || devices->lights[light] == NULL) {
-        return ;
+    if (gLight == nullptr) {
+        ALOGE("LightService unable to get ILight interface.");
+        return;
     }
 
-    uint32_t version = devices->lights[light]->common.version;
+    Type type = static_cast<Type>(light);
+    Flash flash = static_cast<Flash>(flashMode);
+    Brightness brightness = static_cast<Brightness>(brightnessMode);
 
-    memset(&state, 0, sizeof(light_state_t));
+    LightState state{};
 
-    if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) {
-        if (light != LIGHT_INDEX_BACKLIGHT) {
+    if (brightnessMode == static_cast<int>(Brightness::LOW_PERSISTENCE)) {
+        if (light != static_cast<int>(Type::BACKLIGHT)) {
             ALOGE("Cannot set low-persistence mode for non-backlight device.");
             return;
         }
-        if (version < LIGHTS_DEVICE_API_VERSION_2_0) {
-            // HAL impl has not been upgraded to support this.
-            return;
-        }
+        state.flashMode = Flash::NONE;
     } else {
         // Only set non-brightness settings when not in low-persistence mode
         state.color = colorARGB;
-        state.flashMode = flashMode;
-        state.flashOnMS = onMS;
-        state.flashOffMS = offMS;
+        state.flashMode = flash;
+        state.flashOnMs = onMS;
+        state.flashOffMs = offMS;
     }
 
-    state.brightnessMode = brightnessMode;
+    state.brightnessMode = brightness;
 
     {
         ALOGD_IF_SLOW(50, "Excessive delay setting light");
-        devices->lights[light]->set_light(devices->lights[light], &state);
+        gLight->setLight(type, state);
     }
 }
 
 static const JNINativeMethod method_table[] = {
-    { "init_native", "()J", (void*)init_native },
-    { "finalize_native", "(J)V", (void*)finalize_native },
-    { "setLight_native", "(JIIIIII)V", (void*)setLight_native },
+    { "setLight_native", "(IIIIII)V", (void*)setLight_native },
 };
 
-int register_android_server_LightsService(JNIEnv *env)
-{
+int register_android_server_LightsService(JNIEnv *env) {
     return jniRegisterNativeMethods(env, "com/android/server/lights/LightsService",
             method_table, NELEM(method_table));
 }