Skip to content

Commit

Permalink
Preferences - Vibration fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
shauleiz committed Dec 15, 2024
1 parent e5bb26d commit d8e427f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static VibrationEffect getVibrationEffect(String effect, boolean hasAmpli
switch (effect) {
case "ssb": // Single Short Beat
timings = new long[]{50, 50, 50, 50, 50, 100};
timingsNoAmp = new long[] {0, 350};
timingsNoAmp = new long[] {300, 350, 400};//{0, 350, 400};
amplitudes = new int[]{33, 51, 75, 113, 170, 255};
repeatIndex = -1; // Do not repeat
repeatIndexNoAmp = -1; // Do not repeat
Expand Down
42 changes: 41 additions & 1 deletion app/src/main/java/com/product/thetimemachine/ui/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,46 @@ object SoundObj {
AlarmService.sound(TheTimeMachineApp.appContext, null)}
}

object VibrateObj {

private lateinit var timer : CountDownTimer
const val TICK : Long = 100
private var currentPattern : String?= null


fun playVibrate(pattern: String?, duration : Long){

currentPattern = pattern

if (pattern.isNullOrEmpty() || duration <= 0) {
killVibrate()
return
}

timer = object: CountDownTimer(duration, TICK) {
override fun onTick(p0: Long) {
if (!currentPattern.equals(pattern)) cancel()
}

override fun onFinish() {
killVibrate()
}

}

killVibrate()
timer.start()
Log.d("THE_TIME_MACHINE", "playVibrate(): currentPattern = $currentPattern ; pattern = $pattern ; duration = $duration")
AlarmService.VibrateEffect(TheTimeMachineApp.appContext, pattern)
}

fun killVibrate(){
val vibrator =
TheTimeMachineApp.appContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (vibrator.hasAmplitudeControl()) Log.d("THE_TIME_MACHINE", "vibrate(): hasAmplitudeControl")
else Log.d("THE_TIME_MACHINE", "vibrate(): No AmplitudeControl")
vibrator.cancel()}
}

@Composable
fun getListOfItemPreferences(setUpAlarmValues: AlarmViewModel.SetUpAlarmValues) : List<PrefData>{
Expand Down Expand Up @@ -382,7 +422,7 @@ fun ShowPreferences(listOfPrefs: List<PrefData>, onOK : (index: Int, value : Str
// Call from Row/Radio button OnClick
fun playVibOrSound(index : Int, pattern : String?) {
Log.d("THE_TIME_MACHINE", "playVibOrSound(): index = $index ; pattern = $pattern")
if (listOfPrefs[index].title == R.string.vibration_pattern) vibrate(pattern)
if (listOfPrefs[index].title == R.string.vibration_pattern) VibrateObj.playVibrate(pattern, 5000)
else if (listOfPrefs[index].title == R.string.alarm_sounds) SoundObj.playSound(pattern, 4000)
}

Expand Down

0 comments on commit d8e427f

Please sign in to comment.