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);
}
public LightsService(Context context) {
super(context);
- mNativePointer = init_native();
-
for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) {
mLights[i] = new LightImpl(i);
}
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;
}
};
- @Override
- protected void finalize() throws Throwable {
- finalize_native(mNativePointer);
- super.finalize();
- }
-
private Handler mH = new Handler() {
@Override
public void handleMessage(Message msg) {
}
};
- 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;
}
#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));
}