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/>.
|
|
|
|
|
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.Network;
|
|
|
|
import android.net.NetworkCapabilities;
|
|
|
|
import android.net.NetworkRequest;
|
|
|
|
import android.os.Bundle;
|
|
|
|
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;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.Spinner;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.appcompat.widget.SwitchCompat;
|
|
|
|
import androidx.fragment.app.FragmentActivity;
|
|
|
|
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:29:07 +00:00
|
|
|
private SwitchCompat swRlah;
|
2019-05-10 15:51:05 +00:00
|
|
|
private SwitchCompat swRoaming;
|
2019-05-06 14:50:17 +00:00
|
|
|
private TextView tvConnectionType;
|
|
|
|
private TextView tvConnectionRoaming;
|
2019-05-06 07:10:13 +00:00
|
|
|
|
2019-05-06 12:41:03 +00:00
|
|
|
private final static String[] RESET_OPTIONS = new String[]{
|
2019-05-10 15:51:05 +00:00
|
|
|
"metered", "download", "rlah", "roaming"
|
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) {
|
|
|
|
setSubtitle(R.string.title_advanced);
|
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:29:07 +00:00
|
|
|
swRlah = view.findViewById(R.id.swRlah);
|
2019-05-10 15:51:05 +00:00
|
|
|
swRoaming = view.findViewById(R.id.swRoaming);
|
2019-05-06 07:10:13 +00:00
|
|
|
|
2019-05-06 14:50:17 +00:00
|
|
|
tvConnectionType = view.findViewById(R.id.tvConnectionType);
|
|
|
|
tvConnectionRoaming = view.findViewById(R.id.tvConnectionRoaming);
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
swMetered.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
|
|
|
prefs.edit().putBoolean("metered", checked).apply();
|
|
|
|
ServiceSynchronize.reload(getContext(), "metered=" + checked);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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-05-10 15:51:05 +00:00
|
|
|
swRlah.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
2019-05-06 07:10:13 +00:00
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
2019-05-10 15:51:05 +00:00
|
|
|
prefs.edit().putBoolean("rlah", checked).apply();
|
|
|
|
ServiceSynchronize.reload(getContext(), "rlah=" + checked);
|
2019-05-06 07:10:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-10 15:51:05 +00:00
|
|
|
swRoaming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
2019-05-10 15:29:07 +00:00
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
2019-05-10 15:51:05 +00:00
|
|
|
prefs.edit().putBoolean("roaming", checked).apply();
|
|
|
|
ServiceSynchronize.reload(getContext(), "roaming=" + checked);
|
2019-05-10 15:29:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-06 12:41:03 +00:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
|
|
|
|
|
2019-05-06 14:50:17 +00:00
|
|
|
tvConnectionType.setVisibility(View.GONE);
|
|
|
|
tvConnectionRoaming.setVisibility(View.GONE);
|
|
|
|
|
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) {
|
|
|
|
setOptions();
|
|
|
|
}
|
|
|
|
|
2019-05-06 07:10:13 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
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);
|
|
|
|
cm.unregisterNetworkCallback(networkCallback);
|
|
|
|
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
2019-05-06 12:41:03 +00:00
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.menu_options, menu);
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_default:
|
|
|
|
onMenuDefault();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onMenuDefault() {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
for (String option : RESET_OPTIONS)
|
|
|
|
editor.remove(option);
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
2019-05-06 07:10:13 +00:00
|
|
|
private void setOptions() {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
|
|
|
|
swMetered.setChecked(prefs.getBoolean("metered", true));
|
|
|
|
|
|
|
|
int download = prefs.getInt("download", MessageHelper.DEFAULT_ATTACHMENT_DOWNLOAD_SIZE);
|
|
|
|
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 18:59:27 +00:00
|
|
|
swRlah.setChecked(prefs.getBoolean("rlah", true));
|
2019-05-10 15:51:05 +00:00
|
|
|
swRoaming.setChecked(prefs.getBoolean("roaming", true));
|
2019-05-06 07:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
|
|
|
@Override
|
|
|
|
public void onAvailable(Network network) {
|
|
|
|
showConnectionType();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
|
|
|
|
showConnectionType();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLost(Network network) {
|
|
|
|
showConnectionType();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void showConnectionType() {
|
|
|
|
FragmentActivity activity = getActivity();
|
|
|
|
if (activity == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
activity.runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
|
2019-05-12 16:41:51 +00:00
|
|
|
ConnectionHelper.NetworkState networkState = ConnectionHelper.getNetworkState(getContext());
|
2019-05-06 07:10:13 +00:00
|
|
|
|
|
|
|
tvConnectionType.setText(networkState.isUnmetered() ? R.string.title_legend_unmetered : R.string.title_legend_metered);
|
|
|
|
tvConnectionType.setVisibility(networkState.isConnected() ? View.VISIBLE : View.GONE);
|
|
|
|
tvConnectionRoaming.setVisibility(networkState.isRoaming() ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|