Skip to content

Commit

Permalink
Improve default SMS subscription id intelligence
Browse files Browse the repository at this point in the history
Fixes signalapp#5266
// FREEBIE
  • Loading branch information
moxie0 committed Feb 19, 2017
1 parent 1b1470a commit acca4a7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/org/thoughtcrime/securesms/ConversationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
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;
Expand Down Expand Up @@ -98,6 +101,7 @@
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;
Expand Down Expand Up @@ -292,6 +296,12 @@ public void onSuccess(Boolean result) {
}
}

@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}

@Override
protected void onResume() {
super.onResume();
Expand Down Expand Up @@ -325,6 +335,13 @@ protected void onPause() {
fragment.setLastSeen(System.currentTimeMillis());
markLastSeen();
AudioSlidePlayer.stopAll();
EventBus.getDefault().unregister(this);
}

@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}

@Override
Expand Down Expand Up @@ -1168,6 +1185,13 @@ public void run() {
});
}

@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
Expand Down
5 changes: 3 additions & 2 deletions src/org/thoughtcrime/securesms/TransportOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public class TransportOptions {
private Optional<TransportOption> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -140,6 +141,7 @@ public void setDefaultSubscriptionId(@NonNull Recipients recipients, int defaul
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) {
Expand Down Expand Up @@ -284,4 +286,17 @@ public BlockedReader(Context context, Cursor cursor) {
return getCurrent();
}
}

public static class RecipientPreferenceEvent {

private final Recipients recipients;

public RecipientPreferenceEvent(Recipients recipients) {
this.recipients = recipients;
}

public Recipients getRecipients() {
return recipients;
}
}
}
4 changes: 4 additions & 0 deletions src/org/thoughtcrime/securesms/database/SmsDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,10 @@ protected Optional<InsertResult> insertMessageInbox(IncomingTextMessage message,
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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public SubscriptionManagerCompat(Context context) {
this.context = context.getApplicationContext();
}

public Optional<Integer> getPreferredSubscriptionId() {
if (Build.VERSION.SDK_INT < 24) {
return Optional.absent();
}

return Optional.of(SubscriptionManager.getDefaultSmsSubscriptionId());
}

public Optional<SubscriptionInfoCompat> getActiveSubscriptionInfo(int subscriptionId) {
if (Build.VERSION.SDK_INT < 22) {
return Optional.absent();
Expand Down

0 comments on commit acca4a7

Please sign in to comment.