Add abstraction for Android's vibrator service
This commit is contained in:
parent
e252532000
commit
f91f3e268b
4 changed files with 39 additions and 15 deletions
|
@ -3,6 +3,7 @@ package com.fsck.k9.ui.settings
|
||||||
import com.fsck.k9.helper.NamedThreadFactory
|
import com.fsck.k9.helper.NamedThreadFactory
|
||||||
import com.fsck.k9.ui.settings.account.AccountSettingsDataStoreFactory
|
import com.fsck.k9.ui.settings.account.AccountSettingsDataStoreFactory
|
||||||
import com.fsck.k9.ui.settings.account.AccountSettingsViewModel
|
import com.fsck.k9.ui.settings.account.AccountSettingsViewModel
|
||||||
|
import com.fsck.k9.ui.settings.account.getSystemVibrator
|
||||||
import com.fsck.k9.ui.settings.export.SettingsExportViewModel
|
import com.fsck.k9.ui.settings.export.SettingsExportViewModel
|
||||||
import com.fsck.k9.ui.settings.general.GeneralSettingsDataStore
|
import com.fsck.k9.ui.settings.general.GeneralSettingsDataStore
|
||||||
import com.fsck.k9.ui.settings.general.GeneralSettingsViewModel
|
import com.fsck.k9.ui.settings.general.GeneralSettingsViewModel
|
||||||
|
@ -33,6 +34,7 @@ val settingsUiModule = module {
|
||||||
notificationController = get()
|
notificationController = get()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
factory { getSystemVibrator(context = get()) }
|
||||||
|
|
||||||
viewModel { SettingsExportViewModel(context = get(), preferences = get(), settingsExporter = get()) }
|
viewModel { SettingsExportViewModel(context = get(), preferences = get(), settingsExporter = get()) }
|
||||||
viewModel { SettingsImportViewModel(get(), get()) }
|
viewModel { SettingsImportViewModel(get(), get()) }
|
||||||
|
|
|
@ -4,12 +4,10 @@ import android.annotation.SuppressLint
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Vibrator
|
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuInflater
|
import android.view.MenuInflater
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.core.content.getSystemService
|
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import androidx.preference.ListPreference
|
import androidx.preference.ListPreference
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
|
@ -53,8 +51,8 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), ConfirmationDialogFr
|
||||||
private val accountRemover: BackgroundAccountRemover by inject()
|
private val accountRemover: BackgroundAccountRemover by inject()
|
||||||
private val notificationChannelManager: NotificationChannelManager by inject()
|
private val notificationChannelManager: NotificationChannelManager by inject()
|
||||||
private val notificationSettingsUpdater: NotificationSettingsUpdater by inject()
|
private val notificationSettingsUpdater: NotificationSettingsUpdater by inject()
|
||||||
|
private val vibrator: Vibrator by inject()
|
||||||
|
|
||||||
private val vibrator by lazy { requireContext().getSystemService<Vibrator>() }
|
|
||||||
private lateinit var dataStore: AccountSettingsDataStore
|
private lateinit var dataStore: AccountSettingsDataStore
|
||||||
|
|
||||||
private var notificationSoundPreference: NotificationSoundPreference? = null
|
private var notificationSoundPreference: NotificationSoundPreference? = null
|
||||||
|
@ -203,7 +201,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), ConfirmationDialogFr
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initializeNotifications(account: Account) {
|
private fun initializeNotifications(account: Account) {
|
||||||
if (vibrator?.hasVibrator() != true) {
|
if (!vibrator.hasVibrator) {
|
||||||
findPreference<Preference>(PREFERENCE_NOTIFICATION_VIBRATION)?.remove()
|
findPreference<Preference>(PREFERENCE_NOTIFICATION_VIBRATION)?.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,6 @@ package com.fsck.k9.ui.settings.account
|
||||||
import android.app.Dialog
|
import android.app.Dialog
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.VibrationEffect
|
|
||||||
import android.os.Vibrator
|
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.BaseAdapter
|
import android.widget.BaseAdapter
|
||||||
|
@ -14,16 +12,16 @@ import android.widget.SeekBar.OnSeekBarChangeListener
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.widget.SwitchCompat
|
import androidx.appcompat.widget.SwitchCompat
|
||||||
import androidx.core.content.getSystemService
|
|
||||||
import androidx.preference.PreferenceDialogFragmentCompat
|
import androidx.preference.PreferenceDialogFragmentCompat
|
||||||
import com.fsck.k9.NotificationVibration
|
import com.fsck.k9.NotificationVibration
|
||||||
import com.fsck.k9.VibratePattern
|
import com.fsck.k9.VibratePattern
|
||||||
import com.fsck.k9.ui.R
|
import com.fsck.k9.ui.R
|
||||||
import com.fsck.k9.ui.getEnum
|
import com.fsck.k9.ui.getEnum
|
||||||
import com.fsck.k9.ui.putEnum
|
import com.fsck.k9.ui.putEnum
|
||||||
|
import org.koin.android.ext.android.inject
|
||||||
|
|
||||||
class VibrationDialogFragment : PreferenceDialogFragmentCompat() {
|
class VibrationDialogFragment : PreferenceDialogFragmentCompat() {
|
||||||
private val vibrator by lazy { requireContext().getSystemService<Vibrator>() ?: error("Vibrator service missing") }
|
private val vibrator: Vibrator by inject()
|
||||||
|
|
||||||
private val vibrationPreference: VibrationPreference
|
private val vibrationPreference: VibrationPreference
|
||||||
get() = preference as VibrationPreference
|
get() = preference as VibrationPreference
|
||||||
|
@ -83,13 +81,7 @@ class VibrationDialogFragment : PreferenceDialogFragmentCompat() {
|
||||||
val vibrationTimes = adapter.vibrationTimes
|
val vibrationTimes = adapter.vibrationTimes
|
||||||
val vibrationPattern = NotificationVibration.getSystemPattern(vibratePattern, vibrationTimes)
|
val vibrationPattern = NotificationVibration.getSystemPattern(vibratePattern, vibrationTimes)
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
vibrator.vibrate(vibrationPattern)
|
||||||
val vibrationEffect = VibrationEffect.createWaveform(vibrationPattern, -1)
|
|
||||||
vibrator.vibrate(vibrationEffect)
|
|
||||||
} else {
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
vibrator.vibrate(vibrationPattern, -1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class VibrationPatternAdapter(
|
private inner class VibrationPatternAdapter(
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.fsck.k9.ui.settings.account
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.VibrationEffect
|
||||||
|
import androidx.core.content.getSystemService
|
||||||
|
import android.os.Vibrator as VibratorService
|
||||||
|
|
||||||
|
interface Vibrator {
|
||||||
|
val hasVibrator: Boolean
|
||||||
|
fun vibrate(vibrationPattern: LongArray)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class AndroidVibrator(private val vibrator: VibratorService) : Vibrator {
|
||||||
|
override val hasVibrator: Boolean
|
||||||
|
get() = vibrator.hasVibrator()
|
||||||
|
|
||||||
|
override fun vibrate(vibrationPattern: LongArray) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
val vibrationEffect = VibrationEffect.createWaveform(vibrationPattern, -1)
|
||||||
|
vibrator.vibrate(vibrationEffect)
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
vibrator.vibrate(vibrationPattern, -1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun getSystemVibrator(context: Context): Vibrator {
|
||||||
|
val vibratorService = context.getSystemService<VibratorService>() ?: error("Vibrator service missing")
|
||||||
|
return AndroidVibrator(vibratorService)
|
||||||
|
}
|
Loading…
Reference in a new issue