Merge changes Ia639dd01,I12cf8838

* changes:
  Vibrator Service: Support Async Callback APIs
  Vibrator Service: Allow HAL Version Checking
This commit is contained in:
Steven Moreland
2019-10-31 21:23:55 +00:00
committed by Gerrit Code Review
4 changed files with 125 additions and 52 deletions

View File

@@ -49,7 +49,7 @@ java_library_static {
"android.hardware.biometrics.fingerprint-V2.1-java", "android.hardware.biometrics.fingerprint-V2.1-java",
"android.hardware.oemlock-V1.0-java", "android.hardware.oemlock-V1.0-java",
"android.hardware.tetheroffload.control-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.configstore-V1.0-java",
"android.hardware.contexthub-V1.0-java", "android.hardware.contexthub-V1.0-java",
"android.hidl.manager-V1.2-java", "android.hidl.manager-V1.2-java",

View File

@@ -29,6 +29,7 @@ import android.content.res.Resources;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.hardware.input.InputManager; import android.hardware.input.InputManager;
import android.hardware.vibrator.V1_0.EffectStrength; import android.hardware.vibrator.V1_0.EffectStrength;
import android.hardware.vibrator.V1_4.Capabilities;
import android.icu.text.DateFormat; import android.icu.text.DateFormat;
import android.media.AudioAttributes; import android.media.AudioAttributes;
import android.media.AudioManager; 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. // If a vibration is playing for longer than 5s, it's probably not haptic feedback.
private static final long MAX_HAPTIC_FEEDBACK_DURATION = 5000; 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 // 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 // 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 mAllowPriorityVibrationsInLowPowerMode;
private final boolean mSupportsAmplitudeControl; private final boolean mSupportsAmplitudeControl;
private final boolean mSupportsExternalControl; private final boolean mSupportsExternalControl;
private final long mCapabilities;
private final int mDefaultVibrationAmplitude; private final int mDefaultVibrationAmplitude;
private final SparseArray<VibrationEffect> mFallbackEffects; private final SparseArray<VibrationEffect> mFallbackEffects;
private final SparseArray<Integer> mProcStatesCache = new SparseArray(); private final SparseArray<Integer> mProcStatesCache = new SparseArray();
@@ -163,9 +168,10 @@ public class VibratorService extends IVibratorService.Stub
static native void vibratorOff(); static native void vibratorOff();
static native boolean vibratorSupportsAmplitudeControl(); static native boolean vibratorSupportsAmplitudeControl();
static native void vibratorSetAmplitude(int amplitude); 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 boolean vibratorSupportsExternalControl();
static native void vibratorSetExternalControl(boolean enabled); static native void vibratorSetExternalControl(boolean enabled);
static native long vibratorGetCapabilities();
private final IUidObserver mUidObserver = new IUidObserver.Stub() { private final IUidObserver mUidObserver = new IUidObserver.Stub() {
@Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { @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) { public boolean hasTimeoutLongerThan(long millis) {
final long duration = effect.getDuration(); final long duration = effect.getDuration();
return duration >= 0 && duration > millis; return duration >= 0 && duration > millis;
@@ -347,6 +361,7 @@ public class VibratorService extends IVibratorService.Stub
mSupportsAmplitudeControl = vibratorSupportsAmplitudeControl(); mSupportsAmplitudeControl = vibratorSupportsAmplitudeControl();
mSupportsExternalControl = vibratorSupportsExternalControl(); mSupportsExternalControl = vibratorSupportsExternalControl();
mCapabilities = vibratorGetCapabilities();
mContext = context; mContext = context;
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); 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. // Input devices don't support prebaked effect, so skip trying it with them.
if (!usingInputDeviceVibrators) { if (!usingInputDeviceVibrators) {
long timeout = vibratorPerformEffect(prebaked.getId(), long duration = vibratorPerformEffect(prebaked.getId(),
prebaked.getEffectStrength()); prebaked.getEffectStrength(), vib);
long timeout = duration;
if ((mCapabilities & Capabilities.PERFORM_COMPLETION_CALLBACK) != 0) {
timeout *= ASYNC_TIMEOUT_MULTIPLIER;
}
if (timeout > 0) { if (timeout > 0) {
noteVibratorOnLocked(vib.uid, timeout); noteVibratorOnLocked(vib.uid, duration);
return timeout; return timeout;
} }
} }

View File

@@ -130,6 +130,7 @@ cc_defaults {
"android.hardware.vibrator@1.1", "android.hardware.vibrator@1.1",
"android.hardware.vibrator@1.2", "android.hardware.vibrator@1.2",
"android.hardware.vibrator@1.3", "android.hardware.vibrator@1.3",
"android.hardware.vibrator@1.4",
"android.hardware.vr@1.0", "android.hardware.vr@1.0",
"android.frameworks.schedulerservice@1.0", "android.frameworks.schedulerservice@1.0",
"android.frameworks.sensorservice@1.0", "android.frameworks.sensorservice@1.0",

View File

@@ -16,17 +16,12 @@
#define LOG_TAG "VibratorService" #define LOG_TAG "VibratorService"
#include <android/hardware/vibrator/1.0/IVibrator.h> #include <android/hardware/vibrator/1.4/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 "jni.h" #include "jni.h"
#include <nativehelper/JNIHelp.h> #include <nativehelper/JNIHelp.h>
#include "android_runtime/AndroidRuntime.h" #include "android_runtime/AndroidRuntime.h"
#include "core_jni_helpers.h"
#include <utils/misc.h> #include <utils/misc.h>
#include <utils/Log.h> #include <utils/Log.h>
@@ -36,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
using android::hardware::Return; using android::hardware::Return;
using android::hardware::Void;
using android::hardware::vibrator::V1_0::EffectStrength; using android::hardware::vibrator::V1_0::EffectStrength;
using android::hardware::vibrator::V1_0::Status; using android::hardware::vibrator::V1_0::Status;
using android::hardware::vibrator::V1_1::Effect_1_1; 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_1 = android::hardware::vibrator::V1_1;
namespace V1_2 = android::hardware::vibrator::V1_2; namespace V1_2 = android::hardware::vibrator::V1_2;
namespace V1_3 = android::hardware::vibrator::V1_3; namespace V1_3 = android::hardware::vibrator::V1_3;
namespace V1_4 = android::hardware::vibrator::V1_4;
namespace android { 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; static constexpr int NUM_TRIES = 2;
// Creates a Return<R> with STATUS::EX_NULL_POINTER. // Creates a Return<R> with STATUS::EX_NULL_POINTER.
@@ -56,18 +75,19 @@ inline Return<R> NullptrStatus() {
return Return<R>{Status::fromExceptionCode(Status::EX_NULL_POINTER)}; return Return<R>{Status::fromExceptionCode(Status::EX_NULL_POINTER)};
} }
// Helper used to transparently deal with the vibrator HAL becoming unavailable. template <typename I>
template<class R, class I, class... Args0, class... Args1> class HalWrapper {
Return<R> halCall(Return<R> (I::* fn)(Args0...), Args1&&... args1) { public:
static std::unique_ptr<HalWrapper> Create() {
// Assume that if getService returns a nullptr, HAL is not available on the // Assume that if getService returns a nullptr, HAL is not available on the
// device. // device.
static sp<I> sHal = I::getService(); auto hal = I::getService();
static bool sAvailable = sHal != nullptr; return hal ? std::unique_ptr<HalWrapper>(new HalWrapper(std::move(hal))) : nullptr;
if (!sAvailable) {
return NullptrStatus<R>();
} }
// Helper used to transparently deal with the vibrator HAL becoming unavailable.
template<class R, class... Args0, class... Args1>
Return<R> call(Return<R> (I::* fn)(Args0...), Args1&&... args1) {
// Return<R> doesn't have a default constructor, so make a Return<R> with // Return<R> doesn't have a default constructor, so make a Return<R> with
// STATUS::EX_NONE. // STATUS::EX_NONE.
using ::android::hardware::Status; using ::android::hardware::Status;
@@ -75,8 +95,8 @@ Return<R> halCall(Return<R> (I::* fn)(Args0...), Args1&&... args1) {
// Note that ret is guaranteed to be changed after this loop. // Note that ret is guaranteed to be changed after this loop.
for (int i = 0; i < NUM_TRIES; ++i) { for (int i = 0; i < NUM_TRIES; ++i) {
ret = (sHal == nullptr) ? NullptrStatus<R>() ret = (mHal == nullptr) ? NullptrStatus<R>()
: (*sHal.*fn)(std::forward<Args1>(args1)...); : (*mHal.*fn)(std::forward<Args1>(args1)...);
if (ret.isOk()) { if (ret.isOk()) {
break; break;
@@ -84,9 +104,28 @@ Return<R> halCall(Return<R> (I::* fn)(Args0...), Args1&&... args1) {
ALOGE("Failed to issue command to vibrator HAL. Retrying."); ALOGE("Failed to issue command to vibrator HAL. Retrying.");
// Restoring connection to the HAL. // Restoring connection to the HAL.
sHal = I::tryGetService(); mHal = I::tryGetService();
} }
return ret; return ret;
}
private:
HalWrapper(sp<I> &&hal) : mHal(std::move(hal)) {}
private:
sp<I> mHal;
};
template <typename I>
static auto getHal() {
static auto sHalWrapper = HalWrapper<I>::Create();
return sHalWrapper.get();
}
template<class R, class I, class... Args0, class... Args1>
Return<R> halCall(Return<R> (I::* fn)(Args0...), Args1&&... args1) {
auto hal = getHal<I>();
return hal ? hal->call(fn, std::forward<Args1>(args1)...) : NullptrStatus<R>();
} }
template<class R> template<class R>
@@ -99,17 +138,17 @@ bool isValidEffect(jlong effect) {
return val >= *iter.begin() && val <= *std::prev(iter.end()); 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(); 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; 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); Status retStatus = halCall(&V1_0::IVibrator::on, timeout_ms).withDefault(Status::UNKNOWN_ERROR);
if (retStatus != Status::OK) { if (retStatus != Status::OK) {
@@ -117,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); Status retStatus = halCall(&V1_0::IVibrator::off).withDefault(Status::UNKNOWN_ERROR);
if (retStatus != Status::OK) { if (retStatus != Status::OK) {
@@ -125,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); 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)) Status status = halCall(&V1_0::IVibrator::setAmplitude, static_cast<uint32_t>(amplitude))
.withDefault(Status::UNKNOWN_ERROR); .withDefault(Status::UNKNOWN_ERROR);
if (status != Status::OK) { if (status != Status::OK) {
@@ -138,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); 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)) Status status = halCall(&V1_3::IVibrator::setExternalControl, static_cast<uint32_t>(enabled))
.withDefault(Status::UNKNOWN_ERROR); .withDefault(Status::UNKNOWN_ERROR);
if (status != Status::OK) { if (status != Status::OK) {
@@ -151,7 +190,8 @@ static void vibratorSetExternalControl(JNIEnv*, jobject, jboolean enabled) {
} }
} }
static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jlong strength) { static jlong vibratorPerformEffect(JNIEnv* env, jclass, jlong effect, jlong strength,
jobject vibration) {
Status status; Status status;
uint32_t lengthMs; uint32_t lengthMs;
auto callback = [&status, &lengthMs](Status retStatus, uint32_t retLengthMs) { auto callback = [&status, &lengthMs](Status retStatus, uint32_t retLengthMs) {
@@ -161,7 +201,11 @@ static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jlong strengt
EffectStrength effectStrength(static_cast<EffectStrength>(strength)); EffectStrength effectStrength(static_cast<EffectStrength>(strength));
Return<void> ret; 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), ret = halCall(&V1_0::IVibrator::perform, static_cast<V1_0::Effect>(effect),
effectStrength, callback); effectStrength, callback);
} else if (isValidEffect<Effect_1_1>(effect)) { } else if (isValidEffect<Effect_1_1>(effect)) {
@@ -198,6 +242,10 @@ static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jlong strengt
return -1; return -1;
} }
static jlong vibratorGetCapabilities(JNIEnv*, jclass) {
return halCall(&V1_4::IVibrator::getCapabilities).withDefault(0);
}
static const JNINativeMethod method_table[] = { static const JNINativeMethod method_table[] = {
{ "vibratorExists", "()Z", (void*)vibratorExists }, { "vibratorExists", "()Z", (void*)vibratorExists },
{ "vibratorInit", "()V", (void*)vibratorInit }, { "vibratorInit", "()V", (void*)vibratorInit },
@@ -205,13 +253,18 @@ static const JNINativeMethod method_table[] = {
{ "vibratorOff", "()V", (void*)vibratorOff }, { "vibratorOff", "()V", (void*)vibratorOff },
{ "vibratorSupportsAmplitudeControl", "()Z", (void*)vibratorSupportsAmplitudeControl}, { "vibratorSupportsAmplitudeControl", "()Z", (void*)vibratorSupportsAmplitudeControl},
{ "vibratorSetAmplitude", "(I)V", (void*)vibratorSetAmplitude}, { "vibratorSetAmplitude", "(I)V", (void*)vibratorSetAmplitude},
{ "vibratorPerformEffect", "(JJ)J", (void*)vibratorPerformEffect}, { "vibratorPerformEffect", "(JJLcom/android/server/VibratorService$Vibration;)J",
(void*)vibratorPerformEffect},
{ "vibratorSupportsExternalControl", "()Z", (void*)vibratorSupportsExternalControl}, { "vibratorSupportsExternalControl", "()Z", (void*)vibratorSupportsExternalControl},
{ "vibratorSetExternalControl", "(Z)V", (void*)vibratorSetExternalControl}, { "vibratorSetExternalControl", "(Z)V", (void*)vibratorSetExternalControl},
{ "vibratorGetCapabilities", "()J", (void*)vibratorGetCapabilities},
}; };
int register_android_server_VibratorService(JNIEnv *env) 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", return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
method_table, NELEM(method_table)); method_table, NELEM(method_table));
} }