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.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.v7.app.AppCompatActivity;
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 unlocked = false;
public static final String EXTRA_UNLOCKED = "unlocked";
private static final int REQUEST_EXPORT = 1;
private static final int REQUEST_IMPORT = 2;
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);
unlocked = getIntent().getBooleanExtra(EXTRA_UNLOCKED, false);
getFragmentManager().beginTransaction().replace(android.R.id.content, new FragmentSettings()).commit();
}
@Override
public void onDestroy() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
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;
}
});
Preference pref_vpn = screen.findPreference("vpn");
if (Util.isDebuggable(this)) {
pref_vpn.setEnabled(INTENT_VPN_SETTINGS.resolveActivity(this.getPackageManager()) != null);
pref_vpn.setIntent(INTENT_VPN_SETTINGS);
} else
screen.removePreference(pref_vpn);
if (!unlocked)
for (int i = 0; i < screen.getPreferenceCount(); i++)
screen.getPreference(i).setEnabled(false);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
if ("whitelist_wifi".equals(name))
SinkholeService.reload("wifi", this);
else if ("whitelist_other".equals(name))
SinkholeService.reload("other", this);
else if ("whitelist_roaming".equals(name))
SinkholeService.reload("other", this);
else if ("manage_system".equals(name))
SinkholeService.reload(null, this);
else if ("dark_theme".equals(name))
recreate();
}
@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