下面列出了android.os.VibrationEffect#Waveform ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @hide
*/
@Override
public void vibrate(int uid, String opPkg,
VibrationEffect effect, AudioAttributes attributes) {
long[] pattern;
int repeat;
if (effect instanceof VibrationEffect.OneShot) {
VibrationEffect.OneShot oneShot = (VibrationEffect.OneShot) effect;
pattern = new long[] { 0, oneShot.getDuration() };
repeat = -1;
} else if (effect instanceof VibrationEffect.Waveform) {
VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) effect;
pattern = waveform.getTimings();
repeat = waveform.getRepeatIndex();
} else {
// TODO: Add support for prebaked effects
Log.w(TAG, "Pre-baked effects aren't supported on input devices");
return;
}
try {
mIm.vibrate(mDeviceId, pattern, repeat, mToken);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
@GuardedBy("mLock")
private void startVibrationInnerLocked(Vibration vib) {
Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "startVibrationInnerLocked");
try {
mCurrentVibration = vib;
if (vib.effect instanceof VibrationEffect.OneShot) {
Trace.asyncTraceBegin(Trace.TRACE_TAG_VIBRATOR, "vibration", 0);
VibrationEffect.OneShot oneShot = (VibrationEffect.OneShot) vib.effect;
doVibratorOn(oneShot.getDuration(), oneShot.getAmplitude(), vib.uid, vib.usageHint);
mH.postDelayed(mVibrationEndRunnable, oneShot.getDuration());
} else if (vib.effect instanceof VibrationEffect.Waveform) {
// mThread better be null here. doCancelVibrate should always be
// called before startNextVibrationLocked or startVibrationLocked.
Trace.asyncTraceBegin(Trace.TRACE_TAG_VIBRATOR, "vibration", 0);
VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) vib.effect;
mThread = new VibrateThread(waveform, vib.uid, vib.usageHint);
mThread.start();
} else if (vib.effect instanceof VibrationEffect.Prebaked) {
Trace.asyncTraceBegin(Trace.TRACE_TAG_VIBRATOR, "vibration", 0);
long timeout = doVibratorPrebakedEffectLocked(vib);
if (timeout > 0) {
mH.postDelayed(mVibrationEndRunnable, timeout);
}
} else {
Slog.e(TAG, "Unknown vibration type, ignoring");
}
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR);
}
}
private void linkVibration(Vibration vib) {
// Only link against waveforms since they potentially don't have a finish if
// they're repeating. Let other effects just play out until they're done.
if (vib.effect instanceof VibrationEffect.Waveform) {
try {
vib.token.linkToDeath(vib, 0);
} catch (RemoteException e) {
return;
}
}
}
VibrateThread(VibrationEffect.Waveform waveform, int uid, int usageHint) {
mWaveform = waveform;
mUid = uid;
mUsageHint = usageHint;
mTmpWorkSource.set(uid);
mWakeLock.setWorkSource(mTmpWorkSource);
}
/**
* Scale the vibration effect by the intensity as appropriate based its intent.
*/
private void applyVibrationIntensityScalingLocked(Vibration vib, int intensity) {
if (vib.effect instanceof VibrationEffect.Prebaked) {
// Prebaked effects are always just a direct translation from intensity to
// EffectStrength.
VibrationEffect.Prebaked prebaked = (VibrationEffect.Prebaked)vib.effect;
prebaked.setEffectStrength(intensityToEffectStrength(intensity));
return;
}
final int defaultIntensity;
if (vib.isNotification() || vib.isRingtone()) {
defaultIntensity = mVibrator.getDefaultNotificationVibrationIntensity();
} else if (vib.isHapticFeedback()) {
defaultIntensity = mVibrator.getDefaultHapticFeedbackIntensity();
} else {
// If we don't know what kind of vibration we're playing then just skip scaling for
// now.
return;
}
final ScaleLevel scale = mScaleLevels.get(intensity - defaultIntensity);
if (scale == null) {
// We should have scaling levels for all cases, so not being able to scale because of a
// missing level is unexpected.
Slog.e(TAG, "No configured scaling level!"
+ " (current=" + intensity + ", default= " + defaultIntensity + ")");
return;
}
VibrationEffect scaledEffect = null;
if (vib.effect instanceof VibrationEffect.OneShot) {
VibrationEffect.OneShot oneShot = (VibrationEffect.OneShot) vib.effect;
oneShot = oneShot.resolve(mDefaultVibrationAmplitude);
scaledEffect = oneShot.scale(scale.gamma, scale.maxAmplitude);
} else if (vib.effect instanceof VibrationEffect.Waveform) {
VibrationEffect.Waveform waveform = (VibrationEffect.Waveform) vib.effect;
waveform = waveform.resolve(mDefaultVibrationAmplitude);
scaledEffect = waveform.scale(scale.gamma, scale.maxAmplitude);
} else {
Slog.w(TAG, "Unable to apply intensity scaling, unknown VibrationEffect type");
}
if (scaledEffect != null) {
vib.originalEffect = vib.effect;
vib.effect = scaledEffect;
}
}
private void unlinkVibration(Vibration vib) {
if (vib.effect instanceof VibrationEffect.Waveform) {
vib.token.unlinkToDeath(vib, 0);
}
}