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-12-29 08:27:58 +00:00
|
|
|
Copyright 2015-2017 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;
|
2016-07-22 12:12:38 +00:00
|
|
|
import android.content.ContentResolver;
|
2015-11-05 10:26:21 +00:00
|
|
|
import android.content.Context;
|
2016-01-19 17:29:58 +00:00
|
|
|
import android.content.DialogInterface;
|
2015-11-05 10:26:21 +00:00
|
|
|
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;
|
2016-07-22 12:12:38 +00:00
|
|
|
import android.content.res.AssetFileDescriptor;
|
2016-02-01 08:55:49 +00:00
|
|
|
import android.database.Cursor;
|
2015-11-14 08:50:45 +00:00
|
|
|
import android.net.ConnectivityManager;
|
2016-01-31 14:12:35 +00:00
|
|
|
import android.net.Uri;
|
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-03 10:16:21 +00:00
|
|
|
import android.preference.PreferenceFragment;
|
2016-07-23 12:36:23 +00:00
|
|
|
import android.preference.PreferenceGroup;
|
2015-11-04 22:44:17 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-11-06 22:49:03 +00:00
|
|
|
import android.preference.PreferenceScreen;
|
2016-02-01 17:19:25 +00:00
|
|
|
import android.preference.TwoStatePreference;
|
2016-01-31 15:38:38 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2016-02-02 08:01:04 +00:00
|
|
|
import android.support.v4.app.NavUtils;
|
2016-02-01 07:25:08 +00:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2016-02-07 08:28:02 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
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;
|
2016-07-29 07:46:20 +00:00
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.Spanned;
|
2015-12-10 07:38:45 +00:00
|
|
|
import android.text.TextUtils;
|
2016-07-29 07:46:20 +00:00
|
|
|
import android.text.style.ImageSpan;
|
2015-11-05 10:26:21 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Xml;
|
2016-01-19 17:29:58 +00:00
|
|
|
import android.view.LayoutInflater;
|
2016-02-02 08:01:04 +00:00
|
|
|
import android.view.MenuItem;
|
2016-01-19 17:29:58 +00:00
|
|
|
import android.view.View;
|
2015-11-05 10:26:21 +00:00
|
|
|
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;
|
|
|
|
|
2016-01-23 11:48:17 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
2015-11-05 10:26:21 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2016-01-27 07:37:45 +00:00
|
|
|
import java.net.InetAddress;
|
2016-02-03 10:54:17 +00:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2016-02-11 06:50:02 +00:00
|
|
|
import java.net.UnknownHostException;
|
2016-01-09 07:49:50 +00:00
|
|
|
import java.text.SimpleDateFormat;
|
2015-12-08 13:18:06 +00:00
|
|
|
import java.util.ArrayList;
|
2016-01-09 07:49:50 +00:00
|
|
|
import java.util.Date;
|
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";
|
|
|
|
|
2016-02-07 08:28:02 +00:00
|
|
|
private boolean running = false;
|
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;
|
2016-02-05 15:59:40 +00:00
|
|
|
private static final int REQUEST_METERED2 = 3;
|
|
|
|
private static final int REQUEST_METERED3 = 4;
|
|
|
|
private static final int REQUEST_METERED4 = 5;
|
|
|
|
private static final int REQUEST_ROAMING_NATIONAL = 6;
|
|
|
|
private static final int REQUEST_ROAMING_INTERNATIONAL = 7;
|
|
|
|
private static final int REQUEST_HOSTS = 8;
|
2016-01-17 09:42:21 +00:00
|
|
|
|
2016-02-07 08:28:02 +00:00
|
|
|
private AlertDialog dialogFilter = null;
|
|
|
|
|
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);
|
2016-02-01 16:42:36 +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);
|
2016-02-07 08:28:02 +00:00
|
|
|
running = true;
|
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);
|
2016-02-03 10:54:17 +00:00
|
|
|
final PreferenceScreen screen = getPreferenceScreen();
|
2016-02-03 12:23:54 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-12-08 13:18:06 +00:00
|
|
|
|
2016-07-23 12:36:23 +00:00
|
|
|
PreferenceGroup cat_options = (PreferenceGroup) ((PreferenceGroup) screen.findPreference("screen_options")).findPreference("category_options");
|
|
|
|
PreferenceGroup cat_advanced = (PreferenceGroup) ((PreferenceGroup) screen.findPreference("screen_advanced_options")).findPreference("category_advanced_options");
|
2016-07-23 12:36:47 +00:00
|
|
|
PreferenceGroup cat_stats = (PreferenceGroup) ((PreferenceGroup) screen.findPreference("screen_stats")).findPreference("category_stats");
|
2016-07-23 12:36:23 +00:00
|
|
|
PreferenceGroup cat_backup = (PreferenceGroup) ((PreferenceGroup) screen.findPreference("screen_backup")).findPreference("category_backup");
|
2016-02-13 06:54:44 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-27 07:37:45 +00:00
|
|
|
// Wi-Fi home
|
|
|
|
MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) screen.findPreference("wifi_homes");
|
2016-07-03 09:44:47 +00:00
|
|
|
Set<String> ssids = prefs.getStringSet("wifi_homes", new HashSet<String>());
|
|
|
|
if (ssids.size() > 0)
|
|
|
|
pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, TextUtils.join(", ", ssids)));
|
2016-01-27 07:37:45 +00:00
|
|
|
else
|
|
|
|
pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, "-"));
|
|
|
|
|
2016-12-21 15:26:59 +00:00
|
|
|
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
2016-01-27 07:37:45 +00:00
|
|
|
List<CharSequence> listSSID = new ArrayList<>();
|
|
|
|
List<WifiConfiguration> configs = wm.getConfiguredNetworks();
|
|
|
|
if (configs != null)
|
|
|
|
for (WifiConfiguration config : configs)
|
|
|
|
listSSID.add(config.SSID == null ? "NULL" : config.SSID);
|
2016-07-03 09:44:47 +00:00
|
|
|
for (String ssid : ssids)
|
|
|
|
if (!listSSID.contains(ssid))
|
|
|
|
listSSID.add(ssid);
|
2016-01-27 07:37:45 +00:00
|
|
|
pref_wifi_homes.setEntries(listSSID.toArray(new CharSequence[0]));
|
|
|
|
pref_wifi_homes.setEntryValues(listSSID.toArray(new CharSequence[0]));
|
|
|
|
|
2016-02-15 18:49:33 +00:00
|
|
|
Preference pref_reset_usage = screen.findPreference("reset_usage");
|
|
|
|
pref_reset_usage.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
2016-02-13 06:54:44 +00:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2016-02-20 07:36:34 +00:00
|
|
|
Util.areYouSure(ActivitySettings.this, R.string.setting_reset_usage, new Util.DoubtListener() {
|
2016-02-15 18:49:33 +00:00
|
|
|
@Override
|
2016-02-20 07:36:34 +00:00
|
|
|
public void onSure() {
|
|
|
|
new AsyncTask<Object, Object, Throwable>() {
|
|
|
|
@Override
|
|
|
|
protected Throwable doInBackground(Object... objects) {
|
|
|
|
try {
|
|
|
|
DatabaseHelper.getInstance(ActivitySettings.this).resetUsage(-1);
|
|
|
|
return null;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
return 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();
|
2016-02-15 18:49:33 +00:00
|
|
|
}
|
2016-02-20 07:36:34 +00:00
|
|
|
});
|
2016-02-15 18:49:33 +00:00
|
|
|
return false;
|
2016-02-13 06:54:44 +00:00
|
|
|
}
|
|
|
|
});
|
2016-01-31 12:32:58 +00:00
|
|
|
|
2016-02-09 09:53:52 +00:00
|
|
|
// Handle port forwarding
|
|
|
|
Preference pref_forwarding = screen.findPreference("forwarding");
|
|
|
|
pref_forwarding.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
startActivity(new Intent(ActivitySettings.this, ActivityForwarding.class));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-08 06:30:42 +00:00
|
|
|
boolean can = Util.canFilter(this);
|
2017-04-08 08:58:31 +00:00
|
|
|
TwoStatePreference pref_log_app = (TwoStatePreference) screen.findPreference("log_app");
|
2017-04-08 06:30:42 +00:00
|
|
|
TwoStatePreference pref_filter = (TwoStatePreference) screen.findPreference("filter");
|
2017-04-08 08:58:31 +00:00
|
|
|
pref_log_app.setEnabled(can);
|
2017-04-08 06:30:42 +00:00
|
|
|
pref_filter.setEnabled(can);
|
2017-04-11 05:49:28 +00:00
|
|
|
if (!can) {
|
|
|
|
pref_log_app.setSummary(R.string.msg_unavailable);
|
|
|
|
pref_filter.setSummary(R.string.msg_unavailable);
|
|
|
|
}
|
2017-04-08 06:30:42 +00:00
|
|
|
|
2016-01-27 07:37:45 +00:00
|
|
|
// VPN parameters
|
|
|
|
screen.findPreference("vpn4").setTitle(getString(R.string.setting_vpn4, prefs.getString("vpn4", "10.1.10.1")));
|
2016-01-28 06:52:58 +00:00
|
|
|
screen.findPreference("vpn6").setTitle(getString(R.string.setting_vpn6, prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1")));
|
2016-10-12 12:59:39 +00:00
|
|
|
EditTextPreference pref_dns1 = (EditTextPreference) screen.findPreference("dns");
|
|
|
|
EditTextPreference pref_dns2 = (EditTextPreference) screen.findPreference("dns2");
|
2016-12-23 09:25:18 +00:00
|
|
|
EditTextPreference pref_ttl = (EditTextPreference) screen.findPreference("ttl");
|
2016-02-21 13:00:17 +00:00
|
|
|
List<String> def_dns = Util.getDefaultDNS(this);
|
2016-10-12 12:59:39 +00:00
|
|
|
pref_dns1.getEditText().setHint(def_dns.get(0));
|
|
|
|
pref_dns2.getEditText().setHint(def_dns.get(1));
|
|
|
|
pref_dns1.setTitle(getString(R.string.setting_dns, prefs.getString("dns", def_dns.get(0))));
|
|
|
|
pref_dns2.setTitle(getString(R.string.setting_dns, prefs.getString("dns2", def_dns.get(1))));
|
2017-02-04 08:34:07 +00:00
|
|
|
pref_ttl.setTitle(getString(R.string.setting_ttl, prefs.getString("ttl", "259200")));
|
2016-01-27 07:37:45 +00:00
|
|
|
|
2016-07-28 15:46:19 +00:00
|
|
|
// SOCKS5 parameters
|
|
|
|
screen.findPreference("socks5_addr").setTitle(getString(R.string.setting_socks5_addr, prefs.getString("socks5_addr", "-")));
|
|
|
|
screen.findPreference("socks5_port").setTitle(getString(R.string.setting_socks5_port, prefs.getString("socks5_port", "-")));
|
2016-07-28 21:27:13 +00:00
|
|
|
screen.findPreference("socks5_username").setTitle(getString(R.string.setting_socks5_username, prefs.getString("socks5_username", "-")));
|
|
|
|
screen.findPreference("socks5_password").setTitle(getString(R.string.setting_socks5_password, TextUtils.isEmpty(prefs.getString("socks5_username", "")) ? "-" : "*****"));
|
2016-07-28 15:46:19 +00:00
|
|
|
|
2016-02-14 10:01:36 +00:00
|
|
|
// PCAP parameters
|
|
|
|
screen.findPreference("pcap_record_size").setTitle(getString(R.string.setting_pcap_record_size, prefs.getString("pcap_record_size", "64")));
|
|
|
|
screen.findPreference("pcap_file_size").setTitle(getString(R.string.setting_pcap_file_size, prefs.getString("pcap_file_size", "2")));
|
|
|
|
|
2016-07-31 14:01:24 +00:00
|
|
|
// Watchdog
|
2016-08-01 06:42:13 +00:00
|
|
|
screen.findPreference("watchdog").setTitle(getString(R.string.setting_watchdog, prefs.getString("watchdog", "0")));
|
2016-07-31 14:01:24 +00:00
|
|
|
|
2016-11-10 07:54:32 +00:00
|
|
|
// Show resolved
|
|
|
|
Preference pref_show_resolved = screen.findPreference("show_resolved");
|
2017-02-12 09:17:12 +00:00
|
|
|
if (Util.isPlayStoreInstall(this))
|
2016-11-10 07:54:32 +00:00
|
|
|
cat_advanced.removePreference(pref_show_resolved);
|
2017-02-12 09:17:12 +00:00
|
|
|
else
|
2016-11-10 07:54:32 +00:00
|
|
|
pref_show_resolved.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
startActivity(new Intent(ActivitySettings.this, ActivityDns.class));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-12-14 15:09:18 +00:00
|
|
|
// Handle stats
|
2016-07-23 12:36:47 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
|
cat_stats.removePreference(screen.findPreference("show_top"));
|
2015-12-14 15:09:18 +00:00
|
|
|
EditTextPreference pref_stats_frequency = (EditTextPreference) screen.findPreference("stats_frequency");
|
|
|
|
EditTextPreference pref_stats_samples = (EditTextPreference) screen.findPreference("stats_samples");
|
|
|
|
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");
|
2016-01-23 11:48:17 +00:00
|
|
|
pref_export.setEnabled(getIntentCreateExport().resolveActivity(getPackageManager()) != null);
|
2015-12-03 10:23:10 +00:00
|
|
|
pref_export.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2016-01-23 11:48:17 +00:00
|
|
|
startActivityForResult(getIntentCreateExport(), ActivitySettings.REQUEST_EXPORT);
|
2015-12-03 10:23:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle import
|
|
|
|
Preference pref_import = screen.findPreference("import");
|
2016-01-23 11:48:17 +00:00
|
|
|
pref_import.setEnabled(getIntentOpenExport().resolveActivity(getPackageManager()) != null);
|
2015-12-03 10:23:10 +00:00
|
|
|
pref_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2016-01-23 11:48:17 +00:00
|
|
|
startActivityForResult(getIntentOpenExport(), ActivitySettings.REQUEST_IMPORT);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-03 10:54:17 +00:00
|
|
|
// Hosts file settings
|
2016-01-23 20:30:54 +00:00
|
|
|
Preference pref_block_domains = screen.findPreference("use_hosts");
|
2017-02-12 09:17:12 +00:00
|
|
|
EditTextPreference pref_rcode = (EditTextPreference) screen.findPreference("rcode");
|
2016-02-12 09:48:28 +00:00
|
|
|
Preference pref_hosts_import = screen.findPreference("hosts_import");
|
2016-02-03 10:54:17 +00:00
|
|
|
EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
|
2016-02-03 12:23:54 +00:00
|
|
|
final Preference pref_hosts_download = screen.findPreference("hosts_download");
|
2016-01-23 20:30:54 +00:00
|
|
|
|
2017-02-12 09:17:12 +00:00
|
|
|
pref_rcode.setTitle(getString(R.string.setting_rcode, prefs.getString("rcode", "3")));
|
|
|
|
|
2016-01-23 16:08:03 +00:00
|
|
|
if (Util.isPlayStoreInstall(this)) {
|
2016-07-23 12:36:23 +00:00
|
|
|
Log.i(TAG, "Play store install");
|
2016-03-04 10:25:12 +00:00
|
|
|
cat_options.removePreference(screen.findPreference("update_check"));
|
2016-02-13 06:54:44 +00:00
|
|
|
cat_advanced.removePreference(pref_block_domains);
|
2017-02-12 09:17:12 +00:00
|
|
|
cat_advanced.removePreference(pref_rcode);
|
2016-03-08 11:05:17 +00:00
|
|
|
cat_advanced.removePreference(pref_forwarding);
|
2016-02-13 06:54:44 +00:00
|
|
|
cat_backup.removePreference(pref_hosts_import);
|
|
|
|
cat_backup.removePreference(pref_hosts_url);
|
|
|
|
cat_backup.removePreference(pref_hosts_download);
|
2016-01-23 20:30:54 +00:00
|
|
|
|
2016-01-23 16:08:03 +00:00
|
|
|
} else {
|
2016-02-12 09:48:28 +00:00
|
|
|
String last_import = prefs.getString("hosts_last_import", null);
|
|
|
|
String last_download = prefs.getString("hosts_last_download", null);
|
|
|
|
if (last_import != null)
|
|
|
|
pref_hosts_import.setSummary(getString(R.string.msg_import_last, last_import));
|
|
|
|
if (last_download != null)
|
|
|
|
pref_hosts_download.setSummary(getString(R.string.msg_download_last, last_download));
|
|
|
|
|
2016-02-03 10:54:17 +00:00
|
|
|
// Handle hosts import
|
|
|
|
// https://github.com/Free-Software-for-Android/AdAway/wiki/HostsSources
|
2016-02-12 09:48:28 +00:00
|
|
|
pref_hosts_import.setEnabled(getIntentOpenHosts().resolveActivity(getPackageManager()) != null);
|
|
|
|
pref_hosts_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
2016-01-23 16:08:03 +00:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
startActivityForResult(getIntentOpenHosts(), ActivitySettings.REQUEST_HOSTS);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2016-02-03 10:54:17 +00:00
|
|
|
|
|
|
|
// Handle hosts file download
|
|
|
|
pref_hosts_url.setSummary(pref_hosts_url.getText());
|
|
|
|
pref_hosts_download.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
2016-02-03 12:23:54 +00:00
|
|
|
final File tmp = new File(getFilesDir(), "hosts.tmp");
|
2016-02-03 10:54:17 +00:00
|
|
|
final File hosts = new File(getFilesDir(), "hosts.txt");
|
|
|
|
EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
|
|
|
|
try {
|
2016-02-03 12:23:54 +00:00
|
|
|
new DownloadTask(ActivitySettings.this, new URL(pref_hosts_url.getText()), tmp, new DownloadTask.Listener() {
|
2016-02-03 10:54:17 +00:00
|
|
|
@Override
|
|
|
|
public void onCompleted() {
|
2016-02-03 12:23:54 +00:00
|
|
|
if (hosts.exists())
|
|
|
|
hosts.delete();
|
|
|
|
tmp.renameTo(hosts);
|
2016-02-07 08:28:02 +00:00
|
|
|
|
2016-02-03 12:23:54 +00:00
|
|
|
String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
|
2016-02-12 09:48:28 +00:00
|
|
|
prefs.edit().putString("hosts_last_download", last).apply();
|
2016-02-07 08:28:02 +00:00
|
|
|
|
|
|
|
if (running) {
|
|
|
|
pref_hosts_download.setSummary(getString(R.string.msg_download_last, last));
|
|
|
|
Toast.makeText(ActivitySettings.this, R.string.msg_downloaded, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("hosts file download", ActivitySettings.this);
|
2016-02-03 10:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCancelled() {
|
2016-02-03 12:23:54 +00:00
|
|
|
if (tmp.exists())
|
|
|
|
tmp.delete();
|
2016-02-03 10:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onException(Throwable ex) {
|
2016-02-03 12:23:54 +00:00
|
|
|
if (tmp.exists())
|
|
|
|
tmp.delete();
|
2016-02-07 08:28:02 +00:00
|
|
|
|
|
|
|
if (running)
|
|
|
|
Toast.makeText(ActivitySettings.this, ex.getMessage(), Toast.LENGTH_LONG).show();
|
2016-02-03 10:54:17 +00:00
|
|
|
}
|
|
|
|
}).execute();
|
|
|
|
} catch (MalformedURLException ex) {
|
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2016-01-23 16:08:03 +00:00
|
|
|
}
|
2015-12-03 10:23:10 +00:00
|
|
|
|
2016-02-03 10:54:17 +00:00
|
|
|
// Development
|
2016-07-24 08:40:12 +00:00
|
|
|
if (!Util.isDebuggable(this))
|
|
|
|
screen.removePreference(screen.findPreference("screen_development"));
|
2016-02-15 18:49:33 +00:00
|
|
|
|
2016-02-02 18:50:28 +00:00
|
|
|
// Handle technical info
|
|
|
|
Preference.OnPreferenceClickListener listener = new Preference.OnPreferenceClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
updateTechnicalInfo();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Technical info
|
|
|
|
Preference pref_technical_info = screen.findPreference("technical_info");
|
|
|
|
Preference pref_technical_network = screen.findPreference("technical_network");
|
|
|
|
Preference pref_technical_subscription = screen.findPreference("technical_subscription");
|
|
|
|
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);
|
|
|
|
updateTechnicalInfo();
|
2016-07-29 07:46:20 +00:00
|
|
|
|
|
|
|
markPro(screen.findPreference("theme"), ActivityPro.SKU_THEME);
|
|
|
|
markPro(screen.findPreference("install"), ActivityPro.SKU_NOTIFY);
|
|
|
|
markPro(screen.findPreference("show_stats"), ActivityPro.SKU_SPEED);
|
2015-12-03 10:23:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 22:44:17 +00:00
|
|
|
@Override
|
2015-11-14 09:49:15 +00:00
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
2016-02-05 15:59:40 +00:00
|
|
|
// Check if permissions were revoked
|
|
|
|
checkPermissions();
|
2015-12-01 16:28:13 +00:00
|
|
|
|
|
|
|
// Listen for preference changes
|
2016-02-05 15:59:40 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
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
|
|
|
}
|
|
|
|
|
2016-02-07 08:28:02 +00:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
running = false;
|
|
|
|
if (dialogFilter != null) {
|
|
|
|
dialogFilter.dismiss();
|
|
|
|
dialogFilter = null;
|
|
|
|
}
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2016-02-02 08:01:04 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
Log.i(TAG, "Up");
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2016-07-29 07:46:20 +00:00
|
|
|
} else if ("install".equals(name)) {
|
|
|
|
if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_NOTIFY, this)) {
|
2015-12-30 08:58:21 +00:00
|
|
|
prefs.edit().putBoolean(name, false).apply();
|
2016-07-29 07:46:20 +00:00
|
|
|
((TwoStatePreference) getPreferenceScreen().findPreference(name)).setChecked(false);
|
2015-12-30 08:58:21 +00:00
|
|
|
startActivity(new Intent(this, ActivityPro.class));
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 07:46:20 +00:00
|
|
|
} else if ("show_stats".equals(name)) {
|
|
|
|
if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_SPEED, this)) {
|
2016-04-08 06:12:46 +00:00
|
|
|
prefs.edit().putBoolean(name, false).apply();
|
|
|
|
startActivity(new Intent(this, ActivityPro.class));
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 07:46:20 +00:00
|
|
|
((TwoStatePreference) getPreferenceScreen().findPreference(name)).setChecked(prefs.getBoolean(name, false));
|
2015-12-30 08:58:21 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 10:01:36 +00:00
|
|
|
Object value = prefs.getAll().get(name);
|
|
|
|
if (value instanceof String && "".equals(value))
|
|
|
|
prefs.edit().remove(name).apply();
|
|
|
|
|
2016-01-27 07:37:45 +00:00
|
|
|
// Dependencies
|
2016-12-21 15:03:09 +00:00
|
|
|
if ("screen_on".equals(name))
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
|
|
|
else if ("whitelist_wifi".equals(name) ||
|
2015-11-30 13:02:00 +00:00
|
|
|
"screen_wifi".equals(name))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, 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))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, 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))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, 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
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2015-12-31 09:03:32 +00:00
|
|
|
|
2016-01-27 07:37:45 +00:00
|
|
|
} else if ("auto_enable".equals(name))
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_auto, prefs.getString(name, "0")));
|
|
|
|
|
|
|
|
else if ("screen_delay".equals(name))
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_delay, prefs.getString(name, "0")));
|
|
|
|
|
|
|
|
else if ("theme".equals(name) || "dark_theme".equals(name))
|
|
|
|
recreate();
|
|
|
|
|
2016-05-13 18:32:41 +00:00
|
|
|
else if ("subnet".equals(name))
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
2016-01-27 07:37:45 +00:00
|
|
|
else if ("tethering".equals(name))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
|
2016-03-31 08:01:20 +00:00
|
|
|
else if ("lan".equals(name))
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
2016-04-08 06:02:52 +00:00
|
|
|
else if ("ip6".equals(name))
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
2016-01-27 07:37:45 +00:00
|
|
|
else if ("wifi_homes".equals(name)) {
|
|
|
|
MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) getPreferenceScreen().findPreference(name);
|
|
|
|
Set<String> ssid = prefs.getStringSet(name, new HashSet<String>());
|
|
|
|
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, "-"));
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
|
|
|
|
} else if ("use_metered".equals(name))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
|
|
|
|
else if ("unmetered_2g".equals(name) ||
|
|
|
|
"unmetered_3g".equals(name) ||
|
|
|
|
"unmetered_4g".equals(name)) {
|
|
|
|
if (prefs.getBoolean(name, false)) {
|
|
|
|
if (Util.hasPhoneStatePermission(this))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-02-05 15:59:40 +00:00
|
|
|
else {
|
|
|
|
if ("unmetered_2g".equals(name))
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_METERED2);
|
|
|
|
else if ("unmetered_3g".equals(name))
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_METERED3);
|
|
|
|
else if ("unmetered_4g".equals(name))
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_METERED4);
|
|
|
|
}
|
2016-01-27 07:37:45 +00:00
|
|
|
} else
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
|
|
|
|
} else if ("national_roaming".equals(name)) {
|
|
|
|
if (prefs.getBoolean(name, false)) {
|
|
|
|
if (Util.hasPhoneStatePermission(this))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
else
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_NATIONAL);
|
|
|
|
} else
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
|
2017-03-21 15:36:24 +00:00
|
|
|
} else if ("lockdown_wifi".equals(name) || "lockdown_other".equals(name))
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
|
2017-03-21 15:36:24 +00:00
|
|
|
else if ("manage_system".equals(name)) {
|
2016-01-05 21:57:37 +00:00
|
|
|
boolean manage = prefs.getBoolean(name, false);
|
|
|
|
if (!manage)
|
|
|
|
prefs.edit().putBoolean("show_user", true).apply();
|
|
|
|
prefs.edit().putBoolean("show_system", manage).apply();
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2015-12-27 15:04:22 +00:00
|
|
|
|
2016-02-01 07:25:08 +00:00
|
|
|
} else if ("log_app".equals(name)) {
|
|
|
|
Intent ruleset = new Intent(ActivityMain.ACTION_RULES_CHANGED);
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(ruleset);
|
2016-01-05 12:40:02 +00:00
|
|
|
|
2016-01-19 16:54:07 +00:00
|
|
|
} else if ("filter".equals(name)) {
|
2016-01-19 17:29:58 +00:00
|
|
|
// Show dialog
|
2016-09-11 12:13:45 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && prefs.getBoolean(name, false)) {
|
2016-01-19 17:29:58 +00:00
|
|
|
LayoutInflater inflater = LayoutInflater.from(ActivitySettings.this);
|
2016-02-13 14:55:22 +00:00
|
|
|
View view = inflater.inflate(R.layout.filter, null, false);
|
2016-02-07 08:28:02 +00:00
|
|
|
dialogFilter = new AlertDialog.Builder(ActivitySettings.this)
|
2016-01-19 17:29:58 +00:00
|
|
|
.setView(view)
|
|
|
|
.setCancelable(false)
|
|
|
|
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
})
|
2016-02-07 08:28:02 +00:00
|
|
|
.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
|
|
|
@Override
|
|
|
|
public void onDismiss(DialogInterface dialogInterface) {
|
|
|
|
dialogFilter = null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.create();
|
|
|
|
dialogFilter.show();
|
2016-09-11 12:13:45 +00:00
|
|
|
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && !prefs.getBoolean(name, false)) {
|
|
|
|
prefs.edit().putBoolean(name, true).apply();
|
|
|
|
Toast.makeText(ActivitySettings.this, R.string.msg_filter4, Toast.LENGTH_SHORT).show();
|
2016-01-19 17:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:32:16 +00:00
|
|
|
((TwoStatePreference) getPreferenceScreen().findPreference(name)).setChecked(prefs.getBoolean(name, false));
|
|
|
|
|
2016-09-11 12:13:45 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
2016-02-21 08:04:03 +00:00
|
|
|
} else if ("use_hosts".equals(name))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-23 20:30:54 +00:00
|
|
|
|
2016-01-27 07:37:45 +00:00
|
|
|
else if ("vpn4".equals(name)) {
|
2016-07-29 05:18:11 +00:00
|
|
|
String vpn4 = prefs.getString(name, null);
|
2016-01-27 07:37:45 +00:00
|
|
|
try {
|
2016-02-11 06:50:02 +00:00
|
|
|
checkAddress(vpn4);
|
2016-01-27 07:37:45 +00:00
|
|
|
} catch (Throwable ex) {
|
2016-07-29 05:18:11 +00:00
|
|
|
prefs.edit().remove(name).apply();
|
2016-03-09 13:10:33 +00:00
|
|
|
((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
|
2016-02-25 15:01:07 +00:00
|
|
|
if (!TextUtils.isEmpty(vpn4))
|
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
2016-01-27 07:37:45 +00:00
|
|
|
}
|
2016-07-29 05:18:11 +00:00
|
|
|
getPreferenceScreen().findPreference(name).setTitle(
|
|
|
|
getString(R.string.setting_vpn4, prefs.getString(name, "10.1.10.1")));
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
|
|
|
|
} else if ("vpn6".equals(name)) {
|
2016-07-29 05:18:11 +00:00
|
|
|
String vpn6 = prefs.getString(name, null);
|
2016-01-27 07:37:45 +00:00
|
|
|
try {
|
2016-02-11 06:50:02 +00:00
|
|
|
checkAddress(vpn6);
|
2016-01-27 07:37:45 +00:00
|
|
|
} catch (Throwable ex) {
|
2016-07-29 05:18:11 +00:00
|
|
|
prefs.edit().remove(name).apply();
|
2016-03-09 13:10:33 +00:00
|
|
|
((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
|
2016-02-25 15:01:07 +00:00
|
|
|
if (!TextUtils.isEmpty(vpn6))
|
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
2016-01-27 07:37:45 +00:00
|
|
|
}
|
2016-07-29 05:18:11 +00:00
|
|
|
getPreferenceScreen().findPreference(name).setTitle(
|
|
|
|
getString(R.string.setting_vpn6, prefs.getString(name, "fd00:1:fd00:1:fd00:1:fd00:1")));
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-01-27 07:37:45 +00:00
|
|
|
|
2016-10-12 12:59:39 +00:00
|
|
|
} else if ("dns".equals(name) || "dns2".equals(name)) {
|
2016-07-29 05:18:11 +00:00
|
|
|
String dns = prefs.getString(name, null);
|
2016-01-27 07:37:45 +00:00
|
|
|
try {
|
2016-02-11 06:50:02 +00:00
|
|
|
checkAddress(dns);
|
2016-01-27 07:37:45 +00:00
|
|
|
} catch (Throwable ex) {
|
2016-07-29 05:18:11 +00:00
|
|
|
prefs.edit().remove(name).apply();
|
2016-03-09 13:10:33 +00:00
|
|
|
((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
|
2016-02-25 15:01:07 +00:00
|
|
|
if (!TextUtils.isEmpty(dns))
|
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
2016-01-27 07:37:45 +00:00
|
|
|
}
|
2016-02-21 13:00:17 +00:00
|
|
|
getPreferenceScreen().findPreference(name).setTitle(
|
2016-10-12 12:59:39 +00:00
|
|
|
getString(R.string.setting_dns,
|
|
|
|
prefs.getString(name, Util.getDefaultDNS(this).get("dns".equals(name) ? 0 : 1))));
|
2016-07-29 05:18:11 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-03-09 11:07:02 +00:00
|
|
|
|
2016-12-23 09:25:18 +00:00
|
|
|
} else if ("ttl".equals(name))
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(
|
2017-02-04 08:34:07 +00:00
|
|
|
getString(R.string.setting_ttl, prefs.getString(name, "259200")));
|
2016-12-23 09:25:18 +00:00
|
|
|
|
2017-02-10 20:12:04 +00:00
|
|
|
else if ("rcode".equals(name)) {
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(
|
|
|
|
getString(R.string.setting_rcode, prefs.getString(name, "3")));
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
|
|
|
} else if ("socks5_enabled".equals(name))
|
2016-07-28 21:27:13 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
|
|
|
else if ("socks5_addr".equals(name)) {
|
2016-07-29 05:18:11 +00:00
|
|
|
String socks5_addr = prefs.getString(name, null);
|
2016-07-28 15:46:19 +00:00
|
|
|
try {
|
2016-07-29 05:18:11 +00:00
|
|
|
if (!TextUtils.isEmpty(socks5_addr) && !Util.isNumericAddress(socks5_addr))
|
|
|
|
throw new IllegalArgumentException("Bad address");
|
2016-07-28 15:46:19 +00:00
|
|
|
} catch (Throwable ex) {
|
2016-07-29 05:18:11 +00:00
|
|
|
prefs.edit().remove(name).apply();
|
2016-07-28 15:46:19 +00:00
|
|
|
((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
|
2016-07-29 05:18:11 +00:00
|
|
|
if (!TextUtils.isEmpty(socks5_addr))
|
2016-07-28 15:46:19 +00:00
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(
|
2016-07-29 05:18:11 +00:00
|
|
|
getString(R.string.setting_socks5_addr, prefs.getString(name, "-")));
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2016-07-28 15:46:19 +00:00
|
|
|
|
|
|
|
} else if ("socks5_port".equals(name)) {
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_socks5_port, prefs.getString(name, "-")));
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
2016-07-28 21:27:13 +00:00
|
|
|
} else if ("socks5_username".equals(name)) {
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_socks5_username, prefs.getString(name, "-")));
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
|
|
|
} else if ("socks5_password".equals(name)) {
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_socks5_password, TextUtils.isEmpty(prefs.getString(name, "")) ? "-" : "*****"));
|
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
2016-02-14 10:01:36 +00:00
|
|
|
} else if ("pcap_record_size".equals(name) || "pcap_file_size".equals(name)) {
|
|
|
|
if ("pcap_record_size".equals(name))
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_pcap_record_size, prefs.getString(name, "64")));
|
|
|
|
else
|
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_pcap_file_size, prefs.getString(name, "2")));
|
|
|
|
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.setPcap(false, this);
|
2016-02-14 10:01:36 +00:00
|
|
|
|
2017-02-18 12:37:02 +00:00
|
|
|
File pcap_file = new File(getDir("data", MODE_PRIVATE), "netguard.pcap");
|
2016-02-14 10:01:36 +00:00
|
|
|
if (pcap_file.exists() && !pcap_file.delete())
|
|
|
|
Log.w(TAG, "Delete PCAP failed");
|
|
|
|
|
|
|
|
if (prefs.getBoolean("pcap", false))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.setPcap(true, this);
|
2015-12-01 16:07:37 +00:00
|
|
|
|
2016-07-31 14:01:24 +00:00
|
|
|
} else if ("watchdog".equals(name)) {
|
2016-08-01 06:42:13 +00:00
|
|
|
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_watchdog, prefs.getString(name, "0")));
|
2016-07-31 14:01:24 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
|
|
|
|
2016-06-24 20:01:30 +00:00
|
|
|
} else if ("show_stats".equals(name))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reloadStats("changed " + name, this);
|
2015-11-04 22:44:17 +00:00
|
|
|
|
2016-06-24 20:01:30 +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
|
|
|
|
2016-02-03 10:54:17 +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")));
|
2016-01-18 12:07:00 +00:00
|
|
|
|
2016-02-03 10:54:17 +00:00
|
|
|
else if ("hosts_url".equals(name))
|
|
|
|
getPreferenceScreen().findPreference(name).setSummary(prefs.getString(name, "http://www.netguard.me/hosts"));
|
|
|
|
|
|
|
|
else if ("loglevel".equals(name))
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("changed " + name, this);
|
2015-11-04 22:44:17 +00:00
|
|
|
}
|
2015-11-05 10:26:21 +00:00
|
|
|
|
2016-02-11 06:50:02 +00:00
|
|
|
private void checkAddress(String address) throws IllegalArgumentException, UnknownHostException {
|
|
|
|
if (address == null || TextUtils.isEmpty(address.trim()))
|
|
|
|
throw new IllegalArgumentException("Bad address");
|
2016-02-27 08:17:44 +00:00
|
|
|
if (!Util.isNumericAddress(address))
|
|
|
|
throw new IllegalArgumentException("Bad address");
|
2016-02-11 06:50:02 +00:00
|
|
|
InetAddress idns = InetAddress.getByName(address);
|
|
|
|
if (idns.isLoopbackAddress() || idns.isAnyLocalAddress())
|
|
|
|
throw new IllegalArgumentException("Bad address");
|
|
|
|
}
|
|
|
|
|
2016-02-05 15:59:40 +00:00
|
|
|
@TargetApi(Build.VERSION_CODES.M)
|
|
|
|
private void checkPermissions() {
|
2015-12-03 13:05:38 +00:00
|
|
|
PreferenceScreen screen = getPreferenceScreen();
|
2016-02-05 15:59:40 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-12-03 13:05:38 +00:00
|
|
|
|
2016-02-05 15:59:40 +00:00
|
|
|
// Check if permission was revoked
|
|
|
|
if (prefs.getBoolean("whitelist_roaming", false))
|
|
|
|
if (!Util.hasPhoneStatePermission(this)) {
|
|
|
|
prefs.edit().putBoolean("whitelist_roaming", false).apply();
|
|
|
|
((TwoStatePreference) screen.findPreference("whitelist_roaming")).setChecked(false);
|
|
|
|
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_INTERNATIONAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if permission was revoked
|
|
|
|
if (prefs.getBoolean("unmetered_2g", false))
|
|
|
|
if (!Util.hasPhoneStatePermission(this)) {
|
2015-12-02 19:11:06 +00:00
|
|
|
prefs.edit().putBoolean("unmetered_2g", false).apply();
|
2016-02-01 17:19:25 +00:00
|
|
|
((TwoStatePreference) screen.findPreference("unmetered_2g")).setChecked(false);
|
2016-02-05 15:59:40 +00:00
|
|
|
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_METERED2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefs.getBoolean("unmetered_3g", false))
|
|
|
|
if (!Util.hasPhoneStatePermission(this)) {
|
|
|
|
prefs.edit().putBoolean("unmetered_3g", false).apply();
|
2016-02-01 17:19:25 +00:00
|
|
|
((TwoStatePreference) screen.findPreference("unmetered_3g")).setChecked(false);
|
2016-02-05 15:59:40 +00:00
|
|
|
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_METERED3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefs.getBoolean("unmetered_4g", false))
|
|
|
|
if (!Util.hasPhoneStatePermission(this)) {
|
|
|
|
prefs.edit().putBoolean("unmetered_4g", false).apply();
|
2016-02-01 17:19:25 +00:00
|
|
|
((TwoStatePreference) screen.findPreference("unmetered_4g")).setChecked(false);
|
2016-02-05 15:59:40 +00:00
|
|
|
|
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_METERED4);
|
2015-12-02 16:53:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 15:59:40 +00:00
|
|
|
// Check if permission was revoked
|
|
|
|
if (prefs.getBoolean("national_roaming", false))
|
|
|
|
if (!Util.hasPhoneStatePermission(this)) {
|
2015-12-01 16:07:37 +00:00
|
|
|
prefs.edit().putBoolean("national_roaming", false).apply();
|
2016-02-01 17:19:25 +00:00
|
|
|
((TwoStatePreference) screen.findPreference("national_roaming")).setChecked(false);
|
2015-12-02 09:42:24 +00:00
|
|
|
|
2016-02-05 15:59:40 +00:00
|
|
|
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_NATIONAL);
|
2015-12-02 09:42:24 +00:00
|
|
|
}
|
2015-12-01 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 15:59:40 +00:00
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
PreferenceScreen screen = getPreferenceScreen();
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2016-03-09 16:34:21 +00:00
|
|
|
boolean granted = (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
|
2016-02-05 15:59:40 +00:00
|
|
|
|
|
|
|
if (requestCode == REQUEST_METERED2) {
|
|
|
|
prefs.edit().putBoolean("unmetered_2g", granted).apply();
|
|
|
|
((TwoStatePreference) screen.findPreference("unmetered_2g")).setChecked(granted);
|
|
|
|
|
|
|
|
} else if (requestCode == REQUEST_METERED3) {
|
|
|
|
prefs.edit().putBoolean("unmetered_3g", granted).apply();
|
|
|
|
((TwoStatePreference) screen.findPreference("unmetered_3g")).setChecked(granted);
|
|
|
|
|
|
|
|
} else if (requestCode == REQUEST_METERED4) {
|
|
|
|
prefs.edit().putBoolean("unmetered_4g", granted).apply();
|
|
|
|
((TwoStatePreference) screen.findPreference("unmetered_4g")).setChecked(granted);
|
|
|
|
|
|
|
|
} else if (requestCode == REQUEST_ROAMING_NATIONAL) {
|
|
|
|
prefs.edit().putBoolean("national_roaming", granted).apply();
|
|
|
|
((TwoStatePreference) screen.findPreference("national_roaming")).setChecked(granted);
|
|
|
|
|
|
|
|
} else if (requestCode == REQUEST_ROAMING_INTERNATIONAL) {
|
|
|
|
prefs.edit().putBoolean("whitelist_roaming", granted).apply();
|
|
|
|
((TwoStatePreference) screen.findPreference("whitelist_roaming")).setChecked(granted);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (granted)
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("permission granted", this);
|
2016-02-05 15:59:40 +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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-29 07:46:20 +00:00
|
|
|
private void markPro(Preference pref, String sku) {
|
|
|
|
if (sku == null || !IAB.isPurchased(sku, this)) {
|
2016-10-06 11:57:07 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
boolean dark = prefs.getBoolean("dark_theme", false);
|
2016-07-29 07:46:20 +00:00
|
|
|
SpannableStringBuilder ssb = new SpannableStringBuilder(" " + pref.getTitle());
|
2016-10-06 11:57:07 +00:00
|
|
|
ssb.setSpan(new ImageSpan(this, dark ? R.drawable.ic_shopping_cart_white_24dp : R.drawable.ic_shopping_cart_black_24dp), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
2016-07-29 07:46:20 +00:00
|
|
|
pref.setTitle(ssb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2016-01-23 11:48:17 +00:00
|
|
|
} else if (requestCode == REQUEST_HOSTS) {
|
2016-01-23 21:14:04 +00:00
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
handleHosts(data);
|
2016-01-23 11:48:17 +00:00
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Unknown activity result request=" + requestCode);
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 14:12:35 +00:00
|
|
|
private Intent getIntentCreateExport() {
|
2016-06-26 17:32:06 +00:00
|
|
|
Intent intent;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
|
|
|
|
if (Util.isPackageInstalled("org.openintents.filemanager", this)) {
|
|
|
|
intent = new Intent("org.openintents.action.PICK_DIRECTORY");
|
|
|
|
} else {
|
|
|
|
intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=org.openintents.filemanager"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
intent.setType("*/*"); // text/xml
|
|
|
|
intent.putExtra(Intent.EXTRA_TITLE, "netguard_" + new SimpleDateFormat("yyyyMMdd").format(new Date().getTime()) + ".xml");
|
|
|
|
}
|
2016-01-17 09:42:21 +00:00
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2016-01-31 14:12:35 +00:00
|
|
|
private Intent getIntentOpenExport() {
|
2016-06-26 17:32:06 +00:00
|
|
|
Intent intent;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
|
|
|
|
intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
else
|
|
|
|
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
2016-01-17 09:42:21 +00:00
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
2016-01-29 18:01:04 +00:00
|
|
|
intent.setType("*/*"); // text/xml
|
2016-01-17 09:42:21 +00:00
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2016-01-31 14:12:35 +00:00
|
|
|
private Intent getIntentOpenHosts() {
|
2016-06-26 17:32:06 +00:00
|
|
|
Intent intent;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
|
|
|
|
intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
else
|
|
|
|
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
2016-01-23 11:48:17 +00:00
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
2016-01-29 18:01:04 +00:00
|
|
|
intent.setType("*/*"); // text/plain
|
2016-01-23 11:48:17 +00:00
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
private void handleExport(final Intent data) {
|
|
|
|
new AsyncTask<Object, Object, Throwable>() {
|
|
|
|
@Override
|
|
|
|
protected Throwable doInBackground(Object... objects) {
|
|
|
|
OutputStream out = null;
|
|
|
|
try {
|
2016-01-31 14:12:35 +00:00
|
|
|
Uri target = data.getData();
|
|
|
|
if (data.hasExtra("org.openintents.extra.DIR_PATH"))
|
2016-02-02 06:46:50 +00:00
|
|
|
target = Uri.parse(target + "/netguard_" + new SimpleDateFormat("yyyyMMdd").format(new Date().getTime()) + ".xml");
|
2016-01-31 14:12:35 +00:00
|
|
|
Log.i(TAG, "Writing URI=" + target);
|
|
|
|
out = getContentResolver().openOutputStream(target);
|
2015-11-05 10:26:21 +00:00
|
|
|
xmlExport(out);
|
|
|
|
return null;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
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) {
|
2016-02-07 08:28:02 +00:00
|
|
|
if (running) {
|
|
|
|
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();
|
|
|
|
}
|
2015-11-05 10:26:21 +00:00
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
2016-01-23 11:48:17 +00:00
|
|
|
private void handleHosts(final Intent data) {
|
|
|
|
new AsyncTask<Object, Object, Throwable>() {
|
|
|
|
@Override
|
|
|
|
protected Throwable doInBackground(Object... objects) {
|
2016-01-24 13:10:22 +00:00
|
|
|
File hosts = new File(getFilesDir(), "hosts.txt");
|
2016-01-23 21:14:04 +00:00
|
|
|
|
|
|
|
FileOutputStream out = null;
|
|
|
|
InputStream in = null;
|
|
|
|
try {
|
|
|
|
Log.i(TAG, "Reading URI=" + data.getData());
|
2016-07-22 12:12:38 +00:00
|
|
|
ContentResolver resolver = getContentResolver();
|
|
|
|
String[] streamTypes = resolver.getStreamTypes(data.getData(), "*/*");
|
2016-07-23 13:38:42 +00:00
|
|
|
String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]);
|
|
|
|
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(), streamType, null);
|
2016-07-22 12:12:38 +00:00
|
|
|
in = descriptor.createInputStream();
|
2016-01-23 21:14:04 +00:00
|
|
|
out = new FileOutputStream(hosts);
|
|
|
|
|
|
|
|
int len;
|
|
|
|
long total = 0;
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
while ((len = in.read(buf)) > 0) {
|
|
|
|
out.write(buf, 0, len);
|
|
|
|
total += len;
|
|
|
|
}
|
|
|
|
Log.i(TAG, "Copied bytes=" + total);
|
|
|
|
|
2016-01-23 11:48:17 +00:00
|
|
|
return null;
|
2016-01-23 21:14:04 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
return ex;
|
|
|
|
} finally {
|
|
|
|
if (out != null)
|
|
|
|
try {
|
|
|
|
out.close();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
if (in != null)
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2016-01-23 11:48:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Throwable ex) {
|
2016-02-07 08:28:02 +00:00
|
|
|
if (running) {
|
|
|
|
if (ex == null) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivitySettings.this);
|
2016-02-12 09:48:28 +00:00
|
|
|
String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
|
|
|
|
prefs.edit().putString("hosts_last_import", last).apply();
|
|
|
|
|
|
|
|
if (running) {
|
|
|
|
getPreferenceScreen().findPreference("hosts_import").setSummary(getString(R.string.msg_import_last, last));
|
|
|
|
Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2016-02-04 06:11:53 +00:00
|
|
|
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reload("hosts import", ActivitySettings.this);
|
2016-02-07 08:28:02 +00:00
|
|
|
} else
|
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2016-01-23 11:48:17 +00:00
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
Log.i(TAG, "Reading URI=" + data.getData());
|
2016-07-22 12:12:38 +00:00
|
|
|
ContentResolver resolver = getContentResolver();
|
|
|
|
String[] streamTypes = resolver.getStreamTypes(data.getData(), "*/*");
|
2016-07-23 13:38:42 +00:00
|
|
|
String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]);
|
|
|
|
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(), streamType, null);
|
2016-07-22 12:12:38 +00:00
|
|
|
in = descriptor.createInputStream();
|
2015-11-05 10:26:21 +00:00
|
|
|
xmlImport(in);
|
|
|
|
return null;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
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) {
|
2016-02-07 08:28:02 +00:00
|
|
|
if (running) {
|
|
|
|
if (ex == null) {
|
|
|
|
Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.reloadStats("import", ActivitySettings.this);
|
2016-02-07 08:28:02 +00:00
|
|
|
// Update theme, request permissions
|
|
|
|
recreate();
|
|
|
|
} else
|
|
|
|
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2015-11-05 10:26:21 +00:00
|
|
|
}
|
|
|
|
}.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
|
|
|
|
2017-03-19 13:41:03 +00:00
|
|
|
serializer.startTag(null, "roaming");
|
|
|
|
xmlExport(getSharedPreferences("roaming", Context.MODE_PRIVATE), serializer);
|
|
|
|
serializer.endTag(null, "roaming");
|
|
|
|
|
2017-03-21 08:06:04 +00:00
|
|
|
serializer.startTag(null, "lockdown");
|
|
|
|
xmlExport(getSharedPreferences("lockdown", Context.MODE_PRIVATE), serializer);
|
|
|
|
serializer.endTag(null, "lockdown");
|
|
|
|
|
2016-02-21 08:04:03 +00:00
|
|
|
serializer.startTag(null, "apply");
|
|
|
|
xmlExport(getSharedPreferences("apply", Context.MODE_PRIVATE), serializer);
|
|
|
|
serializer.endTag(null, "apply");
|
|
|
|
|
2016-02-19 15:49:32 +00:00
|
|
|
serializer.startTag(null, "notify");
|
|
|
|
xmlExport(getSharedPreferences("notify", Context.MODE_PRIVATE), serializer);
|
|
|
|
serializer.endTag(null, "notify");
|
|
|
|
|
2016-02-01 08:55:49 +00:00
|
|
|
serializer.startTag(null, "filter");
|
|
|
|
filterExport(serializer);
|
|
|
|
serializer.endTag(null, "filter");
|
|
|
|
|
2016-02-29 11:06:12 +00:00
|
|
|
serializer.startTag(null, "forward");
|
|
|
|
forwardExport(serializer);
|
|
|
|
serializer.endTag(null, "forward");
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:55:49 +00:00
|
|
|
private void filterExport(XmlSerializer serializer) throws IOException {
|
2016-02-10 11:50:51 +00:00
|
|
|
Cursor cursor = DatabaseHelper.getInstance(this).getAccess();
|
2016-02-01 08:55:49 +00:00
|
|
|
int colUid = cursor.getColumnIndex("uid");
|
2016-02-05 19:44:12 +00:00
|
|
|
int colVersion = cursor.getColumnIndex("version");
|
|
|
|
int colProtocol = cursor.getColumnIndex("protocol");
|
2016-02-01 08:55:49 +00:00
|
|
|
int colDAddr = cursor.getColumnIndex("daddr");
|
|
|
|
int colDPort = cursor.getColumnIndex("dport");
|
|
|
|
int colTime = cursor.getColumnIndex("time");
|
|
|
|
int colBlock = cursor.getColumnIndex("block");
|
2016-02-29 11:06:12 +00:00
|
|
|
while (cursor.moveToNext())
|
|
|
|
for (String pkg : getPackages(cursor.getInt(colUid))) {
|
2016-02-01 08:55:49 +00:00
|
|
|
serializer.startTag(null, "rule");
|
|
|
|
serializer.attribute(null, "pkg", pkg);
|
2016-02-05 19:44:12 +00:00
|
|
|
serializer.attribute(null, "version", Integer.toString(cursor.getInt(colVersion)));
|
|
|
|
serializer.attribute(null, "protocol", Integer.toString(cursor.getInt(colProtocol)));
|
2016-02-01 08:55:49 +00:00
|
|
|
serializer.attribute(null, "daddr", cursor.getString(colDAddr));
|
|
|
|
serializer.attribute(null, "dport", Integer.toString(cursor.getInt(colDPort)));
|
|
|
|
serializer.attribute(null, "time", Long.toString(cursor.getLong(colTime)));
|
|
|
|
serializer.attribute(null, "block", Integer.toString(cursor.getInt(colBlock)));
|
|
|
|
serializer.endTag(null, "rule");
|
|
|
|
}
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
2016-02-29 11:06:12 +00:00
|
|
|
private void forwardExport(XmlSerializer serializer) throws IOException {
|
|
|
|
Cursor cursor = DatabaseHelper.getInstance(this).getForwarding();
|
|
|
|
int colProtocol = cursor.getColumnIndex("protocol");
|
|
|
|
int colDPort = cursor.getColumnIndex("dport");
|
|
|
|
int colRAddr = cursor.getColumnIndex("raddr");
|
|
|
|
int colRPort = cursor.getColumnIndex("rport");
|
|
|
|
int colRUid = cursor.getColumnIndex("ruid");
|
|
|
|
while (cursor.moveToNext())
|
|
|
|
for (String pkg : getPackages(cursor.getInt(colRUid))) {
|
|
|
|
serializer.startTag(null, "port");
|
|
|
|
serializer.attribute(null, "pkg", pkg);
|
|
|
|
serializer.attribute(null, "protocol", Integer.toString(cursor.getInt(colProtocol)));
|
|
|
|
serializer.attribute(null, "dport", Integer.toString(cursor.getInt(colDPort)));
|
|
|
|
serializer.attribute(null, "raddr", cursor.getString(colRAddr));
|
|
|
|
serializer.attribute(null, "rport", Integer.toString(cursor.getInt(colRPort)));
|
|
|
|
serializer.endTag(null, "port");
|
|
|
|
}
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private String[] getPackages(int uid) {
|
|
|
|
if (uid == 0)
|
|
|
|
return new String[]{"root"};
|
|
|
|
else if (uid == 1013)
|
|
|
|
return new String[]{"mediaserver"};
|
|
|
|
else if (uid == 9999)
|
|
|
|
return new String[]{"nobody"};
|
|
|
|
else {
|
|
|
|
String pkgs[] = getPackageManager().getPackagesForUid(uid);
|
|
|
|
if (pkgs == null)
|
|
|
|
return new String[0];
|
|
|
|
else
|
|
|
|
return pkgs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 10:26:21 +00:00
|
|
|
private void xmlImport(InputStream in) throws IOException, SAXException, ParserConfigurationException {
|
2015-11-08 11:30:39 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2016-01-28 22:19:31 +00:00
|
|
|
prefs.unregisterOnSharedPreferenceChangeListener(this);
|
2015-11-08 11:30:39 +00:00
|
|
|
prefs.edit().putBoolean("enabled", false).apply();
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.stop("import", this);
|
2015-11-08 11:30:39 +00:00
|
|
|
|
2016-02-10 11:50:51 +00:00
|
|
|
XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
|
|
|
|
XmlImportHandler handler = new XmlImportHandler(this);
|
|
|
|
reader.setContentHandler(handler);
|
|
|
|
reader.parse(new InputSource(in));
|
|
|
|
|
|
|
|
xmlImport(handler.application, prefs);
|
|
|
|
xmlImport(handler.wifi, getSharedPreferences("wifi", Context.MODE_PRIVATE));
|
|
|
|
xmlImport(handler.mobile, getSharedPreferences("other", Context.MODE_PRIVATE));
|
|
|
|
xmlImport(handler.screen_wifi, getSharedPreferences("screen_wifi", Context.MODE_PRIVATE));
|
|
|
|
xmlImport(handler.screen_other, getSharedPreferences("screen_other", Context.MODE_PRIVATE));
|
|
|
|
xmlImport(handler.roaming, getSharedPreferences("roaming", Context.MODE_PRIVATE));
|
2017-03-21 08:06:04 +00:00
|
|
|
xmlImport(handler.lockdown, getSharedPreferences("lockdown", Context.MODE_PRIVATE));
|
2016-02-21 08:04:03 +00:00
|
|
|
xmlImport(handler.apply, getSharedPreferences("apply", Context.MODE_PRIVATE));
|
2016-02-19 15:49:32 +00:00
|
|
|
xmlImport(handler.notify, getSharedPreferences("notify", 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);
|
|
|
|
|
2017-03-26 14:43:26 +00:00
|
|
|
DatabaseHelper.clearCache();
|
|
|
|
|
2015-11-08 12:05:55 +00:00
|
|
|
// Refresh UI
|
|
|
|
prefs.edit().putBoolean("imported", true).apply();
|
2016-01-28 22:19:31 +00:00
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
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<>();
|
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<>();
|
2017-03-21 08:06:04 +00:00
|
|
|
public Map<String, Object> lockdown = new HashMap<>();
|
2016-02-21 08:04:03 +00:00
|
|
|
public Map<String, Object> apply = new HashMap<>();
|
2016-02-19 15:49:32 +00:00
|
|
|
public Map<String, Object> notify = new HashMap<>();
|
2015-11-05 10:26:21 +00:00
|
|
|
private Map<String, Object> current = null;
|
|
|
|
|
2016-02-10 11:50:51 +00:00
|
|
|
public XmlImportHandler(Context context) {
|
2015-12-31 15:08:50 +00:00
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
2017-03-21 08:06:04 +00:00
|
|
|
else if (qName.equals("lockdown"))
|
|
|
|
current = lockdown;
|
|
|
|
|
2016-02-21 08:04:03 +00:00
|
|
|
else if (qName.equals("apply"))
|
|
|
|
current = apply;
|
|
|
|
|
2016-02-19 15:49:32 +00:00
|
|
|
else if (qName.equals("notify"))
|
|
|
|
current = notify;
|
|
|
|
|
2016-02-29 11:06:12 +00:00
|
|
|
else if (qName.equals("filter")) {
|
|
|
|
current = null;
|
|
|
|
Log.i(TAG, "Clearing filters");
|
|
|
|
DatabaseHelper.getInstance(context).clearAccess();
|
|
|
|
|
|
|
|
} else if (qName.equals("forward")) {
|
2016-02-01 08:55:49 +00:00
|
|
|
current = null;
|
2016-02-29 11:06:12 +00:00
|
|
|
Log.i(TAG, "Clearing forwards");
|
|
|
|
DatabaseHelper.getInstance(context).deleteForward();
|
2016-02-01 08:55:49 +00:00
|
|
|
|
2016-02-29 11:06:12 +00:00
|
|
|
} else if (qName.equals("setting")) {
|
2015-11-05 10:26:21 +00:00
|
|
|
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 {
|
2016-02-14 09:09:12 +00:00
|
|
|
if (current == application) {
|
|
|
|
// Pro features
|
2016-01-28 22:19:31 +00:00
|
|
|
if ("log".equals(key)) {
|
|
|
|
if (!IAB.isPurchased(ActivityPro.SKU_LOG, context))
|
|
|
|
return;
|
|
|
|
} else 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;
|
|
|
|
}
|
|
|
|
|
2016-02-14 09:09:12 +00:00
|
|
|
if ("hosts_last_import".equals(key) || "hosts_last_download".equals(key))
|
|
|
|
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<>();
|
2016-01-08 17:30:39 +00:00
|
|
|
if (!TextUtils.isEmpty(value))
|
|
|
|
for (String s : value.split("\n"))
|
|
|
|
set.add(s);
|
2015-12-10 07:38:45 +00:00
|
|
|
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
|
|
|
}
|
2016-02-29 11:06:12 +00:00
|
|
|
|
2016-02-01 08:55:49 +00:00
|
|
|
} else if (qName.equals("rule")) {
|
|
|
|
String pkg = attributes.getValue("pkg");
|
|
|
|
|
2016-02-05 19:44:12 +00:00
|
|
|
String version = attributes.getValue("version");
|
|
|
|
String protocol = attributes.getValue("protocol");
|
|
|
|
|
2016-02-01 08:55:49 +00:00
|
|
|
Packet packet = new Packet();
|
2016-02-05 19:44:12 +00:00
|
|
|
packet.version = (version == null ? 4 : Integer.parseInt(version));
|
|
|
|
packet.protocol = (protocol == null ? 6 /* TCP */ : Integer.parseInt(protocol));
|
2016-02-01 08:55:49 +00:00
|
|
|
packet.daddr = attributes.getValue("daddr");
|
|
|
|
packet.dport = Integer.parseInt(attributes.getValue("dport"));
|
|
|
|
packet.time = Long.parseLong(attributes.getValue("time"));
|
|
|
|
|
|
|
|
int block = Integer.parseInt(attributes.getValue("block"));
|
|
|
|
|
|
|
|
try {
|
2016-02-29 11:06:12 +00:00
|
|
|
packet.uid = getUid(pkg);
|
|
|
|
DatabaseHelper.getInstance(context).updateAccess(packet, null, block);
|
|
|
|
} catch (PackageManager.NameNotFoundException ex) {
|
|
|
|
Log.w(TAG, "Package not found pkg=" + pkg);
|
|
|
|
}
|
2016-02-01 08:55:49 +00:00
|
|
|
|
2016-02-29 11:06:12 +00:00
|
|
|
} else if (qName.equals("port")) {
|
|
|
|
String pkg = attributes.getValue("pkg");
|
|
|
|
int protocol = Integer.parseInt(attributes.getValue("protocol"));
|
|
|
|
int dport = Integer.parseInt(attributes.getValue("dport"));
|
|
|
|
String raddr = attributes.getValue("raddr");
|
|
|
|
int rport = Integer.parseInt(attributes.getValue("rport"));
|
2016-02-01 08:55:49 +00:00
|
|
|
|
2016-02-29 11:06:12 +00:00
|
|
|
try {
|
|
|
|
int uid = getUid(pkg);
|
|
|
|
DatabaseHelper.getInstance(context).addForward(protocol, dport, raddr, rport, uid);
|
2016-02-01 08:55:49 +00:00
|
|
|
} catch (PackageManager.NameNotFoundException ex) {
|
|
|
|
Log.w(TAG, "Package not found pkg=" + pkg);
|
|
|
|
}
|
2015-11-05 10:26:21 +00:00
|
|
|
|
|
|
|
} else
|
|
|
|
Log.e(TAG, "Unknown element qname=" + qName);
|
|
|
|
}
|
2016-02-29 11:06:12 +00:00
|
|
|
|
|
|
|
private int getUid(String pkg) throws PackageManager.NameNotFoundException {
|
|
|
|
if ("root".equals(pkg))
|
|
|
|
return 0;
|
|
|
|
else if ("mediaserver".equals(pkg))
|
|
|
|
return 1013;
|
|
|
|
else if ("nobody".equals(pkg))
|
|
|
|
return 9999;
|
|
|
|
else
|
|
|
|
return getPackageManager().getApplicationInfo(pkg, 0).uid;
|
|
|
|
}
|
2015-11-05 10:26:21 +00:00
|
|
|
}
|
2015-11-04 22:44:17 +00:00
|
|
|
}
|