Skip to content

Commit 2e5994a

Browse files
Merge pull request #1607 from session-foundation/ses-4676-call-vibrate
SES-4676 call vibrate
2 parents 8ef6792 + 480523d commit 2e5994a

File tree

8 files changed

+94
-39
lines changed

8 files changed

+94
-39
lines changed

app/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ object OnionRequestAPI {
6060
.map { it.isNotEmpty() }
6161
.stateIn(GlobalScope, SharingStarted.Eagerly, paths.value.isNotEmpty())
6262

63-
private val NON_PENALIZING_STATUSES = setOf(400, 403, 404, 406, 425)
63+
private val NON_PENALIZING_STATUSES = setOf(403, 404, 406, 425)
6464

6565
init {
6666
// Listen for the changes in paths and persist it to the db
@@ -418,10 +418,10 @@ object OnionRequestAPI {
418418
Log.d("Loki","Request returned a non penalizing code ${exception.statusCode} with message: $message")
419419
}
420420
// we do not want to penalize the path/nodes when:
421-
// - the exit node reached the server but the destination returned 5xx
422-
// - the exit node couldn't reach its destination with a 5xx, but the destination was a community (which we can know from the server's name being in the error message)
421+
// - the exit node reached the server but the destination returned 5xx or 400
422+
// - the exit node couldn't reach its destination with a 5xx or 400, but the destination was a community (which we can know from the server's name being in the error message)
423423
else if (destination is Destination.Server &&
424-
(exception.statusCode in 500..504) &&
424+
(exception.statusCode in 500..504 || exception.statusCode == 400) &&
425425
(exception is HTTPRequestFailedAtDestinationException || exception.body?.contains(destination.host) == true)) {
426426
Log.d("Loki","Destination server error - Non path penalizing. Request returned code ${exception.statusCode} with message: $message")
427427
} else if (message == "Loki Server error") {

app/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -930,12 +930,6 @@ interface TextSecurePreferences {
930930
setBooleanPreference(context, FINGERPRINT_KEY_GENERATED, true)
931931
}
932932

933-
@JvmStatic
934-
fun clearAll(context: Context) {
935-
getDefaultSharedPreferences(context).edit().clear().commit()
936-
}
937-
938-
939933
// ----- Get / set methods for if we have already warned the user that saving attachments will allow other apps to access them -----
940934
// Note: We only ever show the warning dialog about this ONCE - when the user accepts this fact we write true to the flag & never show again.
941935
@JvmStatic
@@ -1656,8 +1650,16 @@ class AppTextSecurePreferences @Inject constructor(
16561650
return getBooleanPreference(AUTOPLAY_AUDIO_MESSAGES, false)
16571651
}
16581652

1653+
/**
1654+
* Clear all prefs and reset our observables
1655+
*/
16591656
override fun clearAll() {
1660-
getDefaultSharedPreferences(context).edit().clear().commit()
1657+
pushEnabled.update { false }
1658+
localNumberState.update { null }
1659+
postProLaunchState.update { false }
1660+
hiddenPasswordState.update { false }
1661+
1662+
getDefaultSharedPreferences(context).edit(commit = true) { clear() }
16611663
}
16621664

16631665
override fun getHidePassword() = getBooleanPreference(HIDE_PASSWORD, false)

app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ import org.thoughtcrime.securesms.dependencies.ConfigFactory
173173
import org.thoughtcrime.securesms.giph.ui.GiphyActivity
174174
import org.thoughtcrime.securesms.groups.GroupMembersActivity
175175
import org.thoughtcrime.securesms.groups.OpenGroupManager
176+
import org.thoughtcrime.securesms.home.HomeActivity
176177
import org.thoughtcrime.securesms.home.search.searchName
177178
import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository
178179
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil
@@ -283,7 +284,7 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
283284

284285
private val address: Address.Conversable by lazy {
285286
requireNotNull(IntentCompat.getParcelableExtra(intent, ADDRESS, Address.Conversable::class.java)) {
286-
"Address must be provided in the intent extras"
287+
"Address must be provided in the intent extras to open a conversation"
287288
}
288289
}
289290

@@ -521,6 +522,18 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
521522

522523
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
523524
super.onCreate(savedInstanceState, isReady)
525+
526+
// Check if address is null before proceeding with initialization
527+
if (IntentCompat.getParcelableExtra(intent, ADDRESS, Address.Conversable::class.java) == null) {
528+
Log.w(TAG, "ConversationActivityV2 launched without ADDRESS extra - Returning home")
529+
val intent = Intent(this, HomeActivity::class.java).apply {
530+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
531+
}
532+
startActivity(intent)
533+
finish()
534+
return
535+
}
536+
524537
binding = ActivityConversationV2Binding.inflate(layoutInflater)
525538
setContentView(binding.root)
526539

app/src/main/java/org/thoughtcrime/securesms/util/ClearDataUtils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class ClearDataUtils @Inject constructor(
5050
application.deleteDatabase(DatabaseMigrationManager.CIPHER4_DB_NAME)
5151
application.deleteDatabase(DatabaseMigrationManager.CIPHER3_DB_NAME)
5252

53-
TextSecurePreferences.clearAll(application)
53+
// clear all prefs
54+
prefs.clearAll()
55+
5456
application.getSharedPreferences(ApplicationContext.PREFERENCES_NAME, 0).edit(commit = true) { clear() }
5557
application.cacheDir.deleteRecursively()
5658
application.filesDir.deleteRecursively()

app/src/main/java/org/thoughtcrime/securesms/webrtc/AudioManagerCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ open class AudioManagerCommand: Parcelable {
2626
data class Stop(val playDisconnect: Boolean): AudioManagerCommand()
2727

2828
@Parcelize
29-
data class StartIncomingRinger(val vibrate: Boolean): AudioManagerCommand()
29+
data object StartIncomingRinger: AudioManagerCommand()
3030

3131
@Parcelize
3232
data class SetUserDevice(val device: SignalAudioManager.AudioDevice): AudioManagerCommand()

app/src/main/java/org/thoughtcrime/securesms/webrtc/CallManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ class CallManager @Inject constructor(
823823
}
824824

825825
fun startIncomingRinger() {
826-
signalAudioManager.handleCommand(AudioManagerCommand.StartIncomingRinger(true))
826+
signalAudioManager.handleCommand(AudioManagerCommand.StartIncomingRinger)
827827
}
828828

829829
fun startCommunication() {

app/src/main/java/org/thoughtcrime/securesms/webrtc/audio/IncomingRinger.kt

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package org.thoughtcrime.securesms.webrtc.audio
22

3+
import android.app.NotificationManager
34
import android.content.Context
45
import android.media.AudioAttributes
56
import android.media.AudioManager
67
import android.media.MediaPlayer
78
import android.media.RingtoneManager
9+
import android.os.Build
10+
import android.os.VibrationAttributes
11+
import android.os.VibrationEffect
812
import android.os.Vibrator
13+
import android.os.VibratorManager
914
import org.session.libsession.utilities.ServiceUtil
1015
import org.session.libsignal.utilities.Log
1116

@@ -15,56 +20,89 @@ class IncomingRinger(private val context: Context) {
1520
val PATTERN = longArrayOf(0L, 1000L, 1000L)
1621
}
1722

18-
private val vibrator: Vibrator? = ServiceUtil.getVibrator(context)
1923
var mediaPlayer: MediaPlayer? = null
2024

2125
val isRinging: Boolean
2226
get() = mediaPlayer?.isPlaying ?: false
2327

24-
fun start(vibrate: Boolean) {
28+
fun start() {
2529
val audioManager = ServiceUtil.getAudioManager(context)
2630
mediaPlayer?.release()
2731
mediaPlayer = createMediaPlayer()
28-
val ringerMode = audioManager.ringerMode
2932

30-
if (shouldVibrate(mediaPlayer, ringerMode, vibrate)) {
31-
Log.i(TAG,"Starting vibration")
32-
vibrator?.vibrate(PATTERN, 1)
33-
} else {
34-
Log.i(TAG,"Skipping vibration")
35-
}
33+
// Vibrate if policy/system allows
34+
if (shouldVibrate(audioManager)) vibrate()
3635

36+
// Play ringtone only in NORMAL
3737
mediaPlayer?.let { player ->
38-
if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {
38+
if (audioManager.ringerMode == AudioManager.RINGER_MODE_NORMAL) {
3939
try {
4040
if (!player.isPlaying) {
4141
player.prepare()
4242
player.start()
43-
Log.i(TAG,"Playing ringtone")
43+
Log.i(TAG, "Playing ringtone")
4444
}
4545
} catch (e: Exception) {
46-
Log.e(TAG,"Failed to start mediaPlayer", e)
46+
Log.e(TAG, "Failed to start mediaPlayer", e)
4747
}
4848
}
4949
} ?: run {
50-
Log.w(TAG,"Not ringing, mediaPlayer: ${mediaPlayer?.let{"available"}}, mode: $ringerMode")
50+
Log.w(TAG,"Not ringing, mediaPlayer: ${mediaPlayer?.let{"available"}}")
5151
}
52-
5352
}
5453

5554
fun stop() {
5655
mediaPlayer?.release()
5756
mediaPlayer = null
58-
vibrator?.cancel()
57+
if (Build.VERSION.SDK_INT >= 31) {
58+
context.getSystemService(VibratorManager::class.java)
59+
?.defaultVibrator?.cancel()
60+
} else {
61+
context.getSystemService(Vibrator::class.java)?.cancel()
62+
}
5963
}
6064

61-
private fun shouldVibrate(player: MediaPlayer?, ringerMode: Int, vibrate: Boolean): Boolean {
62-
player ?: return true
65+
private fun shouldVibrate(audioManager: AudioManager): Boolean {
66+
val v = ServiceUtil.getVibrator(context) ?: return false
67+
if (!v.hasVibrator()) return false
6368

64-
if (vibrator == null || !vibrator.hasVibrator()) return false
69+
// Respect 'Do Not Disturb'
70+
val nm = context.getSystemService(NotificationManager::class.java)
71+
when (nm?.currentInterruptionFilter) {
72+
NotificationManager.INTERRUPTION_FILTER_NONE,
73+
NotificationManager.INTERRUPTION_FILTER_ALARMS -> return false
74+
}
6575

66-
return if (vibrate) ringerMode != AudioManager.RINGER_MODE_SILENT
67-
else ringerMode == AudioManager.RINGER_MODE_VIBRATE
76+
return when (audioManager.ringerMode) {
77+
AudioManager.RINGER_MODE_SILENT -> false
78+
AudioManager.RINGER_MODE_VIBRATE -> true
79+
AudioManager.RINGER_MODE_NORMAL -> true
80+
else -> false
81+
}
82+
}
83+
84+
private fun vibrate() {
85+
if (Build.VERSION.SDK_INT >= 31) {
86+
val vm = context.getSystemService(VibratorManager::class.java) ?: return
87+
val v = vm.defaultVibrator
88+
if (!v.hasVibrator()) return
89+
90+
val effect = VibrationEffect.createWaveform(PATTERN, 1)
91+
if (Build.VERSION.SDK_INT >= 33) {
92+
val attrs = VibrationAttributes.Builder()
93+
.setUsage(VibrationAttributes.USAGE_RINGTONE)
94+
.build()
95+
v.vibrate(effect, attrs)
96+
} else {
97+
v.vibrate(effect)
98+
}
99+
} else {
100+
val v = context.getSystemService(Vibrator::class.java) ?: return
101+
if (!v.hasVibrator()) return
102+
103+
val effect = VibrationEffect.createWaveform(PATTERN, 1)
104+
v.vibrate(effect)
105+
}
68106
}
69107

70108
private fun createMediaPlayer(): MediaPlayer? {

app/src/main/java/org/thoughtcrime/securesms/webrtc/audio/SignalAudioManager.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class SignalAudioManager(private val context: Context,
7272
is AudioManagerCommand.Stop -> stop(command.playDisconnect)
7373
is AudioManagerCommand.SetDefaultDevice -> setDefaultAudioDevice(command.device, command.clearUserEarpieceSelection)
7474
is AudioManagerCommand.SetUserDevice -> selectAudioDevice(command.device)
75-
is AudioManagerCommand.StartIncomingRinger -> startIncomingRinger(command.vibrate)
75+
is AudioManagerCommand.StartIncomingRinger -> startIncomingRinger()
7676
is AudioManagerCommand.SilenceIncomingRinger -> silenceIncomingRinger()
7777
is AudioManagerCommand.StartOutgoingRinger -> startOutgoingRinger(command.type)
7878
}
@@ -331,11 +331,11 @@ class SignalAudioManager(private val context: Context,
331331
}
332332
}
333333

334-
private fun startIncomingRinger(vibrate: Boolean) {
335-
Log.i(TAG, "startIncomingRinger(): vibrate: $vibrate")
334+
private fun startIncomingRinger() {
335+
Log.i(TAG, "startIncomingRinger()")
336336
androidAudioManager.mode = AudioManager.MODE_RINGTONE
337337

338-
incomingRinger.start(vibrate)
338+
incomingRinger.start()
339339
}
340340

341341
private fun silenceIncomingRinger() {

0 commit comments

Comments
 (0)