names = new ArrayList<>();
if (hasCapability(IVibrator.CAP_ON_CALLBACK)) {
names.add("ON_CALLBACK");
}
if (hasCapability(IVibrator.CAP_PERFORM_CALLBACK)) {
names.add("PERFORM_CALLBACK");
}
if (hasCapability(IVibrator.CAP_COMPOSE_EFFECTS)) {
names.add("COMPOSE_EFFECTS");
}
if (hasCapability(IVibrator.CAP_COMPOSE_PWLE_EFFECTS)) {
names.add("COMPOSE_PWLE_EFFECTS");
}
if (hasCapability(IVibrator.CAP_ALWAYS_ON_CONTROL)) {
names.add("ALWAYS_ON_CONTROL");
}
if (hasCapability(IVibrator.CAP_AMPLITUDE_CONTROL)) {
names.add("AMPLITUDE_CONTROL");
}
if (hasCapability(IVibrator.CAP_FREQUENCY_CONTROL)) {
names.add("FREQUENCY_CONTROL");
}
if (hasCapability(IVibrator.CAP_EXTERNAL_CONTROL)) {
names.add("EXTERNAL_CONTROL");
}
if (hasCapability(IVibrator.CAP_EXTERNAL_AMPLITUDE_CONTROL)) {
names.add("EXTERNAL_AMPLITUDE_CONTROL");
}
return names.toArray(new String[names.size()]);
}
private String[] getSupportedEffectsNames() {
if (mSupportedEffects == null) {
return new String[0];
}
String[] names = new String[mSupportedEffects.size()];
for (int i = 0; i < mSupportedEffects.size(); i++) {
names[i] = VibrationEffect.effectIdToString(mSupportedEffects.keyAt(i));
}
return names;
}
private String[] getSupportedBrakingNames() {
if (mSupportedBraking == null) {
return new String[0];
}
String[] names = new String[mSupportedBraking.size()];
for (int i = 0; i < mSupportedBraking.size(); i++) {
switch (mSupportedBraking.keyAt(i)) {
case Braking.NONE:
names[i] = "NONE";
break;
case Braking.CLAB:
names[i] = "CLAB";
break;
default:
names[i] = Integer.toString(mSupportedBraking.keyAt(i));
}
}
return names;
}
private String[] getSupportedPrimitivesNames() {
int supportedPrimitivesCount = mSupportedPrimitives.size();
String[] names = new String[supportedPrimitivesCount];
for (int i = 0; i < supportedPrimitivesCount; i++) {
names[i] = VibrationEffect.Composition.primitiveToString(mSupportedPrimitives.keyAt(i));
}
return names;
}
/**
* Describes how frequency should be mapped to absolute values for a specific {@link Vibrator}.
*
* This mapping is defined by the following parameters:
*
*
* - {@code minFrequency}, {@code resonantFrequency} and {@code frequencyResolution}, in
* hertz, provided by the vibrator.
*
- {@code maxAmplitudes} a list of values in [0,1] provided by the vibrator, where
* {@code maxAmplitudes[i]} represents max supported amplitude at frequency
* {@code minFrequency + frequencyResolution * i}.
*
- {@code maxFrequency = minFrequency + frequencyResolution * (maxAmplitudes.length-1)}
*
- {@code suggestedSafeRangeHz} is the suggested frequency range in hertz that should be
* mapped to relative values -1 and 1, where 0 maps to {@code resonantFrequency}.
*
*
* The mapping is defined linearly by the following points:
*
*
* - {@code toHertz(relativeMinFrequency) = minFrequency}
*
- {@code toHertz(-1) = resonantFrequency - safeRange / 2}
*
- {@code toHertz(0) = resonantFrequency}
*
- {@code toHertz(1) = resonantFrequency + safeRange / 2}
*
- {@code toHertz(relativeMaxFrequency) = maxFrequency}
*
*
* @hide
*/
public static final class FrequencyMapping implements Parcelable {
private final float mMinFrequencyHz;
private final float mResonantFrequencyHz;
private final float mFrequencyResolutionHz;
private final float mSuggestedSafeRangeHz;
private final float[] mMaxAmplitudes;
// Relative fields calculated from input values:
private final Range mRelativeFrequencyRange;
FrequencyMapping(Parcel in) {
this(in.readFloat(), in.readFloat(), in.readFloat(), in.readFloat(),
in.createFloatArray());
}
/**
* Default constructor.
*
* @param minFrequencyHz Minimum supported frequency, in hertz.
* @param resonantFrequencyHz The vibrator resonant frequency, in hertz.
* @param frequencyResolutionHz The frequency resolution, in hertz, used by the max
* amplitudes mapping.
* @param suggestedSafeRangeHz The suggested range, in hertz, for the safe relative
* frequency range represented by [-1, 1].
* @param maxAmplitudes The max amplitude supported by each supported frequency,
* starting at minimum frequency with jumps of frequency
* resolution.
* @hide
*/
public FrequencyMapping(float minFrequencyHz, float resonantFrequencyHz,
float frequencyResolutionHz, float suggestedSafeRangeHz, float[] maxAmplitudes) {
mMinFrequencyHz = minFrequencyHz;
mResonantFrequencyHz = resonantFrequencyHz;
mFrequencyResolutionHz = frequencyResolutionHz;
mSuggestedSafeRangeHz = suggestedSafeRangeHz;
mMaxAmplitudes = new float[maxAmplitudes == null ? 0 : maxAmplitudes.length];
if (maxAmplitudes != null) {
System.arraycopy(maxAmplitudes, 0, mMaxAmplitudes, 0, maxAmplitudes.length);
}
float maxFrequencyHz =
minFrequencyHz + frequencyResolutionHz * (mMaxAmplitudes.length - 1);
if (Float.isNaN(resonantFrequencyHz) || Float.isNaN(minFrequencyHz)
|| Float.isNaN(frequencyResolutionHz) || Float.isNaN(suggestedSafeRangeHz)
|| resonantFrequencyHz < minFrequencyHz
|| resonantFrequencyHz > maxFrequencyHz) {
// Some required fields are undefined or have bad values.
// Leave this mapping empty.
mRelativeFrequencyRange = Range.create(0f, 0f);
return;
}
// Calculate actual safe range, limiting the suggested one by the device supported range
float safeDelta = MathUtils.min(
suggestedSafeRangeHz / 2,
resonantFrequencyHz - minFrequencyHz,
maxFrequencyHz - resonantFrequencyHz);
mRelativeFrequencyRange = Range.create(
(minFrequencyHz - resonantFrequencyHz) / safeDelta,
(maxFrequencyHz - resonantFrequencyHz) / safeDelta);
}
/**
* Returns true if this frequency mapping is empty, i.e. the only supported relative
* frequency is 0 (resonant frequency).
*/
public boolean isEmpty() {
return Float.compare(mRelativeFrequencyRange.getLower(),
mRelativeFrequencyRange.getUpper()) == 0;
}
/**
* Returns the frequency value in hertz that is mapped to the given relative frequency.
*
* @return The mapped frequency, in hertz, or {@link Float#NaN} is value outside the device
* supported range.
*/
public float toHertz(float relativeFrequency) {
if (!mRelativeFrequencyRange.contains(relativeFrequency)) {
return Float.NaN;
}
float relativeMinFrequency = mRelativeFrequencyRange.getLower();
if (Float.compare(relativeMinFrequency, 0) == 0) {
// relative supported range is [0,0], so toHertz(0) should be the resonant frequency
return mResonantFrequencyHz;
}
float shift = (mMinFrequencyHz - mResonantFrequencyHz) / relativeMinFrequency;
return mResonantFrequencyHz + relativeFrequency * shift;
}
/**
* Returns the maximum amplitude the vibrator can reach while playing at given relative
* frequency.
*
* @return A value in [0,1] representing the max amplitude supported at given relative
* frequency. This will return 0 if frequency is outside supported range, or if max
* amplitude mapping is empty.
*/
public float getMaxAmplitude(float relativeFrequency) {
float frequencyHz = toHertz(relativeFrequency);
if (Float.isNaN(frequencyHz)) {
// Unsupported frequency requested, vibrator cannot play at this frequency.
return 0;
}
float position = (frequencyHz - mMinFrequencyHz) / mFrequencyResolutionHz;
int floorIndex = (int) Math.floor(position);
int ceilIndex = (int) Math.ceil(position);
if (floorIndex < 0 || floorIndex >= mMaxAmplitudes.length) {
if (mMaxAmplitudes.length > 0) {
// This should never happen if the setup of relative frequencies was correct.
Log.w(TAG, "Max amplitudes has " + mMaxAmplitudes.length
+ " entries and was expected to cover the frequency " + frequencyHz
+ " Hz when starting at min frequency of " + mMinFrequencyHz
+ " Hz with resolution of " + mFrequencyResolutionHz + " Hz.");
}
return 0;
}
if (floorIndex != ceilIndex && ceilIndex < mMaxAmplitudes.length) {
// Value in between two mapped frequency values, use the lowest supported one.
return MathUtils.min(mMaxAmplitudes[floorIndex], mMaxAmplitudes[ceilIndex]);
}
return mMaxAmplitudes[floorIndex];
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(mMinFrequencyHz);
dest.writeFloat(mResonantFrequencyHz);
dest.writeFloat(mFrequencyResolutionHz);
dest.writeFloat(mSuggestedSafeRangeHz);
dest.writeFloatArray(mMaxAmplitudes);
}
@Override
public int describeContents() {
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof FrequencyMapping)) {
return false;
}
FrequencyMapping that = (FrequencyMapping) o;
return Float.compare(mMinFrequencyHz, that.mMinFrequencyHz) == 0
&& Float.compare(mResonantFrequencyHz, that.mResonantFrequencyHz) == 0
&& Float.compare(mFrequencyResolutionHz, that.mFrequencyResolutionHz) == 0
&& Float.compare(mSuggestedSafeRangeHz, that.mSuggestedSafeRangeHz) == 0
&& Arrays.equals(mMaxAmplitudes, that.mMaxAmplitudes);
}
@Override
public int hashCode() {
int hashCode = Objects.hash(mMinFrequencyHz, mFrequencyResolutionHz,
mFrequencyResolutionHz, mSuggestedSafeRangeHz);
hashCode = 31 * hashCode + Arrays.hashCode(mMaxAmplitudes);
return hashCode;
}
@Override
public String toString() {
return "FrequencyMapping{"
+ "mRelativeFrequencyRange=" + mRelativeFrequencyRange
+ ", mMinFrequency=" + mMinFrequencyHz
+ ", mResonantFrequency=" + mResonantFrequencyHz
+ ", mMaxFrequency="
+ (mMinFrequencyHz + mFrequencyResolutionHz * (mMaxAmplitudes.length - 1))
+ ", mFrequencyResolution=" + mFrequencyResolutionHz
+ ", mSuggestedSafeRange=" + mSuggestedSafeRangeHz
+ ", mMaxAmplitudes count=" + mMaxAmplitudes.length
+ '}';
}
@NonNull
public static final Creator CREATOR =
new Creator() {
@Override
public FrequencyMapping createFromParcel(Parcel in) {
return new FrequencyMapping(in);
}
@Override
public FrequencyMapping[] newArray(int size) {
return new FrequencyMapping[size];
}
};
}
/** @hide */
public static final class Builder {
private final int mId;
private long mCapabilities;
private SparseBooleanArray mSupportedEffects;
private SparseBooleanArray mSupportedBraking;
private SparseIntArray mSupportedPrimitives = new SparseIntArray();
private int mPrimitiveDelayMax;
private int mCompositionSizeMax;
private int mPwlePrimitiveDurationMax;
private int mPwleSizeMax;
private float mQFactor = Float.NaN;
private FrequencyMapping mFrequencyMapping =
new FrequencyMapping(Float.NaN, Float.NaN, Float.NaN, Float.NaN, null);
/** A builder class for a {@link VibratorInfo}. */
public Builder(int id) {
mId = id;
}
/** Configure the vibrator capabilities with a combination of IVibrator.CAP_* values. */
@NonNull
public Builder setCapabilities(long capabilities) {
mCapabilities = capabilities;
return this;
}
/** Configure the effects supported with {@link android.hardware.vibrator.Effect} values. */
@NonNull
public Builder setSupportedEffects(int... supportedEffects) {
mSupportedEffects = toSparseBooleanArray(supportedEffects);
return this;
}
/** Configure braking supported with {@link android.hardware.vibrator.Braking} values. */
@NonNull
public Builder setSupportedBraking(int... supportedBraking) {
mSupportedBraking = toSparseBooleanArray(supportedBraking);
return this;
}
/** Configure maximum duration, in milliseconds, of a PWLE primitive. */
@NonNull
public Builder setPwlePrimitiveDurationMax(int pwlePrimitiveDurationMax) {
mPwlePrimitiveDurationMax = pwlePrimitiveDurationMax;
return this;
}
/** Configure maximum number of primitives supported in a single PWLE composed effect. */
@NonNull
public Builder setPwleSizeMax(int pwleSizeMax) {
mPwleSizeMax = pwleSizeMax;
return this;
}
/** Configure the duration of a {@link android.hardware.vibrator.CompositePrimitive}. */
@NonNull
public Builder setSupportedPrimitive(int primitiveId, int duration) {
mSupportedPrimitives.put(primitiveId, duration);
return this;
}
/** Configure maximum delay, in milliseconds, supported in a composed effect primitive. */
@NonNull
public Builder setPrimitiveDelayMax(int primitiveDelayMax) {
mPrimitiveDelayMax = primitiveDelayMax;
return this;
}
/** Configure maximum number of primitives supported in a single composed effect. */
@NonNull
public Builder setCompositionSizeMax(int compositionSizeMax) {
mCompositionSizeMax = compositionSizeMax;
return this;
}
/** Configure the vibrator quality factor. */
@NonNull
public Builder setQFactor(float qFactor) {
mQFactor = qFactor;
return this;
}
/** Configure the vibrator frequency information like resonant frequency and bandwidth. */
@NonNull
public Builder setFrequencyMapping(FrequencyMapping frequencyMapping) {
mFrequencyMapping = frequencyMapping;
return this;
}
/** Build the configured {@link VibratorInfo}. */
@NonNull
public VibratorInfo build() {
return new VibratorInfo(mId, mCapabilities, mSupportedEffects, mSupportedBraking,
mSupportedPrimitives, mPrimitiveDelayMax, mCompositionSizeMax,
mPwlePrimitiveDurationMax, mPwleSizeMax, mQFactor, mFrequencyMapping);
}
/**
* Create a {@link SparseBooleanArray} from given {@code supportedKeys} where each key is
* mapped
* to {@code true}.
*/
@Nullable
private static SparseBooleanArray toSparseBooleanArray(int[] supportedKeys) {
if (supportedKeys == null) {
return null;
}
SparseBooleanArray array = new SparseBooleanArray();
for (int key : supportedKeys) {
array.put(key, true);
}
return array;
}
}
@NonNull
public static final Creator CREATOR =
new Creator() {
@Override
public VibratorInfo createFromParcel(Parcel in) {
return new VibratorInfo(in);
}
@Override
public VibratorInfo[] newArray(int size) {
return new VibratorInfo[size];
}
};
}