session-android/src/org/thoughtcrime/securesms/loki/GeneralUtilities.kt

40 lines
1.7 KiB
Kotlin
Raw Normal View History

2019-06-21 04:18:38 +00:00
package org.thoughtcrime.securesms.loki
2019-10-07 05:15:06 +00:00
import android.content.Context
2019-06-21 04:18:38 +00:00
import android.content.res.Resources
import android.os.Build
import android.support.annotation.ColorRes
2019-10-07 05:15:06 +00:00
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.recipients.Recipient
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus
2019-06-21 05:29:25 +00:00
import kotlin.math.roundToInt
2019-06-21 04:18:38 +00:00
fun Resources.getColorWithID(@ColorRes id: Int, theme: Resources.Theme?): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getColor(id, theme)
} else {
@Suppress("DEPRECATION") getColor(id)
}
2019-06-21 05:29:25 +00:00
}
fun toPx(dp: Int, resources: Resources): Int {
2019-06-21 05:29:25 +00:00
val scale = resources.displayMetrics.density
return (dp * scale).roundToInt()
2019-10-07 05:15:06 +00:00
}
2019-10-15 03:32:23 +00:00
fun isPublicChat(context: Context, recipient: String): Boolean {
2019-10-15 02:39:17 +00:00
return DatabaseFactory.getLokiThreadDatabase(context).getAllPublicChats().values.map { it.server }.contains(recipient)
2019-10-07 05:15:06 +00:00
}
fun getFriendPublicKeys(context: Context, devicePublicKeys: Set<String>): Set<String> {
val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context)
return devicePublicKeys.mapNotNull { device ->
val address = Address.fromSerialized(device)
val recipient = Recipient.from(context, address, false)
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient)
if (threadID < 0) { return@mapNotNull null }
val friendRequestStatus = lokiThreadDatabase.getFriendRequestStatus(threadID)
if (friendRequestStatus == LokiThreadFriendRequestStatus.FRIENDS) device else null
}.toSet()
2019-06-21 04:18:38 +00:00
}