fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/ProxyActivity.java

147 lines
5.8 KiB
Java
Raw Normal View History

2018-01-19 18:20:31 +01:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-01-19 18:20:31 +01:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2018-01-19 18:20:31 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2018-01-19 18:20:31 +01:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2018-01-19 18:20:31 +01:00
2018-01-20 09:46:28 +01:00
import android.content.Context;
import android.content.SharedPreferences;
2018-01-19 18:20:31 +01:00
import android.os.Bundle;
import android.view.MenuItem;
2018-01-20 09:46:28 +01:00
import android.view.View;
2018-01-19 18:20:31 +01:00
import android.view.ViewGroup;
2018-01-20 09:46:28 +01:00
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.helper.Helper;
2018-01-20 09:46:28 +01:00
2018-01-19 18:20:31 +01:00
/**
* Created by Thomas on 19/01/2018.
* Proxy activity class
*/
public class ProxyActivity extends BaseActivity {
2018-01-20 09:46:28 +01:00
private int count2;
2018-01-19 18:20:31 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2018-05-11 14:26:15 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-11-07 19:51:53 +01:00
if (theme == Helper.THEME_LIGHT) {
2019-11-07 19:07:19 +01:00
setTheme(R.style.Dialog);
2019-11-07 19:51:53 +01:00
} else {
2019-11-07 19:07:19 +01:00
setTheme(R.style.DialogDark);
2018-05-11 14:26:15 +02:00
}
2018-01-19 18:20:31 +01:00
setContentView(R.layout.activity_proxy);
getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2018-01-19 18:20:31 +01:00
getSupportActionBar().hide();
2018-01-20 09:46:28 +01:00
//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);
2018-01-20 10:20:16 +01:00
String hostVal = sharedpreferences.getString(Helper.SET_PROXY_HOST, "127.0.0.1");
int portVal = sharedpreferences.getInt(Helper.SET_PROXY_PORT, 8118);
final String login = sharedpreferences.getString(Helper.SET_PROXY_LOGIN, null);
final String pwd = sharedpreferences.getString(Helper.SET_PROXY_PASSWORD, null);
2019-09-06 17:55:14 +02:00
if (hostVal.length() > 0)
2018-01-20 10:20:16 +01:00
host.setText(hostVal);
port.setText(String.valueOf(portVal));
2019-09-06 17:55:14 +02:00
if (login != null && login.length() > 0)
2018-01-20 10:20:16 +01:00
proxy_login.setText(login);
2019-09-06 17:55:14 +02:00
if (pwd != null && proxy_password.length() > 0)
2018-01-20 10:20:16 +01:00
proxy_password.setText(pwd);
2018-01-20 09:46:28 +01:00
count2 = 0;
final Spinner proxy_type = findViewById(R.id.type);
ArrayAdapter<CharSequence> adapterTrans = ArrayAdapter.createFromResource(ProxyActivity.this,
2019-11-20 11:57:25 +01:00
R.array.proxy_type_choice, android.R.layout.simple_spinner_dropdown_item);
2018-01-20 09:46:28 +01:00
proxy_type.setAdapter(adapterTrans);
2018-01-20 10:20:16 +01:00
2018-01-20 09:46:28 +01:00
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) {
2019-09-06 17:55:14 +02:00
if (count2 > 0) {
2018-01-20 09:46:28 +01:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt(Helper.SET_PROXY_TYPE, position);
editor.apply();
2019-09-06 17:55:14 +02:00
} else {
2018-01-20 09:46:28 +01:00
count2++;
}
}
2019-09-06 17:55:14 +02:00
2018-01-20 09:46:28 +01:00
@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);
2019-09-06 17:55:14 +02:00
if (portVal.matches("\\d+"))
2018-08-30 17:49:26 +02:00
editor.putInt(Helper.SET_PROXY_PORT, Integer.parseInt(portVal));
2018-01-20 09:46:28 +01:00
editor.putString(Helper.SET_PROXY_LOGIN, proxy_loginVal);
editor.putString(Helper.SET_PROXY_PASSWORD, proxy_passwordVal);
editor.apply();
finish();
}
});
2018-01-19 18:20:31 +01:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}