Minor refactoring

This commit is contained in:
nielsandriesse 2020-04-16 16:56:12 +10:00
parent 1183b76ef5
commit 84c783fae1
5 changed files with 60 additions and 64 deletions

View File

@ -6,6 +6,8 @@ buildscript {
ext.kovenant_version = "3.3.0" ext.kovenant_version = "3.3.0"
ext.identicon_version = "v11" ext.identicon_version = "v11"
ext.rss_parser_version = "2.0.4" ext.rss_parser_version = "2.0.4"
ext.google_services_version = "4.3.3"
ext.firebase_messaging_version = "18.0.0"
repositories { repositories {
mavenLocal() mavenLocal()
@ -16,7 +18,7 @@ buildscript {
classpath "com.android.tools.build:gradle:$gradle_version" classpath "com.android.tools.build:gradle:$gradle_version"
classpath files('libs/gradle-witness.jar') classpath files('libs/gradle-witness.jar')
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.3' classpath "com.google.gms:google-services:$google_services_version"
} }
} }
@ -89,7 +91,7 @@ dependencies {
implementation 'android.arch.lifecycle:extensions:1.1.1' implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'android.arch.lifecycle:common-java8:1.1.1' implementation 'android.arch.lifecycle:common-java8:1.1.1'
implementation 'com.google.firebase:firebase-messaging:18.0.0' implementation "com.google.firebase:firebase-messaging:$firebase_messaging_version"
implementation 'com.google.android.exoplayer:exoplayer-core:2.9.1' implementation 'com.google.android.exoplayer:exoplayer-core:2.9.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.1' implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.1'

View File

@ -30,10 +30,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.multidex.MultiDexApplication; import android.support.multidex.MultiDexApplication;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import org.conscrypt.Conscrypt; import org.conscrypt.Conscrypt;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -103,6 +100,7 @@ import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol;
import org.whispersystems.signalservice.loki.api.LokiP2PAPI; import org.whispersystems.signalservice.loki.api.LokiP2PAPI;
import org.whispersystems.signalservice.loki.api.LokiP2PAPIDelegate; import org.whispersystems.signalservice.loki.api.LokiP2PAPIDelegate;
import org.whispersystems.signalservice.loki.api.LokiPoller; import org.whispersystems.signalservice.loki.api.LokiPoller;
import org.whispersystems.signalservice.loki.api.LokiPushNotificationAcknowledgement;
import org.whispersystems.signalservice.loki.api.LokiSwarmAPI; import org.whispersystems.signalservice.loki.api.LokiSwarmAPI;
import org.whispersystems.signalservice.loki.api.fileserver.LokiFileServerAPI; import org.whispersystems.signalservice.loki.api.fileserver.LokiFileServerAPI;
import org.whispersystems.signalservice.loki.api.publicchats.LokiPublicChat; import org.whispersystems.signalservice.loki.api.publicchats.LokiPublicChat;
@ -188,6 +186,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
ProcessLifecycleOwner.get().getLifecycle().addObserver(this); ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
// Loki - Set up P2P API if needed // Loki - Set up P2P API if needed
setUpP2PAPI(); setUpP2PAPI();
// Loki - Set up push notification acknowledgement
LokiPushNotificationAcknowledgement.Companion.configureIfNeeded(BuildConfig.DEBUG);
// Loki - Update device mappings // Loki - Update device mappings
if (setUpStorageAPIIfNeeded()) { if (setUpStorageAPIIfNeeded()) {
String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this); String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this);
@ -205,7 +205,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
// Loki - Set up public chat manager // Loki - Set up public chat manager
lokiPublicChatManager = new LokiPublicChatManager(this); lokiPublicChatManager = new LokiPublicChatManager(this);
updatePublicChatProfilePictureIfNeeded(); updatePublicChatProfilePictureIfNeeded();
setUpFirebaseDeviceToken(); setUpFCMIfNeeded();
} }
@Override @Override
@ -462,21 +462,16 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
}, this); }, this);
} }
public void setUpFirebaseDeviceToken() { public void setUpFCMIfNeeded() {
Context context = this; Context context = this;
FirebaseInstanceId.getInstance().getInstanceId() FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> {
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) { if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException()); Log.w(TAG, "getInstanceId failed", task.getException());
return; return;
} }
// Get new Instance ID token
String token = task.getResult().getToken(); String token = task.getResult().getToken();
String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context); String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context);
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, context); LokiPushNotificationManager.register(token, userHexEncodedPublicKey, context);
}
}); });
} }

View File

@ -5,17 +5,21 @@ import okhttp3.*
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.libsignal.logging.Log import org.whispersystems.libsignal.logging.Log
import org.whispersystems.signalservice.internal.util.JsonUtil import org.whispersystems.signalservice.internal.util.JsonUtil
import org.whispersystems.signalservice.loki.api.LokiPushNotificationAcknowledgement
import java.io.IOException import java.io.IOException
object LokiPushNotificationManager { object LokiPushNotificationManager {
//const val server = "https://live.apns.getsession.org/"
const val server = "https://dev.apns.getsession.org/"
const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000
private val connection = OkHttpClient() private val connection = OkHttpClient()
fun disableRemoteNotification(token: String, context: Context?) { private val server by lazy {
val parameters = mapOf("token" to token) LokiPushNotificationAcknowledgement.shared.server
val url = "${server}register" }
private const val tokenExpirationInterval = 2 * 24 * 60 * 60 * 1000
fun disableFCM(token: String, context: Context?) {
val parameters = mapOf( "token" to token )
val url = "${server}/register"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
val request = Request.Builder().url(url).post(body).build() val request = Request.Builder().url(url).post(body).build()
connection.newCall(request).enqueue(object : Callback { connection.newCall(request).enqueue(object : Callback {
@ -27,28 +31,27 @@ object LokiPushNotificationManager {
val json = JsonUtil.fromJson(bodyAsString, Map::class.java) val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
val code = json?.get("code") as? Int val code = json?.get("code") as? Int
if (code != null && code != 0) { if (code != null && code != 0) {
TextSecurePreferences.setIsUsingRemoteNotification(context, false) TextSecurePreferences.setIsUsingFCM(context, false)
} else { } else {
Log.d("Loki", "Couldn't disable remote notification due to error: ${json?.get("message") as? String}.") Log.d("Loki", "Couldn't disable FCM due to error: ${json?.get("message") as? String ?: "null"}.")
} }
} }
} }
} }
override fun onFailure(call: Call, exception: IOException) { override fun onFailure(call: Call, exception: IOException) {
Log.d("Loki", "Couldn't disable remote notification.") Log.d("Loki", "Couldn't disable FCM.")
} }
}) })
} }
@JvmStatic @JvmStatic
fun register(token: String, hexEncodedPublicKey: String, context: Context?) { fun register(token: String, hexEncodedPublicKey: String, context: Context?) {
if (token == TextSecurePreferences.getTokenForRemoteNotification(context) && System.currentTimeMillis() - TextSecurePreferences.getLastTimeForTokenUploading(context) < tokenExpirationInterval) { val oldToken = TextSecurePreferences.getFCMToken(context)
return val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
} if (token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
val parameters = mapOf( "token" to token, "pubKey" to hexEncodedPublicKey )
val parameters = mapOf("token" to token, "pubKey" to hexEncodedPublicKey) val url = "${server}/register"
val url = "${server}register"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
val request = Request.Builder().url(url).post(body).build() val request = Request.Builder().url(url).post(body).build()
connection.newCall(request).enqueue(object : Callback { connection.newCall(request).enqueue(object : Callback {
@ -60,21 +63,19 @@ object LokiPushNotificationManager {
val json = JsonUtil.fromJson(bodyAsString, Map::class.java) val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
val code = json?.get("code") as? Int val code = json?.get("code") as? Int
if (code != null && code != 0) { if (code != null && code != 0) {
TextSecurePreferences.setIsUsingRemoteNotification(context, true) TextSecurePreferences.setIsUsingFCM(context, true)
TextSecurePreferences.setTokenForRemoteNotification(context, token) TextSecurePreferences.setFCMToken(context, token)
TextSecurePreferences.setLastTimeForTokenUploading(context, System.currentTimeMillis()) TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
} else { } else {
Log.d("Loki", "Couldn't register device token due to error: ${json?.get("message") as? String}.") Log.d("Loki", "Couldn't register for FCM due to error: ${json?.get("message") as? String ?: "null"}.")
} }
} }
} }
} }
override fun onFailure(call: Call, exception: IOException) { override fun onFailure(call: Call, exception: IOException) {
Log.d("Loki", "Couldn't register device token.") Log.d("Loki", "Couldn't register for FCM.")
} }
}) })
} }
} }

View File

@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.service
import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage import com.google.firebase.messaging.RemoteMessage
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.jobs.PushContentReceiveJob import org.thoughtcrime.securesms.jobs.PushContentReceiveJob
import org.thoughtcrime.securesms.loki.LokiPushNotificationManager import org.thoughtcrime.securesms.loki.LokiPushNotificationManager
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
@ -11,11 +10,11 @@ import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
import org.whispersystems.signalservice.internal.util.Base64 import org.whispersystems.signalservice.internal.util.Base64
import org.whispersystems.signalservice.loki.messaging.LokiMessageWrapper import org.whispersystems.signalservice.loki.messaging.LokiMessageWrapper
class PushNotificationService: FirebaseMessagingService() { class PushNotificationService : FirebaseMessagingService() {
override fun onNewToken(token: String) { override fun onNewToken(token: String) {
super.onNewToken(token) super.onNewToken(token)
Log.d("Loki", "new token ${token}") Log.d("Loki", "New FCM token: $token.")
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this) val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this)
LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this) LokiPushNotificationManager.register(token, userHexEncodedPublicKey, this)
} }
@ -34,6 +33,4 @@ class PushNotificationService: FirebaseMessagingService() {
Log.d("Loki", "Failed to decode data for message.") Log.d("Loki", "Failed to decode data for message.")
} }
} }
} }

View File

@ -185,34 +185,35 @@ public class TextSecurePreferences {
private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode"; private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode";
//Session for Push Notification // region FCM
private static final String IS_USING_REMOTE_NOTIFICATION = "pref_is_using_remote_notification"; private static final String IS_USING_FCM = "pref_is_using_fcm";
private static final String TOKEN_FOR_REMOTE_NOTIFICATION = "pref_token_for_remote_notification"; private static final String FCM_TOKEN = "pref_fcm_token";
private static final String LAST_TIME_FOR_TOKEN_UPLOADING = "pref_last_time_for_token_uploading"; private static final String LAST_FCM_TOKEN_UPLOAD_TIME = "pref_last_fcm_token_upload_time";
public static boolean isUsingRemoteNotification(Context context) { public static boolean isUsingFCM(Context context) {
return getBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, false); return getBooleanPreference(context, IS_USING_FCM, false);
} }
public static void setIsUsingRemoteNotification(Context context, boolean value) { public static void setIsUsingFCM(Context context, boolean value) {
setBooleanPreference(context, IS_USING_REMOTE_NOTIFICATION, value); setBooleanPreference(context, IS_USING_FCM, value);
} }
public static String getTokenForRemoteNotification(Context context) { public static String getFCMToken(Context context) {
return getStringPreference(context, TOKEN_FOR_REMOTE_NOTIFICATION, ""); return getStringPreference(context, FCM_TOKEN, "");
} }
public static void setTokenForRemoteNotification(Context context, String value) { public static void setFCMToken(Context context, String value) {
setStringPreference(context, TOKEN_FOR_REMOTE_NOTIFICATION, value); setStringPreference(context, FCM_TOKEN, value);
} }
public static long getLastTimeForTokenUploading(Context context) { public static long getLastFCMUploadTime(Context context) {
return getLongPreference(context, LAST_TIME_FOR_TOKEN_UPLOADING, 0); return getLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, 0);
} }
public static void setLastTimeForTokenUploading(Context context, long value) { public static void setLastFCMUploadTime(Context context, long value) {
setLongPreference(context, LAST_TIME_FOR_TOKEN_UPLOADING, value); setLongPreference(context, LAST_FCM_TOKEN_UPLOAD_TIME, value);
} }
// endregion
public static boolean isScreenLockEnabled(@NonNull Context context) { public static boolean isScreenLockEnabled(@NonNull Context context) {
return getBooleanPreference(context, SCREEN_LOCK, false); return getBooleanPreference(context, SCREEN_LOCK, false);