2014-07-19 02:31:03 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.Manifest;
|
2014-07-19 02:31:03 +00:00
|
|
|
import android.content.Context;
|
2016-02-06 00:10:33 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-03-11 21:23:45 +00:00
|
|
|
|
2019-04-19 15:37:26 +00:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
2016-02-06 00:10:33 +00:00
|
|
|
import org.thoughtcrime.securesms.util.CharacterCalculator;
|
2015-03-11 21:23:45 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MmsCharacterCalculator;
|
|
|
|
import org.thoughtcrime.securesms.util.PushCharacterCalculator;
|
|
|
|
import org.thoughtcrime.securesms.util.SmsCharacterCalculator;
|
2016-02-06 00:10:33 +00:00
|
|
|
import org.thoughtcrime.securesms.util.dualsim.SubscriptionInfoCompat;
|
|
|
|
import org.thoughtcrime.securesms.util.dualsim.SubscriptionManagerCompat;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2015-03-11 21:23:45 +00:00
|
|
|
|
2019-04-19 15:37:26 +00:00
|
|
|
import java.util.Iterator;
|
2014-07-19 02:31:03 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2015-03-11 21:23:45 +00:00
|
|
|
|
|
|
|
import static org.thoughtcrime.securesms.TransportOption.Type;
|
2014-07-19 02:31:03 +00:00
|
|
|
|
|
|
|
public class TransportOptions {
|
2015-03-11 21:23:45 +00:00
|
|
|
|
2014-07-19 02:31:03 +00:00
|
|
|
private static final String TAG = TransportOptions.class.getSimpleName();
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
private final List<OnTransportChangedListener> listeners = new LinkedList<>();
|
2015-02-16 10:38:09 +00:00
|
|
|
private final Context context;
|
2015-03-11 21:23:45 +00:00
|
|
|
private final List<TransportOption> enabledTransports;
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
private Type defaultTransportType = Type.SMS;
|
|
|
|
private Optional<Integer> defaultSubscriptionId = Optional.absent();
|
|
|
|
private Optional<TransportOption> selectedOption = Optional.absent();
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public TransportOptions(Context context, boolean media) {
|
2017-02-18 04:43:24 +00:00
|
|
|
this.context = context;
|
|
|
|
this.enabledTransports = initializeAvailableTransports(media);
|
|
|
|
this.defaultSubscriptionId = new SubscriptionManagerCompat(context).getPreferredSubscriptionId();
|
2015-03-11 21:23:45 +00:00
|
|
|
}
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public void reset(boolean media) {
|
|
|
|
List<TransportOption> transportOptions = initializeAvailableTransports(media);
|
2016-02-06 00:10:33 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
this.enabledTransports.clear();
|
|
|
|
this.enabledTransports.addAll(transportOptions);
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
if (selectedOption.isPresent() && !isEnabled(selectedOption.get())) {
|
|
|
|
setSelectedTransport(null);
|
2015-03-11 21:23:45 +00:00
|
|
|
} else {
|
2016-02-06 00:10:33 +00:00
|
|
|
this.defaultTransportType = Type.SMS;
|
|
|
|
this.defaultSubscriptionId = Optional.absent();
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
notifyTransportChangeListeners();
|
|
|
|
}
|
|
|
|
}
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public void setDefaultTransport(Type type) {
|
2016-02-06 00:10:33 +00:00
|
|
|
this.defaultTransportType = type;
|
|
|
|
|
|
|
|
if (!selectedOption.isPresent()) {
|
|
|
|
notifyTransportChangeListeners();
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
public void setDefaultSubscriptionId(Optional<Integer> subscriptionId) {
|
2019-01-15 08:41:05 +00:00
|
|
|
if (defaultSubscriptionId.equals(subscriptionId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
this.defaultSubscriptionId = subscriptionId;
|
|
|
|
|
|
|
|
if (!selectedOption.isPresent()) {
|
|
|
|
notifyTransportChangeListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSelectedTransport(@Nullable TransportOption transportOption) {
|
|
|
|
this.selectedOption = Optional.fromNullable(transportOption);
|
|
|
|
notifyTransportChangeListeners();
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public boolean isManualSelection() {
|
2016-02-06 00:10:33 +00:00
|
|
|
return this.selectedOption.isPresent();
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
public @NonNull TransportOption getSelectedTransport() {
|
|
|
|
if (selectedOption.isPresent()) return selectedOption.get();
|
|
|
|
|
|
|
|
if (defaultSubscriptionId.isPresent()) {
|
|
|
|
for (TransportOption transportOption : enabledTransports) {
|
|
|
|
if (transportOption.getType() == defaultTransportType &&
|
|
|
|
(int)defaultSubscriptionId.get() == transportOption.getSimSubscriptionId().or(-1))
|
|
|
|
{
|
|
|
|
return transportOption;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (TransportOption transportOption : enabledTransports) {
|
|
|
|
if (transportOption.getType() == defaultTransportType) {
|
|
|
|
return transportOption;
|
|
|
|
}
|
|
|
|
}
|
2015-03-11 21:23:45 +00:00
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
throw new AssertionError("No options of default type!");
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public void disableTransport(Type type) {
|
2019-04-19 15:37:26 +00:00
|
|
|
TransportOption selected = selectedOption.orNull();
|
2015-11-02 20:22:46 +00:00
|
|
|
|
2019-04-19 15:37:26 +00:00
|
|
|
Iterator<TransportOption> iterator = enabledTransports.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
TransportOption option = iterator.next();
|
2015-11-02 20:22:46 +00:00
|
|
|
|
2019-04-19 15:37:26 +00:00
|
|
|
if (option.isType(type)) {
|
|
|
|
if (selected == option) {
|
|
|
|
setSelectedTransport(null);
|
|
|
|
}
|
|
|
|
iterator.remove();
|
2015-11-02 20:22:46 +00:00
|
|
|
}
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public List<TransportOption> getEnabledTransports() {
|
|
|
|
return enabledTransports;
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public void addOnTransportChangedListener(OnTransportChangedListener listener) {
|
|
|
|
this.listeners.add(listener);
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
private List<TransportOption> initializeAvailableTransports(boolean isMediaMessage) {
|
2015-07-01 00:45:39 +00:00
|
|
|
List<TransportOption> results = new LinkedList<>();
|
2015-03-11 21:23:45 +00:00
|
|
|
|
|
|
|
if (isMediaMessage) {
|
2016-02-06 00:10:33 +00:00
|
|
|
results.addAll(getTransportOptionsForSimCards(context.getString(R.string.ConversationActivity_transport_insecure_mms),
|
|
|
|
context.getString(R.string.conversation_activity__type_message_mms_insecure),
|
|
|
|
new MmsCharacterCalculator()));
|
2015-03-11 21:23:45 +00:00
|
|
|
} else {
|
2016-02-06 00:10:33 +00:00
|
|
|
results.addAll(getTransportOptionsForSimCards(context.getString(R.string.ConversationActivity_transport_insecure_sms),
|
|
|
|
context.getString(R.string.conversation_activity__type_message_sms_insecure),
|
|
|
|
new SmsCharacterCalculator()));
|
2015-03-11 21:23:45 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 00:45:39 +00:00
|
|
|
results.add(new TransportOption(Type.TEXTSECURE, R.drawable.ic_send_push_white_24dp,
|
|
|
|
context.getResources().getColor(R.color.textsecure_primary),
|
2015-09-23 20:35:18 +00:00
|
|
|
context.getString(R.string.ConversationActivity_transport_signal),
|
2015-03-11 21:23:45 +00:00
|
|
|
context.getString(R.string.conversation_activity__type_message_push),
|
|
|
|
new PushCharacterCalculator()));
|
|
|
|
|
|
|
|
return results;
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
private @NonNull List<TransportOption> getTransportOptionsForSimCards(@NonNull String text,
|
|
|
|
@NonNull String composeHint,
|
|
|
|
@NonNull CharacterCalculator characterCalculator)
|
|
|
|
{
|
|
|
|
List<TransportOption> results = new LinkedList<>();
|
|
|
|
SubscriptionManagerCompat subscriptionManager = new SubscriptionManagerCompat(context);
|
2017-11-25 06:00:30 +00:00
|
|
|
List<SubscriptionInfoCompat> subscriptions;
|
|
|
|
|
|
|
|
if (Permissions.hasAll(context, Manifest.permission.READ_PHONE_STATE)) {
|
|
|
|
subscriptions = subscriptionManager.getActiveSubscriptionInfoList();
|
|
|
|
} else {
|
|
|
|
subscriptions = new LinkedList<>();
|
|
|
|
}
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
if (subscriptions.size() < 2) {
|
|
|
|
results.add(new TransportOption(Type.SMS, R.drawable.ic_send_sms_white_24dp,
|
|
|
|
context.getResources().getColor(R.color.grey_600),
|
|
|
|
text, composeHint, characterCalculator));
|
|
|
|
} else {
|
|
|
|
for (SubscriptionInfoCompat subscriptionInfo : subscriptions) {
|
|
|
|
results.add(new TransportOption(Type.SMS, R.drawable.ic_send_sms_white_24dp,
|
|
|
|
context.getResources().getColor(R.color.grey_600),
|
|
|
|
text, composeHint, characterCalculator,
|
|
|
|
Optional.of(subscriptionInfo.getDisplayName()),
|
|
|
|
Optional.of(subscriptionInfo.getSubscriptionId())));
|
|
|
|
}
|
|
|
|
}
|
2015-03-11 21:23:45 +00:00
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
return results;
|
2015-03-11 21:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyTransportChangeListeners() {
|
2015-02-16 10:38:09 +00:00
|
|
|
for (OnTransportChangedListener listener : listeners) {
|
2016-02-06 00:10:33 +00:00
|
|
|
listener.onChange(getSelectedTransport(), selectedOption.isPresent());
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
private boolean isEnabled(TransportOption transportOption) {
|
|
|
|
for (TransportOption option : enabledTransports) {
|
|
|
|
if (option.equals(transportOption)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:31:03 +00:00
|
|
|
public interface OnTransportChangedListener {
|
2016-02-06 00:10:33 +00:00
|
|
|
public void onChange(TransportOption newTransport, boolean manuallySelected);
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
}
|