-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to set proxy in InstantOnboardingActivity
- Loading branch information
Showing
6 changed files
with
318 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/main/java/org/thoughtcrime/securesms/ProxySettingsActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters