[MUSIC PLAYING]
CLAIRE LEE: Hi, everyone.
My name is Claire.
TYLER FREEMAN: I'm Tyler.
CLAIRE LEE: Today, we're going to talk about advanced haptics
API, including when to use, what to use, and how to use.
So you want to add haptic effects for your app,
and you found these two constants--
class-- HapticFeedbackConstants and VibrationEffect.
But you may still have questions like,
so what's the difference between these two?
Or what should I use for my app?
We are here to answer your question.
First, if you're working on very specific UI
element with specific input event,
such as keyboard for typing or a button for clicking,
then HapticFeedbackConstants is a good place to start.
Assuming that you're working on a button,
then it comes with a click event.
Then, just remember one thing.
The reason that you want to have haptic feedback for a click
event is, in general, to simulate
the behavior of the physical hard button, which mostly
come with a pairwise interaction model of pressing
and releasing.
Just remember the pairwise interaction
of press and release and go find your haptic constants.
For example, for a keyboard, you can find KEYBOARD_PRESS
and KEYBOARD_RELEASE effect.
For any other clickable element, you
can also find another pair, such as VIRTUAL_KEY
and VIRTUAL_KEY_RELEASE.
Then, what about gesture?
Haptic for gesture is usually happening
like, do while fashion.
While I apply my gesture, I want to feel haptic.
We call this types of effect as haptic texture.
Haptic texture is recommended to be subtle, yet crisp,
because it has to be repeated very quickly.
So think about the clock app.
Along the clock UI, you want to apply a gesture to set time.
And then you want to deliver haptic texture
to represent the virtual tickmark under your finger.
Similarly, when you drag the blue text handler in the text
view left and right, when the selected area is changing
by letter or word, you want to deliver haptic texture
to represent the change.
To support these types of interactions,
we predefined constants called CLOCK_TICK
and TEXT_HANDLE_MOVE.
So now, assuming that you know which
constants to use-- in this case, VIRTUAL_KEY
and VIRTUAL_KEY_RELEASE-- then the next step
is to call those constants by performHapticFeedback,
and you're good to go.
TYLER FREEMAN: Now, there is another API,
the VibrationEffect API, and that's
through the VIBRATOR_SERVICE.
And this API you want to use when
you care more about the strength of the haptic happening
rather than more the semantic meaning or the gesture
involved with it.
The other advantage to this API is
you can use it without a view.
So HapticFeedbackConstants require a view,
but this you can call in a background service
in case you're trying to notify the user without a UI.
So the basic starting haptic we have is EFFECT_CLICK.
This is kind of a medium-strength haptic.
It's very crisp, a nice feeling on modern devices.
So we recommend you use that.
If you want a lighter haptic feel, you can use EFFECT_TICK.
And for a stronger, more intense haptic,
you can use EFFECT_HEAVY_CLICK.
There's also EFFECT_DOUBLE_CLICK,
which is just two clicks, one after another.
And using this is pretty easy.
You just call VibrationEffect.createPrebaked,
and then you pass it the constant
of the effect you want to use.
There is also a way that you can define your own custom
waveform.
And this is if you want to create your own custom
haptic, which is pretty cool.
So in this example, we have, basically,
a waveform that looks-- you see this image down here.
That's kind of what we're trying to make.
But basically, you pass this function, createWaveform,
an array of timings, an array of amplitudes.
So we're going to start out with a full-strength vibration,
which is an amplitude of 255, for 100 milliseconds.
And then we're going to pause for 200 milliseconds.
That's an amplitude of 0.
And then we're going to do a half-strength vibration
for 300 milliseconds.
So you can see that in these arrays,
and you get what's down in the corner there.
If you want to feel these for yourself on a new Pixel 4,
come find us upstairs in the afternoon.
We have a demo app.
You can try out all the constants
and make your own waveforms.
And if you have any questions or future
requests for future haptics, please come talk to us as well.
Thank you.
[APPLAUSE]
[MUSIC PLAYING]