Allows to define the proxy

This commit is contained in:
stom79 2018-01-20 09:46:28 +01:00
parent 6565d4b2f0
commit d695dfc692
10 changed files with 143 additions and 13 deletions

View File

@ -178,6 +178,9 @@
<activity android:name=".activities.InstanceHealthActivity"
android:theme="@style/Base.V7.Theme.AppCompat.Dialog"
android:excludeFromRecents="true"/>
<activity android:name=".activities.ProxyActivity"
android:theme="@style/Base.V7.Theme.AppCompat.Dialog"
android:excludeFromRecents="true"/>
<activity android:name=".activities.ManageAccountsInListActivity"
android:windowSoftInputMode="adjustPan"
android:theme="@style/Base.V7.Theme.AppCompat.Dialog"

View File

@ -740,6 +740,10 @@ public abstract class BaseMainActivity extends BaseActivity
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return true;
case R.id.action_proxy:
intent = new Intent(getApplicationContext(), ProxyActivity.class);
startActivity(intent);
return true;
case R.id.action_export:
if(Build.VERSION.SDK_INT >= 23 ){
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {

View File

@ -390,6 +390,9 @@ public class LoginActivity extends BaseActivity {
}else if(id == R.id.action_privacy){
Intent intent = new Intent(getApplicationContext(), PrivacyActivity.class);
startActivity(intent);
}else if(id == R.id.action_proxy){
Intent intent = new Intent(getApplicationContext(), ProxyActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}

View File

@ -16,11 +16,28 @@ package fr.gouv.etalab.mastodon.activities;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import java.lang.reflect.Proxy;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.helper.Helper;
import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
/**
@ -30,7 +47,7 @@ import fr.gouv.etalab.mastodon.R;
public class ProxyActivity extends BaseActivity {
private int count2;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -43,6 +60,70 @@ public class ProxyActivity extends BaseActivity {
if( getSupportActionBar() != null)
getSupportActionBar().hide();
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
//Enable proxy
boolean enable_proxy = sharedpreferences.getBoolean(Helper.SET_PROXY_ENABLED, false);
final CheckBox set_enable_proxy = findViewById(R.id.enable_proxy);
set_enable_proxy.setChecked(enable_proxy);
set_enable_proxy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SET_PROXY_ENABLED, set_enable_proxy.isChecked());
editor.apply();
}
});
Button save = findViewById(R.id.set_proxy_save);
final EditText host = findViewById(R.id.host);
final EditText port = findViewById(R.id.port);
final EditText proxy_login = findViewById(R.id.proxy_login);
final EditText proxy_password = findViewById(R.id.proxy_password);
count2 = 0;
final Spinner proxy_type = findViewById(R.id.type);
ArrayAdapter<CharSequence> adapterTrans = ArrayAdapter.createFromResource(ProxyActivity.this,
R.array.proxy_type, android.R.layout.simple_spinner_item);
proxy_type.setAdapter(adapterTrans);
proxy_type.setSelection(sharedpreferences.getInt(Helper.SET_PROXY_TYPE, 0));
proxy_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if( count2 > 0){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt(Helper.SET_PROXY_TYPE, position);
editor.apply();
}else {
count2++;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String hostVal = host.getText().toString().trim();
String portVal = port.getText().toString().trim();
String proxy_loginVal = proxy_login.getText().toString().trim();
String proxy_passwordVal = proxy_password.getText().toString().trim();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SET_PROXY_HOST, hostVal);
editor.putInt(Helper.SET_PROXY_PORT, Integer.parseInt(portVal));
editor.putString(Helper.SET_PROXY_LOGIN, proxy_loginVal);
editor.putString(Helper.SET_PROXY_PASSWORD, proxy_passwordVal);
editor.apply();
finish();
}
});
}
@Override

View File

@ -78,11 +78,15 @@ public class HttpsConnection {
this.context = context;
sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean proxyEnabled = sharedpreferences.getBoolean(Helper.SET_PROXY_ENABLED, false);
int type = sharedpreferences.getInt(Helper.SET_PROXY_TYPE, 0);
proxy = null;
if( proxyEnabled ){
String host = sharedpreferences.getString(Helper.SET_PROXY_HOST, "127.0.0.1");
int port = sharedpreferences.getInt(Helper.SET_PROXY_PORT, 8118);
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
if( type == 0 )
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
else
proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port));
final String login = sharedpreferences.getString(Helper.SET_PROXY_LOGIN, null);
final String pwd = sharedpreferences.getString(Helper.SET_PROXY_PASSWORD, null);
if( login != null) {

View File

@ -271,6 +271,7 @@ public class Helper {
//Proxy
public static final String SET_PROXY_ENABLED = "set_proxy_enabled";
public static final String SET_PROXY_TYPE = "set_proxy_type";
public static final String SET_PROXY_HOST = "set_proxy_host";
public static final String SET_PROXY_PORT = "set_proxy_port";
public static final String SET_PROXY_LOGIN = "set_proxy_login";

View File

@ -10,12 +10,23 @@
android:layout_width="match_parent"
android:layout_height="300dp">
<CheckBox
android:id="@+id/enable_proxy"
android:checked="false"
android:text="@string/proxy_enable"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/enable_proxy"
android:checked="false"
android:text="@string/proxy_enable"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/type"
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -46,4 +57,14 @@
android:hint="@string/poxy_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="5dp"
android:id="@+id/set_proxy_save"
android:layout_gravity="center"
android:gravity="center"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:maxWidth="150dp"
android:text="@string/set_save_changes"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@ -25,6 +25,10 @@
android:id="@+id/action_export"
android:title="@string/data_export"
app:showAsAction="never" />
<item
android:id="@+id/action_proxy"
android:title="@string/proxy_set"
app:showAsAction="never" />
<item
android:id="@+id/action_logout"
android:title="@string/action_logout"

View File

@ -9,4 +9,8 @@
android:id="@+id/action_privacy"
android:title="@string/action_privacy"
app:showAsAction="never" />
<item
android:id="@+id/action_proxy"
android:title="@string/proxy_set"
app:showAsAction="never" />
</menu>

View File

@ -475,11 +475,16 @@
<string name="data_export_toots">Export statuses for %1$s</string>
<string name="data_export_success">%1$s toots out of %2$s have been exported.</string>
<string name="data_export_error">Something went wrong when exporting data for %1$s</string>
<!-- Proxy -->
<string name="proxy_set">Proxy</string>
<string name="proxy_type">Type</string>
<string name="proxy_enable">Enable proxy?</string>
<string name="poxy_host">host</string>
<string name="poxy_port">port</string>
<string name="poxy_login">login</string>
<string name="poxy_password">password</string>
<string name="poxy_host">Host</string>
<string name="poxy_port">Port</string>
<string name="poxy_login">Login</string>
<string name="poxy_password">Password</string>
<string-array translatable="false" name="proxy_type">
<item>HTTP</item>
<item>SOCKS</item>
</string-array>
</resources>