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

Vibrate #4997

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Vibrate #4997

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
37 changes: 37 additions & 0 deletions app/src/main/java/one/mixin/android/extension/ContextExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,43 @@ fun Context.vibrate(
}
}


fun Context.startVibration(
effect: VibrationEffect? = null,
pattern: LongArray = longArrayOf(0, 1000), // 持续振动的模式
) {
val vibrator =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
} else {
@Suppress("DEPRECATION")
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}

if (effect != null && Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(effect)
} else if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createWaveform(pattern, 0)) // 0 表示从头开始循环
} else {
@Suppress("DEPRECATION")
vibrator.vibrate(pattern, -1) // -1 表示无限循环
}
}

fun Context.stopVibration() {
val vibrator =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
} else {
@Suppress("DEPRECATION")
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}

vibrator.cancel() // 停止当前的振动
}

fun Context.tickVibrate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
vibrate(VibrationEffect.createPredefined(EFFECT_TICK))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ object NotificationGenerator : Injector() {
} else {
NotificationCompat.PRIORITY_HIGH
}

notificationBuilder.setVibrate(longArrayOf(0, 500, 1000))
var person: Person? = null
supportsR({
val name = conversation.getConversationName()
Expand Down
20 changes: 14 additions & 6 deletions app/src/main/java/one/mixin/android/webrtc/CallAudioManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import one.mixin.android.extension.safeActivate
import one.mixin.android.extension.safeStop
import one.mixin.android.extension.selectEarpiece
import one.mixin.android.extension.selectSpeakerphone
import one.mixin.android.extension.startVibration
import one.mixin.android.extension.stopVibration
import one.mixin.android.util.AudioPlayer
import one.mixin.android.util.MusicPlayer
import timber.log.Timber
Expand Down Expand Up @@ -123,6 +125,7 @@ class CallAudioManager(
mediaPlayer?.release()
mediaPlayer = null
}
context.stopVibration()
vibrator?.cancel()
audioSwitch.safeActivate()
}
Expand Down Expand Up @@ -179,22 +182,27 @@ class CallAudioManager(
}
mediaPlayer?.isLooping = true

val sound =
if (isInitiator) {
R.raw.call_outgoing
} else {
R.raw.call_incoming
}
val sound = if (isInitiator) {
R.raw.call_outgoing
} else {
R.raw.call_incoming
}

val uri = Uri.parse("android.resource://${context.packageName}/$sound")
try {
mediaPlayer?.setDataSource(context, uri)
mediaPlayer?.prepare()
mediaPlayer?.start()

if (!isInitiator) {
context.startVibration()
}
} catch (e: Exception) {
Timber.w("$TAG_AUDIO mediaPlayer start, $e")
}
}


interface Callback {
fun customAudioDeviceAvailable(available: Boolean)
}
Expand Down
Loading