Skip to content

Commit

Permalink
we loove haptic feedback! (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartixxx32 authored Jan 8, 2025
1 parent 19c9899 commit ff5a178
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId = "com.bartixxx.opflashcontrol"
minSdk = 31
targetSdk = 35
versionCode = 14
versionName = "1.1.1"
versionCode = 15
versionName = "1.1.2"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">
<!-- Required permissions -->
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:allowBackup="true"
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/bartixxx/opflashcontrol/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,23 @@ abstract class BaseActivity : AppCompatActivity() {
}

protected fun setupSlider(
slider: Slider, // Use Slider from Material3
slider: Slider,
textView: TextView,
label: String,
onStopTracking: (Int) -> Unit
) {
var lastIntegerValue = slider.value.toInt() // Track the last integer value

slider.addOnChangeListener { _, value, _ ->
val progress = value.toInt() // Convert from Float to Int
Log.d("SliderProgress", "$label Progress: $progress") // Debugging log

// Vibrate only if the integer part of the slider value has changed
if (progress != lastIntegerValue) {
VibrationUtil.vibrate50(this)
lastIntegerValue = progress
}

Log.d("SliderProgress", "$label Progress: $progress") // Debugging log
textView.text = "$label: $progress"
}

Expand Down
20 changes: 16 additions & 4 deletions app/src/main/java/com/bartixxx/opflashcontrol/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ class MainActivity : BaseActivity() {
}
}

on.setOnClickListener { toggleLEDs(true) }
off.setOnClickListener { toggleLEDs(false) }
destroyer.setOnClickListener { executeExtraFunction() }
navigateToMainActivity2.setOnClickListener { navigateToMainActivity2() }
on.setOnClickListener {
VibrationUtil.vibrate100(this@MainActivity)
toggleLEDs(true)
}
off.setOnClickListener {
VibrationUtil.vibrate100(this@MainActivity)
toggleLEDs(false)
}
destroyer.setOnClickListener {
VibrationUtil.vibrate100(this@MainActivity)
executeExtraFunction()
}
navigateToMainActivity2.setOnClickListener {
VibrationUtil.vibrate100(this@MainActivity)
navigateToMainActivity2()
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions app/src/main/java/com/bartixxx/opflashcontrol/MainActivity2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,22 @@ class MainActivity2 : BaseActivity() {
if (isLedOn) controlLeds("on", WHITE_LED_PATH, YELLOW_LED_PATH, WHITE2_LED_PATH, YELLOW2_LED_PATH, whiteBrightness, yellowBrightness, white2Brightness, progress)
}

on.setOnClickListener { toggleLEDs(true) }
off.setOnClickListener { toggleLEDs(false) }
destroyer.setOnClickListener { executeExtraFunction() }
navigateBackToMainActivity.setOnClickListener { navigateBackToMainActivity() }
on.setOnClickListener {
VibrationUtil.vibrate100(this@MainActivity2)
toggleLEDs(true)
}
off.setOnClickListener {
VibrationUtil.vibrate100(this@MainActivity2)
toggleLEDs(false)
}
destroyer.setOnClickListener {
VibrationUtil.vibrate100(this@MainActivity2)
executeExtraFunction()
}
navigateBackToMainActivity.setOnClickListener {
VibrationUtil.vibrate100(this@MainActivity2)
navigateBackToMainActivity()
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/bartixxx/opflashcontrol/VibrationUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bartixxx.opflashcontrol

import android.content.Context
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator

object VibrationUtil {
fun vibrate50(context: Context, duration: Long = 50L) {
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
// Deprecated in API 26
vibrator.vibrate(duration)
}
}
fun vibrate100(context: Context, duration: Long = 100L) {
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
// Deprecated in API 26
vibrator.vibrate(duration)
}
}
}

0 comments on commit ff5a178

Please sign in to comment.