I created this kind of 'VibratorHelper' class to h...
# codereview
t
I created this kind of 'VibratorHelper' class to help with vibration, opinions?
Copy code
class VibratorHelper {
    companion object {
        private val vibrationNotSupportedMessage: String = "Your device (${Build.MANUFACTURER + Build.MODEL} API ${Build.VERSION.SDK_INT}) does not support vibration."

        fun vibrateFor(milliseconds: Long, context: Context) {
            val service = context.getSystemService(VIBRATOR_SERVICE) as Vibrator

            @Suppress("DEPRECATION")
            if (!service.hasVibrator()) Toast.makeText(context, vibrationNotSupportedMessage, Toast.LENGTH_SHORT) else service.vibrate(milliseconds)
        }

        fun vibrateIndefinitely(context: Context) = vibrateFor(Long.MAX_VALUE, context)
    }
}
e
there's a reason it's deprecated. the documentation points to the replacement.
t
@ephemient ok... thanks for this
@ephemient btw why did they deprecate this? Like what is the reason?
@ephemient also would you recommend this to be wrapped in a companion object, or just an interface with a class with no companion object?
e
there is no good reason to create a class that is empty except for a companion object. just use a top-level object, or in this case, top-level functions
t
@ephemient what is a top-level object?
e
Copy code
object VibrationHelper {
    ....
}
but there isn't even a good reason to do that here
t
@ephemient so just function?
e
Copy code
import androidx.core.content.getSystemService

fun Context.vibrate(millis: Long, amplitude: Int = VibrationEffect.DEFAULT_AMPLITUDE) {
    val vibrator = getSystemService<Vibrator>()
    if (vibrator?.hasVibrator() != true) {
        Toast.makeText(this, R.string.vibration_not_supported, Toast.LENGTH_SHORT).show()
        return
    }
    vibrator.vibrate(VibrationEffect.createOneShot(millis, amplitude))
}
define your string in a translatable strings.xml
why do you tag username on every reply? it floods the mentions tab in slack. I'm getting notified on thread replies even without it.
😁 1
t
ok, sorry for this. Why do I do it? I don't know... Why is your profile pic a robot?
e
once in a while to call attention or clarify when there are multiple parties chatting is fine. every message is too much.