2014-07-18 19:31:03 -07:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-09-20 13:27:18 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2019-05-22 14:02:21 -03:00
|
|
|
import android.support.v7.widget.AppCompatImageButton;
|
2014-07-18 19:31:03 -07:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.TransportOption;
|
|
|
|
import org.thoughtcrime.securesms.TransportOptions;
|
|
|
|
import org.thoughtcrime.securesms.TransportOptions.OnTransportChangedListener;
|
2015-03-11 14:23:45 -07:00
|
|
|
import org.thoughtcrime.securesms.TransportOptionsPopup;
|
2015-11-26 19:04:27 +01:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2014-07-18 19:31:03 -07:00
|
|
|
|
2019-05-22 14:02:21 -03:00
|
|
|
public class SendButton extends AppCompatImageButton
|
2015-03-11 14:23:45 -07:00
|
|
|
implements TransportOptions.OnTransportChangedListener,
|
|
|
|
TransportOptionsPopup.SelectedListener,
|
|
|
|
View.OnLongClickListener
|
|
|
|
{
|
|
|
|
|
2015-07-22 14:21:27 -07:00
|
|
|
private final TransportOptions transportOptions;
|
|
|
|
|
|
|
|
private Optional<TransportOptionsPopup> transportOptionsPopup = Optional.absent();
|
2014-07-18 19:31:03 -07:00
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public SendButton(Context context) {
|
|
|
|
super(context);
|
2015-07-22 14:21:27 -07:00
|
|
|
this.transportOptions = initializeTransportOptions(false);
|
2015-11-26 19:04:27 +01:00
|
|
|
ViewUtil.mirrorIfRtl(this, getContext());
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public SendButton(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
2015-07-22 14:21:27 -07:00
|
|
|
this.transportOptions = initializeTransportOptions(false);
|
2015-11-26 19:04:27 +01:00
|
|
|
ViewUtil.mirrorIfRtl(this, getContext());
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public SendButton(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
2015-07-22 14:21:27 -07:00
|
|
|
this.transportOptions = initializeTransportOptions(false);
|
2015-11-26 19:04:27 +01:00
|
|
|
ViewUtil.mirrorIfRtl(this, getContext());
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
private TransportOptions initializeTransportOptions(boolean media) {
|
2019-03-28 15:04:38 -03:00
|
|
|
if (isInEditMode()) return null;
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
TransportOptions transportOptions = new TransportOptions(getContext(), media);
|
|
|
|
transportOptions.addOnTransportChangedListener(this);
|
|
|
|
|
|
|
|
setOnLongClickListener(this);
|
|
|
|
|
|
|
|
return transportOptions;
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
2015-07-22 14:21:27 -07:00
|
|
|
private TransportOptionsPopup getTransportOptionsPopup() {
|
|
|
|
if (!transportOptionsPopup.isPresent()) {
|
|
|
|
transportOptionsPopup = Optional.of(new TransportOptionsPopup(getContext(), this, this));
|
|
|
|
}
|
|
|
|
return transportOptionsPopup.get();
|
2015-03-11 14:23:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isManualSelection() {
|
|
|
|
return transportOptions.isManualSelection();
|
2015-02-16 02:38:09 -08:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
public void addOnTransportChangedListener(OnTransportChangedListener listener) {
|
|
|
|
transportOptions.addOnTransportChangedListener(listener);
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public TransportOption getSelectedTransport() {
|
|
|
|
return transportOptions.getSelectedTransport();
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
public void resetAvailableTransports(boolean isMediaMessage) {
|
|
|
|
transportOptions.reset(isMediaMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void disableTransport(TransportOption.Type type) {
|
|
|
|
transportOptions.disableTransport(type);
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
public void setDefaultTransport(TransportOption.Type type) {
|
|
|
|
transportOptions.setDefaultTransport(type);
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
2018-09-20 13:27:18 -07:00
|
|
|
public void setTransport(@NonNull TransportOption option) {
|
|
|
|
transportOptions.setSelectedTransport(option);
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
public void setDefaultSubscriptionId(Optional<Integer> subscriptionId) {
|
|
|
|
transportOptions.setDefaultSubscriptionId(subscriptionId);
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
@Override
|
|
|
|
public void onSelected(TransportOption option) {
|
2016-02-05 16:10:33 -08:00
|
|
|
transportOptions.setSelectedTransport(option);
|
2015-07-22 14:21:27 -07:00
|
|
|
getTransportOptionsPopup().dismiss();
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
@Override
|
2016-02-05 16:10:33 -08:00
|
|
|
public void onChange(TransportOption newTransport, boolean isManualSelection) {
|
2015-03-11 14:23:45 -07:00
|
|
|
setImageResource(newTransport.getDrawable());
|
|
|
|
setContentDescription(newTransport.getDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View v) {
|
2018-11-20 09:59:23 -08:00
|
|
|
if (isEnabled() && transportOptions.getEnabledTransports().size() > 1) {
|
2015-07-22 14:21:27 -07:00
|
|
|
getTransportOptionsPopup().display(transportOptions.getEnabledTransports());
|
2015-03-11 14:23:45 -07:00
|
|
|
return true;
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
2015-03-11 14:23:45 -07:00
|
|
|
|
|
|
|
return false;
|
2014-07-18 19:31:03 -07:00
|
|
|
}
|
|
|
|
}
|