1 /* //device/libs/android_runtime/android_os_Vibrator.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #define LOG_TAG "HardwareService"
19
20 #include "jni.h"
21 #include "JNIHelp.h"
22 #include "android_runtime/AndroidRuntime.h"
23
24 #include <utils/misc.h>
25 #include <utils/Log.h>
26 #include <hardware_legacy/vibrator.h>
27 #include <hardware/hardware.h>
28 #include <hardware/lights.h>
29
30 #include <stdio.h>
31 //#include <string.h>
32
33 namespace android
34 {
35
36 // These values must correspond with the LIGHT_ID constants in
37 // HardwareService.java
38 enum {
39 LIGHT_INDEX_BACKLIGHT = 0,
40 LIGHT_INDEX_KEYBOARD = 1,
41 LIGHT_INDEX_BUTTONS = 2,
42 LIGHT_INDEX_BATTERY = 3,
43 LIGHT_INDEX_NOTIFICATIONS = 4,
44 LIGHT_INDEX_ATTENTION = 5,
45 LIGHT_COUNT
46 };
47
48 struct Devices {
49 light_device_t* lights[LIGHT_COUNT];
50 };
51
get_device(hw_module_t * module,char const * name)52 static light_device_t* get_device(hw_module_t* module, char const* name)
53 {
54 int err;
55 hw_device_t* device;
56 err = module->methods->open(module, name, &device);
57 if (err == 0) {
58 return (light_device_t*)device;
59 } else {
60 return NULL;
61 }
62 }
63
init_native(JNIEnv * env,jobject clazz)64 static jint init_native(JNIEnv *env, jobject clazz)
65 {
66 int err;
67 hw_module_t* module;
68 Devices* devices;
69
70 devices = (Devices*)malloc(sizeof(Devices));
71
72 err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
73 if (err == 0) {
74 devices->lights[LIGHT_INDEX_BACKLIGHT]
75 = get_device(module, LIGHT_ID_BACKLIGHT);
76 devices->lights[LIGHT_INDEX_KEYBOARD]
77 = get_device(module, LIGHT_ID_KEYBOARD);
78 devices->lights[LIGHT_INDEX_BUTTONS]
79 = get_device(module, LIGHT_ID_BUTTONS);
80 devices->lights[LIGHT_INDEX_BATTERY]
81 = get_device(module, LIGHT_ID_BATTERY);
82 devices->lights[LIGHT_INDEX_NOTIFICATIONS]
83 = get_device(module, LIGHT_ID_NOTIFICATIONS);
84 devices->lights[LIGHT_INDEX_ATTENTION]
85 = get_device(module, LIGHT_ID_ATTENTION);
86 } else {
87 memset(devices, 0, sizeof(Devices));
88 }
89
90 return (jint)devices;
91 }
92
finalize_native(JNIEnv * env,jobject clazz,int ptr)93 static void finalize_native(JNIEnv *env, jobject clazz, int ptr)
94 {
95 Devices* devices = (Devices*)ptr;
96 if (devices == NULL) {
97 return;
98 }
99
100 free(devices);
101 }
102
setLight_native(JNIEnv * env,jobject clazz,int ptr,int light,int colorARGB,int flashMode,int onMS,int offMS,int brightnessMode)103 static void setLight_native(JNIEnv *env, jobject clazz, int ptr,
104 int light, int colorARGB, int flashMode, int onMS, int offMS, int brightnessMode)
105 {
106 Devices* devices = (Devices*)ptr;
107 light_state_t state;
108
109 if (light < 0 || light >= LIGHT_COUNT || devices->lights[light] == NULL) {
110 return ;
111 }
112
113 memset(&state, 0, sizeof(light_state_t));
114 state.color = colorARGB;
115 state.flashMode = flashMode;
116 state.flashOnMS = onMS;
117 state.flashOffMS = offMS;
118 state.brightnessMode = brightnessMode;
119
120 devices->lights[light]->set_light(devices->lights[light], &state);
121 }
122
vibratorOn(JNIEnv * env,jobject clazz,jlong timeout_ms)123 static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms)
124 {
125 // LOGI("vibratorOn\n");
126 vibrator_on(timeout_ms);
127 }
128
vibratorOff(JNIEnv * env,jobject clazz)129 static void vibratorOff(JNIEnv *env, jobject clazz)
130 {
131 // LOGI("vibratorOff\n");
132 vibrator_off();
133 }
134
135 static JNINativeMethod method_table[] = {
136 { "init_native", "()I", (void*)init_native },
137 { "finalize_native", "(I)V", (void*)finalize_native },
138 { "setLight_native", "(IIIIIII)V", (void*)setLight_native },
139 { "vibratorOn", "(J)V", (void*)vibratorOn },
140 { "vibratorOff", "()V", (void*)vibratorOff }
141 };
142
register_android_server_HardwareService(JNIEnv * env)143 int register_android_server_HardwareService(JNIEnv *env)
144 {
145 return jniRegisterNativeMethods(env, "com/android/server/HardwareService",
146 method_table, NELEM(method_table));
147 }
148
149 };
150