FairEmail/app/src/main/java/eu/faircode/email/FragmentOptionsConnection.java

398 lines
15 KiB
Java
Raw Normal View History

2019-05-06 07:10:13 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail 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.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2021-01-01 07:56:36 +00:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2019-05-06 07:10:13 +00:00
*/
import android.content.Context;
2019-06-14 19:54:36 +00:00
import android.content.Intent;
2019-05-06 07:10:13 +00:00
import android.content.SharedPreferences;
2020-06-14 16:34:13 +00:00
import android.content.pm.PackageManager;
2019-05-06 07:10:13 +00:00
import android.net.ConnectivityManager;
2020-10-27 13:14:54 +00:00
import android.net.LinkProperties;
2019-05-06 07:10:13 +00:00
import android.net.Network;
import android.net.NetworkCapabilities;
2020-10-27 13:14:54 +00:00
import android.net.NetworkInfo;
2019-05-06 07:10:13 +00:00
import android.net.NetworkRequest;
2019-06-14 20:03:56 +00:00
import android.os.Build;
2019-05-06 07:10:13 +00:00
import android.os.Bundle;
2019-06-14 20:03:56 +00:00
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;
2019-05-06 07:10:13 +00:00
import android.view.LayoutInflater;
2019-05-06 12:41:03 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2019-05-06 07:10:13 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
2019-06-14 19:54:36 +00:00
import android.widget.Button;
2019-05-06 07:10:13 +00:00
import android.widget.CompoundButton;
2019-10-21 11:26:15 +00:00
import android.widget.EditText;
2019-05-06 07:10:13 +00:00
import android.widget.Spinner;
import android.widget.TextView;
2019-07-14 10:32:32 +00:00
import android.widget.Toast;
2019-05-06 07:10:13 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceManager;
2019-05-06 12:41:03 +00:00
public class FragmentOptionsConnection extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
2019-05-06 07:10:13 +00:00
private SwitchCompat swMetered;
private Spinner spDownload;
2019-05-10 15:51:05 +00:00
private SwitchCompat swRoaming;
2019-06-14 09:18:08 +00:00
private SwitchCompat swRlah;
private EditText etTimeout;
2020-10-27 12:22:23 +00:00
private SwitchCompat swPreferIp4;
private SwitchCompat swStandaloneVpn;
2020-11-08 19:51:44 +00:00
private SwitchCompat swTcpKeepAlive;
2020-11-18 07:25:20 +00:00
private TextView tvTcpKeepAliveHint;
2020-01-29 11:44:55 +00:00
private SwitchCompat swSslHarden;
2019-06-14 19:54:36 +00:00
private Button btnManage;
2020-10-27 13:14:54 +00:00
private TextView tvNetworkMetered;
private TextView tvNetworkRoaming;
private TextView tvNetworkInfo;
2019-05-06 07:10:13 +00:00
2019-05-06 12:41:03 +00:00
private final static String[] RESET_OPTIONS = new String[]{
"metered", "download", "roaming", "rlah", "timeout", "prefer_ip4", "standalone_vpn", "tcp_keep_alive", "ssl_harden"
2019-05-06 12:41:03 +00:00
};
2019-05-06 07:10:13 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2019-06-12 14:15:46 +00:00
setSubtitle(R.string.title_setup);
2019-05-06 12:41:03 +00:00
setHasOptionsMenu(true);
2019-05-06 07:10:13 +00:00
View view = inflater.inflate(R.layout.fragment_options_connection, container, false);
// Get controls
swMetered = view.findViewById(R.id.swMetered);
spDownload = view.findViewById(R.id.spDownload);
2019-05-10 15:51:05 +00:00
swRoaming = view.findViewById(R.id.swRoaming);
2019-06-14 09:18:08 +00:00
swRlah = view.findViewById(R.id.swRlah);
etTimeout = view.findViewById(R.id.etTimeout);
2020-10-27 12:22:23 +00:00
swPreferIp4 = view.findViewById(R.id.swPreferIp4);
swStandaloneVpn = view.findViewById(R.id.swStandaloneVpn);
2020-11-08 19:51:44 +00:00
swTcpKeepAlive = view.findViewById(R.id.swTcpKeepAlive);
2020-11-18 07:25:20 +00:00
tvTcpKeepAliveHint = view.findViewById(R.id.tvTcpKeepAliveHint);
2020-01-29 11:44:55 +00:00
swSslHarden = view.findViewById(R.id.swSslHarden);
2019-06-14 19:54:36 +00:00
btnManage = view.findViewById(R.id.btnManage);
2019-05-06 07:10:13 +00:00
2020-10-27 13:14:54 +00:00
tvNetworkMetered = view.findViewById(R.id.tvNetworkMetered);
tvNetworkRoaming = view.findViewById(R.id.tvNetworkRoaming);
tvNetworkInfo = view.findViewById(R.id.tvNetworkInfo);
2019-05-06 14:50:17 +00:00
2019-05-06 13:30:30 +00:00
setOptions();
2019-05-06 07:10:13 +00:00
// Wire controls
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean debug = prefs.getBoolean("debug", false);
2019-05-06 07:10:13 +00:00
swMetered.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("metered", checked).apply();
}
});
spDownload.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
int[] values = getResources().getIntArray(R.array.downloadValues);
prefs.edit().putInt("download", values[position]).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("download").apply();
}
});
2019-06-14 09:18:08 +00:00
swRoaming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2019-05-06 07:10:13 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-06-14 09:18:08 +00:00
prefs.edit().putBoolean("roaming", checked).apply();
2019-05-06 07:10:13 +00:00
}
});
2019-06-14 09:18:08 +00:00
swRlah.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2019-05-10 15:29:07 +00:00
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-06-14 09:18:08 +00:00
prefs.edit().putBoolean("rlah", checked).apply();
2019-05-10 15:29:07 +00:00
}
});
etTimeout.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
int timeout = (s.length() > 0 ? Integer.parseInt(s.toString()) : 0);
if (timeout == 0)
prefs.edit().remove("timeout").apply();
else
prefs.edit().putInt("timeout", timeout).apply();
} catch (NumberFormatException ex) {
Log.e(ex);
}
}
@Override
public void afterTextChanged(Editable s) {
// Do nothing
}
});
2020-10-27 12:22:23 +00:00
swPreferIp4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("prefer_ip4", checked).apply();
}
});
swStandaloneVpn.setVisibility(debug || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
swStandaloneVpn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("standalone_vpn", checked).apply();
}
});
2020-11-18 07:25:20 +00:00
swTcpKeepAlive.setVisibility(debug || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
tvTcpKeepAliveHint.setVisibility(debug || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
2020-11-08 19:51:44 +00:00
swTcpKeepAlive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
try {
System.setProperty("fairemail.tcp_keep_alive", Boolean.toString(checked));
} catch (Throwable ex) {
Log.e(ex);
}
prefs.edit().putBoolean("tcp_keep_alive", checked).apply();
}
});
2020-01-29 11:44:55 +00:00
swSslHarden.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("ssl_harden", checked).apply();
}
});
2019-06-14 20:03:56 +00:00
final Intent manage = getIntentConnectivity();
2020-06-14 16:34:13 +00:00
PackageManager pm = getContext().getPackageManager();
2019-06-14 20:03:56 +00:00
btnManage.setVisibility(
2020-06-14 16:34:13 +00:00
manage.resolveActivity(pm) == null // system whitelisted
2019-06-14 20:03:56 +00:00
? View.GONE : View.VISIBLE);
2019-06-14 19:54:36 +00:00
btnManage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(manage);
}
});
2019-05-06 12:41:03 +00:00
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
2020-10-27 13:14:54 +00:00
tvNetworkMetered.setVisibility(View.GONE);
tvNetworkRoaming.setVisibility(View.GONE);
tvNetworkInfo.setVisibility(View.GONE);
2019-05-06 14:50:17 +00:00
2019-05-06 07:10:13 +00:00
return view;
}
2019-05-06 12:41:03 +00:00
@Override
public void onDestroyView() {
PreferenceManager.getDefaultSharedPreferences(getContext()).unregisterOnSharedPreferenceChangeListener(this);
super.onDestroyView();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if ("timeout".equals(key))
return;
2019-09-10 06:53:07 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
setOptions();
2019-05-06 12:41:03 +00:00
}
2019-05-06 07:10:13 +00:00
@Override
public void onResume() {
super.onResume();
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
2019-06-14 09:20:14 +00:00
if (cm == null)
return;
2021-01-03 15:34:53 +00:00
showConnectionType();
2019-05-06 07:10:13 +00:00
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
cm.registerNetworkCallback(builder.build(), networkCallback);
}
@Override
public void onPause() {
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
2019-06-14 09:20:14 +00:00
if (cm == null)
return;
2019-05-06 07:10:13 +00:00
cm.unregisterNetworkCallback(networkCallback);
super.onPause();
}
2019-05-06 12:41:03 +00:00
@Override
2019-06-14 09:20:14 +00:00
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
2019-05-06 12:41:03 +00:00
inflater.inflate(R.menu.menu_options, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
2019-06-14 09:20:14 +00:00
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
2021-02-05 10:15:02 +00:00
if (item.getItemId() == R.id.menu_default) {
onMenuDefault();
return true;
2019-05-06 12:41:03 +00:00
}
2021-02-05 10:15:02 +00:00
return super.onOptionsItemSelected(item);
2019-05-06 12:41:03 +00:00
}
private void onMenuDefault() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = prefs.edit();
for (String option : RESET_OPTIONS)
editor.remove(option);
editor.apply();
2019-07-14 10:32:32 +00:00
ToastEx.makeText(getContext(), R.string.title_setup_done, Toast.LENGTH_LONG).show();
2019-05-06 12:41:03 +00:00
}
2019-05-06 07:10:13 +00:00
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swMetered.setChecked(prefs.getBoolean("metered", true));
2020-07-13 13:34:31 +00:00
int download = prefs.getInt("download", MessageHelper.DEFAULT_DOWNLOAD_SIZE);
2019-05-06 07:10:13 +00:00
int[] downloadValues = getResources().getIntArray(R.array.downloadValues);
for (int pos = 0; pos < downloadValues.length; pos++)
if (downloadValues[pos] == download) {
spDownload.setSelection(pos);
break;
}
2019-05-10 15:51:05 +00:00
swRoaming.setChecked(prefs.getBoolean("roaming", true));
2019-06-14 09:18:08 +00:00
swRlah.setChecked(prefs.getBoolean("rlah", true));
int timeout = prefs.getInt("timeout", 0);
etTimeout.setText(timeout == 0 ? null : Integer.toString(timeout));
etTimeout.setHint(Integer.toString(EmailService.DEFAULT_CONNECT_TIMEOUT));
swPreferIp4.setChecked(prefs.getBoolean("prefer_ip4", true));
swStandaloneVpn.setChecked(prefs.getBoolean("standalone_vpn", false));
2020-11-08 21:17:08 +00:00
swTcpKeepAlive.setChecked(prefs.getBoolean("tcp_keep_alive", false));
2020-01-29 11:44:55 +00:00
swSslHarden.setChecked(prefs.getBoolean("ssl_harden", false));
2019-05-06 07:10:13 +00:00
}
2019-06-14 20:03:56 +00:00
private static Intent getIntentConnectivity() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
return new Intent(Settings.ACTION_WIRELESS_SETTINGS);
else
return new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
}
2019-05-06 07:10:13 +00:00
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
2019-06-14 09:20:14 +00:00
public void onAvailable(@NonNull Network network) {
2019-05-06 07:10:13 +00:00
showConnectionType();
}
@Override
2019-06-14 09:20:14 +00:00
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
2019-05-06 07:10:13 +00:00
showConnectionType();
}
2020-11-04 17:57:48 +00:00
@Override
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
showConnectionType();
}
2019-05-06 07:10:13 +00:00
@Override
2019-06-14 09:20:14 +00:00
public void onLost(@NonNull Network network) {
2019-05-06 07:10:13 +00:00
showConnectionType();
}
};
private void showConnectionType() {
2020-12-02 07:44:43 +00:00
final Context context = getContext();
if (context == null) {
tvNetworkMetered.setVisibility(View.GONE);
tvNetworkRoaming.setVisibility(View.GONE);
tvNetworkInfo.setVisibility(View.GONE);
return;
}
final ConnectionHelper.NetworkState networkState = ConnectionHelper.getNetworkState(context);
2020-10-27 13:14:54 +00:00
final StringBuilder sb = new StringBuilder();
2020-12-02 07:44:43 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2020-10-27 13:14:54 +00:00
boolean debug = prefs.getBoolean("debug", false);
if ((debug || BuildConfig.DEBUG) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
try {
2020-12-02 07:44:43 +00:00
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2020-10-27 13:14:54 +00:00
Network active = (cm == null ? null : cm.getActiveNetwork());
if (active != null) {
NetworkInfo ni = cm.getNetworkInfo(active);
if (ni != null)
sb.append(ni).append("\r\n\r\n");
NetworkCapabilities nc = cm.getNetworkCapabilities(active);
if (nc != null)
sb.append(nc).append("\r\n\r\n");
LinkProperties lp = cm.getLinkProperties(active);
if (lp != null)
sb.append(lp).append("\r\n\r\n");
}
} catch (Throwable ex) {
Log.e(ex);
}
2020-09-17 14:52:19 +00:00
getMainHandler().post(new Runnable() {
@Override
public void run() {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
2020-10-27 13:14:54 +00:00
tvNetworkMetered.setText(networkState.isUnmetered() ? R.string.title_legend_unmetered : R.string.title_legend_metered);
tvNetworkInfo.setText(sb.toString());
tvNetworkMetered.setVisibility(networkState.isConnected() ? View.VISIBLE : View.GONE);
tvNetworkRoaming.setVisibility(networkState.isRoaming() ? View.VISIBLE : View.GONE);
tvNetworkInfo.setVisibility(sb.length() == 0 ? View.GONE : View.VISIBLE);
}
}
});
2019-05-06 07:10:13 +00:00
}
}