diff --git a/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt b/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt index 060cdda1a9..580ade8175 100644 --- a/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt +++ b/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt @@ -10,8 +10,10 @@ import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.DatabaseFactory +import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob import org.thoughtcrime.securesms.logging.Log import org.thoughtcrime.securesms.recipients.Recipient +import org.thoughtcrime.securesms.sms.MessageSender import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.libsignal.util.guava.Optional import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair @@ -112,20 +114,24 @@ fun signAndSendPairingAuthorisationMessage(context: Context, pairingAuthorisatio Log.d("Loki", "Failed to sign pairing authorization.") return } - retryIfNeeded(8) { + DatabaseFactory.getLokiAPIDatabase(context).insertOrUpdatePairingAuthorisation(signedPairingAuthorisation) + TextSecurePreferences.setMultiDevice(context, true) + + val sendPromise = retryIfNeeded(8) { sendPairingAuthorisationMessage(context, pairingAuthorisation.secondaryDevicePublicKey, signedPairingAuthorisation) }.fail { Log.d("Loki", "Failed to send pairing authorization message to ${pairingAuthorisation.secondaryDevicePublicKey}.") } - DatabaseFactory.getLokiAPIDatabase(context).insertOrUpdatePairingAuthorisation(signedPairingAuthorisation) - TextSecurePreferences.setMultiDevice(context, true) - // Call function after a short delay - Handler().postDelayed({ - LokiStorageAPI.shared.updateUserDeviceMappings().fail { - Log.w("Loki", "Failed to update device mapping") - } - }, 100) + val updatePromise = LokiStorageAPI.shared.updateUserDeviceMappings().fail { + Log.d("Loki", "Failed to update device mapping") + } + + // If both promises complete successfully then we should sync our contacts + all(listOf(sendPromise, updatePromise), cancelOthersOnError = false).success { + Log.d("Loki", "Successfully pairing with a secondary device! Syncing contacts.") + MessageSender.sendContactSyncMessage(context) + } } fun isOneOfOurDevices(context: Context, address: Address): Promise { diff --git a/src/org/thoughtcrime/securesms/sms/MessageSender.java b/src/org/thoughtcrime/securesms/sms/MessageSender.java index 242a7722d8..3e9a62035a 100644 --- a/src/org/thoughtcrime/securesms/sms/MessageSender.java +++ b/src/org/thoughtcrime/securesms/sms/MessageSender.java @@ -35,6 +35,7 @@ import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.database.model.SmsMessageRecord; import org.thoughtcrime.securesms.jobmanager.JobManager; import org.thoughtcrime.securesms.jobs.MmsSendJob; +import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob; import org.thoughtcrime.securesms.jobs.PushGroupSendJob; import org.thoughtcrime.securesms.jobs.PushMediaSendJob; import org.thoughtcrime.securesms.jobs.PushTextSendJob; @@ -77,6 +78,14 @@ public class MessageSender { private enum MessageType { TEXT, MEDIA } + public static void sendContactSyncMessage(Context context) { + ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context, true)); + } + + public static void sendContactSyncMessage(Context context, Address address) { + ApplicationContext.getInstance(context).getJobManager().add(new MultiDeviceContactUpdateJob(context, address)); + } + public static void sendBackgroundMessageToAllDevices(Context context, String contactHexEncodedPublicKey) { // Send the background message to the original pubkey sendBackgroundMessage(context, contactHexEncodedPublicKey);