feat: add support for firebase and split out google services as a dependency for only the play version of the app. Add support for requests in new pn server
parent
2246a5d9ce
commit
8d4f2445f2
@ -1,4 +0,0 @@
|
||||
package org.thoughtcrime.securesms.dependencies;
|
||||
|
||||
public interface InjectableType {
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.thoughtcrime.securesms.dependencies
|
||||
|
||||
import dagger.hilt.EntryPoint
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import org.thoughtcrime.securesms.notifications.PushManager
|
||||
|
||||
@EntryPoint
|
||||
@InstallIn(SingletonComponent::class)
|
||||
interface PushComponent {
|
||||
fun providePushManager(): PushManager
|
||||
}
|
@ -1,271 +0,0 @@
|
||||
package org.thoughtcrime.securesms.jobs;
|
||||
|
||||
|
||||
import android.app.DownloadManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import org.session.libsession.messaging.utilities.Data;
|
||||
import org.thoughtcrime.securesms.jobmanager.Job;
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
||||
import org.session.libsignal.utilities.Log;
|
||||
import org.thoughtcrime.securesms.service.UpdateApkReadyListener;
|
||||
import org.session.libsession.utilities.FileUtils;
|
||||
import org.session.libsignal.utilities.Hex;
|
||||
import org.session.libsignal.utilities.JsonUtil;
|
||||
import org.session.libsession.utilities.TextSecurePreferences;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
import network.loki.messenger.BuildConfig;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class UpdateApkJob extends BaseJob {
|
||||
|
||||
public static final String KEY = "UpdateApkJob";
|
||||
|
||||
private static final String TAG = UpdateApkJob.class.getSimpleName();
|
||||
|
||||
public UpdateApkJob() {
|
||||
this(new Job.Parameters.Builder()
|
||||
.setQueue("UpdateApkJob")
|
||||
.addConstraint(NetworkConstraint.KEY)
|
||||
.setMaxAttempts(3)
|
||||
.build());
|
||||
}
|
||||
|
||||
private UpdateApkJob(@NonNull Job.Parameters parameters) {
|
||||
super(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull
|
||||
Data serialize() {
|
||||
return Data.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String getFactoryKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRun() throws IOException, PackageManager.NameNotFoundException {
|
||||
if (!BuildConfig.PLAY_STORE_DISABLED) return;
|
||||
|
||||
Log.i(TAG, "Checking for APK update...");
|
||||
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
Request request = new Request.Builder().url(String.format("%s/latest.json", BuildConfig.NOPLAY_UPDATE_URL)).build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("Bad response: " + response.message());
|
||||
}
|
||||
|
||||
UpdateDescriptor updateDescriptor = JsonUtil.fromJson(response.body().string(), UpdateDescriptor.class);
|
||||
byte[] digest = Hex.fromStringCondensed(updateDescriptor.getDigest());
|
||||
|
||||
Log.i(TAG, "Got descriptor: " + updateDescriptor);
|
||||
|
||||
if (updateDescriptor.getVersionCode() > getVersionCode()) {
|
||||
DownloadStatus downloadStatus = getDownloadStatus(updateDescriptor.getUrl(), digest);
|
||||
|
||||
Log.i(TAG, "Download status: " + downloadStatus.getStatus());
|
||||
|
||||
if (downloadStatus.getStatus() == DownloadStatus.Status.COMPLETE) {
|
||||
Log.i(TAG, "Download status complete, notifying...");
|
||||
handleDownloadNotify(downloadStatus.getDownloadId());
|
||||
} else if (downloadStatus.getStatus() == DownloadStatus.Status.MISSING) {
|
||||
Log.i(TAG, "Download status missing, starting download...");
|
||||
handleDownloadStart(updateDescriptor.getUrl(), updateDescriptor.getVersionName(), digest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onShouldRetry(@NonNull Exception e) {
|
||||
return e instanceof IOException;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCanceled() {
|
||||
Log.w(TAG, "Update check failed");
|
||||
}
|
||||
|
||||
private int getVersionCode() throws PackageManager.NameNotFoundException {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
||||
|
||||
return packageInfo.versionCode;
|
||||
}
|
||||
|
||||
private DownloadStatus getDownloadStatus(String uri, byte[] theirDigest) {
|
||||
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
DownloadManager.Query query = new DownloadManager.Query();
|
||||
|
||||
query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_SUCCESSFUL);
|
||||
|
||||
long pendingDownloadId = TextSecurePreferences.getUpdateApkDownloadId(context);
|
||||
byte[] pendingDigest = getPendingDigest(context);
|
||||
Cursor cursor = downloadManager.query(query);
|
||||
|
||||
try {
|
||||
DownloadStatus status = new DownloadStatus(DownloadStatus.Status.MISSING, -1);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
int jobStatus = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
|
||||
String jobRemoteUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_URI));
|
||||
long downloadId = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
|
||||
byte[] digest = getDigestForDownloadId(downloadId);
|
||||
|
||||
if (jobRemoteUri != null && jobRemoteUri.equals(uri) && downloadId == pendingDownloadId) {
|
||||
|
||||
if (jobStatus == DownloadManager.STATUS_SUCCESSFUL &&
|
||||
digest != null && pendingDigest != null &&
|
||||
MessageDigest.isEqual(pendingDigest, theirDigest) &&
|
||||
MessageDigest.isEqual(digest, theirDigest))
|
||||
{
|
||||
return new DownloadStatus(DownloadStatus.Status.COMPLETE, downloadId);
|
||||
} else if (jobStatus != DownloadManager.STATUS_SUCCESSFUL) {
|
||||
status = new DownloadStatus(DownloadStatus.Status.PENDING, downloadId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
} finally {
|
||||
if (cursor != null) cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleDownloadStart(String uri, String versionName, byte[] digest) {
|
||||
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(uri));
|
||||
|
||||
downloadRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
|
||||
downloadRequest.setTitle("Downloading Signal update");
|
||||
downloadRequest.setDescription("Downloading Signal " + versionName);
|
||||
downloadRequest.setVisibleInDownloadsUi(false);
|
||||
downloadRequest.setDestinationInExternalFilesDir(context, null, "signal-update.apk");
|
||||
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
|
||||
|
||||
long downloadId = downloadManager.enqueue(downloadRequest);
|
||||
TextSecurePreferences.setUpdateApkDownloadId(context, downloadId);
|
||||
TextSecurePreferences.setUpdateApkDigest(context, Hex.toStringCondensed(digest));
|
||||
}
|
||||
|
||||
private void handleDownloadNotify(long downloadId) {
|
||||
Intent intent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
|
||||
intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, downloadId);
|
||||
|
||||
new UpdateApkReadyListener().onReceive(context, intent);
|
||||
}
|
||||
|
||||
private @Nullable byte[] getDigestForDownloadId(long downloadId) {
|
||||
try {
|
||||
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
FileInputStream fin = new FileInputStream(downloadManager.openDownloadedFile(downloadId).getFileDescriptor());
|
||||
byte[] digest = FileUtils.getFileDigest(fin);
|
||||
|
||||
fin.close();
|
||||
|
||||
return digest;
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable byte[] getPendingDigest(Context context) {
|
||||
try {
|
||||
String encodedDigest = TextSecurePreferences.getUpdateApkDigest(context);
|
||||
|
||||
if (encodedDigest == null) return null;
|
||||
|
||||
return Hex.fromStringCondensed(encodedDigest);
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class UpdateDescriptor {
|
||||
@JsonProperty
|
||||
private int versionCode;
|
||||
|
||||
@JsonProperty
|
||||
private String versionName;
|
||||
|
||||
@JsonProperty
|
||||
private String url;
|
||||
|
||||
@JsonProperty
|
||||
private String sha256sum;
|
||||
|
||||
|
||||
public int getVersionCode() {
|
||||
return versionCode;
|
||||
}
|
||||
|
||||
public String getVersionName() {
|
||||
return versionName;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public @NonNull String toString() {
|
||||
return "[" + versionCode + ", " + versionName + ", " + url + "]";
|
||||
}
|
||||
|
||||
public String getDigest() {
|
||||
return sha256sum;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DownloadStatus {
|
||||
enum Status {
|
||||
PENDING,
|
||||
COMPLETE,
|
||||
MISSING
|
||||
}
|
||||
|
||||
private final Status status;
|
||||
private final long downloadId;
|
||||
|
||||
DownloadStatus(Status status, long downloadId) {
|
||||
this.status = status;
|
||||
this.downloadId = downloadId;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public long getDownloadId() {
|
||||
return downloadId;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Factory implements Job.Factory<UpdateApkJob> {
|
||||
@Override
|
||||
public @NonNull UpdateApkJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
||||
return new UpdateApkJob(parameters);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package org.thoughtcrime.securesms.notifications
|
||||
|
||||
interface PushManager {
|
||||
fun register(force: Boolean)
|
||||
fun unregister(token: String)
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package org.thoughtcrime.securesms.service;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import org.session.libsignal.utilities.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.ApplicationContext;
|
||||
import network.loki.messenger.BuildConfig;
|
||||
import org.thoughtcrime.securesms.jobs.UpdateApkJob;
|
||||
import org.session.libsession.utilities.TextSecurePreferences;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class UpdateApkRefreshListener extends PersistentAlarmManagerListener {
|
||||
|
||||
private static final String TAG = UpdateApkRefreshListener.class.getSimpleName();
|
||||
|
||||
private static final long INTERVAL = TimeUnit.HOURS.toMillis(6);
|
||||
|
||||
@Override
|
||||
protected long getNextScheduledExecutionTime(Context context) {
|
||||
return TextSecurePreferences.getUpdateApkRefreshTime(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long onAlarm(Context context, long scheduledTime) {
|
||||
Log.i(TAG, "onAlarm...");
|
||||
|
||||
if (scheduledTime != 0 && BuildConfig.PLAY_STORE_DISABLED) {
|
||||
Log.i(TAG, "Queueing APK update job...");
|
||||
ApplicationContext.getInstance(context)
|
||||
.getJobManager()
|
||||
.add(new UpdateApkJob());
|
||||
}
|
||||
|
||||
long newTime = System.currentTimeMillis() + INTERVAL;
|
||||
TextSecurePreferences.setUpdateApkRefreshTime(context, newTime);
|
||||
|
||||
return newTime;
|
||||
}
|
||||
|
||||
public static void schedule(Context context) {
|
||||
new UpdateApkRefreshListener().onReceive(context, new Intent());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<application tools:node="merge">
|
||||
<service
|
||||
android:name="org.thoughtcrime.securesms.notifications.PushNotificationService"
|
||||
android:enabled="true"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="com.google.firebase.MESSAGING_EVENT" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
</application>
|
||||
|
||||
</manifest>
|
@ -0,0 +1,142 @@
|
||||
package org.thoughtcrime.securesms.notifications
|
||||
|
||||
import android.content.Context
|
||||
import com.goterl.lazysodium.LazySodiumAndroid
|
||||
import com.goterl.lazysodium.SodiumAndroid
|
||||
import com.goterl.lazysodium.interfaces.AEAD
|
||||
import com.goterl.lazysodium.interfaces.Sign
|
||||
import com.goterl.lazysodium.utils.Key
|
||||
import com.goterl.lazysodium.utils.KeyPair
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.decodeFromStream
|
||||
import nl.komponents.kovenant.Promise
|
||||
import nl.komponents.kovenant.functional.map
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionRequest
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionResponse
|
||||
import org.session.libsession.snode.OnionRequestAPI
|
||||
import org.session.libsession.snode.SnodeAPI
|
||||
import org.session.libsession.snode.Version
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
import org.session.libsession.utilities.TextSecurePreferences.Companion.getLocalNumber
|
||||
import org.session.libsignal.utilities.Base64
|
||||
import org.session.libsignal.utilities.Log
|
||||
import org.session.libsignal.utilities.Namespace
|
||||
import org.session.libsignal.utilities.retryIfNeeded
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
||||
import org.thoughtcrime.securesms.crypto.KeyPairUtilities
|
||||
|
||||
class FirebasePushManager(private val context: Context, private val prefs: TextSecurePreferences): PushManager {
|
||||
|
||||
companion object {
|
||||
private const val maxRetryCount = 4
|
||||
private const val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
||||
}
|
||||
|
||||
private var firebaseInstanceIdJob: Job? = null
|
||||
private val sodium = LazySodiumAndroid(SodiumAndroid())
|
||||
|
||||
private fun getOrCreateNotificationKey(): Key {
|
||||
if (IdentityKeyUtil.retrieve(context, IdentityKeyUtil.NOTIFICATION_KEY) == null) {
|
||||
// generate the key and store it
|
||||
val key = sodium.keygen(AEAD.Method.XCHACHA20_POLY1305_IETF)
|
||||
IdentityKeyUtil.save(context, IdentityKeyUtil.NOTIFICATION_KEY, key.asHexString)
|
||||
}
|
||||
return Key.fromHexString(
|
||||
IdentityKeyUtil.retrieve(
|
||||
context,
|
||||
IdentityKeyUtil.NOTIFICATION_KEY
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override fun register(force: Boolean) {
|
||||
val currentInstanceIdJob = firebaseInstanceIdJob
|
||||
if (currentInstanceIdJob != null && currentInstanceIdJob.isActive && !force) return
|
||||
|
||||
if (force && currentInstanceIdJob != null) {
|
||||
currentInstanceIdJob.cancel(null)
|
||||
}
|
||||
|
||||
firebaseInstanceIdJob = getFcmInstanceId { task ->
|
||||
// context in here is Dispatchers.IO
|
||||
if (!task.isSuccessful) {
|
||||
Log.w(
|
||||
"Loki",
|
||||
"FirebaseInstanceId.getInstance().getInstanceId() failed." + task.exception
|
||||
)
|
||||
return@getFcmInstanceId
|
||||
}
|
||||
val token: String = task.result?.token ?: return@getFcmInstanceId
|
||||
val userPublicKey = getLocalNumber(context) ?: return@getFcmInstanceId
|
||||
val userEdKey = KeyPairUtilities.getUserED25519KeyPair(context) ?: return@getFcmInstanceId
|
||||
if (prefs.isUsingFCM()) {
|
||||
register(token, userPublicKey, userEdKey, force)
|
||||
} else {
|
||||
unregister(token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun unregister(token: String) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
fun register(token: String, publicKey: String, userEd25519Key: KeyPair, force: Boolean, namespaces: List<Int> = listOf(Namespace.DEFAULT)) {
|
||||
val oldToken = TextSecurePreferences.getFCMToken(context)
|
||||
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
||||
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
|
||||
|
||||
val pnKey = getOrCreateNotificationKey()
|
||||
|
||||
val timestamp = SnodeAPI.nowWithOffset / 1000 // get timestamp in ms -> s
|
||||
// if we want to support passing namespace list, here is the place to do it
|
||||
val sigData = "MONITOR${publicKey}${timestamp}1${namespaces.joinToString(separator = ",")}".encodeToByteArray()
|
||||
val signature = ByteArray(Sign.BYTES)
|
||||
sodium.cryptoSignDetached(signature, sigData, sigData.size.toLong(), userEd25519Key.secretKey.asBytes)
|
||||
val requestParameters = SubscriptionRequest (
|
||||
pubkey = publicKey,
|
||||
session_ed25519 = userEd25519Key.publicKey.asHexString,
|
||||
namespaces = listOf(Namespace.DEFAULT),
|
||||
data = true, // only permit data subscription for now (?)
|
||||
service = "firebase",
|
||||
sig_ts = timestamp,
|
||||
signature = Base64.encodeBytes(signature),
|
||||
service_info = mapOf("token" to token),
|
||||
enc_key = pnKey.asHexString,
|
||||
)
|
||||
|
||||
val url = "${PushNotificationAPI.server}/subscribe"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), Json.encodeToString(requestParameters))
|
||||
val request = Request.Builder().url(url).post(body)
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
getResponseBody(request.build()).map { response ->
|
||||
if (response.isSuccess()) {
|
||||
TextSecurePreferences.setIsUsingFCM(context, true)
|
||||
TextSecurePreferences.setFCMToken(context, token)
|
||||
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
|
||||
} else {
|
||||
val (_, message) = response.errorInfo()
|
||||
Log.d("Loki", "Couldn't register for FCM due to error: $message.")
|
||||
}
|
||||
}.fail { exception ->
|
||||
Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getResponseBody(request: Request): Promise<SubscriptionResponse, Exception> {
|
||||
return OnionRequestAPI.sendOnionRequest(request,
|
||||
PushNotificationAPI.server,
|
||||
PushNotificationAPI.serverPublicKey, Version.V4).map { response ->
|
||||
Json.decodeFromStream(response.body!!.inputStream())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package org.thoughtcrime.securesms.notifications
|
||||
|
||||
import android.content.Context
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object FirebasePushModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideFirebasePushManager(
|
||||
@ApplicationContext context: Context,
|
||||
prefs: TextSecurePreferences,
|
||||
): PushManager = FirebasePushManager(context, prefs)
|
||||
}
|
Loading…
Reference in New Issue