Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.VIBRATE" />

<!-- Devices running Android 12L (API level 32) or lower -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
Expand Down Expand Up @@ -76,4 +77,4 @@
</provider>
</application>

</manifest>
</manifest>
19 changes: 16 additions & 3 deletions app/src/main/java/me/rosuh/easywatermark/utils/VibrateHelper.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.rosuh.easywatermark.utils

import android.os.Build
import android.view.HapticFeedbackConstants
import android.view.View

Expand All @@ -9,18 +10,30 @@ class VibrateHelper private constructor() {
private var cd: Long = 20L

fun doVibrate(view: View) {
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
return
}
if (System.currentTimeMillis() - latestVibration <= cd) {
val now = System.currentTimeMillis()
if (now - latestVibration <= cd) {
return
}
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP)
latestVibration = now
performHapticFeedbackSafely {
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP)
}
}

companion object {
fun get(): VibrateHelper {
return VibrateHelper()
}

internal fun performHapticFeedbackSafely(performFeedback: () -> Boolean): Boolean {
return try {
performFeedback()
} catch (_: SecurityException) {
false
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.rosuh.easywatermark.utils

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class VibrateHelperTest {
@Test
fun performHapticFeedbackSafelyReturnsFalseWhenPermissionIsDenied() {
val result = VibrateHelper.performHapticFeedbackSafely {
throw SecurityException("missing android.permission.VIBRATE")
}

assertFalse(result)
}

@Test
fun performHapticFeedbackSafelyReturnsDelegateResult() {
assertTrue(VibrateHelper.performHapticFeedbackSafely { true })
assertFalse(VibrateHelper.performHapticFeedbackSafely { false })
}
}
Loading