Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 1 | /* |
| 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 Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 25 | #include <hardware/vibrator.h> |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 26 | |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | namespace android |
| 30 | { |
| 31 | |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 32 | static hw_module_t *gVibraModule = NULL; |
| 33 | static vibrator_device_t *gVibraDevice = NULL; |
| 34 | |
| 35 | static 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 Gampe | 26872f4 | 2014-09-24 21:39:34 -0700 | [diff] [blame] | 52 | static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */) |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 53 | { |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 54 | if (gVibraModule && gVibraDevice) { |
| 55 | return JNI_TRUE; |
| 56 | } else { |
| 57 | return JNI_FALSE; |
| 58 | } |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Andreas Gampe | 26872f4 | 2014-09-24 21:39:34 -0700 | [diff] [blame] | 61 | static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms) |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 62 | { |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 63 | 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 Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Andreas Gampe | 26872f4 | 2014-09-24 21:39:34 -0700 | [diff] [blame] | 73 | static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */) |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 74 | { |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 75 | 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 Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 85 | static const JNINativeMethod method_table[] = { |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 86 | { "vibratorExists", "()Z", (void*)vibratorExists }, |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 87 | { "vibratorInit", "()V", (void*)vibratorInit }, |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 88 | { "vibratorOn", "(J)V", (void*)vibratorOn }, |
| 89 | { "vibratorOff", "()V", (void*)vibratorOff } |
| 90 | }; |
| 91 | |
| 92 | int register_android_server_VibratorService(JNIEnv *env) |
| 93 | { |
| 94 | return jniRegisterNativeMethods(env, "com/android/server/VibratorService", |
| 95 | method_table, NELEM(method_table)); |
| 96 | } |
| 97 | |
| 98 | }; |