From acca4a72d36094cfb9f46c450915737983d81bc9 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 17 Feb 2017 20:43:24 -0800 Subject: [PATCH] Improve default SMS subscription id intelligence Fixes #5266 // FREEBIE --- .../securesms/ConversationActivity.java | 24 +++++++++++++++++++ .../securesms/TransportOptions.java | 5 ++-- .../database/RecipientPreferenceDatabase.java | 15 ++++++++++++ .../securesms/database/SmsDatabase.java | 4 ++++ .../dualsim/SubscriptionManagerCompat.java | 8 +++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 6c252aabfa..85a4f64d19 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -65,6 +65,9 @@ import android.widget.Toast; import com.google.android.gms.location.places.ui.PlacePicker; import com.google.protobuf.ByteString; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; import org.thoughtcrime.redphone.RedPhone; import org.thoughtcrime.redphone.RedPhoneService; import org.thoughtcrime.securesms.TransportOptions.OnTransportChangedListener; @@ -98,6 +101,7 @@ import org.thoughtcrime.securesms.database.DraftDatabase.Drafts; import org.thoughtcrime.securesms.database.GroupDatabase; import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo; import org.thoughtcrime.securesms.database.MmsSmsColumns.Types; +import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientPreferenceEvent; import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences; import org.thoughtcrime.securesms.database.ThreadDatabase; import org.thoughtcrime.securesms.jobs.MultiDeviceBlockedUpdateJob; @@ -292,6 +296,12 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity } } + @Override + protected void onStart() { + super.onStart(); + EventBus.getDefault().register(this); + } + @Override protected void onResume() { super.onResume(); @@ -325,6 +335,13 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity fragment.setLastSeen(System.currentTimeMillis()); markLastSeen(); AudioSlidePlayer.stopAll(); + EventBus.getDefault().unregister(this); + } + + @Override + protected void onStop() { + super.onStop(); + EventBus.getDefault().unregister(this); } @Override @@ -1168,6 +1185,13 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity }); } + @Subscribe(threadMode = ThreadMode.MAIN) + public void onRecipientPreferenceUpdate(final RecipientPreferenceEvent event) { + if (event.getRecipients().getSortedIdsString().equals(this.recipients.getSortedIdsString())) { + new RecipientPreferencesTask().execute(this.recipients); + } + } + private void initializeReceivers() { securityUpdateReceiver = new BroadcastReceiver() { @Override diff --git a/src/org/thoughtcrime/securesms/TransportOptions.java b/src/org/thoughtcrime/securesms/TransportOptions.java index a8aca89b3e..52219506ce 100644 --- a/src/org/thoughtcrime/securesms/TransportOptions.java +++ b/src/org/thoughtcrime/securesms/TransportOptions.java @@ -30,8 +30,9 @@ public class TransportOptions { private Optional selectedOption = Optional.absent(); public TransportOptions(Context context, boolean media) { - this.context = context; - this.enabledTransports = initializeAvailableTransports(media); + this.context = context; + this.enabledTransports = initializeAvailableTransports(media); + this.defaultSubscriptionId = new SubscriptionManagerCompat(context).getPreferredSubscriptionId(); } public void reset(boolean media) { diff --git a/src/org/thoughtcrime/securesms/database/RecipientPreferenceDatabase.java b/src/org/thoughtcrime/securesms/database/RecipientPreferenceDatabase.java index c20c3f7650..1db7cd083a 100644 --- a/src/org/thoughtcrime/securesms/database/RecipientPreferenceDatabase.java +++ b/src/org/thoughtcrime/securesms/database/RecipientPreferenceDatabase.java @@ -10,6 +10,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; +import org.greenrobot.eventbus.EventBus; import org.thoughtcrime.securesms.color.MaterialColor; import org.thoughtcrime.securesms.recipients.RecipientFactory; import org.thoughtcrime.securesms.recipients.Recipients; @@ -140,6 +141,7 @@ public class RecipientPreferenceDatabase extends Database { ContentValues values = new ContentValues(); values.put(DEFAULT_SUBSCRIPTION_ID, defaultSubscriptionId); updateOrInsert(recipients, values); + EventBus.getDefault().post(new RecipientPreferenceEvent(recipients)); } public void setBlocked(Recipients recipients, boolean blocked) { @@ -284,4 +286,17 @@ public class RecipientPreferenceDatabase extends Database { return getCurrent(); } } + + public static class RecipientPreferenceEvent { + + private final Recipients recipients; + + public RecipientPreferenceEvent(Recipients recipients) { + this.recipients = recipients; + } + + public Recipients getRecipients() { + return recipients; + } + } } diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java index 50254103e1..7dd684fae9 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -567,6 +567,10 @@ public class SmsDatabase extends MessagingDatabase { DatabaseFactory.getThreadDatabase(context).update(threadId, true); } + if (message.getSubscriptionId() != -1) { + DatabaseFactory.getRecipientPreferenceDatabase(context).setDefaultSubscriptionId(recipients, message.getSubscriptionId()); + } + notifyConversationListeners(threadId); jobManager.add(new TrimThreadJob(context, threadId)); diff --git a/src/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java b/src/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java index 8196638d7b..586182e3b0 100644 --- a/src/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java +++ b/src/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java @@ -19,6 +19,14 @@ public class SubscriptionManagerCompat { this.context = context.getApplicationContext(); } + public Optional getPreferredSubscriptionId() { + if (Build.VERSION.SDK_INT < 24) { + return Optional.absent(); + } + + return Optional.of(SubscriptionManager.getDefaultSmsSubscriptionId()); + } + public Optional getActiveSubscriptionInfo(int subscriptionId) { if (Build.VERSION.SDK_INT < 22) { return Optional.absent();