diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index cc2970402b..1088f82b82 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1204,18 +1204,14 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } private void sendBackgroundMessage(String contactHexEncodedPublicKey) { - new Handler(Looper.getMainLooper()).post(new Runnable() { - - @Override - public void run() { - SignalServiceMessageSender messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender(); - SignalServiceAddress address = new SignalServiceAddress(contactHexEncodedPublicKey); - SignalServiceDataMessage message = new SignalServiceDataMessage(System.currentTimeMillis(), ""); - try { - messageSender.sendMessage(0, address, Optional.absent(), message); // The message ID doesn't matter - } catch (Exception e) { - Log.d("Loki", "Failed to send background message to: " + contactHexEncodedPublicKey + "."); - } + Util.runOnMain(() -> { + SignalServiceMessageSender messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender(); + SignalServiceAddress address = new SignalServiceAddress(contactHexEncodedPublicKey); + SignalServiceDataMessage message = new SignalServiceDataMessage(System.currentTimeMillis(), ""); + try { + messageSender.sendMessage(0, address, Optional.absent(), message); // The message ID doesn't matter + } catch (Exception e) { + Log.d("Loki", "Failed to send background message to: " + contactHexEncodedPublicKey + "."); } }); } diff --git a/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialog.kt b/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialog.kt index 1bdb136b23..2d0bb2496c 100644 --- a/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialog.kt +++ b/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialog.kt @@ -10,6 +10,8 @@ import org.whispersystems.signalservice.loki.api.LokiDeviceLinkingSessionListene import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation import org.whispersystems.signalservice.loki.api.LokiStorageAPI import org.whispersystems.signalservice.loki.utilities.retryIfNeeded +import org.thoughtcrime.securesms.util.Util + class DeviceLinkingDialog private constructor(private val context: Context, private val mode: DeviceLinkingView.Mode, private val delegate: DeviceLinkingDialogDelegate? = null): DeviceLinkingViewDelegate, LokiDeviceLinkingSessionListener { private lateinit var view: DeviceLinkingView @@ -93,14 +95,18 @@ class DeviceLinkingDialog private constructor(private val context: Context, priv // region Loki Device Session Listener override fun onDeviceLinkingRequestReceived(authorisation: LokiPairingAuthorisation) { - view.requestUserAuthorization(authorisation) + Util.runOnMain { + view.requestUserAuthorization(authorisation) + } // Stop listening to any more requests LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests() } override fun onDeviceLinkRequestAccepted(authorisation: LokiPairingAuthorisation) { - view.onDeviceLinkAuthorized(authorisation) + Util.runOnMain { + view.onDeviceLinkAuthorized(authorisation) + } // Stop listening to any more requests LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests() diff --git a/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt b/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt index 0095656413..e63d19c41b 100644 --- a/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt +++ b/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt @@ -108,8 +108,6 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe return } - this.pairingAuthorisation = authorisation - spinner.visibility = View.GONE val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams titleTextViewLayoutParams.topMargin = toPx(16, resources) @@ -120,6 +118,8 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe val hexEncodedPublicKey = authorisation.secondaryDevicePubKey.removing05PrefixIfNeeded() mnemonicTextView.text = MnemonicCodec(languageFileDirectory).encode(hexEncodedPublicKey).split(" ").slice(0 until 3).joinToString(" ") authorizeButton.visibility = View.VISIBLE + + this.pairingAuthorisation = authorisation } private fun authorize() { diff --git a/src/org/thoughtcrime/securesms/loki/SeedActivity.kt b/src/org/thoughtcrime/securesms/loki/SeedActivity.kt index ece0e42c3b..39caab9dd6 100644 --- a/src/org/thoughtcrime/securesms/loki/SeedActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/SeedActivity.kt @@ -232,7 +232,7 @@ class SeedActivity : BaseActionBarActivity() { // Send the request to the other user CoroutineScope(Dispatchers.Main).launch { retryIfNeeded(3) { - sendAuthorisationMessage(this@SeedActivity, authorisation.primaryDevicePubKey, authorisation) + sendAuthorisationMessage(this@SeedActivity, authorisation.primaryDevicePubKey, authorisation).get() }.failUi { dialog.dismiss() resetRegistration() diff --git a/src/org/thoughtcrime/securesms/loki/Utilities.kt b/src/org/thoughtcrime/securesms/loki/Utilities.kt index 9ac5e51be3..ef78f38733 100644 --- a/src/org/thoughtcrime/securesms/loki/Utilities.kt +++ b/src/org/thoughtcrime/securesms/loki/Utilities.kt @@ -6,6 +6,7 @@ import android.os.Looper import nl.komponents.kovenant.Promise import nl.komponents.kovenant.deferred import org.thoughtcrime.securesms.ApplicationContext +import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.logging.Log import org.whispersystems.libsignal.util.guava.Optional import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair @@ -14,30 +15,30 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation fun sendAuthorisationMessage(context: Context, contactHexEncodedPublicKey: String, authorisation: LokiPairingAuthorisation): Promise { - val deferred = deferred() - Handler(Looper.getMainLooper()).post { - val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender() - val address = SignalServiceAddress(contactHexEncodedPublicKey) - // A REQUEST should always act as a friend request. A GRANT should always be replying back as a normal message. - val message = SignalServiceDataMessage.newBuilder().withBody("").withPairingAuthorisation(authorisation).asFriendRequest(authorisation.type == LokiPairingAuthorisation.Type.REQUEST).build() - try { - Log.d("Loki", "Sending authorisation message to $contactHexEncodedPublicKey") - val result = messageSender.sendMessage(0, address, Optional.absent(), message) - if (result.success == null) { - val exception = when { - result.isNetworkFailure -> "Failed to send authorisation message because of a Network Error" - else -> "Failed to send authorisation message" - } + val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender() + val address = SignalServiceAddress(contactHexEncodedPublicKey) + val message = SignalServiceDataMessage.newBuilder().withBody("").withPairingAuthorisation(authorisation) - throw Exception(exception) - } - - deferred.resolve(Unit) - } catch (e: Exception) { - Log.d("Loki", "Failed to send authorisation message to: $contactHexEncodedPublicKey.") - deferred.reject(e) - } + // A REQUEST should always act as a friend request. A GRANT should always be replying back as a normal message. + if (authorisation.type == LokiPairingAuthorisation.Type.REQUEST) { + val preKeyBundle = DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(address.number) + message.asFriendRequest(true).withPreKeyBundle(preKeyBundle) } - return deferred.promise + return try { + Log.d("Loki", "Sending authorisation message to $contactHexEncodedPublicKey") + val result = messageSender.sendMessage(0, address, Optional.absent(), message.build()) + if (result.success == null) { + val exception = when { + result.isNetworkFailure -> "Failed to send authorisation message because of a Network Error" + else -> "Failed to send authorisation message" + } + + throw Exception(exception) + } + Promise.ofSuccess(Unit) + } catch (e: Exception) { + Log.d("Loki", "Failed to send authorisation message to: $contactHexEncodedPublicKey.") + Promise.ofFail(e) + } } \ No newline at end of file