Skip to content

Commit

Permalink
refactor: improve VibrationUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
Irineu333 committed Oct 1, 2023
1 parent f572843 commit cf9be18
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

package com.neo.speaktouch.intercepter

import android.os.Vibrator
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import com.neo.speaktouch.intercepter.interfece.Interceptor
import com.neo.speaktouch.utils.`object`.VibratorUtil
import com.neo.speaktouch.utils.VibrationUtil

class HapticInterceptor(val vibrator: Vibrator) : Interceptor {
class HapticInterceptor(
private val vibration: VibrationUtil
) : Interceptor {

override fun handle(event: AccessibilityEvent) {

if (!vibrator.hasVibrator()) return
if (!vibration.hasVibrator()) return

if (event.eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
handleFocusVibration(event.source ?: return)
Expand All @@ -38,15 +39,14 @@ class HapticInterceptor(val vibrator: Vibrator) : Interceptor {
private fun handleFocusVibration(nodeInfo: AccessibilityNodeInfo) {

if (nodeInfo.isClickable) {
VibratorUtil.vibrateEffectHeavyClick(
vibrator,
)
vibration.vibrateEffectHeavyClick()
return
}

VibratorUtil.vibrateEffectTick(
vibrator,
)
vibration.vibrateEffectTick()

return
}

override fun finish() = vibration.finish()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.neo.speaktouch.intercepter.HapticInterceptor
import com.neo.speaktouch.intercepter.SpeechInterceptor
import com.neo.speaktouch.intercepter.interfece.Interceptor
import com.neo.speaktouch.utils.extension.getLog
import com.neo.speaktouch.utils.`object`.VibratorUtil
import com.neo.speaktouch.utils.VibrationUtil
import timber.log.Timber

class SpeakTouchService : AccessibilityService() {
Expand All @@ -41,7 +41,7 @@ class SpeakTouchService : AccessibilityService() {

interceptors.add(
HapticInterceptor(
vibrator = VibratorUtil.createVibrator(this),
vibration = VibrationUtil(this),
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Legacy effects equivalent to android.os.VibrationEffect.
*
* Copyright (C) 2023 Irineu A. Silva.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.neo.speaktouch.utils

sealed class LegacyVibrationEffect(val pattern: LongArray) {
object HeavyClick : LegacyVibrationEffect(
pattern = longArrayOf(
0, 50, 50
)
)

object Tick : LegacyVibrationEffect(
pattern = longArrayOf(
0, 30
)
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Vibration compatibility.
* Vibration utility wrapper.
*
* Copyright (C) 2023 Irineu A. Silva.
*
Expand All @@ -16,69 +16,65 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.neo.speaktouch.utils.`object`
package com.neo.speaktouch.utils

import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import com.neo.speaktouch.R
import com.neo.speaktouch.utils.extension.toLongArray

object VibratorUtil {
class VibrationUtil(private val vibrator: Vibrator) {

fun vibrateEffectHeavyClick(vibrator: Vibrator) {
@Suppress("deprecation")
constructor(context: Context) : this(
context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
)

fun vibrateEffectHeavyClick() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
vibrator.vibrate(
VibrationEffect.createPredefined(
VibrationEffect.EFFECT_HEAVY_CLICK
)
)
} else {
@Suppress("deprecation")
vibrator.vibrate(
LegacyVibrationEffect.HeavyClick.pattern,
REPEAT
vibrateLegacy(
LegacyVibrationEffect.HeavyClick
)
}
}

fun vibrateEffectTick(vibrator: Vibrator) {
fun vibrateEffectTick() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
vibrator.vibrate(
VibrationEffect.createPredefined(
VibrationEffect.EFFECT_TICK
)
)
} else {
@Suppress("deprecation")
vibrator.vibrate(
LegacyVibrationEffect.Tick.pattern,
REPEAT
vibrateLegacy(
LegacyVibrationEffect.Tick,
)
}
}

@Suppress("deprecation")
fun createVibrator(context: Context): Vibrator {

return context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
fun vibrateLegacy(
legacyVibrationEffect: LegacyVibrationEffect,
repeat: Int = DEFAULT_REPEAT,
) {
vibrator.vibrate(
legacyVibrationEffect.pattern,
repeat
)
}

private const val REPEAT = -1
}
fun hasVibrator() = vibrator.hasVibrator()

fun finish() = vibrator.cancel()

companion object {
private const val DEFAULT_REPEAT = -1
}
}

sealed class LegacyVibrationEffect(val pattern : LongArray) {
object HeavyClick : LegacyVibrationEffect(
pattern = longArrayOf(
0, 50, 50
)
)
object Tick : LegacyVibrationEffect(
pattern = longArrayOf(
0, 30
)
)
}

0 comments on commit cf9be18

Please sign in to comment.