2020-05-12 11:46:11 +10:00
|
|
|
package org.thoughtcrime.securesms.loki.api
|
2020-04-15 10:24:30 +10:00
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import okhttp3.*
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
|
|
|
import org.whispersystems.libsignal.logging.Log
|
|
|
|
import org.whispersystems.signalservice.internal.util.JsonUtil
|
2020-04-16 16:56:12 +10:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiPushNotificationAcknowledgement
|
2020-04-15 10:24:30 +10:00
|
|
|
import java.io.IOException
|
|
|
|
|
|
|
|
object LokiPushNotificationManager {
|
|
|
|
private val connection = OkHttpClient()
|
|
|
|
|
2020-04-16 16:56:12 +10:00
|
|
|
private val server by lazy {
|
|
|
|
LokiPushNotificationAcknowledgement.shared.server
|
|
|
|
}
|
|
|
|
|
2020-04-21 12:21:07 +10:00
|
|
|
private const val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
2020-04-16 16:56:12 +10:00
|
|
|
|
2020-04-17 12:11:27 +10:00
|
|
|
@JvmStatic
|
|
|
|
fun unregister(token: String, context: Context?) {
|
2020-04-16 16:56:12 +10:00
|
|
|
val parameters = mapOf( "token" to token )
|
2020-05-12 11:46:11 +10:00
|
|
|
val url = "$server/register"
|
2020-04-15 10:24:30 +10:00
|
|
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
|
|
|
val request = Request.Builder().url(url).post(body).build()
|
|
|
|
connection.newCall(request).enqueue(object : Callback {
|
|
|
|
|
|
|
|
override fun onResponse(call: Call, response: Response) {
|
|
|
|
when (response.code()) {
|
|
|
|
200 -> {
|
|
|
|
val bodyAsString = response.body()!!.string()
|
|
|
|
val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
|
|
|
|
val code = json?.get("code") as? Int
|
|
|
|
if (code != null && code != 0) {
|
2020-04-16 16:56:12 +10:00
|
|
|
TextSecurePreferences.setIsUsingFCM(context, false)
|
2020-04-15 10:24:30 +10:00
|
|
|
} else {
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "Couldn't disable FCM due to error: ${json?.get("message") as? String ?: "null"}.")
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call, exception: IOException) {
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "Couldn't disable FCM.")
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
2020-04-17 13:26:11 +10:00
|
|
|
fun register(token: String, hexEncodedPublicKey: String, context: Context?, force: Boolean) {
|
2020-04-16 16:56:12 +10:00
|
|
|
val oldToken = TextSecurePreferences.getFCMToken(context)
|
|
|
|
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
2020-04-21 12:21:07 +10:00
|
|
|
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
|
2020-04-16 16:56:12 +10:00
|
|
|
val parameters = mapOf( "token" to token, "pubKey" to hexEncodedPublicKey )
|
2020-05-12 11:46:11 +10:00
|
|
|
val url = "$server/register"
|
2020-04-15 10:24:30 +10:00
|
|
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
|
|
|
val request = Request.Builder().url(url).post(body).build()
|
|
|
|
connection.newCall(request).enqueue(object : Callback {
|
|
|
|
|
|
|
|
override fun onResponse(call: Call, response: Response) {
|
|
|
|
when (response.code()) {
|
|
|
|
200 -> {
|
|
|
|
val bodyAsString = response.body()!!.string()
|
|
|
|
val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
|
|
|
|
val code = json?.get("code") as? Int
|
|
|
|
if (code != null && code != 0) {
|
2020-04-16 16:56:12 +10:00
|
|
|
TextSecurePreferences.setIsUsingFCM(context, true)
|
|
|
|
TextSecurePreferences.setFCMToken(context, token)
|
|
|
|
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
|
2020-04-15 10:24:30 +10:00
|
|
|
} else {
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "Couldn't register for FCM due to error: ${json?.get("message") as? String ?: "null"}.")
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call, exception: IOException) {
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "Couldn't register for FCM.")
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|