Skip to content
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 @@ -60,7 +60,7 @@ object OnionRequestAPI {
.map { it.isNotEmpty() }
.stateIn(GlobalScope, SharingStarted.Eagerly, paths.value.isNotEmpty())

private val NON_PENALIZING_STATUSES = setOf(400, 403, 404, 406, 425)
private val NON_PENALIZING_STATUSES = setOf(403, 404, 406, 425)

init {
// Listen for the changes in paths and persist it to the db
Expand Down Expand Up @@ -418,10 +418,10 @@ object OnionRequestAPI {
Log.d("Loki","Request returned a non penalizing code ${exception.statusCode} with message: $message")
}
// we do not want to penalize the path/nodes when:
// - the exit node reached the server but the destination returned 5xx
// - 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)
// - the exit node reached the server but the destination returned 5xx or 400
// - 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)
else if (destination is Destination.Server &&
(exception.statusCode in 500..504) &&
(exception.statusCode in 500..504 || exception.statusCode == 400) &&
(exception is HTTPRequestFailedAtDestinationException || exception.body?.contains(destination.host) == true)) {
Log.d("Loki","Destination server error - Non path penalizing. Request returned code ${exception.statusCode} with message: $message")
} else if (message == "Loki Server error") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,12 +930,6 @@ interface TextSecurePreferences {
setBooleanPreference(context, FINGERPRINT_KEY_GENERATED, true)
}

@JvmStatic
fun clearAll(context: Context) {
getDefaultSharedPreferences(context).edit().clear().commit()
}


// ----- Get / set methods for if we have already warned the user that saving attachments will allow other apps to access them -----
// 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.
@JvmStatic
Expand Down Expand Up @@ -1656,8 +1650,16 @@ class AppTextSecurePreferences @Inject constructor(
return getBooleanPreference(AUTOPLAY_AUDIO_MESSAGES, false)
}

/**
* Clear all prefs and reset our observables
*/
override fun clearAll() {
getDefaultSharedPreferences(context).edit().clear().commit()
pushEnabled.update { false }
localNumberState.update { null }
postProLaunchState.update { false }
hiddenPasswordState.update { false }

getDefaultSharedPreferences(context).edit(commit = true) { clear() }
}

override fun getHidePassword() = getBooleanPreference(HIDE_PASSWORD, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ import org.thoughtcrime.securesms.dependencies.ConfigFactory
import org.thoughtcrime.securesms.giph.ui.GiphyActivity
import org.thoughtcrime.securesms.groups.GroupMembersActivity
import org.thoughtcrime.securesms.groups.OpenGroupManager
import org.thoughtcrime.securesms.home.HomeActivity
import org.thoughtcrime.securesms.home.search.searchName
import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil
Expand Down Expand Up @@ -283,7 +284,7 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,

private val address: Address.Conversable by lazy {
requireNotNull(IntentCompat.getParcelableExtra(intent, ADDRESS, Address.Conversable::class.java)) {
"Address must be provided in the intent extras"
"Address must be provided in the intent extras to open a conversation"
}
}

Expand Down Expand Up @@ -521,6 +522,18 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,

override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
super.onCreate(savedInstanceState, isReady)

// Check if address is null before proceeding with initialization
if (IntentCompat.getParcelableExtra(intent, ADDRESS, Address.Conversable::class.java) == null) {
Log.w(TAG, "ConversationActivityV2 launched without ADDRESS extra - Returning home")
val intent = Intent(this, HomeActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
startActivity(intent)
finish()
return
}

binding = ActivityConversationV2Binding.inflate(layoutInflater)
setContentView(binding.root)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class ClearDataUtils @Inject constructor(
application.deleteDatabase(DatabaseMigrationManager.CIPHER4_DB_NAME)
application.deleteDatabase(DatabaseMigrationManager.CIPHER3_DB_NAME)

TextSecurePreferences.clearAll(application)
// clear all prefs
prefs.clearAll()

application.getSharedPreferences(ApplicationContext.PREFERENCES_NAME, 0).edit(commit = true) { clear() }
application.cacheDir.deleteRecursively()
application.filesDir.deleteRecursively()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class AudioManagerCommand: Parcelable {
data class Stop(val playDisconnect: Boolean): AudioManagerCommand()

@Parcelize
data class StartIncomingRinger(val vibrate: Boolean): AudioManagerCommand()
data object StartIncomingRinger: AudioManagerCommand()

@Parcelize
data class SetUserDevice(val device: SignalAudioManager.AudioDevice): AudioManagerCommand()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ class CallManager @Inject constructor(
}

fun startIncomingRinger() {
signalAudioManager.handleCommand(AudioManagerCommand.StartIncomingRinger(true))
signalAudioManager.handleCommand(AudioManagerCommand.StartIncomingRinger)
}

fun startCommunication() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.thoughtcrime.securesms.webrtc.audio

import android.app.NotificationManager
import android.content.Context
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
import android.media.RingtoneManager
import android.os.Build
import android.os.VibrationAttributes
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import org.session.libsession.utilities.ServiceUtil
import org.session.libsignal.utilities.Log

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

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

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

fun start(vibrate: Boolean) {
fun start() {
val audioManager = ServiceUtil.getAudioManager(context)
mediaPlayer?.release()
mediaPlayer = createMediaPlayer()
val ringerMode = audioManager.ringerMode

if (shouldVibrate(mediaPlayer, ringerMode, vibrate)) {
Log.i(TAG,"Starting vibration")
vibrator?.vibrate(PATTERN, 1)
} else {
Log.i(TAG,"Skipping vibration")
}
// Vibrate if policy/system allows
if (shouldVibrate(audioManager)) vibrate()

// Play ringtone only in NORMAL
mediaPlayer?.let { player ->
if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {
if (audioManager.ringerMode == AudioManager.RINGER_MODE_NORMAL) {
try {
if (!player.isPlaying) {
player.prepare()
player.start()
Log.i(TAG,"Playing ringtone")
Log.i(TAG, "Playing ringtone")
}
} catch (e: Exception) {
Log.e(TAG,"Failed to start mediaPlayer", e)
Log.e(TAG, "Failed to start mediaPlayer", e)
}
}
} ?: run {
Log.w(TAG,"Not ringing, mediaPlayer: ${mediaPlayer?.let{"available"}}, mode: $ringerMode")
Log.w(TAG,"Not ringing, mediaPlayer: ${mediaPlayer?.let{"available"}}")
}

}

fun stop() {
mediaPlayer?.release()
mediaPlayer = null
vibrator?.cancel()
if (Build.VERSION.SDK_INT >= 31) {
context.getSystemService(VibratorManager::class.java)
?.defaultVibrator?.cancel()
} else {
context.getSystemService(Vibrator::class.java)?.cancel()
}
}

private fun shouldVibrate(player: MediaPlayer?, ringerMode: Int, vibrate: Boolean): Boolean {
player ?: return true
private fun shouldVibrate(audioManager: AudioManager): Boolean {
val v = ServiceUtil.getVibrator(context) ?: return false
if (!v.hasVibrator()) return false

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

return if (vibrate) ringerMode != AudioManager.RINGER_MODE_SILENT
else ringerMode == AudioManager.RINGER_MODE_VIBRATE
return when (audioManager.ringerMode) {
AudioManager.RINGER_MODE_SILENT -> false
AudioManager.RINGER_MODE_VIBRATE -> true
AudioManager.RINGER_MODE_NORMAL -> true
else -> false
}
}

private fun vibrate() {
if (Build.VERSION.SDK_INT >= 31) {
val vm = context.getSystemService(VibratorManager::class.java) ?: return
val v = vm.defaultVibrator
if (!v.hasVibrator()) return

val effect = VibrationEffect.createWaveform(PATTERN, 1)
if (Build.VERSION.SDK_INT >= 33) {
val attrs = VibrationAttributes.Builder()
.setUsage(VibrationAttributes.USAGE_RINGTONE)
.build()
v.vibrate(effect, attrs)
} else {
v.vibrate(effect)
}
} else {
val v = context.getSystemService(Vibrator::class.java) ?: return
if (!v.hasVibrator()) return

val effect = VibrationEffect.createWaveform(PATTERN, 1)
v.vibrate(effect)
}
}

private fun createMediaPlayer(): MediaPlayer? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SignalAudioManager(private val context: Context,
is AudioManagerCommand.Stop -> stop(command.playDisconnect)
is AudioManagerCommand.SetDefaultDevice -> setDefaultAudioDevice(command.device, command.clearUserEarpieceSelection)
is AudioManagerCommand.SetUserDevice -> selectAudioDevice(command.device)
is AudioManagerCommand.StartIncomingRinger -> startIncomingRinger(command.vibrate)
is AudioManagerCommand.StartIncomingRinger -> startIncomingRinger()
is AudioManagerCommand.SilenceIncomingRinger -> silenceIncomingRinger()
is AudioManagerCommand.StartOutgoingRinger -> startOutgoingRinger(command.type)
}
Expand Down Expand Up @@ -331,11 +331,11 @@ class SignalAudioManager(private val context: Context,
}
}

private fun startIncomingRinger(vibrate: Boolean) {
Log.i(TAG, "startIncomingRinger(): vibrate: $vibrate")
private fun startIncomingRinger() {
Log.i(TAG, "startIncomingRinger()")
androidAudioManager.mode = AudioManager.MODE_RINGTONE

incomingRinger.start(vibrate)
incomingRinger.start()
}

private fun silenceIncomingRinger() {
Expand Down