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 | |
Prashant Malani | a9cdaac | 2016-09-16 13:51:18 -0700 | [diff] [blame] | 19 | #include <android/hardware/vibrator/1.0/IVibrator.h> |
| 20 | #include <android/hardware/vibrator/1.0/types.h> |
| 21 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 22 | #include "jni.h" |
| 23 | #include "JNIHelp.h" |
| 24 | #include "android_runtime/AndroidRuntime.h" |
| 25 | |
| 26 | #include <utils/misc.h> |
| 27 | #include <utils/Log.h> |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 28 | #include <hardware/vibrator.h> |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 29 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 30 | #include <inttypes.h> |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 33 | using android::hardware::Return; |
| 34 | using android::hardware::vibrator::V1_0::Effect; |
| 35 | using android::hardware::vibrator::V1_0::EffectStrength; |
Prashant Malani | a9cdaac | 2016-09-16 13:51:18 -0700 | [diff] [blame] | 36 | using android::hardware::vibrator::V1_0::IVibrator; |
| 37 | using android::hardware::vibrator::V1_0::Status; |
| 38 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 39 | namespace android |
| 40 | { |
| 41 | |
Prashant Malani | a9cdaac | 2016-09-16 13:51:18 -0700 | [diff] [blame] | 42 | static sp<IVibrator> mHal; |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 43 | |
| 44 | static void vibratorInit(JNIEnv /* env */, jobject /* clazz */) |
| 45 | { |
Prashant Malani | a9cdaac | 2016-09-16 13:51:18 -0700 | [diff] [blame] | 46 | /* TODO(b/31632518) */ |
| 47 | if (mHal != nullptr) { |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 48 | return; |
| 49 | } |
Chris Phoenix | c310a90 | 2017-01-19 15:54:28 -0800 | [diff] [blame] | 50 | mHal = IVibrator::getService(); |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Andreas Gampe | 26872f4 | 2014-09-24 21:39:34 -0700 | [diff] [blame] | 53 | static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */) |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 54 | { |
Prashant Malani | a9cdaac | 2016-09-16 13:51:18 -0700 | [diff] [blame] | 55 | if (mHal != nullptr) { |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 56 | return JNI_TRUE; |
| 57 | } else { |
| 58 | return JNI_FALSE; |
| 59 | } |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Andreas Gampe | 26872f4 | 2014-09-24 21:39:34 -0700 | [diff] [blame] | 62 | static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms) |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 63 | { |
Prashant Malani | a9cdaac | 2016-09-16 13:51:18 -0700 | [diff] [blame] | 64 | if (mHal != nullptr) { |
| 65 | Status retStatus = mHal->on(timeout_ms); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 66 | if (retStatus != Status::OK) { |
| 67 | ALOGE("vibratorOn command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus)); |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 68 | } |
| 69 | } else { |
| 70 | ALOGW("Tried to vibrate but there is no vibrator device."); |
| 71 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Andreas Gampe | 26872f4 | 2014-09-24 21:39:34 -0700 | [diff] [blame] | 74 | static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */) |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 75 | { |
Prashant Malani | a9cdaac | 2016-09-16 13:51:18 -0700 | [diff] [blame] | 76 | if (mHal != nullptr) { |
| 77 | Status retStatus = mHal->off(); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 78 | if (retStatus != Status::OK) { |
| 79 | ALOGE("vibratorOff command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus)); |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 80 | } |
| 81 | } else { |
| 82 | ALOGW("Tried to stop vibrating but there is no vibrator device."); |
| 83 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 86 | static jlong vibratorSupportsAmplitudeControl(JNIEnv*, jobject) { |
| 87 | if (mHal != nullptr) { |
| 88 | return mHal->supportsAmplitudeControl(); |
| 89 | } else { |
| 90 | ALOGW("Unable to get max vibration amplitude, there is no vibrator device."); |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | static void vibratorSetAmplitude(JNIEnv*, jobject, jint amplitude) { |
| 96 | if (mHal != nullptr) { |
| 97 | Status status = mHal->setAmplitude(static_cast<uint32_t>(amplitude)); |
| 98 | if (status != Status::OK) { |
| 99 | ALOGE("Failed to set vibrator amplitude (%" PRIu32 ").", |
| 100 | static_cast<uint32_t>(status)); |
| 101 | } |
| 102 | } else { |
| 103 | ALOGW("Unable to set vibration amplitude, there is no vibrator device."); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jint strength) { |
| 108 | if (mHal != nullptr) { |
| 109 | Status status; |
| 110 | uint32_t lengthMs; |
| 111 | mHal->perform(static_cast<Effect>(effect), static_cast<EffectStrength>(strength), |
| 112 | [&status, &lengthMs](Status retStatus, uint32_t retLengthMs) { |
| 113 | status = retStatus; |
| 114 | lengthMs = retLengthMs; |
| 115 | }); |
| 116 | if (status == Status::OK) { |
| 117 | return lengthMs; |
| 118 | } else if (status != Status::UNSUPPORTED_OPERATION) { |
| 119 | // Don't warn on UNSUPPORTED_OPERATION, that's a normal even and just means the motor |
| 120 | // doesn't have a pre-defined waveform to perform for it, so we should just fall back |
| 121 | // to the framework waveforms. |
| 122 | ALOGE("Failed to perform haptic effect: effect=%" PRId64 ", strength=%" PRId32 |
| 123 | ", error=%" PRIu32 ").", static_cast<int64_t>(effect), |
| 124 | static_cast<int32_t>(strength), static_cast<uint32_t>(status)); |
| 125 | } |
| 126 | } else { |
| 127 | ALOGW("Unable to perform haptic effect, there is no vibrator device."); |
| 128 | } |
| 129 | return -1; |
| 130 | } |
| 131 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 132 | static const JNINativeMethod method_table[] = { |
Dianne Hackborn | ea9020e | 2010-11-04 11:39:12 -0700 | [diff] [blame] | 133 | { "vibratorExists", "()Z", (void*)vibratorExists }, |
Vincent Becker | e6904fb | 2012-08-10 14:17:33 +0200 | [diff] [blame] | 134 | { "vibratorInit", "()V", (void*)vibratorInit }, |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 135 | { "vibratorOn", "(J)V", (void*)vibratorOn }, |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 136 | { "vibratorOff", "()V", (void*)vibratorOff }, |
| 137 | { "vibratorSupportsAmplitudeControl", "()Z", (void*)vibratorSupportsAmplitudeControl}, |
| 138 | { "vibratorSetAmplitude", "(I)V", (void*)vibratorSetAmplitude}, |
| 139 | { "vibratorPerformEffect", "(JJ)J", (void*)vibratorPerformEffect} |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | int register_android_server_VibratorService(JNIEnv *env) |
| 143 | { |
| 144 | return jniRegisterNativeMethods(env, "com/android/server/VibratorService", |
| 145 | method_table, NELEM(method_table)); |
| 146 | } |
| 147 | |
| 148 | }; |