Improve default SMS subscription id intelligence

Fixes #5266
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2017-02-17 20:43:24 -08:00
parent 1b1470aae2
commit acca4a72d3
5 changed files with 54 additions and 2 deletions

View File

@ -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

View File

@ -32,6 +32,7 @@ public class TransportOptions {
public TransportOptions(Context context, boolean media) {
this.context = context;
this.enabledTransports = initializeAvailableTransports(media);
this.defaultSubscriptionId = new SubscriptionManagerCompat(context).getPreferredSubscriptionId();
}
public void reset(boolean media) {

View File

@ -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;
}
}
}

View File

@ -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));

View File

@ -19,6 +19,14 @@ public class SubscriptionManagerCompat {
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();