Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vibration issues by using VibratorManager #192

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.michelinside.glucodatahandler.common.notification

import android.content.Context
import android.os.Build
import android.os.CombinedVibration
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
Expand All @@ -28,7 +29,7 @@ object Vibrator {
fun vibrate(pattern: LongArray, repeat: Int = -1, amplitude: Int = -1): Int {
cancel()
val duration = if(repeat == -1) pattern.sum().toInt() else -1
if (vibrator.hasAmplitudeControl() && amplitude > 0) {
val effect = if (vibrator.hasAmplitudeControl() && amplitude > 0) {
Log.d(LOG_ID, "Vibrate for $duration ms with amplitude $amplitude")
val amplitudePatterns = IntArray(pattern.size)
amplitudePatterns[0] = 0
Expand All @@ -38,11 +39,24 @@ object Vibrator {
else
amplitudePatterns[i] = 0
}
vibrator.vibrate(VibrationEffect.createWaveform(pattern, amplitudePatterns, repeat))
VibrationEffect.createWaveform(pattern, amplitudePatterns, repeat)
} else {
Log.d(LOG_ID, "Vibrate for $duration ms")
vibrator.vibrate(VibrationEffect.createWaveform(pattern, repeat))
VibrationEffect.createWaveform(pattern, repeat)
}

try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Use new API for Android 12+
val vibratorManager = GlucoDataService.context!!.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vibrator instance is already the default vibrator of VibratorManager!? Also cancel will not work!?
So I think, we will need something more....
I will test.

vibratorManager.vibrate(CombinedVibration.createParallel(effect))
} else {
vibrator.vibrate(effect)
}
} catch (e: Exception) {
Log.e(LOG_ID, "Vibration failed", e)
}

return duration
}

Expand Down