Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: vibration methods to use Duration #16803

Merged
merged 1 commit into from
Aug 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ import com.ichi2.anki.utils.formatAsString
import com.ichi2.annotations.NeedsTest
import com.ichi2.audio.AudioRecordingController.RecordingState.AppendToRecording
import com.ichi2.audio.AudioRecordingController.RecordingState.ImmediatePlayback
import com.ichi2.compat.CompatHelper
import com.ichi2.compat.Compat
import com.ichi2.compat.CompatHelper.Companion.compat
import com.ichi2.ui.FixedTextView
import com.ichi2.utils.Permissions.canRecordAudio
import com.ichi2.utils.UiUtil
import timber.log.Timber
import java.io.File
import java.io.IOException
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

// TODO : stop audio time view flickering
class AudioRecordingController :
Expand Down Expand Up @@ -428,7 +430,7 @@ class AudioRecordingController :
}

private fun discardAudio() {
CompatHelper.compat.vibrate(context, 20)
vibrate(20.milliseconds)
setUiState(state.clear())
tempAudioPath = generateTempAudioFile(context).also { tempAudioPath = it }
stopAudioPlayer()
Expand Down Expand Up @@ -460,7 +462,7 @@ class AudioRecordingController :

fun toggleSave(vibrate: Boolean = true) {
Timber.i("recording completed")
if (vibrate) CompatHelper.compat.vibrate(context, 20)
if (vibrate) vibrate(20.milliseconds)
stopAndSaveRecording()
// show this snackbar only in the edit field/multimedia activity
if (inEditField) (context as Activity).showSnackbar(context.resources.getString(R.string.audio_saved))
Expand Down Expand Up @@ -507,7 +509,7 @@ class AudioRecordingController :
}
}
}
CompatHelper.compat.vibrate(context, 20)
vibrate(20.milliseconds)
}

private fun resetAudioPlayer() {
Expand Down Expand Up @@ -619,7 +621,7 @@ class AudioRecordingController :
}

private fun clearRecording() {
CompatHelper.compat.vibrate(context, 20)
vibrate(20.milliseconds)
audioTimer.stop()
setUiState(state.clear())
audioRecorder.stopRecording()
Expand Down Expand Up @@ -687,6 +689,11 @@ class AudioRecordingController :
}
}

/**
* @see Compat.vibrate
*/
private fun vibrate(duration: Duration) = compat.vibrate(context, duration)

companion object {
var isRecording = false
var isAudioRecordingSaved = false
Expand Down
6 changes: 5 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/compat/Compat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import java.io.InputStream
import java.io.OutputStream
import java.io.Serializable
import java.util.Locale
import kotlin.time.Duration

/**
* This interface defines a set of functions that are not available on all platforms.
Expand Down Expand Up @@ -77,7 +78,7 @@ import java.util.Locale
interface Compat {
fun setupNotificationChannel(context: Context)
fun setTooltipTextByContentDescription(view: View)
fun vibrate(context: Context, durationMillis: Long)
fun vibrate(context: Context, duration: Duration)
fun getMediaRecorder(context: Context): MediaRecorder
fun resolveActivity(packageManager: PackageManager, intent: Intent, flags: ResolveInfoFlagsCompat): ResolveInfo?
fun resolveService(packageManager: PackageManager, intent: Intent, flags: ResolveInfoFlagsCompat): ResolveInfo?
Expand Down Expand Up @@ -228,3 +229,6 @@ interface Compat {
@Suppress("PropertyName")
val AXIS_GESTURE_SCROLL_Y_DISTANCE: Int
}

context (Context)
fun Compat.vibrate(duration: Duration) = vibrate(this@Context, duration)
5 changes: 3 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatV23.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import java.io.InputStream
import java.io.OutputStream
import java.io.Serializable
import java.util.Locale
import kotlin.time.Duration

/** Baseline implementation of [Compat]. Check [Compat]'s for more detail. */
@KotlinCleanup("add extension method logging file.delete() failure" + "Fix Deprecation")
Expand All @@ -57,9 +58,9 @@ open class CompatV23 : Compat {
}

// Until API 26 just specify time, after that specify effect also
override fun vibrate(context: Context, durationMillis: Long) {
override fun vibrate(context: Context, duration: Duration) {
val vibratorManager = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator?
vibratorManager?.vibrate(durationMillis)
vibratorManager?.vibrate(duration.inWholeMilliseconds)
}

// Until API 26 do the copy using streams
Expand Down
5 changes: 3 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatV26.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import kotlin.time.Duration

/** Implementation of [Compat] for SDK level 26 and higher. Check [Compat]'s for more detail. */
@TargetApi(26)
Expand All @@ -51,10 +52,10 @@ open class CompatV26 : CompatV24(), Compat {
}

@Suppress("DEPRECATION") // VIBRATOR_SERVICE => VIBRATOR_MANAGER_SERVICE handled in CompatV31
override fun vibrate(context: Context, durationMillis: Long) {
override fun vibrate(context: Context, duration: Duration) {
val vibratorManager = context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator
if (vibratorManager != null) {
val effect = VibrationEffect.createOneShot(durationMillis, VibrationEffect.DEFAULT_AMPLITUDE)
val effect = VibrationEffect.createOneShot(duration.inWholeMilliseconds, VibrationEffect.DEFAULT_AMPLITUDE)
vibratorManager.vibrate(effect)
}
}
Expand Down
5 changes: 3 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatV31.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import android.content.Context
import android.media.MediaRecorder
import android.os.VibrationEffect
import android.os.VibratorManager
import kotlin.time.Duration

/** Implementation of [Compat] for SDK level 31 */
@TargetApi(31)
open class CompatV31 : CompatV29(), Compat {
override fun vibrate(context: Context, durationMillis: Long) {
override fun vibrate(context: Context, duration: Duration) {
val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
val effect = VibrationEffect.createOneShot(durationMillis, VibrationEffect.DEFAULT_AMPLITUDE)
val effect = VibrationEffect.createOneShot(duration.inWholeMilliseconds, VibrationEffect.DEFAULT_AMPLITUDE)
val vibrator = vibratorManager.defaultVibrator
vibrator.vibrate(effect)
}
Expand Down