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