package eu.faircode.netguard;
/*
This file is part of NetGuard.
NetGuard 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.
NetGuard 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 NetGuard. If not, see .
Copyright 2015 by Marcel Bokhorst (M66B)
*/
import android.Manifest;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;
import android.widget.Toast;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
public class ActivitySettings extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "NetGuard.Settings";
private boolean phone_state = false;
private Preference pref_technical = null;
private static final int REQUEST_EXPORT = 1;
private static final int REQUEST_IMPORT = 2;
private static final int REQUEST_METERED = 3;
private static final int REQUEST_ROAMING_NATIONAL = 4;
private static final int REQUEST_ROAMING_INTERNATIONAL = 5;
private static final Intent INTENT_VPN_SETTINGS = new Intent("android.net.vpn.SETTINGS");
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new FragmentSettings()).commit();
getSupportActionBar().setTitle(R.string.menu_settings);
}
private void refreshScreen() {
// TODO: better solution
getFragmentManager().beginTransaction().replace(android.R.id.content, new FragmentSettings()).commit();
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// Check if permission was revoked
if (prefs.getBoolean("unmetered_2g", false) ||
prefs.getBoolean("unmetered_3g", false) ||
prefs.getBoolean("unmetered_4g", false))
if (!Util.hasPhoneStatePermission(this)) {
prefs.edit().putBoolean("unmetered_2g", false).apply();
prefs.edit().putBoolean("unmetered_3g", false).apply();
prefs.edit().putBoolean("unmetered_4g", false).apply();
refreshScreen();
}
// Check if permission was revoked
if (prefs.getBoolean("national_roaming", false))
if (!Util.hasPhoneStatePermission(this)) {
prefs.edit().putBoolean("national_roaming", false).apply();
refreshScreen();
}
// Listen for preference changes
prefs.registerOnSharedPreferenceChangeListener(this);
// Listen for interactive state changes
IntentFilter ifInteractive = new IntentFilter();
ifInteractive.addAction(Intent.ACTION_SCREEN_ON);
ifInteractive.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(interactiveStateReceiver, ifInteractive);
// Listen for connectivity updates
IntentFilter ifConnectivity = new IntentFilter();
ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(connectivityChangedReceiver, ifConnectivity);
if (Util.hasPhoneStatePermission(this)) {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | PhoneStateListener.LISTEN_SERVICE_STATE);
phone_state = true;
}
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
unregisterReceiver(interactiveStateReceiver);
unregisterReceiver(connectivityChangedReceiver);
if (phone_state) {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
phone_state = false;
}
}
public void setup(PreferenceScreen screen) {
Preference pref_export = screen.findPreference("export");
pref_export.setEnabled(getIntentCreateDocument().resolveActivity(getPackageManager()) != null);
pref_export.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(getIntentCreateDocument(), ActivitySettings.REQUEST_EXPORT);
return true;
}
});
Preference pref_import = screen.findPreference("import");
pref_import.setEnabled(getIntentCreateDocument().resolveActivity(getPackageManager()) != null);
pref_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(getIntentOpenDocument(), ActivitySettings.REQUEST_IMPORT);
return true;
}
});
pref_technical = screen.findPreference("technical");
if (Util.isDebuggable(this)) {
pref_technical.setEnabled(INTENT_VPN_SETTINGS.resolveActivity(this.getPackageManager()) != null);
pref_technical.setIntent(INTENT_VPN_SETTINGS);
}
pref_technical.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
updateTechnicalInfo();
return true;
}
});
updateTechnicalInfo();
if (!Util.hasTelephony(this)) {
PreferenceCategory defaults = (PreferenceCategory) screen.findPreference("category_defaults");
defaults.removePreference(screen.findPreference("whitelist_other"));
defaults.removePreference(screen.findPreference("screen_other"));
defaults.removePreference(screen.findPreference("whitelist_roaming"));
PreferenceCategory options = (PreferenceCategory) screen.findPreference("category_options");
options.removePreference(screen.findPreference("use_metered"));
options.removePreference(screen.findPreference("national_roaming"));
}
}
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
if ("whitelist_wifi".equals(name) ||
"screen_wifi".equals(name))
SinkholeService.reload("wifi", "setting changed", this);
else if ("whitelist_other".equals(name) ||
"screen_other".equals(name))
SinkholeService.reload("other", "setting changed", this);
else if ("whitelist_roaming".equals(name)) {
if (prefs.getBoolean(name, false)) {
if (Util.hasPhoneStatePermission(this))
SinkholeService.reload("other", "setting changed", this);
else
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_INTERNATIONAL);
} else
SinkholeService.reload("other", "setting changed", this);
} else if ("unmetered_2g".equals(name) ||
"unmetered_3g".equals(name) ||
"unmetered_4g".equals(name)) {
if (prefs.getBoolean(name, false)) {
if (Util.hasPhoneStatePermission(this))
SinkholeService.reload("other", "setting changed", this);
else
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_METERED);
} else
SinkholeService.reload("other", "setting changed", this);
} else if ("national_roaming".equals(name)) {
if (prefs.getBoolean(name, false)) {
if (Util.hasPhoneStatePermission(this))
SinkholeService.reload("other", "setting changed", this);
else
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_NATIONAL);
} else
SinkholeService.reload("other", "setting changed", this);
} else if ("use_metered".equals(name) ||
"manage_system".equals(name))
SinkholeService.reload(null, "setting changed", this);
else if ("dark_theme".equals(name))
recreate();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_METERED)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
SinkholeService.reload("other", "permission granted", this);
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("unmetered_2g", false).apply();
prefs.edit().putBoolean("unmetered_3g", false).apply();
prefs.edit().putBoolean("unmetered_4g", false).apply();
refreshScreen();
}
else if (requestCode == REQUEST_ROAMING_NATIONAL)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
SinkholeService.reload("other", "permission granted", this);
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("national_roaming", false).apply();
refreshScreen();
}
else if (requestCode == REQUEST_ROAMING_INTERNATIONAL)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
SinkholeService.reload("other", "permission granted", this);
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("whitelist_roaming", false).apply();
refreshScreen();
}
}
private BroadcastReceiver interactiveStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Util.logExtras(intent);
updateTechnicalInfo();
}
};
private BroadcastReceiver connectivityChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Util.logExtras(intent);
updateTechnicalInfo();
}
};
private PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onDataConnectionStateChanged(int state) {
updateTechnicalInfo();
}
@Override
public void onServiceStateChanged(ServiceState serviceState) {
updateTechnicalInfo();
}
};
private void updateTechnicalInfo() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
StringBuilder sb = new StringBuilder();
sb.append(String.format("Interactive %B\r\n", Util.isInteractive(this)));
sb.append(String.format("Telephony %B\r\n", Util.hasTelephony(this)));
sb.append(String.format("Connected %B\r\n", Util.isConnected(this)));
sb.append(String.format("WiFi %B\r\n", Util.isWifiActive(this)));
sb.append(String.format("Metered %B\r\n", Util.isMeteredNetwork(this)));
sb.append(String.format("Roaming %B\r\n", Util.isRoaming(this)));
if (tm.getSimState() == TelephonyManager.SIM_STATE_READY)
sb.append(String.format("SIM %s/%s/%s\r\n", tm.getSimCountryIso(), tm.getSimOperatorName(), tm.getSimOperator()));
if (tm.getNetworkType() != TelephonyManager.NETWORK_TYPE_UNKNOWN)
sb.append(String.format("Network %s/%s/%s\r\n", tm.getNetworkCountryIso(), tm.getNetworkOperatorName(), tm.getNetworkOperator()));
for (Network network : cm.getAllNetworks()) {
NetworkInfo ni = cm.getNetworkInfo(network);
if (ni != null)
sb.append(ni.getTypeName())
.append("/")
.append(ni.getSubtypeName())
.append(" ").append(ni.getDetailedState())
.append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo())
.append(ni.getType() == ConnectivityManager.TYPE_MOBILE ? " " + Util.getNetworkGeneration(ni.getSubtype()) : "")
.append(ni.isRoaming() ? " R" : "")
.append("\r\n");
}
pref_technical.setSummary(sb.toString());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
Log.i(TAG, "onActivityResult request=" + requestCode + " result=" + requestCode + " ok=" + (resultCode == RESULT_OK));
if (requestCode == REQUEST_EXPORT) {
if (resultCode == RESULT_OK && data != null)
handleExport(data);
} else if (requestCode == REQUEST_IMPORT) {
if (resultCode == RESULT_OK && data != null)
handleImport(data);
} else {
Log.w(TAG, "Unknown activity result request=" + requestCode);
super.onActivityResult(requestCode, resultCode, data);
}
}
private void handleExport(final Intent data) {
new AsyncTask