Skip to content

Commit

Permalink
feat: enhance haptic feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Sep 21, 2021
1 parent f058710 commit 2c9bc2f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.osfans.trime.ime.keyboard

import android.content.Context
import android.inputmethodservice.InputMethodService
import android.media.AudioManager
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.speech.tts.TextToSpeech
import android.view.HapticFeedbackConstants
import android.view.KeyEvent
import android.view.LayoutInflater
import com.osfans.trime.databinding.InputRootBinding
import com.osfans.trime.ime.core.Preferences
import java.util.Locale
import kotlin.math.ln
Expand All @@ -18,21 +17,19 @@ import kotlin.math.ln
* Manage the key press effects, such as vibration, sound, speaking and so on.
*/
class InputFeedbackManager(
context: Context
private val ims: InputMethodService
) {
private val prefs: Preferences = Preferences.defaultInstance()
private var inputRootBinding: InputRootBinding? = null

private var vibrator: Vibrator? = null
private var audioManager: AudioManager? = null
private var tts: TextToSpeech? = null

init {
try {
inputRootBinding = InputRootBinding.inflate(LayoutInflater.from(context))
vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator
audioManager = context.getSystemService(Context.AUDIO_SERVICE) as? AudioManager
tts = TextToSpeech(context) { }
vibrator = ims.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator
audioManager = ims.getSystemService(Context.AUDIO_SERVICE) as? AudioManager
tts = TextToSpeech(ims) { }
} catch (e: Exception) {
e.printStackTrace()
}
Expand All @@ -43,11 +40,13 @@ class InputFeedbackManager(
*/
fun keyPressVibrate() {
if (prefs.keyboard.vibrationEnabled) {
var vibrationDuration = prefs.keyboard.vibrationDuration.toLong()
val vibrationDuration = prefs.keyboard.vibrationDuration.toLong()
var vibrationAmplitude = prefs.keyboard.vibrationAmplitude

val hapticsPerformed = if (vibrationDuration <0 && vibrationAmplitude < 0) {
inputRootBinding?.keyboard?.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP)
val hapticsPerformed = if (vibrationDuration < 0) {
ims.window?.window?.decorView?.performHapticFeedback(
HapticFeedbackConstants.KEYBOARD_TAP,
HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING)
} else {
false
}
Expand All @@ -56,16 +55,6 @@ class InputFeedbackManager(
return
}

if (vibrationDuration == -1L) {
vibrationDuration = 36
}

if (vibrationAmplitude == -1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrationAmplitude = VibrationEffect.DEFAULT_AMPLITUDE
} else if (vibrationAmplitude == -1) {
vibrationAmplitude = 36
}

if (vibrationAmplitude > 0) {
vibrationAmplitude = (vibrationAmplitude / 2.0).toInt().coerceAtLeast(1)
}
Expand Down Expand Up @@ -152,13 +141,10 @@ class InputFeedbackManager(
}

fun destroy() {
inputRootBinding = null
vibrator = null
audioManager = null
if (tts != null) {
tts?.let {
it.stop()
}.also { tts = null }
tts?.stop().also { tts = null }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* This file has been adapted from florisboard */
package com.osfans.trime.settings.components

import android.content.Context
Expand Down Expand Up @@ -28,6 +29,8 @@ import com.osfans.trime.databinding.PreferenceWidgetSeekbarBinding
*/
class DialogSeekBarPreference : Preference {
private var defaultValue: Int = 0
private var systemDefaultValue: Int = -1
private var systemDefaultValueText: String? = null
private var min: Int = 0
private var max: Int = 100
private var step: Int = 1
Expand All @@ -45,6 +48,8 @@ class DialogSeekBarPreference : Preference {
step = 1
}
defaultValue = getInt(R.styleable.DialogSeekBarPreferenceAttrs_android_defaultValue, defaultValue)
systemDefaultValue = getInt(R.styleable.DialogSeekBarPreferenceAttrs_systemDefaultValue, min - 1)
systemDefaultValueText = getString(R.styleable.DialogSeekBarPreferenceAttrs_systemDefaultValueText)
unit = getString(R.styleable.DialogSeekBarPreferenceAttrs_unit) ?: unit
recycle()
}
Expand All @@ -65,10 +70,16 @@ class DialogSeekBarPreference : Preference {

/**
* Generates the text for the given [value] and adds the defined [unit] at the end.
* If [systemDefaultValueText] is not null this method tries to match the given [value] with
* [systemDefaultValue] and returns [systemDefaultValueText] upon matching.
*/
private fun getTextForValue(value: Any): String {
return if (value !is Int) {
if (value !is Int) {
"??$unit"
}
val systemDefValText = systemDefaultValueText
return if (value == systemDefaultValue && systemDefValText != null) {
systemDefValText
} else {
value.toString() + unit
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,6 @@
<string name="about__support">支持</string>
<string name="about__used_libraries">我们使用的库</string>
<string name="about__used_library_dialog_title">同文输入法 | 开源软件库</string>
<string name="other__selection_sense_title">用Ctrl+Left/Right快速移动光标</string>
<string name="other__selection_sense_title">用 Ctrl+Left/Right 快速移动光标</string>
<string name="pref__system_default">系统默认</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,6 @@
<string name="about__support">支援</string>
<string name="about__used_libraries">我們使用的庫</string>
<string name="about__used_library_dialog_title">同文輸入法 | 開源軟體庫</string>
<string name="other__selection_sense_title">使用Ctrl+Left / Ctrl+Right快速移動遊標</string>
<string name="other__selection_sense_title">使用 Ctrl+Left / Ctrl+Right 快速移動遊標</string>
<string name="pref__system_default">系統預設</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<attr name="max" format="integer" />
<attr name="seekBarIncrement" format="integer" />
<attr name="android:defaultValue" format="integer" />
<attr name="systemDefaultValue" format="integer" />
<attr name="systemDefaultValueText" format="string" />
<attr name="unit" format="string" />
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@
<string name="about__used_libraries">Open Source Libraries We Use</string>
<string name="about__used_library_dialog_title">Trime | Open Source Libraries</string>
<string name="other__selection_sense_title">Uset Ctrl+Left/Right to move the cursor</string>
<string name="pref__system_default">System Default</string>
</resources>
22 changes: 14 additions & 8 deletions app/src/main/res/xml/keyboard_preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,45 @@
app:iconSpaceReserved="false"
android:title="@string/keyboard__key_sound_title"/>
<com.osfans.trime.settings.components.DialogSeekBarPreference
android:widgetLayout="@layout/preference_widget_seekbar"
app:allowDividerAbove="false"
android:key="keyboard__key_sound_volume"
app:iconSpaceReserved="false"
android:title="@string/keyboard__key_sound_volume_title"
app:min="0"
app:max="100"
app:seekBarIncrement="1"
android:defaultValue="100"
android:defaultValue="-1"
app:systemDefaultValue="-1"
app:systemDefaultValueText="@string/pref__system_default"
app:unit="%"
android:dependency="keyboard__key_sound"/>
<SwitchPreferenceCompat android:key="keyboard__key_vibration"
app:iconSpaceReserved="false"
android:title="@string/keyboard__key_vibration_title"/>
<com.osfans.trime.settings.components.DialogSeekBarPreference
android:widgetLayout="@layout/preference_widget_seekbar"
app:allowDividerAbove="false"
android:key="keyboard__key_vibration_duration"
app:iconSpaceReserved="false"
android:title="@string/keyboard__key_vibration_duration_title"
app:min="0"
app:max="100"
app:seekBarIncrement="1"
android:defaultValue="10"
android:defaultValue="-1"
app:systemDefaultValue="-1"
app:systemDefaultValueText="@string/pref__system_default"
app:unit="@string/unit__time_ms"
android:dependency="keyboard__key_vibration"/>
<com.osfans.trime.settings.components.DialogSeekBarPreference
android:widgetLayout="@layout/preference_widget_seekbar"
app:allowDividerAbove="false"
android:key="keyboard__key_vibration_amplitude"
app:iconSpaceReserved="false"
android:title="@string/keyboard__key_vibration_amplitude_title"
app:min="0"
app:max="255"
app:seekBarIncrement="1"
android:defaultValue="0"
android:defaultValue="-1"
app:systemDefaultValue="-1"
app:systemDefaultValueText="@string/pref__system_default"
app:unit=""
android:dependency="keyboard__key_vibration"/>
<!-- <SwitchPreference android:key="key_vibrate_default_amplitude" android:title="@string/key_vibrate_default_amplitude"/>-->
Expand All @@ -93,7 +99,7 @@
app:iconSpaceReserved="false"
android:title="@string/keyboard__speak_key_commit_title"/>
<com.osfans.trime.settings.components.DialogSeekBarPreference
android:widgetLayout="@layout/preference_widget_seekbar"
app:allowDividerAbove="false"
android:key="keyboard__key_long_press_timeout"
app:iconSpaceReserved="false"
android:title="@string/keyboard__long_press_timeout_title"
Expand All @@ -103,7 +109,7 @@
android:defaultValue="20"
app:unit="@string/unit__time_ms"/>
<com.osfans.trime.settings.components.DialogSeekBarPreference
android:widgetLayout="@layout/preference_widget_seekbar"
app:allowDividerAbove="false"
android:key="keyboard__key_repeat_interval"
app:iconSpaceReserved="false"
android:title="@string/keyboard__key_repeat_interval_title"
Expand Down

0 comments on commit 2c9bc2f

Please sign in to comment.