srctree

Robin Linden parent 88c6a9c8 8ee94324
Clean up Detekt lint warnings

inlinesplit
atox/src/main/kotlin/ToxService.kt added: 56, removed: 36, total 20
@@ -26,10 +26,11 @@ import ltd.evilcorp.domain.tox.Tox
import ltd.evilcorp.domain.tox.ToxSaveStatus
 
private const val TAG = "ToxService"
private const val NOTIFICATION_ID = 1984
private const val BOOTSTRAP_INTERVAL_MS = 60_000L
 
class ToxService : LifecycleService() {
private val channelId = "ToxService"
private val notificationId = 1984
 
private var connectionStatus: ConnectionStatus? = null
 
@@ -87,16 +88,16 @@ class ToxService : LifecycleService() {
}
 
createNotificationChannel()
startForeground(notificationId, notificationFor(connectionStatus))
startForeground(NOTIFICATION_ID, notificationFor(connectionStatus))
 
userRepository.get(tox.publicKey.string())
.filter { user: User? -> user != null }.asLiveData().observe(this) { user ->
if (user.connectionStatus == connectionStatus) return@observe
connectionStatus = user.connectionStatus
notifier.notify(notificationId, notificationFor(connectionStatus))
notifier.notify(NOTIFICATION_ID, notificationFor(connectionStatus))
if (connectionStatus == ConnectionStatus.None) {
Log.i(TAG, "Gone offline, scheduling bootstrap")
bootstrapTimer.schedule(60_000, 60_000) {
bootstrapTimer.schedule(BOOTSTRAP_INTERVAL_MS, BOOTSTRAP_INTERVAL_MS) {
Log.i(TAG, "Been offline for too long, bootstrapping")
tox.isBootstrapNeeded = true
}
 
atox/src/main/kotlin/tox/EventListenerCallbacks.kt added: 56, removed: 36, total 20
@@ -50,6 +50,9 @@ private fun isImage(filename: String) = try {
false
}
 
private const val FINGERPRINT_LEN = 8
private fun String.fingerprint() = this.take(FINGERPRINT_LEN)
 
@Singleton
class EventListenerCallbacks @Inject constructor(
private val ctx: Context,
@@ -169,12 +172,12 @@ class EventListenerCallbacks @Inject constructor(
 
fun setUp(listener: ToxAvEventListener) = with(listener) {
callHandler = { pk, audioEnabled, videoEnabled ->
Log.e(TAG, "call ${pk.take(8)} $audioEnabled $videoEnabled")
Log.e(TAG, "call ${pk.fingerprint()} $audioEnabled $videoEnabled")
notificationHelper.showPendingCallNotification(contactByPublicKey(pk))
}
 
callStateHandler = { pk, callState ->
Log.e(TAG, "callState ${pk.take(8)} $callState")
Log.e(TAG, "callState ${pk.fingerprint()} $callState")
if (callState.contains(ToxavFriendCallState.FINISHED) || callState.contains(ToxavFriendCallState.ERROR)) {
audioPlayer?.stop()
audioPlayer?.release()
@@ -185,7 +188,7 @@ class EventListenerCallbacks @Inject constructor(
}
 
videoBitRateHandler = { pk, bitRate ->
Log.e(TAG, "videoBitRate ${pk.take(8)} $bitRate")
Log.e(TAG, "videoBitRate ${pk.fingerprint()} $bitRate")
}
 
videoReceiveFrameHandler = { pk,
@@ -194,7 +197,7 @@ class EventListenerCallbacks @Inject constructor(
yStride, uStride, vStride ->
Log.v(
TAG,
"videoReceiveFrame ${pk.take(8)}" +
"videoReceiveFrame ${pk.fingerprint()}" +
"$width $height" +
"${y.size} ${u.size} ${v.size}" +
"$yStride $uStride $vStride"
@@ -202,7 +205,7 @@ class EventListenerCallbacks @Inject constructor(
}
 
audioBitRateHandler = { pk, bitRate ->
Log.e(TAG, "audioBitRate ${pk.take(8)} $bitRate")
Log.e(TAG, "audioBitRate ${pk.fingerprint()} $bitRate")
}
 
audioReceiveFrameHandler = { _, pcm, channels, samplingRate ->
 
atox/src/main/kotlin/ui/call/CallFragment.kt added: 56, removed: 36, total 20
@@ -5,9 +5,7 @@
package ltd.evilcorp.atox.ui.call
 
import android.Manifest
import android.content.res.Resources
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
@@ -32,9 +30,6 @@ private const val PERMISSION = Manifest.permission.RECORD_AUDIO
 
private const val CALL_BACKGROUND_SIZE_DP = 500
 
private fun dpToPx(dp: Float, res: Resources): Int =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.displayMetrics).toInt()
 
class CallFragment : BaseFragment<FragmentCallBinding>(FragmentCallBinding::inflate) {
private val vm: CallViewModel by viewModels { vmFactory }
 
@@ -51,7 +46,7 @@ class CallFragment : BaseFragment<FragmentCallBinding>(FragmentCallBinding::infl
override fun onViewCreated(view: View, savedInstanceState: Bundle?) = binding.run {
ViewCompat.setOnApplyWindowInsetsListener(view) { _, compat ->
val insets = compat.getInsets(WindowInsetsCompat.Type.systemBars())
controlContainer.updatePadding(bottom = insets.bottom + dpToPx(16f, resources))
controlContainer.updatePadding(bottom = insets.bottom + controlContainer.paddingTop)
compat
}
 
 
atox/src/main/kotlin/ui/settings/SettingsViewModel.kt added: 56, removed: 36, total 20
@@ -32,6 +32,8 @@ import ltd.evilcorp.domain.tox.Tox
import ltd.evilcorp.domain.tox.ToxSaveStatus
import ltd.evilcorp.domain.tox.testToxSave
 
private const val TOX_SHUTDOWN_POLL_DELAY_MS = 200L
 
enum class ProxyStatus {
Good,
BadPort,
@@ -113,7 +115,7 @@ class SettingsViewModel @Inject constructor(
 
viewModelScope.launch {
while (tox.started) {
delay(200)
delay(TOX_SHUTDOWN_POLL_DELAY_MS)
}
toxStarter.tryLoadTox(password)
_committed.value = true
 
filename was Deleted added: 56, removed: 36, total 20
@@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2021 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
 
package ltd.evilcorp.domain
 
private const val RADIX_HEX = 16
fun String.toHex() = this.toInt(RADIX_HEX)
 
domain/src/main/kotlin/feature/CallManager.kt added: 56, removed: 36, total 20
@@ -28,6 +28,10 @@ sealed class CallState {
 
private const val TAG = "CallManager"
 
private const val AUDIO_CHANNELS = 1
private const val AUDIO_SAMPLING_RATE_HZ = 48_000
private const val AUDIO_SEND_INTERVAL_MS = 20
 
@Singleton
class CallManager @Inject constructor(
private val tox: Tox,
@@ -68,7 +72,7 @@ class CallManager @Inject constructor(
fun startSendingAudio(): Boolean {
val to = (inCall.value as CallState.InCall?)?.publicKey ?: return false
 
val recorder = AudioCapture(48_000, 1)
val recorder = AudioCapture(AUDIO_SAMPLING_RATE_HZ, AUDIO_CHANNELS)
if (!recorder.isOk()) {
return false
}
@@ -89,13 +93,13 @@ class CallManager @Inject constructor(
val start = System.currentTimeMillis()
val audioFrame = recorder.read()
try {
tox.sendAudio(to, audioFrame, 1, 48_000)
tox.sendAudio(to, audioFrame, AUDIO_CHANNELS, AUDIO_SAMPLING_RATE_HZ)
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
val elapsed = System.currentTimeMillis() - start
if (elapsed < 20) {
delay(20 - elapsed)
if (elapsed < AUDIO_SEND_INTERVAL_MS) {
delay(AUDIO_SEND_INTERVAL_MS - elapsed)
}
}
recorder.stop()
 
domain/src/main/kotlin/tox/ToxIdValidator.kt added: 56, removed: 36, total 20
@@ -1,9 +1,13 @@
// SPDX-FileCopyrightText: 2019-2020 aTox contributors
// SPDX-FileCopyrightText: 2019-2021 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
 
package ltd.evilcorp.domain.tox
 
import ltd.evilcorp.domain.toHex
 
private const val CHECKSUM_BLOCK_SIZE = 4
 
class ToxIdValidator {
enum class Result {
NO_ERROR,
@@ -16,8 +20,8 @@ class ToxIdValidator {
fun validate(toxID: ToxID) = when {
!toxID.string().matches(Regex("[0-9A-Fa-f]*")) -> Result.NOT_HEX
toxID.string().length != TOX_ID_LENGTH -> Result.INCORRECT_LENGTH
toxID.string().chunked(4).map {
Integer.parseInt(it, 16)
toxID.string().chunked(CHECKSUM_BLOCK_SIZE).map {
it.toHex()
}.fold(0) { b1, b2 -> b1 xor b2 } != 0 -> Result.INVALID_CHECKSUM
else -> Result.NO_ERROR
}
 
domain/src/main/kotlin/tox/ToxUtil.kt added: 56, removed: 36, total 20
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2020 aTox contributors
// SPDX-FileCopyrightText: 2019-2021 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
 
@@ -15,8 +15,9 @@ import ltd.evilcorp.core.vo.ConnectionStatus
import ltd.evilcorp.core.vo.FileKind
import ltd.evilcorp.core.vo.MessageType
import ltd.evilcorp.core.vo.UserStatus
import ltd.evilcorp.domain.toHex
 
fun String.hexToBytes(): ByteArray = chunked(2).map { it.uppercase().toInt(16).toByte() }.toByteArray()
fun String.hexToBytes(): ByteArray = chunked(2).map { it.uppercase().toHex().toByte() }.toByteArray()
fun ByteArray.bytesToHex(): String = this.joinToString("") { "%02X".format(it) }
fun ToxUserStatus.toUserStatus(): UserStatus = UserStatus.values()[this.ordinal]
fun ToxConnection.toConnectionStatus(): ConnectionStatus = ConnectionStatus.values()[this.ordinal]
 
domain/src/main/kotlin/tox/ToxWrapper.kt added: 56, removed: 36, total 20
@@ -20,6 +20,8 @@ import ltd.evilcorp.core.vo.UserStatus
 
private const val TAG = "ToxWrapper"
 
private const val AUDIO_BIT_RATE = 128
 
enum class CustomPacketError {
Success,
Empty,
@@ -175,8 +177,8 @@ class ToxWrapper(
private fun contactByKey(pk: PublicKey): Int = tox.friendByPublicKey(pk.bytes())
 
// ToxAv, probably move these.
fun startCall(pk: PublicKey) = av.call(contactByKey(pk), 128, 0)
fun answerCall(pk: PublicKey) = av.answer(contactByKey(pk), 128, 0)
fun startCall(pk: PublicKey) = av.call(contactByKey(pk), AUDIO_BIT_RATE, 0)
fun answerCall(pk: PublicKey) = av.answer(contactByKey(pk), AUDIO_BIT_RATE, 0)
fun endCall(pk: PublicKey) = av.callControl(contactByKey(pk), ToxavCallControl.CANCEL)
fun sendAudio(pk: PublicKey, pcm: ShortArray, channels: Int, samplingRate: Int) =
av.audioSendFrame(contactByKey(pk), pcm, pcm.size, channels, samplingRate)