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> 25 #include <hardware/vibrator.h> 26 27 #include <stdio.h> 28 29 namespace android 30 { 31 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 52 static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */) 53 { 54 if (gVibraModule && gVibraDevice) { 55 return JNI_TRUE; 56 } else { 57 return JNI_FALSE; 58 } 59 } 60 61 static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms) 62 { 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 } 71 } 72 73 static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */) 74 { 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 } 83 } 84 85 static const JNINativeMethod method_table[] = { 86 { "vibratorExists", "()Z", (void*)vibratorExists }, 87 { "vibratorInit", "()V", (void*)vibratorInit }, 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 }; 99