Skip to content

Instantly share code, notes, and snippets.

@alextcn
Created May 24, 2018 08:52
Show Gist options
  • Save alextcn/277eb26f6a06311e95fb0269ba12d186 to your computer and use it in GitHub Desktop.
Save alextcn/277eb26f6a06311e95fb0269ba12d186 to your computer and use it in GitHub Desktop.
package io.jackqack.hodler.utils
import android.content.Context
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
object Vibrator {
private const val VIBRATE_LENGTH_LONG_TOUCH = 50L
private const val VIBRATE_AMPLITUDE_LONG_TOUCH = 75
fun longClick(context: Context) {
val vibe = context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator?
if (vibe == null || !vibe.hasVibrator()) return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val amplitude = if (vibe.hasAmplitudeControl()) VIBRATE_AMPLITUDE_LONG_TOUCH else VibrationEffect.DEFAULT_AMPLITUDE
vibe.vibrate(VibrationEffect.createOneShot(VIBRATE_LENGTH_LONG_TOUCH, amplitude))
} else {
vibe.vibrate(VIBRATE_LENGTH_LONG_TOUCH)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment