From 98cfd93b97f291eff16c674e9a40ac22c9b4630f Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 24 Oct 2019 14:26:54 +1100 Subject: [PATCH] Enable note to self. Fix note to self crashing. --- .../securesms/components/AvatarImageView.java | 10 ++++++++-- .../conversation/ConversationActivity.java | 3 ++- .../thoughtcrime/securesms/jobs/PushDecryptJob.java | 13 ++++++++++++- .../securesms/loki/MultiDeviceUtilities.kt | 9 +++++++++ .../securesms/loki/NewConversationActivity.kt | 5 +++-- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/org/thoughtcrime/securesms/components/AvatarImageView.java b/src/org/thoughtcrime/securesms/components/AvatarImageView.java index 9158ab0689..6eae6b42aa 100644 --- a/src/org/thoughtcrime/securesms/components/AvatarImageView.java +++ b/src/org/thoughtcrime/securesms/components/AvatarImageView.java @@ -107,8 +107,9 @@ public class AvatarImageView extends AppCompatImageView { if (w == 0 || h == 0 || recipient == null) { return; } Drawable image; + Context context = this.getContext(); if (recipient.isGroupRecipient()) { - Context context = this.getContext(); + String name = Optional.fromNullable(recipient.getName()).or(Optional.fromNullable(TextSecurePreferences.getProfileName(context))).or(""); MaterialColor fallbackColor = recipient.getColor(); @@ -119,7 +120,12 @@ public class AvatarImageView extends AppCompatImageView { image = new GeneratedContactPhoto(name, R.drawable.ic_profile_default).asDrawable(context, fallbackColor.toAvatarColor(context)); } else { - image = new JazzIdenticonDrawable(w, h, recipient.getAddress().serialize().toLowerCase()); + // Default to primary device image + String ourPublicKey = TextSecurePreferences.getLocalNumber(context); + String ourPrimaryDevice = TextSecurePreferences.getMasterHexEncodedPublicKey(context); + String recipientAddress = recipient.getAddress().serialize(); + String profileAddress = (ourPrimaryDevice != null && ourPublicKey.equals(recipientAddress)) ? ourPrimaryDevice : recipientAddress; + image = new JazzIdenticonDrawable(w, h, profileAddress.toLowerCase()); } setImageDrawable(image); } diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java index 1c5291450f..f09cf1d7aa 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -722,7 +722,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity if (isSingleConversation() && getRecipient().getContactUri() == null) { inflater.inflate(R.menu.conversation_add_to_contacts, menu); } - */ + if (recipient != null && recipient.isLocalNumber()) { if (isSecureText) menu.findItem(R.id.menu_call_secure).setVisible(false); @@ -734,6 +734,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity muteItem.setVisible(false); } } + */ searchViewItem = menu.findItem(R.id.menu_search); diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 209d934aef..f5604b331d 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -305,6 +305,12 @@ public class PushDecryptJob extends BaseJob implements InjectableType { Optional rawSenderDisplayName = content.senderDisplayName; if (rawSenderDisplayName.isPresent() && rawSenderDisplayName.get().length() > 0) { setDisplayName(envelope.getSource(), rawSenderDisplayName.get()); + + // If we got a name from our primary device then we also set that + String ourPrimaryDevice = TextSecurePreferences.getMasterHexEncodedPublicKey(context); + if (ourPrimaryDevice != null && envelope.getSource().equals(ourPrimaryDevice)) { + TextSecurePreferences.setProfileName(context, rawSenderDisplayName.get()); + } } // TODO: Deleting the display name @@ -1526,7 +1532,12 @@ public class PushDecryptJob extends BaseJob implements InjectableType { // Get the primary device LokiStorageAPI.shared.getPrimaryDevicePublicKey(contentSender).success(primaryDevice -> { - String publicKey = primaryDevice == null ? contentSender : primaryDevice; + String publicKey = (primaryDevice != null) ? primaryDevice : contentSender; + // If our the public key matches our primary device then we need to forward the message to ourselves (Note to self) + String ourPrimaryDevice = TextSecurePreferences.getMasterHexEncodedPublicKey(context); + if (ourPrimaryDevice != null && ourPrimaryDevice.equals(publicKey)) { + publicKey = TextSecurePreferences.getLocalNumber(context); + } device.set(publicKey); return Unit.INSTANCE; }).fail(exception -> { diff --git a/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt b/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt index 56f534b27b..2410f49d76 100644 --- a/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt +++ b/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt @@ -58,9 +58,11 @@ fun shouldAutomaticallyBecomeFriendsWithDevice(publicKey: String, context: Conte val future = SettableFuture() storageAPI.getPrimaryDevicePublicKey(publicKey).success { primaryDevicePublicKey -> if (primaryDevicePublicKey == null) { + // If the public key doesn't have any other devices then go through regular friend request logic future.set(false) return@success } + // If we are the primary device and the public key is our secondary device then we should become friends val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) if (primaryDevicePublicKey == userHexEncodedPublicKey) { storageAPI.getSecondaryDevicePublicKeys(userHexEncodedPublicKey).success { secondaryDevices -> @@ -70,6 +72,13 @@ fun shouldAutomaticallyBecomeFriendsWithDevice(publicKey: String, context: Conte } return@success } + // If we share the same primary device then we should become friends + val ourPrimaryDevice = TextSecurePreferences.getMasterHexEncodedPublicKey(context) + if (ourPrimaryDevice != null && ourPrimaryDevice == primaryDevicePublicKey) { + future.set(true) + return@success + } + // If we are friends with the primary device then we should become friends val primaryDevice = Recipient.from(context, Address.fromSerialized(primaryDevicePublicKey), false) val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(primaryDevice) if (threadID < 0) { diff --git a/src/org/thoughtcrime/securesms/loki/NewConversationActivity.kt b/src/org/thoughtcrime/securesms/loki/NewConversationActivity.kt index bacbdaa5f4..16be98fd73 100644 --- a/src/org/thoughtcrime/securesms/loki/NewConversationActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/NewConversationActivity.kt @@ -68,8 +68,9 @@ class NewConversationActivity : PassphraseRequiredActionBarActivity(), ScanListe fun startNewConversationIfPossible(hexEncodedPublicKey: String) { if (!PublicKeyValidation.isValid(hexEncodedPublicKey)) { return Toast.makeText(this, R.string.fragment_new_conversation_invalid_public_key_message, Toast.LENGTH_SHORT).show() } val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this) - if (hexEncodedPublicKey == userHexEncodedPublicKey) { return Toast.makeText(this, R.string.fragment_new_conversation_note_to_self_not_supported_message, Toast.LENGTH_SHORT).show() } - val contact = Recipient.from(this, Address.fromSerialized(hexEncodedPublicKey), true) + // If we try to contact our master then redirect to note to self + val contactPublicKey = if (TextSecurePreferences.getMasterHexEncodedPublicKey(this) == hexEncodedPublicKey) userHexEncodedPublicKey else hexEncodedPublicKey + val contact = Recipient.from(this, Address.fromSerialized(contactPublicKey), true) val intent = Intent(this, ConversationActivity::class.java) intent.putExtra(ConversationActivity.ADDRESS_EXTRA, contact.address) intent.putExtra(ConversationActivity.TEXT_EXTRA, getIntent().getStringExtra(ConversationActivity.TEXT_EXTRA))