2014-07-19 02:31:03 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
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;
|
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
|
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
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
private Type selectedType;
|
|
|
|
private boolean manuallySelected;
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public TransportOptions(Context context, boolean media) {
|
|
|
|
this.context = context;
|
|
|
|
this.enabledTransports = initializeAvailableTransports(media);
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
setDefaultTransport(Type.SMS);
|
|
|
|
}
|
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);
|
|
|
|
this.enabledTransports.clear();
|
|
|
|
this.enabledTransports.addAll(transportOptions);
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
if (!find(selectedType).isPresent()) {
|
|
|
|
this.manuallySelected = false;
|
|
|
|
setTransport(Type.SMS);
|
|
|
|
} else {
|
|
|
|
notifyTransportChangeListeners();
|
|
|
|
}
|
|
|
|
}
|
2014-07-19 02:31:03 +00:00
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public void setDefaultTransport(Type type) {
|
|
|
|
if (!this.manuallySelected) {
|
|
|
|
setTransport(type);
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public void setSelectedTransport(Type type) {
|
|
|
|
this.manuallySelected= true;
|
|
|
|
setTransport(type);
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public boolean isManualSelection() {
|
|
|
|
return manuallySelected;
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public TransportOption getSelectedTransport() {
|
|
|
|
Optional<TransportOption> option = find(selectedType);
|
|
|
|
|
|
|
|
if (option.isPresent()) return option.get();
|
|
|
|
else throw new AssertionError("Selected type isn't present!");
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
public void disableTransport(Type type) {
|
|
|
|
Optional<TransportOption> option = find(type);
|
|
|
|
if (option.isPresent()) {
|
|
|
|
enabledTransports.remove(option.get());
|
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) {
|
2015-07-01 00:45:39 +00:00
|
|
|
results.add(new TransportOption(Type.SMS, R.drawable.ic_send_sms_white_24dp,
|
|
|
|
context.getResources().getColor(R.color.grey_600),
|
2015-03-11 21:23:45 +00:00
|
|
|
context.getString(R.string.ConversationActivity_transport_insecure_mms),
|
|
|
|
context.getString(R.string.conversation_activity__type_message_mms_insecure),
|
|
|
|
new MmsCharacterCalculator()));
|
|
|
|
} else {
|
2015-07-01 00:45:39 +00:00
|
|
|
results.add(new TransportOption(Type.SMS, R.drawable.ic_send_sms_white_24dp,
|
|
|
|
context.getResources().getColor(R.color.grey_600),
|
2015-03-11 21:23:45 +00:00
|
|
|
context.getString(R.string.ConversationActivity_transport_insecure_sms),
|
|
|
|
context.getString(R.string.conversation_activity__type_message_sms_insecure),
|
|
|
|
new SmsCharacterCalculator()));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
private void setTransport(Type type) {
|
|
|
|
this.selectedType = type;
|
|
|
|
|
|
|
|
notifyTransportChangeListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyTransportChangeListeners() {
|
2015-02-16 10:38:09 +00:00
|
|
|
for (OnTransportChangedListener listener : listeners) {
|
2014-07-19 02:31:03 +00:00
|
|
|
listener.onChange(getSelectedTransport());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
private Optional<TransportOption> find(Type type) {
|
|
|
|
for (TransportOption option : enabledTransports) {
|
|
|
|
if (option.isType(type)) {
|
|
|
|
return Optional.of(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Optional.absent();
|
2014-07-19 02:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnTransportChangedListener {
|
|
|
|
public void onChange(TransportOption newTransport);
|
|
|
|
}
|
|
|
|
}
|