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

165 lines
6.7 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;
2019-06-11 19:38:26 +02:00
import androidx.core.content.ContextCompat;
2018-01-19 18:20:31 +01:00
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;
import android.view.Window;
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.helper.Helper;
import app.fedilab.android.R;
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);
requestWindowFeature(Window.FEATURE_NO_TITLE);
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);
switch (theme){
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme_NoActionBar);
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(ProxyActivity.this, R.color.mastodonC3__));
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark_NoActionBar);
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(ProxyActivity.this, R.color.mastodonC1));
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack_NoActionBar);
2018-05-12 12:06:43 +02:00
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(ProxyActivity.this, R.color.black_3));
2018-05-11 14:26:15 +02:00
break;
default:
setTheme(R.style.AppThemeDark_NoActionBar);
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(ProxyActivity.this, R.color.mastodonC1));
}
2018-01-19 18:20:31 +01:00
setContentView(R.layout.activity_proxy);
getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if( getSupportActionBar() != null)
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);
if( hostVal.length() > 0)
host.setText(hostVal);
port.setText(String.valueOf(portVal));
if( login != null && login.length() > 0)
proxy_login.setText(login);
if( pwd != null && proxy_password.length() > 0)
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,
2018-01-20 11:26:44 +01:00
R.array.proxy_type_choice, android.R.layout.simple_spinner_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) {
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);
2018-08-30 17:49:26 +02:00
if( portVal.matches("\\d+") )
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);
}
}
}