Skip to content

Commit

Permalink
allow to set proxy in InstantOnboardingActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Sep 5, 2024
1 parent 8ef136d commit 04260ee
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@
</intent-filter>
</activity>

<activity android:name=".ProxySettingsActivity"
android:label="@string/proxy_settings"
android:windowSoftInputMode="stateHidden"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>

<activity android:name=".LogViewActivity"
android:label="@string/pref_log_header"
android:windowSoftInputMode="stateHidden"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.text.TextUtils;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
Expand Down Expand Up @@ -132,13 +133,29 @@ protected void onNewIntent(Intent intent) {
handleIntent();
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getMenuInflater().inflate(R.menu.instant_onboarding_menu, menu);
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId() == android.R.id.home) {

switch (item.getItemId()) {
case android.R.id.home:
getOnBackPressedDispatcher().onBackPressed();
return true;
case R.id.menu_proxy_settings:
startActivity(new Intent(this, ProxySettingsActivity.class));
return true;
case R.id.menu_view_log:
startActivity(new Intent(this, LogViewActivity.class));
return true;
}

return false;
}

Expand Down
150 changes: 150 additions & 0 deletions src/main/java/org/thoughtcrime/securesms/ProxySettingsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package org.thoughtcrime.securesms;

import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_SOCKS5_ENABLED;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_SOCKS5_HOST;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_SOCKS5_PASSWORD;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_SOCKS5_PORT;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_SOCKS5_USER;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.SwitchCompat;
import androidx.constraintlayout.widget.Group;

import com.google.android.material.textfield.TextInputEditText;

import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.util.DynamicTheme;

public class ProxySettingsActivity extends BaseActionBarActivity {

private enum VerificationType {
SERVER,
PORT,
}

private final DynamicTheme dynamicTheme = new DynamicTheme();

private SwitchCompat proxySwitch;
private Group proxyGroup;

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
dynamicTheme.onCreate(this);
setContentView(R.layout.proxy_settings_activity);

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(R.string.proxy_settings);
actionBar.setDisplayHomeAsUpEnabled(true);
}

proxyGroup = findViewById(R.id.proxy_group);

proxySwitch = findViewById(R.id.proxy_switch);
proxySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
proxyGroup.setVisibility(isChecked? View.VISIBLE : View.GONE);
});
proxySwitch.setChecked(DcHelper.getInt(this, CONFIG_SOCKS5_ENABLED) == 1);

TextInputEditText proxyHostInput = findViewById(R.id.proxy_host_text);
proxyHostInput.setOnFocusChangeListener((view, focused) -> focusListener(view, focused, VerificationType.SERVER));
proxyHostInput.setText(DcHelper.get(this, CONFIG_SOCKS5_HOST));

TextInputEditText proxyPortInput = findViewById(R.id.proxy_port_text);
proxyPortInput.setOnFocusChangeListener((view, focused) -> focusListener(view, focused, VerificationType.PORT));
proxyPortInput.setText(DcHelper.get(this, CONFIG_SOCKS5_PORT));

TextInputEditText proxyUserInput = findViewById(R.id.proxy_user_text);
proxyUserInput.setText(DcHelper.get(this, CONFIG_SOCKS5_USER));

TextInputEditText proxyPasswordInput = findViewById(R.id.proxy_password_text);
proxyPasswordInput.setText(DcHelper.get(this, CONFIG_SOCKS5_PASSWORD));
}

@Override
public void onResume() {
super.onResume();
dynamicTheme.onResume(this);
}

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onDestroy() {
super.onDestroy();
saveConfig();
}

private void focusListener(View view, boolean focused, VerificationType type) {

if (!focused) {
TextInputEditText inputEditText = (TextInputEditText) view;
switch (type) {
case SERVER:
verifyServer(inputEditText);
break;
case PORT:
verifyPort(inputEditText);
break;
}
}
}

private void verifyServer(TextInputEditText view) {
String server = view.getText().toString();
if (!TextUtils.isEmpty(server) && !Patterns.DOMAIN_NAME.matcher(server).matches()
&& !Patterns.IP_ADDRESS.matcher(server).matches()
&& !Patterns.WEB_URL.matcher(server).matches()
&& !"localhost".equals(server)) {
view.setError(getString(R.string.login_error_server));
}
}

private void verifyPort(TextInputEditText view) {
String portString = view.getText().toString();
if (!portString.isEmpty()) {
String error = getString(R.string.login_error_port);
try {
int port = Integer.valueOf(portString);
if (port < 1 || port > 65535) {
view.setError(error);
}
} catch (NumberFormatException exception) {
view.setError(error);
}
}
}

private void saveConfig() {
DcHelper.getContext(this).setConfigInt(CONFIG_SOCKS5_ENABLED, proxySwitch.isChecked()? 1 : 0);
setConfig(R.id.proxy_host_text, CONFIG_SOCKS5_HOST, true);
setConfig(R.id.proxy_port_text, CONFIG_SOCKS5_PORT, true);
setConfig(R.id.proxy_user_text, CONFIG_SOCKS5_USER, true);
setConfig(R.id.proxy_password_text, CONFIG_SOCKS5_PASSWORD, false);
}

private void setConfig(@IdRes int viewId, String configTarget, boolean doTrim) {
TextInputEditText view = findViewById(viewId);
String value = view.getText().toString();
if(doTrim) {
value = value.trim();
}
DcHelper.getContext(this).setConfig(configTarget, value.isEmpty()? null : value);
}

}
128 changes: 128 additions & 0 deletions src/main/res/layout/proxy_settings_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".ProxySettingsActivity">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_root_start_shifted"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:orientation="vertical"
app:layout_constraintGuide_begin="14dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_root_start"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:orientation="vertical"
app:layout_constraintGuide_begin="16dp" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_root_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="16dp" />

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/proxy_switch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="10dp"
android:text="@string/proxy_use_proxy"
app:layout_constraintEnd_toEndOf="@id/guideline_root_end"
app:layout_constraintStart_toStartOf="@id/guideline_root_start"
app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Group
android:id="@+id/proxy_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="visible"
app:constraint_referenced_ids="proxy_host, proxy_port, proxy_user, proxy_password" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/proxy_host"
android:layout_width="0dp"
android:layout_height="58dp"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="@id/guideline_root_end"
app:layout_constraintStart_toStartOf="@id/guideline_root_start"
app:layout_constraintTop_toBottomOf="@id/proxy_switch">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/proxy_host_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/login_socks5_host"
android:inputType="textUri|textNoSuggestions" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/proxy_port"
android:layout_width="0dp"
android:layout_height="58dp"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="@id/guideline_root_end"
app:layout_constraintStart_toStartOf="@id/guideline_root_start"
app:layout_constraintTop_toBottomOf="@id/proxy_host">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/proxy_port_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/login_socks5_port"
android:inputType="number" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/proxy_user"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@id/guideline_root_end"
app:layout_constraintStart_toStartOf="@id/guideline_root_start"
app:layout_constraintTop_toBottomOf="@id/proxy_port">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/proxy_user_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:hint="@string/login_socks5_user" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/proxy_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@id/guideline_root_end"
app:layout_constraintStart_toStartOf="@id/guideline_root_start"
app:layout_constraintTop_toBottomOf="@id/proxy_user"
app:passwordToggleEnabled="true">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/proxy_password_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/login_socks5_password"
android:inputType="textPassword" />

</com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>
15 changes: 15 additions & 0 deletions src/main/res/menu/instant_onboarding_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:title="@string/proxy_settings"
android:id="@+id/menu_proxy_settings"
app:showAsAction="never"/>

<item android:title="@string/pref_view_log"
android:id="@+id/menu_view_log"
app:showAsAction="never"/>

</menu>
2 changes: 2 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@
<string name="login_smtp_port">SMTP Port</string>
<string name="login_smtp_security">SMTP Security</string>
<string name="login_auth_method">Authorization Method</string>
<string name="proxy_settings">Proxy Settings</string>
<string name="proxy_use_proxy">Use Proxy</string>
<!-- the word "SOCKS5" here and in the following strings should not be translated in most cases -->
<string name="login_socks5">SOCKS5</string>
<string name="login_socks5_use_socks5">Use SOCKS5</string>
Expand Down

0 comments on commit 04260ee

Please sign in to comment.