Class Haptics
Typed haptic feedback: impacts, selection ticks and notification feedback, mapped to the platform's own effects.
On iOS these are the UIImpactFeedbackGenerator,
UISelectionFeedbackGenerator and UINotificationFeedbackGenerator
effects. On Android they are the view's haptic feedback constants. Where
a port has no typed haptics the call falls back to
Display#vibrate(int) with a duration that matches the type, and where
the device has no vibrator nothing happens.
Referencing this class makes the Android build declare the VIBRATE
permission.
Button save = new Button("Save");
save.addActionListener(e -> Haptics.notification(Haptics.SUCCESS));
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intA task failed.static final intA heavy impact, for large elements colliding.static final intA light impact, for small elements snapping into place.static final intA medium impact.static final intA rigid, sharp impact.static final intA selection change tick, such as a picker moving one row.static final intA soft, damped impact.static final intA task completed successfully.static final intA task produced a warning. -
Method Summary
Modifier and TypeMethodDescriptionstatic intfallbackDuration(int type) The vibration length, in milliseconds, used for a type when the platform has no typed haptics.static voidimpact(int style) Plays an impact.static booleanTrue when the platform plays typed haptics.static voidnotification(int type) Plays notification feedback.static voidpattern(long[] timings) Plays a vibration pattern.static voidPlays a selection change tick.
-
Field Details
-
LIGHT
public static final int LIGHTA light impact, for small elements snapping into place.- See Also:
-
MEDIUM
public static final int MEDIUMA medium impact.- See Also:
-
HEAVY
public static final int HEAVYA heavy impact, for large elements colliding.- See Also:
-
SOFT
public static final int SOFTA soft, damped impact.- See Also:
-
RIGID
public static final int RIGIDA rigid, sharp impact.- See Also:
-
SELECTION
public static final int SELECTIONA selection change tick, such as a picker moving one row.- See Also:
-
SUCCESS
public static final int SUCCESSA task completed successfully.- See Also:
-
WARNING
public static final int WARNINGA task produced a warning.- See Also:
-
ERROR
public static final int ERRORA task failed.- See Also:
-
-
Method Details
-
isSupported
public static boolean isSupported()True when the platform plays typed haptics. When false, every method here still works and falls back to a short vibration.
Returns
true when typed haptics are available
-
impact
public static void impact(int style) Plays an impact.
Parameters
style:LIGHT,MEDIUM,HEAVY,SOFTorRIGID
-
selection
public static void selection()Plays a selection change tick. -
notification
public static void notification(int type) Plays notification feedback.
Parameters
type:SUCCESS,WARNINGorERROR
-
pattern
public static void pattern(long[] timings) Plays a vibration pattern. Android plays it as a waveform; iOS plays an impact at the start of every "on" segment; elsewhere the first "on" segment becomes a plain vibration.
Parameters
timings: alternating off and on durations in milliseconds, starting with an off duration, such as{0, 40, 60, 40}
-
fallbackDuration
public static int fallbackDuration(int type) The vibration length, in milliseconds, used for a type when the platform has no typed haptics.
Parameters
type: a type constant of this class
Returns
the fallback duration in milliseconds
-