blob: b0aab599155cbe9d9186a0525539057f91e6aa1c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //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
The Android Open Source Project10592532009-03-18 17:39:46 -070018#define LOG_TAG "HardwareService"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
20#include "jni.h"
21#include "JNIHelp.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include "android_runtime/AndroidRuntime.h"
The Android Open Source Project10592532009-03-18 17:39:46 -070023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/misc.h>
25#include <utils/Log.h>
26#include <hardware_legacy/vibrator.h>
The Android Open Source Project10592532009-03-18 17:39:46 -070027#include <hardware/hardware.h>
28#include <hardware/lights.h>
29
30#include <stdio.h>
31//#include <string.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33namespace android
34{
35
The Android Open Source Project10592532009-03-18 17:39:46 -070036// These values must correspond with the LIGHT_ID constants in
37// HardwareService.java
38enum {
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
48struct Devices {
49 light_device_t* lights[LIGHT_COUNT];
50};
51
52static 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
64static 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
93static 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
103static void setLight_native(JNIEnv *env, jobject clazz, int ptr,
104 int light, int colorARGB, int flashMode, int onMS, int offMS)
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
119 devices->lights[light]->set_light(devices->lights[light], &state);
120}
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms)
123{
124 // LOGI("vibratorOn\n");
125 vibrator_on(timeout_ms);
126}
127
128static void vibratorOff(JNIEnv *env, jobject clazz)
129{
130 // LOGI("vibratorOff\n");
131 vibrator_off();
132}
133
134static JNINativeMethod method_table[] = {
The Android Open Source Project10592532009-03-18 17:39:46 -0700135 { "init_native", "()I", (void*)init_native },
136 { "finalize_native", "(I)V", (void*)init_native },
137 { "setLight_native", "(IIIIII)V", (void*)setLight_native },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 { "vibratorOn", "(J)V", (void*)vibratorOn },
139 { "vibratorOff", "()V", (void*)vibratorOff }
140};
141
The Android Open Source Project10592532009-03-18 17:39:46 -0700142int register_android_server_HardwareService(JNIEnv *env)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143{
144 return jniRegisterNativeMethods(env, "com/android/server/HardwareService",
145 method_table, NELEM(method_table));
146}
147
148};