Skip to content

Commit

Permalink
Clean up several UX interactions with proxy entry.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Feb 3, 2021
1 parent e798f3f commit 64fe78f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 16 deletions.
Expand Up @@ -3,6 +3,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.internal.configuration.SignalProxy;

public final class ProxyValues extends SignalStoreValues {
Expand All @@ -19,8 +20,11 @@ public final class ProxyValues extends SignalStoreValues {
void onFirstEverAppLaunch() {
}


public void enableProxy(@NonNull SignalProxy proxy) {
if (Util.isEmpty(proxy.getHost())) {
throw new IllegalArgumentException("Empty host!");
}

getStore().beginWrite()
.putBoolean(KEY_PROXY_ENABLED, true)
.putString(KEY_HOST, proxy.getHost())
Expand Down Expand Up @@ -67,4 +71,9 @@ public void setProxy(@Nullable SignalProxy proxy) {
return null;
}
}

public @Nullable String getProxyHost() {
SignalProxy proxy = getProxy();
return proxy != null ? proxy.getHost() : null;
}
}
Expand Up @@ -20,9 +20,12 @@
import com.dd.CircularProgressButton;

import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.contactshare.SimpleTextWatcher;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.net.PipeConnectivityListener;
import org.thoughtcrime.securesms.util.SignalProxyUtil;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.ViewUtil;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.internal.configuration.SignalProxy;

Expand Down Expand Up @@ -54,6 +57,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
this.saveButton = view.findViewById(R.id.edit_proxy_save);
this.shareButton = view.findViewById(R.id.edit_proxy_share);

proxyText.addTextChangedListener(new SimpleTextWatcher() {
@Override
public void onTextChanged(String text) {
onProxyTextChanged(text);
}
});

this.proxyText.setText(Optional.fromNullable(SignalStore.proxy().getProxy()).transform(SignalProxy::getHost).or(""));
this.proxySwitch.setChecked(SignalStore.proxy().isProxyEnabled());

Expand Down Expand Up @@ -87,12 +97,8 @@ private void presentUiState(@NonNull EditProxyViewModel.UiState uiState) {
case ALL_ENABLED:
proxyText.setEnabled(true);
proxyText.setAlpha(1);
saveButton.setEnabled(true);
saveButton.setAlpha(1);
shareButton.setEnabled(true);
shareButton.setAlpha(1);
proxyTitle.setAlpha(1);
proxyStatus.setVisibility(View.VISIBLE);
onProxyTextChanged(proxyText.getText().toString());
break;
case ALL_DISABLED:
proxyText.setEnabled(false);
Expand Down Expand Up @@ -137,12 +143,16 @@ private void presentEvent(@NonNull EditProxyViewModel.Event event) {
new AlertDialog.Builder(requireContext())
.setTitle(R.string.preferences_success)
.setMessage(R.string.preferences_you_are_connected_to_the_proxy)
.setPositiveButton(android.R.string.ok, (d, i) -> d.dismiss())
.setPositiveButton(android.R.string.ok, (d, i) -> {
d.dismiss();
requireActivity().onBackPressed();
})
.show();
break;
case PROXY_FAILURE:
proxyStatus.setVisibility(View.INVISIBLE);
proxyText.setText(Optional.fromNullable(SignalStore.proxy().getProxy()).transform(SignalProxy::getHost).or(""));
ViewUtil.focusAndMoveCursorToEndAndOpenKeyboard(proxyText);
new AlertDialog.Builder(requireContext())
.setTitle(R.string.preferences_failed_to_connect)
.setMessage(R.string.preferences_couldnt_connect_to_the_proxy)
Expand Down Expand Up @@ -172,10 +182,32 @@ private void onSaveClicked() {
}

private void onShareClicked() {
String host = proxyText.getText().toString();
String link = SignalProxyUtil.generateProxyUrl(proxyText.getText().toString());
ShareCompat.IntentBuilder.from(requireActivity())
.setText(host)
.setText(link)
.setType("text/plain")
.startChooser();
}

private void onProxyTextChanged(@NonNull String text) {
if (Util.isEmpty(text)) {
saveButton.setEnabled(false);
saveButton.setAlpha(0.5f);
shareButton.setEnabled(false);
shareButton.setAlpha(0.5f);
proxyStatus.setVisibility(View.INVISIBLE);
} else {
saveButton.setEnabled(true);
saveButton.setAlpha(1);
shareButton.setEnabled(true);
shareButton.setAlpha(1);

String trueHost = SignalProxyUtil.convertUserEnteredAddressToHost(proxyText.getText().toString());
if (SignalStore.proxy().isProxyEnabled() && trueHost.equals(SignalStore.proxy().getProxyHost())) {
proxyStatus.setVisibility(View.VISIBLE);
} else {
proxyStatus.setVisibility(View.INVISIBLE);
}
}
}
}
Expand Up @@ -12,6 +12,7 @@
import org.thoughtcrime.securesms.util.SignalProxyUtil;
import org.thoughtcrime.securesms.util.SingleLiveEvent;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.internal.configuration.SignalProxy;

import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -41,7 +42,7 @@ void onToggleProxy(boolean enabled) {
if (enabled) {
SignalProxy currentProxy = SignalStore.proxy().getProxy();

if (currentProxy != null) {
if (currentProxy != null && !Util.isEmpty(currentProxy.getHost())) {
SignalProxyUtil.enableProxy(currentProxy);
}
uiState.postValue(UiState.ALL_ENABLED);
Expand All @@ -52,8 +53,7 @@ void onToggleProxy(boolean enabled) {
}

public void onSaveClicked(@NonNull String host) {
String parsedHost = SignalProxyUtil.parseHostFromProxyLink(host);
String trueHost = parsedHost != null ? parsedHost : host;
String trueHost = SignalProxyUtil.convertUserEnteredAddressToHost(host);

saveState.postValue(SaveState.IN_PROGRESS);

Expand Down
Expand Up @@ -243,7 +243,9 @@ public SignalServiceConfiguration getConfiguration(Context context) {
}

public SignalServiceConfiguration getConfiguration(@Nullable String localNumber) {
if (localNumber == null) return this.uncensoredConfiguration;
if (localNumber == null || SignalStore.proxy().isProxyEnabled()) {
return this.uncensoredConfiguration;
}

if (SignalStore.internalValues().forcedCensorship()) {
return this.censorshipConfiguration.get(COUNTRY_CODE_QATAR);
Expand Down
Expand Up @@ -213,7 +213,7 @@ public static void handleGroupLinkUrl(@NonNull FragmentActivity activity,
* Otherwise returns false, indicating was not a proxy link.
*/
public static boolean handlePotentialProxyLinkUrl(@NonNull FragmentActivity activity, @NonNull String potentialProxyLinkUrl) {
String proxy = SignalProxyUtil.parseHostFromProxyLink(potentialProxyLinkUrl);
String proxy = SignalProxyUtil.parseHostFromProxyDeepLink(potentialProxyLinkUrl);

if (proxy != null) {
ProxyBottomSheetFragment.showForProxy(activity.getSupportFragmentManager(), proxy);
Expand Down
Expand Up @@ -100,10 +100,10 @@ public static boolean testWebsocketConnection(long timeout) {
}

/**
* If this is a valid proxy link, this will return the embedded host. If not, it will return
* If this is a valid proxy deep link, this will return the embedded host. If not, it will return
* null.
*/
public static @Nullable String parseHostFromProxyLink(@NonNull String proxyLink) {
public static @Nullable String parseHostFromProxyDeepLink(@NonNull String proxyLink) {
try {
URI uri = new URI(proxyLink);

Expand All @@ -130,4 +130,24 @@ public static boolean testWebsocketConnection(long timeout) {
return null;
}
}

/**
* Takes in an address that could be in various formats, and converts it to the format we should
* be storing and connecting to.
*/
public static @NonNull String convertUserEnteredAddressToHost(@NonNull String host) {
String parsedHost = SignalProxyUtil.parseHostFromProxyDeepLink(host);
return parsedHost != null ? parsedHost : host;
}

public static @NonNull String generateProxyUrl(@NonNull String link) {
String host = link;
String parsed = parseHostFromProxyDeepLink(link);

if (parsed != null) {
host = parsed;
}

return "https://" + PROXY_LINK_HOST + "/#" + host;
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/layout/edit_proxy_fragment.xml
Expand Up @@ -40,6 +40,8 @@
android:id="@+id/edit_proxy_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:hint="@string/preferences_enter_proxy_address"
app:layout_constraintTop_toBottomOf="@id/edit_proxy_address_title"
tools:hint="https://proxy.parker.org"/>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -2325,6 +2325,7 @@
<string name="preferences_you_are_connected_to_the_proxy">You are connected to the proxy. You can turn the proxy off at any time from Settings.</string>
<string name="preferences_success">Success</string>
<string name="preferences_failed_to_connect">Failed to connect</string>
<string name="preferences_enter_proxy_address">Enter proxy address</string>


<string name="configurable_single_select__customize_option">Customize option</string>
Expand Down

0 comments on commit 64fe78f

Please sign in to comment.