2015-11-04 22:44:17 +00:00
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-01 13:52:09 +00:00
|
|
|
Copyright 2015-2016 by Marcel Bokhorst (M66B)
|
2015-11-04 22:44:17 +00:00
|
|
|
*/
|
|
|
|
|
2015-12-01 16:07:37 +00:00
|
|
|
import android.Manifest;
|
|
|
|
import android.annotation.TargetApi;
|
2015-11-14 09:49:15 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
2015-11-05 10:26:21 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-11-14 09:49:15 +00:00
|
|
|
import android.content.IntentFilter;
|
2015-11-04 22:44:17 +00:00
|
|
|
import android.content.SharedPreferences;
|
2015-12-01 16:07:37 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2015-11-14 08:50:45 +00:00
|
|
|
import android.net.ConnectivityManager;
|
2015-12-08 13:18:06 +00:00
|
|
|
import android.net.wifi.WifiConfiguration;
|
|
|
|
import android.net.wifi.WifiManager;
|
2015-11-05 10:26:21 +00:00
|
|
|
import android.os.AsyncTask;
|
2015-12-01 16:07:37 +00:00
|
|
|
import android.os.Build;
|
2015-11-04 22:44:17 +00:00
|
|
|
import android.os.Bundle;
|
2015-12-09 16:43:27 +00:00
|
|
|
import android.preference.EditTextPreference;
|
2016-01-02 15:38:24 +00:00
|
|
|
import android.preference.ListPreference;
|
2015-12-10 07:38:45 +00:00
|
|
|
import android.preference.MultiSelectListPreference;
|
2015-11-06 22:49:03 +00:00
|
|
|
import android.preference.Preference;
|
2015-12-01 19:19:26 +00:00
|
|
|
import android.preference.PreferenceCategory;
|
2015-12-03 10:16:21 +00:00
|
|
|
import android.preference.PreferenceFragment;
|
2015-11-04 22:44:17 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-11-06 22:49:03 +00:00
|
|
|
import android.preference.PreferenceScreen;
|
2015-12-03 10:16:21 +00:00
|
|
|
import android.preference.SwitchPreference;
|
2015-11-04 22:44:17 +00:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2015-12-01 18:58:27 +00:00
|
|
|
import android.telephony.PhoneStateListener;
|
|
|
|
import android.telephony.ServiceState;
|
2015-11-30 15:05:08 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
2015-12-10 07:38:45 +00:00
|
|
|
import android.text.TextUtils;
|
2015-11-05 10:26:21 +00:00
|
|
|
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;
|
2015-12-08 13:18:06 +00:00
|
|
|
import java.util.ArrayList;
|
2015-11-05 10:26:21 +00:00
|
|
|
import java.util.HashMap;
|
2015-12-10 07:38:45 +00:00
|
|
|
import java.util.HashSet;
|
2015-12-08 13:18:06 +00:00
|
|
|
import java.util.List;
|
2015-11-05 10:26:21 +00:00
|
|
|
import java.util.Map;
|
2015-12-10 07:38:45 +00:00
|
|
|
import java.util.Set;
|
2015-11-05 10:26:21 +00:00
|
|
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.parsers.SAXParserFactory;
|
2015-11-04 22:44:17 +00:00
|
|
|
|
|
|
|
public class ActivitySettings extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2015-11-05 10:26:21 +00:00
|
|
|
private static final String TAG = "NetGuard.Settings";
|
|
|
|
|
2015-12-01 18:58:27 +00:00
|
|
|
private boolean phone_state = false;
|
2015-11-14 09:49:15 +00:00
|
|
|
|
2015-11-06 22:49:03 +00:00
|
|
|
private static final int REQUEST_EXPORT = 1;
|
|
|
|
private static final int REQUEST_IMPORT = 2;
|
2015-12-02 16:53:03 +00:00
|
|
|
private static final int REQUEST_METERED = 3;
|
|
|
|
private static final int REQUEST_ROAMING_NATIONAL = 4;
|
|
|
|
private static final int REQUEST_ROAMING_INTERNATIONAL = 5;
|
2015-11-06 22:49:03 +00:00
|
|
|
private static final Intent INTENT_VPN_SETTINGS = new Intent("android.net.vpn.SETTINGS");
|
2015-11-05 10:26:21 +00:00
|
|
|
|
2015-11-04 22:44:17 +00:00
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2016-01-02 15:38:24 +00:00
|
|
|
Util.setTheme(this);
|
2015-11-04 22:44:17 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
2015-12-02 19:11:06 +00:00
|
|
|
getFragmentManager().beginTransaction().replace(android.R.id.content, new FragmentSettings()).commit();
|
2015-11-10 13:55:20 +00:00
|
|
|
getSupportActionBar().setTitle(R.string.menu_settings);
|
2015-11-04 22:44:17 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 13:05:38 +00:00
|
|
|
private PreferenceScreen getPreferenceScreen() {
|
|
|
|
return ((PreferenceFragment) getFragmentManager().findFragmentById(android.R.id.content)).getPreferenceScreen();
|
|
|
|
}
|
|
|
|
|
2015-12-03 10:23:10 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostCreate(Bundle savedInstanceState) {
|
|
|
|
super.onPostCreate(savedInstanceState);
|
2015-12-03 13:05:38 +00:00
|
|
|
PreferenceScreen screen = getPreferenceScreen();
|
2015-12-08 13:18:06 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2015-12-09 16:43:27 +00:00
|
|
|
// Wi-Fi home
|
2015-12-10 12:40:33 +00:00
|
|
|
MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) screen.findPreference("wifi_homes");
|
2015-12-10 07:38:45 +00:00
|
|
|
Set<String> ssid = prefs.getStringSet("wifi_homes", new HashSet<String>());
|
2015-12-10 12:40:33 +00:00
|
|
|
if (ssid.size() > 0)
|
|
|
|
pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, TextUtils.join(", ", ssid)));
|
|
|
|
else
|
|
|
|
pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, "-"));
|
2015-12-08 13:18:06 +00:00
|
|
|
|
|
|
|
WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
|
|
|
|
List<CharSequence> listSSID = new ArrayList<>();
|
2015-12-08 15:43:25 +00:00
|
|
|
List<WifiConfiguration> configs = wm.getConfiguredNetworks();
|
|
|
|
if (configs != null)
|
|
|
|
for (WifiConfiguration config : configs)
|
2015-12-14 07:21:33 +00:00
|
|
|
listSSID.add(config.SSID == null ? "NULL" : config.SSID);
|
2015-12-10 12:40:33 +00:00
|
|
|
pref_wifi_homes.setEntries(listSSID.toArray(new CharSequence[0]));
|
|
|
|
pref_wifi_homes.setEntryValues(listSSID.toArray(new CharSequence[0]));
|
2015-12-03 13:05:38 +00:00
|
|
|
|
|
|
|
// Handle auto enable
|
|
|
|
Preference pref_auto_enable = screen.findPreference("auto_enable");
|
|
|
|
pref_auto_enable.setTitle(getString(R.string.setting_auto, prefs.getString("auto_enable", "0")));
|
2015-12-03 10:23:10 +00:00
|
|
|
|
2016-01-01 09:06:02 +00:00
|
|
|
// Handle screen delay
|
|
|
|
Preference pref_screen_delay = screen.findPreference("screen_delay");
|
|
|
|
pref_screen_delay.setTitle(getString(R.string.setting_delay, prefs.getString("screen_delay", "0")));
|
|
|
|
|
2016-01-02 15:38:24 +00:00
|
|
|
// Handle theme
|
|
|
|
Preference pref_screen_theme = screen.findPreference("theme");
|
|
|
|
String theme = prefs.getString("theme", "teal");
|
|
|
|
String[] themeNames = getResources().getStringArray(R.array.themeNames);
|
|
|
|
String[] themeValues = getResources().getStringArray(R.array.themeValues);
|
|
|
|
for (int i = 0; i < themeNames.length; i++)
|
|
|
|
if (theme.equals(themeValues[i])) {
|
|
|
|
pref_screen_theme.setTitle(getString(R.string.setting_theme, themeNames[i]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-12-14 15:09:18 +00:00
|
|
|
// Handle stats
|
|
|
|
EditTextPreference pref_stats_base = (EditTextPreference) screen.findPreference("stats_base");
|
|
|
|
EditTextPreference pref_stats_frequency = (EditTextPreference) screen.findPreference("stats_frequency");
|
|
|
|
EditTextPreference pref_stats_samples = (EditTextPreference) screen.findPreference("stats_samples");
|
|
|
|
pref_stats_base.setTitle(getString(R.string.setting_stats_base, prefs.getString("stats_base", "5")));
|
|
|
|
pref_stats_frequency.setTitle(getString(R.string.setting_stats_frequency, prefs.getString("stats_frequency", "1000")));
|
|
|
|
pref_stats_samples.setTitle(getString(R.string.setting_stats_samples, prefs.getString("stats_samples", "90")));
|
|
|
|
|
2015-12-03 10:23:10 +00:00
|
|
|
// Handle export
|
|
|
|
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) {
|
2016-01-02 12:31:47 +00:00
|
|
|
startActivityForResult(getIntentCreateDocument(), ActivitySettings.REQUEST_EXPORT);
|
2015-12-03 10:23:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle import
|
|
|
|
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) {
|
2016-01-02 12:31:47 +00:00
|
|
|
startActivityForResult(getIntentOpenDocument(), ActivitySettings.REQUEST_IMPORT);
|
2015-12-03 10:23:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle technical info
|
2015-12-06 08:55:37 +00:00
|
|
|
Preference.OnPreferenceClickListener listener = new Preference.OnPreferenceClickListener() {
|
2015-12-03 10:23:10 +00:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
updateTechnicalInfo();
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-06 08:55:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Preference pref_technical_info = screen.findPreference("technical_info");
|
|
|
|
Preference pref_technical_network = screen.findPreference("technical_network");
|
|
|
|
Preference pref_technical_subscription = screen.findPreference("technical_subscription");
|
|
|
|
if (Util.isDebuggable(this)) {
|
|
|
|
pref_technical_info.setEnabled(INTENT_VPN_SETTINGS.resolveActivity(this.getPackageManager()) != null);
|
|
|
|
pref_technical_info.setIntent(INTENT_VPN_SETTINGS);
|
|
|
|
}
|
|
|
|
pref_technical_info.setOnPreferenceClickListener(listener);
|
|
|
|
pref_technical_network.setOnPreferenceClickListener(listener);
|
|
|
|
pref_technical_subscription.setOnPreferenceClickListener(listener);
|
2015-12-03 10:23:10 +00:00
|
|
|
updateTechnicalInfo();
|
|
|
|
|
2015-12-08 16:00:40 +00:00
|
|
|
// Handle devices without wifi
|
|
|
|
if (!Util.hasWifi(this)) {
|
|
|
|
PreferenceCategory defaults = (PreferenceCategory) screen.findPreference("category_defaults");
|
|
|
|
defaults.removePreference(screen.findPreference("whitelist_wifi"));
|
|
|
|
defaults.removePreference(screen.findPreference("screen_wifi"));
|
|
|
|
|
|
|
|
PreferenceCategory options = (PreferenceCategory) screen.findPreference("category_options");
|
2015-12-10 07:38:45 +00:00
|
|
|
options.removePreference(screen.findPreference("wifi_homes"));
|
2015-12-08 16:00:40 +00:00
|
|
|
options.removePreference(screen.findPreference("use_metered"));
|
|
|
|
}
|
|
|
|
|
2015-12-03 10:23:10 +00:00
|
|
|
// Handle devices without telephony
|
|
|
|
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("unmetered_2g"));
|
|
|
|
options.removePreference(screen.findPreference("unmetered_3g"));
|
|
|
|
options.removePreference(screen.findPreference("unmetered_4g"));
|
|
|
|
options.removePreference(screen.findPreference("national_roaming"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 22:44:17 +00:00
|
|
|
@Override
|
2015-11-14 09:49:15 +00:00
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
2015-12-03 13:05:38 +00:00
|
|
|
PreferenceScreen screen = getPreferenceScreen();
|
2015-11-14 09:49:15 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-12-01 16:28:13 +00:00
|
|
|
|
2015-12-03 10:16:21 +00:00
|
|
|
// TODO: check permision for whitelist_roaming
|
|
|
|
|
2015-12-01 16:28:13 +00:00
|
|
|
// Check if permission was revoked
|
2015-12-02 19:11:06 +00:00
|
|
|
if (prefs.getBoolean("unmetered_2g", false) ||
|
|
|
|
prefs.getBoolean("unmetered_3g", false) ||
|
|
|
|
prefs.getBoolean("unmetered_4g", false))
|
2015-12-02 16:53:03 +00:00
|
|
|
if (!Util.hasPhoneStatePermission(this)) {
|
2015-12-02 19:11:06 +00:00
|
|
|
prefs.edit().putBoolean("unmetered_2g", false).apply();
|
|
|
|
prefs.edit().putBoolean("unmetered_3g", false).apply();
|
|
|
|
prefs.edit().putBoolean("unmetered_4g", false).apply();
|
2015-12-03 10:16:21 +00:00
|
|
|
|
2015-12-03 13:05:38 +00:00
|
|
|
((SwitchPreference) screen.findPreference("unmetered_2g")).setChecked(false);
|
|
|
|
((SwitchPreference) screen.findPreference("unmetered_3g")).setChecked(false);
|
|
|
|
((SwitchPreference) screen.findPreference("unmetered_4g")).setChecked(false);
|
2015-12-02 16:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if permission was revoked
|
|
|
|
if (prefs.getBoolean("national_roaming", false))
|
|
|
|
if (!Util.hasPhoneStatePermission(this)) {
|
|
|
|
prefs.edit().putBoolean("national_roaming", false).apply();
|
2015-12-03 13:05:38 +00:00
|
|
|
((SwitchPreference) screen.findPreference("national_roaming")).setChecked(false);
|
2015-12-02 16:53:03 +00:00
|
|
|
}
|
2015-12-01 16:28:13 +00:00
|
|
|
|
|
|
|
// Listen for preference changes
|
2015-11-14 09:49:15 +00:00
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
|
2015-11-15 15:39:59 +00:00
|
|
|
// Listen for interactive state changes
|
|
|
|
IntentFilter ifInteractive = new IntentFilter();
|
|
|
|
ifInteractive.addAction(Intent.ACTION_SCREEN_ON);
|
|
|
|
ifInteractive.addAction(Intent.ACTION_SCREEN_OFF);
|
|
|
|
registerReceiver(interactiveStateReceiver, ifInteractive);
|
|
|
|
|
2015-11-14 09:49:15 +00:00
|
|
|
// Listen for connectivity updates
|
|
|
|
IntentFilter ifConnectivity = new IntentFilter();
|
|
|
|
ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
|
|
registerReceiver(connectivityChangedReceiver, ifConnectivity);
|
2015-12-01 18:58:27 +00:00
|
|
|
|
|
|
|
if (Util.hasPhoneStatePermission(this)) {
|
|
|
|
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
2015-12-02 16:53:03 +00:00
|
|
|
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | PhoneStateListener.LISTEN_SERVICE_STATE);
|
2015-12-01 18:58:27 +00:00
|
|
|
phone_state = true;
|
|
|
|
}
|
2015-11-14 09:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
|
2015-11-10 13:41:32 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.unregisterOnSharedPreferenceChangeListener(this);
|
2015-11-14 09:49:15 +00:00
|
|
|
|
2015-11-15 15:39:59 +00:00
|
|
|
unregisterReceiver(interactiveStateReceiver);
|
2015-11-14 09:49:15 +00:00
|
|
|
unregisterReceiver(connectivityChangedReceiver);
|
2015-12-01 18:58:27 +00:00
|
|
|
|
|
|
|
if (phone_state) {
|
|
|
|
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
|
|
|
|
phone_state = false;
|
|
|
|
}
|
2015-11-04 22:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-01 16:07:37 +00:00
|
|
|
@TargetApi(Build.VERSION_CODES.M)
|
2015-11-04 22:44:17 +00:00
|
|
|
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
|
2015-12-31 09:03:32 +00:00
|
|
|
// Pro features
|
2016-01-02 15:38:24 +00:00
|
|
|
if ("theme".equals(name)) {
|
|
|
|
if (!"teal".equals(prefs.getString(name, "teal")) && !IAB.isPurchased(ActivityPro.SKU_THEME, this)) {
|
|
|
|
prefs.edit().putString(name, "teal").apply();
|
|
|
|
((ListPreference) getPreferenceScreen().findPreference(name)).setValue("teal");
|
2015-12-30 08:58:21 +00:00
|
|
|
startActivity(new Intent(this, ActivityPro.class));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if ("show_stats".equals(name)) {
|
2016-01-01 18:03:41 +00:00
|
|
|
if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_SPEED, this)) {
|
2015-12-30 08:58:21 +00:00
|
|
|
prefs.edit().putBoolean(name, false).apply();
|
|
|
|
((SwitchPreference) getPreferenceScreen().findPreference(name)).setChecked(false);
|
|
|
|
startActivity(new Intent(this, ActivityPro.class));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:02:00 +00:00
|
|
|
if ("whitelist_wifi".equals(name) ||
|
|
|
|
"screen_wifi".equals(name))
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.reload("wifi", "setting changed", this);
|
2015-11-04 22:44:17 +00:00
|
|
|
|
2015-11-29 10:17:16 +00:00
|
|
|
else if ("whitelist_other".equals(name) ||
|
2015-12-02 09:42:24 +00:00
|
|
|
"screen_other".equals(name))
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.reload("other", "setting changed", this);
|
2015-11-04 22:44:17 +00:00
|
|
|
|
2015-12-02 09:42:24 +00:00
|
|
|
else if ("whitelist_roaming".equals(name)) {
|
2015-12-01 16:07:37 +00:00
|
|
|
if (prefs.getBoolean(name, false)) {
|
|
|
|
if (Util.hasPhoneStatePermission(this))
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.reload("other", "setting changed", this);
|
2015-12-01 16:07:37 +00:00
|
|
|
else
|
2015-12-02 09:42:24 +00:00
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_INTERNATIONAL);
|
|
|
|
} else
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.reload("other", "setting changed", this);
|
2015-12-31 09:03:32 +00:00
|
|
|
|
2015-12-27 15:09:07 +00:00
|
|
|
} else if ("manage_system".equals(name)) {
|
2015-12-31 09:03:32 +00:00
|
|
|
prefs.edit().putBoolean("show_system", prefs.getBoolean(name, false)).apply();
|
2015-12-27 15:09:07 +00:00
|
|
|
SinkholeService.reload(null, "setting changed", this);
|
2015-12-27 15:04:22 +00:00
|
|
|
|
2016-01-05 21:43:18 +00:00
|
|
|
} else if ("log".equals(name)) {
|
|
|
|
if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_LOG, this)) {
|
|
|
|
prefs.edit().putBoolean(name, false).apply();
|
|
|
|
((SwitchPreference) getPreferenceScreen().findPreference(name)).setChecked(false);
|
|
|
|
startActivity(new Intent(this, ActivityPro.class));
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
SinkholeService.reload(null, "setting changed", this);
|
2016-01-05 12:40:02 +00:00
|
|
|
|
2016-01-05 21:43:18 +00:00
|
|
|
} else if ("auto_enable".equals(name))
|
2015-12-27 15:04:22 +00:00
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_auto, prefs.getString(name, "0")));
|
2015-12-27 11:06:10 +00:00
|
|
|
|
2016-01-01 09:06:02 +00:00
|
|
|
else if ("screen_delay".equals(name))
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_delay, prefs.getString(name, "0")));
|
|
|
|
|
2016-01-02 15:38:24 +00:00
|
|
|
else if ("theme".equals(name) || "dark_theme".equals(name)) {
|
|
|
|
SinkholeService.setTheme(this);
|
2015-12-27 15:09:07 +00:00
|
|
|
recreate();
|
|
|
|
|
2016-01-02 15:38:24 +00:00
|
|
|
} else if ("wifi_homes".equals(name)) {
|
2015-12-10 07:38:45 +00:00
|
|
|
MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) getPreferenceScreen().findPreference(name);
|
|
|
|
Set<String> ssid = prefs.getStringSet(name, new HashSet<String>());
|
2015-12-10 12:40:33 +00:00
|
|
|
if (ssid.size() > 0)
|
|
|
|
pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, TextUtils.join(", ", ssid)));
|
|
|
|
else
|
|
|
|
pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, "-"));
|
2015-12-08 13:18:06 +00:00
|
|
|
SinkholeService.reload(null, "setting changed", this);
|
2015-12-03 13:05:38 +00:00
|
|
|
|
2015-12-26 08:58:28 +00:00
|
|
|
} else if ("use_metered".equals(name))
|
|
|
|
SinkholeService.reload(null, "setting changed", this);
|
|
|
|
|
|
|
|
else if ("unmetered_2g".equals(name) ||
|
2015-12-02 19:11:06 +00:00
|
|
|
"unmetered_3g".equals(name) ||
|
|
|
|
"unmetered_4g".equals(name)) {
|
|
|
|
if (prefs.getBoolean(name, false)) {
|
2015-12-02 16:53:03 +00:00
|
|
|
if (Util.hasPhoneStatePermission(this))
|
|
|
|
SinkholeService.reload("other", "setting changed", this);
|
|
|
|
else
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_METERED);
|
2015-12-02 19:11:06 +00:00
|
|
|
} else
|
|
|
|
SinkholeService.reload("other", "setting changed", this);
|
2015-12-02 09:42:24 +00:00
|
|
|
|
|
|
|
} else if ("national_roaming".equals(name)) {
|
|
|
|
if (prefs.getBoolean(name, false)) {
|
|
|
|
if (Util.hasPhoneStatePermission(this))
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.reload("other", "setting changed", this);
|
2015-12-02 09:42:24 +00:00
|
|
|
else
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_NATIONAL);
|
2015-12-01 16:07:37 +00:00
|
|
|
} else
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.reload("other", "setting changed", this);
|
2015-12-01 16:07:37 +00:00
|
|
|
|
2015-12-27 15:09:07 +00:00
|
|
|
} else if ("show_stats".equals(name)) {
|
2015-12-30 17:54:09 +00:00
|
|
|
SinkholeService.reloadStats("setting changed", this);
|
2015-11-04 22:44:17 +00:00
|
|
|
|
2015-12-27 15:09:07 +00:00
|
|
|
} else if ("stats_base".equals(name)) {
|
2015-12-30 17:54:09 +00:00
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_base, prefs.getString(name, "5")));
|
2015-12-27 15:09:07 +00:00
|
|
|
|
|
|
|
} else if ("stats_frequency".equals(name)) {
|
2015-12-30 17:54:09 +00:00
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_frequency, prefs.getString(name, "1000")));
|
2015-12-27 15:09:07 +00:00
|
|
|
|
|
|
|
} else if ("stats_samples".equals(name)) {
|
2015-12-30 17:54:09 +00:00
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_samples, prefs.getString(name, "90")));
|
2015-12-27 15:09:07 +00:00
|
|
|
}
|
2015-11-04 22:44:17 +00:00
|
|
|
}
|
2015-11-05 10:26:21 +00:00
|
|
|
|
2015-12-01 16:07:37 +00:00
|
|
|
@Override
|
2015-12-27 15:04:22 +00:00
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
2015-12-03 13:05:38 +00:00
|
|
|
PreferenceScreen screen = getPreferenceScreen();
|
|
|
|
|
2015-12-02 16:53:03 +00:00
|
|
|
if (requestCode == REQUEST_METERED)
|
2015-12-01 16:07:37 +00:00
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.reload("other", "permission granted", this);
|
|
|
|
else {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-12-02 19:11:06 +00:00
|
|
|
prefs.edit().putBoolean("unmetered_2g", false).apply();
|
|
|
|
prefs.edit().putBoolean("unmetered_3g", false).apply();
|
|
|
|
prefs.edit().putBoolean("unmetered_4g", false).apply();
|
2015-12-03 13:05:38 +00:00
|
|
|
((SwitchPreference) screen.findPreference("unmetered_2g")).setChecked(false);
|
|
|
|
((SwitchPreference) screen.findPreference("unmetered_3g")).setChecked(false);
|
|
|
|
((SwitchPreference) screen.findPreference("unmetered_4g")).setChecked(false);
|
2015-12-02 16:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (requestCode == REQUEST_ROAMING_NATIONAL)
|
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
|
|
|
|
SinkholeService.reload("other", "permission granted", this);
|
2015-12-01 16:07:37 +00:00
|
|
|
else {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.edit().putBoolean("national_roaming", false).apply();
|
2015-12-03 13:05:38 +00:00
|
|
|
((SwitchPreference) screen.findPreference("national_roaming")).setChecked(false);
|
2015-12-01 16:07:37 +00:00
|
|
|
}
|
2015-12-02 09:42:24 +00:00
|
|
|
|
|
|
|
else if (requestCode == REQUEST_ROAMING_INTERNATIONAL)
|
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.reload("other", "permission granted", this);
|
2015-12-02 09:42:24 +00:00
|
|
|
else {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.edit().putBoolean("whitelist_roaming", false).apply();
|
2015-12-03 13:05:38 +00:00
|
|
|
((SwitchPreference) screen.findPreference("whitelist_roaming")).setChecked(false);
|
2015-12-02 09:42:24 +00:00
|
|
|
}
|
2015-12-01 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 15:39:59 +00:00
|
|
|
private BroadcastReceiver interactiveStateReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-11-20 10:34:23 +00:00
|
|
|
Util.logExtras(intent);
|
2015-11-29 14:29:56 +00:00
|
|
|
updateTechnicalInfo();
|
2015-11-15 15:39:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-14 09:49:15 +00:00
|
|
|
private BroadcastReceiver connectivityChangedReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-11-20 10:34:23 +00:00
|
|
|
Util.logExtras(intent);
|
2015-11-29 14:29:56 +00:00
|
|
|
updateTechnicalInfo();
|
2015-11-14 09:49:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-01 18:58:27 +00:00
|
|
|
private PhoneStateListener phoneStateListener = new PhoneStateListener() {
|
2015-12-02 16:53:03 +00:00
|
|
|
@Override
|
|
|
|
public void onDataConnectionStateChanged(int state) {
|
|
|
|
updateTechnicalInfo();
|
|
|
|
}
|
|
|
|
|
2015-12-01 18:58:27 +00:00
|
|
|
@Override
|
|
|
|
public void onServiceStateChanged(ServiceState serviceState) {
|
|
|
|
updateTechnicalInfo();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-29 14:29:56 +00:00
|
|
|
private void updateTechnicalInfo() {
|
2015-12-04 07:41:27 +00:00
|
|
|
PreferenceScreen screen = getPreferenceScreen();
|
|
|
|
Preference pref_technical_info = screen.findPreference("technical_info");
|
|
|
|
Preference pref_technical_network = screen.findPreference("technical_network");
|
|
|
|
Preference pref_technical_subscription = screen.findPreference("technical_subscription");
|
|
|
|
|
2015-12-06 11:43:09 +00:00
|
|
|
pref_technical_info.setSummary(Util.getGeneralInfo(this));
|
|
|
|
pref_technical_network.setSummary(Util.getNetworkInfo(this));
|
|
|
|
pref_technical_subscription.setSummary(Util.getSubscriptionInfo(this));
|
2015-11-14 09:49:15 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
@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<Object, Object, Throwable>() {
|
|
|
|
@Override
|
|
|
|
protected Throwable doInBackground(Object... objects) {
|
|
|
|
OutputStream out = null;
|
|
|
|
try {
|
|
|
|
out = getContentResolver().openOutputStream(data.getData());
|
|
|
|
Log.i(TAG, "Writing URI=" + data.getData());
|
|
|
|
xmlExport(out);
|
|
|
|
return null;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-11-20 09:10:22 +00:00
|
|
|
Util.sendCrashReport(ex, ActivitySettings.this);
|
2015-11-05 10:26:21 +00:00
|
|
|
return ex;
|
|
|
|
} finally {
|
|
|
|
if (out != null)
|
|
|
|
try {
|
|
|
|
out.close();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Throwable ex) {
|
|
|
|
if (ex == null)
|
|
|
|
Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
|
|
|
|
else
|
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
2015-11-06 22:49:03 +00:00
|
|
|
private static Intent getIntentCreateDocument() {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
intent.setType("text/xml");
|
|
|
|
intent.putExtra(Intent.EXTRA_TITLE, "netguard.xml");
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Intent getIntentOpenDocument() {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
intent.setType("text/xml");
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
private void handleImport(final Intent data) {
|
|
|
|
new AsyncTask<Object, Object, Throwable>() {
|
|
|
|
@Override
|
|
|
|
protected Throwable doInBackground(Object... objects) {
|
|
|
|
InputStream in = null;
|
|
|
|
try {
|
|
|
|
in = getContentResolver().openInputStream(data.getData());
|
|
|
|
Log.i(TAG, "Reading URI=" + data.getData());
|
|
|
|
xmlImport(in);
|
|
|
|
return null;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-11-20 09:10:22 +00:00
|
|
|
Util.sendCrashReport(ex, ActivitySettings.this);
|
2015-11-05 10:26:21 +00:00
|
|
|
return ex;
|
|
|
|
} finally {
|
|
|
|
if (in != null)
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Throwable ex) {
|
2015-12-01 16:25:07 +00:00
|
|
|
if (ex == null) {
|
2015-11-05 10:26:21 +00:00
|
|
|
Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
|
2015-12-02 19:25:21 +00:00
|
|
|
recreate();
|
2015-12-01 16:25:07 +00:00
|
|
|
} else
|
2015-11-05 10:26:21 +00:00
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void xmlExport(OutputStream out) throws IOException {
|
|
|
|
XmlSerializer serializer = Xml.newSerializer();
|
|
|
|
serializer.setOutput(out, "UTF-8");
|
|
|
|
serializer.startDocument(null, true);
|
|
|
|
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
|
|
|
|
serializer.startTag(null, "netguard");
|
|
|
|
|
|
|
|
serializer.startTag(null, "application");
|
|
|
|
xmlExport(PreferenceManager.getDefaultSharedPreferences(this), serializer);
|
|
|
|
serializer.endTag(null, "application");
|
|
|
|
|
|
|
|
serializer.startTag(null, "wifi");
|
|
|
|
xmlExport(getSharedPreferences("wifi", Context.MODE_PRIVATE), serializer);
|
|
|
|
serializer.endTag(null, "wifi");
|
|
|
|
|
|
|
|
serializer.startTag(null, "mobile");
|
|
|
|
xmlExport(getSharedPreferences("other", Context.MODE_PRIVATE), serializer);
|
|
|
|
serializer.endTag(null, "mobile");
|
|
|
|
|
2015-11-21 08:48:33 +00:00
|
|
|
serializer.startTag(null, "screen_wifi");
|
|
|
|
xmlExport(getSharedPreferences("screen_wifi", Context.MODE_PRIVATE), serializer);
|
|
|
|
serializer.endTag(null, "screen_wifi");
|
|
|
|
|
|
|
|
serializer.startTag(null, "screen_other");
|
|
|
|
xmlExport(getSharedPreferences("screen_other", Context.MODE_PRIVATE), serializer);
|
|
|
|
serializer.endTag(null, "screen_other");
|
2015-11-05 10:26:21 +00:00
|
|
|
|
|
|
|
serializer.endTag(null, "netguard");
|
|
|
|
serializer.endDocument();
|
|
|
|
serializer.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void xmlExport(SharedPreferences prefs, XmlSerializer serializer) throws IOException {
|
|
|
|
Map<String, ?> settings = prefs.getAll();
|
|
|
|
for (String key : settings.keySet()) {
|
|
|
|
Object value = settings.get(key);
|
2015-11-08 12:05:55 +00:00
|
|
|
|
|
|
|
if ("imported".equals(key))
|
|
|
|
continue;
|
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
if (value instanceof Boolean) {
|
|
|
|
serializer.startTag(null, "setting");
|
|
|
|
serializer.attribute(null, "key", key);
|
|
|
|
serializer.attribute(null, "type", "boolean");
|
|
|
|
serializer.attribute(null, "value", value.toString());
|
|
|
|
serializer.endTag(null, "setting");
|
2015-11-08 12:05:55 +00:00
|
|
|
|
2015-11-18 07:36:53 +00:00
|
|
|
} else if (value instanceof Integer) {
|
|
|
|
serializer.startTag(null, "setting");
|
|
|
|
serializer.attribute(null, "key", key);
|
|
|
|
serializer.attribute(null, "type", "integer");
|
|
|
|
serializer.attribute(null, "value", value.toString());
|
|
|
|
serializer.endTag(null, "setting");
|
|
|
|
|
2015-12-03 13:05:38 +00:00
|
|
|
} else if (value instanceof String) {
|
|
|
|
serializer.startTag(null, "setting");
|
|
|
|
serializer.attribute(null, "key", key);
|
|
|
|
serializer.attribute(null, "type", "string");
|
|
|
|
serializer.attribute(null, "value", value.toString());
|
|
|
|
serializer.endTag(null, "setting");
|
|
|
|
|
2015-12-10 07:38:45 +00:00
|
|
|
} else if (value instanceof Set) {
|
|
|
|
Set<String> set = (Set<String>) value;
|
|
|
|
serializer.startTag(null, "setting");
|
|
|
|
serializer.attribute(null, "key", key);
|
|
|
|
serializer.attribute(null, "type", "set");
|
|
|
|
serializer.attribute(null, "value", TextUtils.join("\n", set));
|
|
|
|
serializer.endTag(null, "setting");
|
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
} else
|
|
|
|
Log.e(TAG, "Unknown key=" + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void xmlImport(InputStream in) throws IOException, SAXException, ParserConfigurationException {
|
2015-11-08 11:30:39 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.edit().putBoolean("enabled", false).apply();
|
2015-12-02 16:53:03 +00:00
|
|
|
SinkholeService.stop("import", this);
|
2015-11-08 11:30:39 +00:00
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
|
2015-12-31 15:08:50 +00:00
|
|
|
XmlImportHandler handler = new XmlImportHandler(this);
|
2015-11-05 10:26:21 +00:00
|
|
|
reader.setContentHandler(handler);
|
|
|
|
reader.parse(new InputSource(in));
|
|
|
|
|
2015-11-08 11:30:39 +00:00
|
|
|
xmlImport(handler.application, prefs);
|
2015-11-05 10:26:21 +00:00
|
|
|
xmlImport(handler.wifi, getSharedPreferences("wifi", Context.MODE_PRIVATE));
|
|
|
|
xmlImport(handler.mobile, getSharedPreferences("other", Context.MODE_PRIVATE));
|
2015-11-24 11:00:22 +00:00
|
|
|
xmlImport(handler.unused, getSharedPreferences("unused", Context.MODE_PRIVATE));
|
|
|
|
xmlImport(handler.screen_wifi, getSharedPreferences("screen_wifi", Context.MODE_PRIVATE));
|
|
|
|
xmlImport(handler.screen_other, getSharedPreferences("screen_other", Context.MODE_PRIVATE));
|
2015-11-08 11:30:39 +00:00
|
|
|
xmlImport(handler.roaming, getSharedPreferences("roaming", Context.MODE_PRIVATE));
|
2015-11-08 12:05:55 +00:00
|
|
|
|
2015-11-24 11:00:22 +00:00
|
|
|
// Upgrade imported settings
|
|
|
|
Receiver.upgrade(true, this);
|
|
|
|
|
2015-11-08 12:05:55 +00:00
|
|
|
// Refresh UI
|
|
|
|
prefs.edit().putBoolean("imported", true).apply();
|
2015-11-05 10:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void xmlImport(Map<String, Object> settings, SharedPreferences prefs) {
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
|
2015-11-08 11:30:39 +00:00
|
|
|
// Clear existing setting
|
2015-11-05 10:26:21 +00:00
|
|
|
for (String key : prefs.getAll().keySet())
|
2015-11-08 11:30:39 +00:00
|
|
|
if (!"enabled".equals(key))
|
|
|
|
editor.remove(key);
|
2015-11-05 10:26:21 +00:00
|
|
|
|
2015-11-08 11:30:39 +00:00
|
|
|
// Apply new settings
|
2015-11-05 10:26:21 +00:00
|
|
|
for (String key : settings.keySet()) {
|
|
|
|
Object value = settings.get(key);
|
|
|
|
if (value instanceof Boolean)
|
2015-11-24 11:00:22 +00:00
|
|
|
editor.putBoolean(key, (Boolean) value);
|
2015-11-18 07:36:53 +00:00
|
|
|
else if (value instanceof Integer)
|
|
|
|
editor.putInt(key, (Integer) value);
|
2015-12-03 13:05:38 +00:00
|
|
|
else if (value instanceof String)
|
|
|
|
editor.putString(key, (String) value);
|
2015-12-10 07:38:45 +00:00
|
|
|
else if (value instanceof Set)
|
|
|
|
editor.putStringSet(key, (Set<String>) value);
|
2015-11-05 10:26:21 +00:00
|
|
|
else
|
|
|
|
Log.e(TAG, "Unknown type=" + value.getClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class XmlImportHandler extends DefaultHandler {
|
2015-12-31 15:08:50 +00:00
|
|
|
private Context context;
|
2015-11-08 11:30:39 +00:00
|
|
|
public boolean enabled = false;
|
2015-11-05 10:26:21 +00:00
|
|
|
public Map<String, Object> application = new HashMap<>();
|
|
|
|
public Map<String, Object> wifi = new HashMap<>();
|
|
|
|
public Map<String, Object> mobile = new HashMap<>();
|
|
|
|
public Map<String, Object> unused = new HashMap<>();
|
2015-11-21 08:48:33 +00:00
|
|
|
public Map<String, Object> screen_wifi = new HashMap<>();
|
|
|
|
public Map<String, Object> screen_other = new HashMap<>();
|
2015-11-08 11:30:39 +00:00
|
|
|
public Map<String, Object> roaming = new HashMap<>();
|
2015-11-05 10:26:21 +00:00
|
|
|
private Map<String, Object> current = null;
|
|
|
|
|
2015-12-31 15:08:50 +00:00
|
|
|
public XmlImportHandler(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
@Override
|
|
|
|
public void startElement(String uri, String localName, String qName, Attributes attributes) {
|
|
|
|
if (qName.equals("netguard"))
|
|
|
|
; // Ignore
|
|
|
|
|
|
|
|
else if (qName.equals("application"))
|
|
|
|
current = application;
|
|
|
|
|
|
|
|
else if (qName.equals("wifi"))
|
|
|
|
current = wifi;
|
|
|
|
|
|
|
|
else if (qName.equals("mobile"))
|
|
|
|
current = mobile;
|
|
|
|
|
|
|
|
else if (qName.equals("unused"))
|
|
|
|
current = unused;
|
|
|
|
|
2015-11-21 08:48:33 +00:00
|
|
|
else if (qName.equals("screen_wifi"))
|
|
|
|
current = screen_wifi;
|
|
|
|
|
|
|
|
else if (qName.equals("screen_other"))
|
|
|
|
current = screen_other;
|
|
|
|
|
2015-11-08 11:30:39 +00:00
|
|
|
else if (qName.equals("roaming"))
|
2015-11-21 08:48:33 +00:00
|
|
|
current = roaming;
|
2015-11-08 11:30:39 +00:00
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
else if (qName.equals("setting")) {
|
|
|
|
String key = attributes.getValue("key");
|
|
|
|
String type = attributes.getValue("type");
|
|
|
|
String value = attributes.getValue("value");
|
|
|
|
|
|
|
|
if (current == null)
|
|
|
|
Log.e(TAG, "No current key=" + key);
|
|
|
|
else {
|
2015-11-08 11:30:39 +00:00
|
|
|
if ("enabled".equals(key))
|
|
|
|
enabled = Boolean.parseBoolean(value);
|
|
|
|
else {
|
2015-12-31 15:08:50 +00:00
|
|
|
// Pro features
|
2016-01-03 08:10:12 +00:00
|
|
|
if (current == application)
|
2016-01-02 15:38:24 +00:00
|
|
|
if ("theme".equals(key)) {
|
2016-01-01 18:03:41 +00:00
|
|
|
if (!IAB.isPurchased(ActivityPro.SKU_THEME, context))
|
2015-12-31 15:08:50 +00:00
|
|
|
return;
|
|
|
|
} else if ("show_stats".equals(key)) {
|
2016-01-01 18:03:41 +00:00
|
|
|
if (!IAB.isPurchased(ActivityPro.SKU_SPEED, context))
|
2015-12-31 15:08:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-08 11:30:39 +00:00
|
|
|
if ("boolean".equals(type))
|
|
|
|
current.put(key, Boolean.parseBoolean(value));
|
2015-11-18 07:36:53 +00:00
|
|
|
else if ("integer".equals(type))
|
|
|
|
current.put(key, Integer.parseInt(value));
|
2015-12-03 13:05:38 +00:00
|
|
|
else if ("string".equals(type))
|
|
|
|
current.put(key, value);
|
2015-12-10 07:38:45 +00:00
|
|
|
else if ("set".equals(type)) {
|
|
|
|
Set<String> set = new HashSet<>();
|
2015-12-10 12:40:33 +00:00
|
|
|
for (String s : value.split("\n"))
|
2015-12-10 07:38:45 +00:00
|
|
|
set.add(s);
|
|
|
|
current.put(key, set);
|
|
|
|
} else
|
2015-11-08 11:30:39 +00:00
|
|
|
Log.e(TAG, "Unknown type key=" + key);
|
|
|
|
}
|
2015-11-05 10:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else
|
|
|
|
Log.e(TAG, "Unknown element qname=" + qName);
|
|
|
|
}
|
|
|
|
}
|
2015-11-04 22:44:17 +00:00
|
|
|
}
|