Vibrator Service: Support Async Callback APIs
Bug: 136220871 Test: TBD Change-Id: Ia639dd01081218db0bc3029316a58a03b9d4d927
This commit is contained in:
committed by
Harpreet \"Eli\" Sangha
parent
92818ea47c
commit
7a4f1d23ea
@@ -106,7 +106,7 @@ java_library_static {
|
||||
"android.hardware.biometrics.fingerprint-V2.1-java",
|
||||
"android.hardware.oemlock-V1.0-java",
|
||||
"android.hardware.tetheroffload.control-V1.0-java",
|
||||
"android.hardware.vibrator-V1.0-java",
|
||||
"android.hardware.vibrator-V1.4-java",
|
||||
"android.hardware.configstore-V1.0-java",
|
||||
"android.hardware.contexthub-V1.0-java",
|
||||
"android.hidl.manager-V1.2-java",
|
||||
|
||||
@@ -29,6 +29,7 @@ import android.content.res.Resources;
|
||||
import android.database.ContentObserver;
|
||||
import android.hardware.input.InputManager;
|
||||
import android.hardware.vibrator.V1_0.EffectStrength;
|
||||
import android.hardware.vibrator.V1_4.Capabilities;
|
||||
import android.icu.text.DateFormat;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
@@ -108,6 +109,9 @@ public class VibratorService extends IVibratorService.Stub
|
||||
// If a vibration is playing for longer than 5s, it's probably not haptic feedback.
|
||||
private static final long MAX_HAPTIC_FEEDBACK_DURATION = 5000;
|
||||
|
||||
// If HAL supports callbacks set the timeout to ASYNC_TIMEOUT_MULTIPLIER * duration.
|
||||
private static final long ASYNC_TIMEOUT_MULTIPLIER = 2;
|
||||
|
||||
|
||||
// A mapping from the intensity adjustment to the scaling to apply, where the intensity
|
||||
// adjustment is defined as the delta between the default intensity level and the user selected
|
||||
@@ -123,6 +127,7 @@ public class VibratorService extends IVibratorService.Stub
|
||||
private final boolean mAllowPriorityVibrationsInLowPowerMode;
|
||||
private final boolean mSupportsAmplitudeControl;
|
||||
private final boolean mSupportsExternalControl;
|
||||
private final long mCapabilities;
|
||||
private final int mDefaultVibrationAmplitude;
|
||||
private final SparseArray<VibrationEffect> mFallbackEffects;
|
||||
private final SparseArray<Integer> mProcStatesCache = new SparseArray();
|
||||
@@ -163,9 +168,10 @@ public class VibratorService extends IVibratorService.Stub
|
||||
static native void vibratorOff();
|
||||
static native boolean vibratorSupportsAmplitudeControl();
|
||||
static native void vibratorSetAmplitude(int amplitude);
|
||||
static native long vibratorPerformEffect(long effect, long strength);
|
||||
static native long vibratorPerformEffect(long effect, long strength, Vibration vibration);
|
||||
static native boolean vibratorSupportsExternalControl();
|
||||
static native void vibratorSetExternalControl(boolean enabled);
|
||||
static native long vibratorGetCapabilities();
|
||||
|
||||
private final IUidObserver mUidObserver = new IUidObserver.Stub() {
|
||||
@Override public void onUidStateChanged(int uid, int procState, long procStateSeq) {
|
||||
@@ -226,6 +232,14 @@ public class VibratorService extends IVibratorService.Stub
|
||||
}
|
||||
}
|
||||
|
||||
private void onComplete() {
|
||||
synchronized (mLock) {
|
||||
if (this == mCurrentVibration) {
|
||||
doCancelVibrateLocked();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasTimeoutLongerThan(long millis) {
|
||||
final long duration = effect.getDuration();
|
||||
return duration >= 0 && duration > millis;
|
||||
@@ -347,6 +361,7 @@ public class VibratorService extends IVibratorService.Stub
|
||||
|
||||
mSupportsAmplitudeControl = vibratorSupportsAmplitudeControl();
|
||||
mSupportsExternalControl = vibratorSupportsExternalControl();
|
||||
mCapabilities = vibratorGetCapabilities();
|
||||
|
||||
mContext = context;
|
||||
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
|
||||
@@ -1135,10 +1150,14 @@ public class VibratorService extends IVibratorService.Stub
|
||||
}
|
||||
// Input devices don't support prebaked effect, so skip trying it with them.
|
||||
if (!usingInputDeviceVibrators) {
|
||||
long timeout = vibratorPerformEffect(prebaked.getId(),
|
||||
prebaked.getEffectStrength());
|
||||
long duration = vibratorPerformEffect(prebaked.getId(),
|
||||
prebaked.getEffectStrength(), vib);
|
||||
long timeout = duration;
|
||||
if ((mCapabilities & Capabilities.PERFORM_COMPLETION_CALLBACK) != 0) {
|
||||
timeout *= ASYNC_TIMEOUT_MULTIPLIER;
|
||||
}
|
||||
if (timeout > 0) {
|
||||
noteVibratorOnLocked(vib.uid, timeout);
|
||||
noteVibratorOnLocked(vib.uid, duration);
|
||||
return timeout;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,6 +141,7 @@ cc_defaults {
|
||||
"android.hardware.vibrator@1.1",
|
||||
"android.hardware.vibrator@1.2",
|
||||
"android.hardware.vibrator@1.3",
|
||||
"android.hardware.vibrator@1.4",
|
||||
"android.hardware.vr@1.0",
|
||||
"android.frameworks.schedulerservice@1.0",
|
||||
"android.frameworks.sensorservice@1.0",
|
||||
|
||||
@@ -16,17 +16,12 @@
|
||||
|
||||
#define LOG_TAG "VibratorService"
|
||||
|
||||
#include <android/hardware/vibrator/1.0/IVibrator.h>
|
||||
#include <android/hardware/vibrator/1.0/types.h>
|
||||
#include <android/hardware/vibrator/1.0/IVibrator.h>
|
||||
#include <android/hardware/vibrator/1.1/types.h>
|
||||
#include <android/hardware/vibrator/1.2/IVibrator.h>
|
||||
#include <android/hardware/vibrator/1.2/types.h>
|
||||
#include <android/hardware/vibrator/1.3/IVibrator.h>
|
||||
#include <android/hardware/vibrator/1.4/IVibrator.h>
|
||||
|
||||
#include "jni.h"
|
||||
#include <nativehelper/JNIHelp.h>
|
||||
#include "android_runtime/AndroidRuntime.h"
|
||||
#include "core_jni_helpers.h"
|
||||
|
||||
#include <utils/misc.h>
|
||||
#include <utils/Log.h>
|
||||
@@ -36,6 +31,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
using android::hardware::Return;
|
||||
using android::hardware::Void;
|
||||
using android::hardware::vibrator::V1_0::EffectStrength;
|
||||
using android::hardware::vibrator::V1_0::Status;
|
||||
using android::hardware::vibrator::V1_1::Effect_1_1;
|
||||
@@ -44,9 +40,32 @@ namespace V1_0 = android::hardware::vibrator::V1_0;
|
||||
namespace V1_1 = android::hardware::vibrator::V1_1;
|
||||
namespace V1_2 = android::hardware::vibrator::V1_2;
|
||||
namespace V1_3 = android::hardware::vibrator::V1_3;
|
||||
namespace V1_4 = android::hardware::vibrator::V1_4;
|
||||
|
||||
namespace android {
|
||||
|
||||
static jmethodID sMethodIdOnComplete;
|
||||
|
||||
class VibratorCallback : public V1_4::IVibratorCallback {
|
||||
public:
|
||||
VibratorCallback(JNIEnv *env, jobject vibration) :
|
||||
mVibration(MakeGlobalRefOrDie(env, vibration)) {}
|
||||
|
||||
~VibratorCallback() {
|
||||
JNIEnv *env = AndroidRuntime::getJNIEnv();
|
||||
env->DeleteGlobalRef(mVibration);
|
||||
}
|
||||
|
||||
Return<void> onComplete() override {
|
||||
auto env = AndroidRuntime::getJNIEnv();
|
||||
env->CallVoidMethod(mVibration, sMethodIdOnComplete);
|
||||
return Void();
|
||||
}
|
||||
|
||||
private:
|
||||
jobject mVibration;
|
||||
};
|
||||
|
||||
static constexpr int NUM_TRIES = 2;
|
||||
|
||||
// Creates a Return<R> with STATUS::EX_NULL_POINTER.
|
||||
@@ -119,17 +138,17 @@ bool isValidEffect(jlong effect) {
|
||||
return val >= *iter.begin() && val <= *std::prev(iter.end());
|
||||
}
|
||||
|
||||
static void vibratorInit(JNIEnv /* env */, jobject /* clazz */)
|
||||
static void vibratorInit(JNIEnv *env, jclass clazz)
|
||||
{
|
||||
halCall(&V1_0::IVibrator::ping).isOk();
|
||||
}
|
||||
|
||||
static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */)
|
||||
static jboolean vibratorExists(JNIEnv* /* env */, jclass /* clazz */)
|
||||
{
|
||||
return halCall(&V1_0::IVibrator::ping).isOk() ? JNI_TRUE : JNI_FALSE;
|
||||
}
|
||||
|
||||
static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms)
|
||||
static void vibratorOn(JNIEnv* /* env */, jclass /* clazz */, jlong timeout_ms)
|
||||
{
|
||||
Status retStatus = halCall(&V1_0::IVibrator::on, timeout_ms).withDefault(Status::UNKNOWN_ERROR);
|
||||
if (retStatus != Status::OK) {
|
||||
@@ -137,7 +156,7 @@ static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms)
|
||||
}
|
||||
}
|
||||
|
||||
static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */)
|
||||
static void vibratorOff(JNIEnv* /* env */, jclass /* clazz */)
|
||||
{
|
||||
Status retStatus = halCall(&V1_0::IVibrator::off).withDefault(Status::UNKNOWN_ERROR);
|
||||
if (retStatus != Status::OK) {
|
||||
@@ -145,11 +164,11 @@ static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */)
|
||||
}
|
||||
}
|
||||
|
||||
static jlong vibratorSupportsAmplitudeControl(JNIEnv*, jobject) {
|
||||
static jlong vibratorSupportsAmplitudeControl(JNIEnv*, jclass) {
|
||||
return halCall(&V1_0::IVibrator::supportsAmplitudeControl).withDefault(false);
|
||||
}
|
||||
|
||||
static void vibratorSetAmplitude(JNIEnv*, jobject, jint amplitude) {
|
||||
static void vibratorSetAmplitude(JNIEnv*, jclass, jint amplitude) {
|
||||
Status status = halCall(&V1_0::IVibrator::setAmplitude, static_cast<uint32_t>(amplitude))
|
||||
.withDefault(Status::UNKNOWN_ERROR);
|
||||
if (status != Status::OK) {
|
||||
@@ -158,11 +177,11 @@ static void vibratorSetAmplitude(JNIEnv*, jobject, jint amplitude) {
|
||||
}
|
||||
}
|
||||
|
||||
static jboolean vibratorSupportsExternalControl(JNIEnv*, jobject) {
|
||||
static jboolean vibratorSupportsExternalControl(JNIEnv*, jclass) {
|
||||
return halCall(&V1_3::IVibrator::supportsExternalControl).withDefault(false);
|
||||
}
|
||||
|
||||
static void vibratorSetExternalControl(JNIEnv*, jobject, jboolean enabled) {
|
||||
static void vibratorSetExternalControl(JNIEnv*, jclass, jboolean enabled) {
|
||||
Status status = halCall(&V1_3::IVibrator::setExternalControl, static_cast<uint32_t>(enabled))
|
||||
.withDefault(Status::UNKNOWN_ERROR);
|
||||
if (status != Status::OK) {
|
||||
@@ -171,7 +190,8 @@ static void vibratorSetExternalControl(JNIEnv*, jobject, jboolean enabled) {
|
||||
}
|
||||
}
|
||||
|
||||
static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jint strength) {
|
||||
static jlong vibratorPerformEffect(JNIEnv* env, jclass, jlong effect, jint strength,
|
||||
jobject vibration) {
|
||||
Status status;
|
||||
uint32_t lengthMs;
|
||||
auto callback = [&status, &lengthMs](Status retStatus, uint32_t retLengthMs) {
|
||||
@@ -181,7 +201,11 @@ static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jint strength
|
||||
EffectStrength effectStrength(static_cast<EffectStrength>(strength));
|
||||
|
||||
Return<void> ret;
|
||||
if (isValidEffect<V1_0::Effect>(effect)) {
|
||||
if (auto hal = getHal<V1_4::IVibrator>(); hal && isValidEffect<V1_3::Effect>(effect)) {
|
||||
sp<VibratorCallback> effectCallback = new VibratorCallback(env, vibration);
|
||||
ret = hal->call(&V1_4::IVibrator::perform_1_4, static_cast<V1_3::Effect>(effect),
|
||||
effectStrength, effectCallback, callback);
|
||||
} else if (isValidEffect<V1_0::Effect>(effect)) {
|
||||
ret = halCall(&V1_0::IVibrator::perform, static_cast<V1_0::Effect>(effect),
|
||||
effectStrength, callback);
|
||||
} else if (isValidEffect<Effect_1_1>(effect)) {
|
||||
@@ -218,6 +242,10 @@ static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jint strength
|
||||
return -1;
|
||||
}
|
||||
|
||||
static jlong vibratorGetCapabilities(JNIEnv*, jclass) {
|
||||
return halCall(&V1_4::IVibrator::getCapabilities).withDefault(0);
|
||||
}
|
||||
|
||||
static const JNINativeMethod method_table[] = {
|
||||
{ "vibratorExists", "()Z", (void*)vibratorExists },
|
||||
{ "vibratorInit", "()V", (void*)vibratorInit },
|
||||
@@ -225,13 +253,18 @@ static const JNINativeMethod method_table[] = {
|
||||
{ "vibratorOff", "()V", (void*)vibratorOff },
|
||||
{ "vibratorSupportsAmplitudeControl", "()Z", (void*)vibratorSupportsAmplitudeControl},
|
||||
{ "vibratorSetAmplitude", "(I)V", (void*)vibratorSetAmplitude},
|
||||
{ "vibratorPerformEffect", "(JJ)J", (void*)vibratorPerformEffect},
|
||||
{ "vibratorPerformEffect", "(JJLcom/android/server/VibratorService$Vibration;)J",
|
||||
(void*)vibratorPerformEffect},
|
||||
{ "vibratorSupportsExternalControl", "()Z", (void*)vibratorSupportsExternalControl},
|
||||
{ "vibratorSetExternalControl", "(Z)V", (void*)vibratorSetExternalControl},
|
||||
{ "vibratorGetCapabilities", "()J", (void*)vibratorGetCapabilities},
|
||||
};
|
||||
|
||||
int register_android_server_VibratorService(JNIEnv *env)
|
||||
{
|
||||
sMethodIdOnComplete = GetMethodIDOrDie(env,
|
||||
FindClassOrDie(env, "com/android/server/VibratorService$Vibration"),
|
||||
"onComplete", "()V");
|
||||
return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
|
||||
method_table, NELEM(method_table));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user