blob: 03fbd19094ba01734b16b6bacb0bdef49cd29892 [file] [log] [blame]
Mike Lockwood3a322132009-11-24 00:30:52 -05001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "VibratorService"
18
19#include "jni.h"
20#include "JNIHelp.h"
21#include "android_runtime/AndroidRuntime.h"
22
23#include <utils/misc.h>
24#include <utils/Log.h>
Vincent Beckere6904fb2012-08-10 14:17:33 +020025#include <hardware/vibrator.h>
Mike Lockwood3a322132009-11-24 00:30:52 -050026
27#include <stdio.h>
28
29namespace android
30{
31
Vincent Beckere6904fb2012-08-10 14:17:33 +020032static hw_module_t *gVibraModule = NULL;
33static vibrator_device_t *gVibraDevice = NULL;
34
35static void vibratorInit(JNIEnv /* env */, jobject /* clazz */)
36{
37 if (gVibraModule != NULL) {
38 return;
39 }
40
41 int err = hw_get_module(VIBRATOR_HARDWARE_MODULE_ID, (hw_module_t const**)&gVibraModule);
42
43 if (err) {
44 ALOGE("Couldn't load %s module (%s)", VIBRATOR_HARDWARE_MODULE_ID, strerror(-err));
45 } else {
46 if (gVibraModule) {
47 vibrator_open(gVibraModule, &gVibraDevice);
48 }
49 }
50}
51
Andreas Gampe26872f42014-09-24 21:39:34 -070052static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */)
Dianne Hackbornea9020e2010-11-04 11:39:12 -070053{
Vincent Beckere6904fb2012-08-10 14:17:33 +020054 if (gVibraModule && gVibraDevice) {
55 return JNI_TRUE;
56 } else {
57 return JNI_FALSE;
58 }
Dianne Hackbornea9020e2010-11-04 11:39:12 -070059}
60
Andreas Gampe26872f42014-09-24 21:39:34 -070061static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms)
Mike Lockwood3a322132009-11-24 00:30:52 -050062{
Vincent Beckere6904fb2012-08-10 14:17:33 +020063 if (gVibraDevice) {
64 int err = gVibraDevice->vibrator_on(gVibraDevice, timeout_ms);
65 if (err != 0) {
66 ALOGE("The hw module failed in vibrator_on: %s", strerror(-err));
67 }
68 } else {
69 ALOGW("Tried to vibrate but there is no vibrator device.");
70 }
Mike Lockwood3a322132009-11-24 00:30:52 -050071}
72
Andreas Gampe26872f42014-09-24 21:39:34 -070073static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */)
Mike Lockwood3a322132009-11-24 00:30:52 -050074{
Vincent Beckere6904fb2012-08-10 14:17:33 +020075 if (gVibraDevice) {
76 int err = gVibraDevice->vibrator_off(gVibraDevice);
77 if (err != 0) {
78 ALOGE("The hw module failed in vibrator_off(): %s", strerror(-err));
79 }
80 } else {
81 ALOGW("Tried to stop vibrating but there is no vibrator device.");
82 }
Mike Lockwood3a322132009-11-24 00:30:52 -050083}
84
Daniel Micay76f6a862015-09-19 17:31:01 -040085static const JNINativeMethod method_table[] = {
Dianne Hackbornea9020e2010-11-04 11:39:12 -070086 { "vibratorExists", "()Z", (void*)vibratorExists },
Vincent Beckere6904fb2012-08-10 14:17:33 +020087 { "vibratorInit", "()V", (void*)vibratorInit },
Mike Lockwood3a322132009-11-24 00:30:52 -050088 { "vibratorOn", "(J)V", (void*)vibratorOn },
89 { "vibratorOff", "()V", (void*)vibratorOff }
90};
91
92int register_android_server_VibratorService(JNIEnv *env)
93{
94 return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
95 method_table, NELEM(method_table));
96}
97
98};