Example usage for android.content SharedPreferences getStringSet

List of usage examples for android.content SharedPreferences getStringSet

Introduction

In this page you can find the example usage for android.content SharedPreferences getStringSet.

Prototype

@Nullable
Set<String> getStringSet(String key, @Nullable Set<String> defValues);

Source Link

Document

Retrieve a set of String values from the preferences.

Usage

From source file:eu.faircode.netguard.ActivitySettings.java

@Override
@TargetApi(Build.VERSION_CODES.M)/* w ww  . j a  va 2s  .c om*/
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
    // Pro features
    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");
            startActivity(new Intent(this, ActivityPro.class));
            return;
        }
    } else if ("install".equals(name)) {
        if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_NOTIFY, this)) {
            prefs.edit().putBoolean(name, false).apply();
            ((TwoStatePreference) getPreferenceScreen().findPreference(name)).setChecked(false);
            startActivity(new Intent(this, ActivityPro.class));
            return;
        }
    } else if ("show_stats".equals(name)) {
        if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_SPEED, this)) {
            prefs.edit().putBoolean(name, false).apply();
            startActivity(new Intent(this, ActivityPro.class));
            return;
        }
        ((TwoStatePreference) getPreferenceScreen().findPreference(name))
                .setChecked(prefs.getBoolean(name, false));
    }

    Object value = prefs.getAll().get(name);
    if (value instanceof String && "".equals(value))
        prefs.edit().remove(name).apply();

    // Dependencies
    if ("screen_on".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("whitelist_wifi".equals(name) || "screen_wifi".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("whitelist_other".equals(name) || "screen_other".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("whitelist_roaming".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    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();

    else if ("subnet".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("tethering".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("lan".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("ip6".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    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, "-"));
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("use_metered".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("unmetered_2g".equals(name) || "unmetered_3g".equals(name) || "unmetered_4g".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("national_roaming".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("eu_roaming".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("disable_on_call".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (checkPermissions(name))
                ServiceSinkhole.reload("changed " + name, this, false);
        } else
            ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("lockdown_wifi".equals(name) || "lockdown_other".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("manage_system".equals(name)) {
        boolean manage = prefs.getBoolean(name, false);
        if (!manage)
            prefs.edit().putBoolean("show_user", true).apply();
        prefs.edit().putBoolean("show_system", manage).apply();
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("log_app".equals(name)) {
        Intent ruleset = new Intent(ActivityMain.ACTION_RULES_CHANGED);
        LocalBroadcastManager.getInstance(this).sendBroadcast(ruleset);

    } else if ("filter".equals(name)) {
        // Show dialog
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && prefs.getBoolean(name, false)) {
            LayoutInflater inflater = LayoutInflater.from(ActivitySettings.this);
            View view = inflater.inflate(R.layout.filter, null, false);
            dialogFilter = new AlertDialog.Builder(ActivitySettings.this).setView(view).setCancelable(false)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing
                        }
                    }).setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {
                            dialogFilter = null;
                        }
                    }).create();
            dialogFilter.show();
        } 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();
        }

        ((TwoStatePreference) getPreferenceScreen().findPreference(name))
                .setChecked(prefs.getBoolean(name, false));

        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("use_hosts".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("vpn4".equals(name)) {
        String vpn4 = prefs.getString(name, null);
        try {
            checkAddress(vpn4);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(vpn4))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_vpn4, prefs.getString(name, "10.1.10.1")));
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("vpn6".equals(name)) {
        String vpn6 = prefs.getString(name, null);
        try {
            checkAddress(vpn6);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(vpn6))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name).setTitle(
                getString(R.string.setting_vpn6, prefs.getString(name, "fd00:1:fd00:1:fd00:1:fd00:1")));
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("dns".equals(name) || "dns2".equals(name)) {
        String dns = prefs.getString(name, null);
        try {
            checkAddress(dns);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(dns))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_dns,
                prefs.getString(name, Util.getDefaultDNS(this).get("dns".equals(name) ? 0 : 1))));
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("ttl".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_ttl, prefs.getString(name, "259200")));

    else if ("rcode".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_rcode, prefs.getString(name, "3")));
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("socks5_enabled".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);

    else if ("socks5_addr".equals(name)) {
        String socks5_addr = prefs.getString(name, null);
        try {
            if (!TextUtils.isEmpty(socks5_addr) && !Util.isNumericAddress(socks5_addr))
                throw new IllegalArgumentException("Bad address");
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(socks5_addr))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_socks5_addr, prefs.getString(name, "-")));
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("socks5_port".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_socks5_port, prefs.getString(name, "-")));
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("socks5_username".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_socks5_username, prefs.getString(name, "-")));
        ServiceSinkhole.reload("changed " + name, this, false);

    } 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, false);

    } 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")));

        ServiceSinkhole.setPcap(false, this);

        File pcap_file = new File(getDir("data", MODE_PRIVATE), "netguard.pcap");
        if (pcap_file.exists() && !pcap_file.delete())
            Log.w(TAG, "Delete PCAP failed");

        if (prefs.getBoolean("pcap", false))
            ServiceSinkhole.setPcap(true, this);

    } else if ("watchdog".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_watchdog, prefs.getString(name, "0")));
        ServiceSinkhole.reload("changed " + name, this, false);

    } else if ("show_stats".equals(name))
        ServiceSinkhole.reloadStats("changed " + name, this);

    else if ("stats_frequency".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_stats_frequency, prefs.getString(name, "1000")));

    else if ("stats_samples".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_stats_samples, prefs.getString(name, "90")));

    else if ("hosts_url".equals(name))
        getPreferenceScreen().findPreference(name)
                .setSummary(prefs.getString(name, "http://www.netguard.me/hosts"));

    else if ("loglevel".equals(name))
        ServiceSinkhole.reload("changed " + name, this, false);
}

From source file:android_network.hetnet.vpn_service.ActivitySettings.java

@Override
@TargetApi(Build.VERSION_CODES.M)//from   ww w .  java  2 s.co  m
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
    // Pro features
    if ("theme".equals(name)) {
        if (!"teal".equals(prefs.getString(name, "teal"))) {
            prefs.edit().putString(name, "teal").apply();
            ((ListPreference) getPreferenceScreen().findPreference(name)).setValue("teal");
            return;
        }
    } else if ("install".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            prefs.edit().putBoolean(name, false).apply();
            ((TwoStatePreference) getPreferenceScreen().findPreference(name)).setChecked(false);
            return;
        }
    } else if ("show_stats".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            prefs.edit().putBoolean(name, false).apply();
            return;
        }
        ((TwoStatePreference) getPreferenceScreen().findPreference(name))
                .setChecked(prefs.getBoolean(name, false));
    }

    Object value = prefs.getAll().get(name);
    if (value instanceof String && "".equals(value))
        prefs.edit().remove(name).apply();

    // Dependencies
    if ("screen_on".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("whitelist_wifi".equals(name) || "screen_wifi".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("whitelist_other".equals(name) || "screen_other".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("whitelist_roaming".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            else
                requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE },
                        REQUEST_ROAMING_INTERNATIONAL);
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } 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();

    else if ("subnet".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("tethering".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("lan".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("ip6".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    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, "-"));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("use_metered".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("unmetered_2g".equals(name) || "unmetered_3g".equals(name) || "unmetered_4g".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            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);
            }
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } else if ("national_roaming".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            else
                requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE },
                        REQUEST_ROAMING_NATIONAL);
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } else if ("manage_system".equals(name)) {
        boolean manage = prefs.getBoolean(name, true);
        if (!manage)
            prefs.edit().putBoolean("show_user", true).apply();
        prefs.edit().putBoolean("show_system", manage).apply();
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("log_app".equals(name)) {
        Intent ruleset = new Intent(MainActivity.ACTION_RULES_CHANGED);
        LocalBroadcastManager.getInstance(this).sendBroadcast(ruleset);

    } else if ("filter".equals(name)) {
        // Show dialog
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && prefs.getBoolean(name, false)) {
            LayoutInflater inflater = LayoutInflater.from(ActivitySettings.this);
            View view = inflater.inflate(R.layout.filter, null, false);
            dialogFilter = new AlertDialog.Builder(ActivitySettings.this).setView(view).setCancelable(false)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing
                        }
                    }).setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {
                            dialogFilter = null;
                        }
                    }).create();
            dialogFilter.show();
        } 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();
        }

        ((TwoStatePreference) getPreferenceScreen().findPreference(name))
                .setChecked(prefs.getBoolean(name, false));

        ServiceSinkhole.reload("changed " + name, this);

    } else if ("use_hosts".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("vpn4".equals(name)) {
        String vpn4 = prefs.getString(name, null);
        try {
            checkAddress(vpn4);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(vpn4))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_vpn4, prefs.getString(name, "10.1.10.1")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("vpn6".equals(name)) {
        String vpn6 = prefs.getString(name, null);
        try {
            checkAddress(vpn6);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(vpn6))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name).setTitle(
                getString(R.string.setting_vpn6, prefs.getString(name, "fd00:1:fd00:1:fd00:1:fd00:1")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("dns".equals(name) || "dns2".equals(name)) {
        String dns = prefs.getString(name, null);
        try {
            checkAddress(dns);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(dns))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_dns,
                prefs.getString(name, Util.getDefaultDNS(this).get("dns".equals(name) ? 0 : 1))));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("ttl".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_ttl, prefs.getString(name, "259200")));

    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))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("socks5_addr".equals(name)) {
        String socks5_addr = prefs.getString(name, null);
        try {
            if (!TextUtils.isEmpty(socks5_addr) && !Util.isNumericAddress(socks5_addr))
                throw new IllegalArgumentException("Bad address");
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(socks5_addr))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_socks5_addr, prefs.getString(name, "-")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("socks5_port".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_socks5_port, prefs.getString(name, "-")));
        ServiceSinkhole.reload("changed " + name, this);

    } 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);

    } 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")));

        ServiceSinkhole.setPcap(false, this);

        File pcap_file = new File(getDir("data", MODE_PRIVATE), "netguard.pcap");
        if (pcap_file.exists() && !pcap_file.delete())
            Log.w(TAG, "Delete PCAP failed");

        if (prefs.getBoolean("pcap", false))
            ServiceSinkhole.setPcap(true, this);

    } else if ("watchdog".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_watchdog, prefs.getString(name, "0")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("show_stats".equals(name))
        ServiceSinkhole.reloadStats("changed " + name, this);

    else if ("stats_frequency".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_stats_frequency, prefs.getString(name, "1000")));

    else if ("stats_samples".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_stats_samples, prefs.getString(name, "90")));

    else if ("hosts_url".equals(name))
        getPreferenceScreen().findPreference(name)
                .setSummary(prefs.getString(name, "http://www.netguard.me/hosts"));

    else if ("loglevel".equals(name))
        ServiceSinkhole.reload("changed " + name, this);
}

From source file:com.master.metehan.filtereagle.ActivitySettings.java

@Override
@TargetApi(Build.VERSION_CODES.M)/*  w ww .j a v a2 s . c o  m*/
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
    // Pro features
    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");
            startActivity(new Intent(this, ActivityPro.class));
            return;
        }
    } else if ("show_stats".equals(name)) {
        if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_SPEED, this)) {
            prefs.edit().putBoolean(name, false).apply();
            ((TwoStatePreference) getPreferenceScreen().findPreference(name)).setChecked(false);
            startActivity(new Intent(this, ActivityPro.class));
            return;
        }
    } else if ("install".equals(name)) {
        if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_NOTIFY, this)) {
            prefs.edit().putBoolean(name, false).apply();
            ((TwoStatePreference) getPreferenceScreen().findPreference(name)).setChecked(false);
            startActivity(new Intent(this, ActivityPro.class));
            return;
        }
    }

    Object value = prefs.getAll().get(name);
    if (value instanceof String && "".equals(value))
        prefs.edit().remove(name).apply();

    // Dependencies
    if ("whitelist_wifi".equals(name) || "screen_wifi".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("whitelist_other".equals(name) || "screen_other".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("whitelist_roaming".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            else
                requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE },
                        REQUEST_ROAMING_INTERNATIONAL);
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } 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();

    else if ("subnet".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("tethering".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("lan".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("ip6".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    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, "-"));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("use_metered".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("unmetered_2g".equals(name) || "unmetered_3g".equals(name) || "unmetered_4g".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            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);
            }
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } else if ("national_roaming".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            else
                requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE },
                        REQUEST_ROAMING_NATIONAL);
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } else if ("manage_system".equals(name)) {
        boolean manage = prefs.getBoolean(name, false);
        if (!manage)
            prefs.edit().putBoolean("show_user", true).apply();
        prefs.edit().putBoolean("show_system", manage).apply();
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("log_app".equals(name)) {
        Intent ruleset = new Intent(ActivityMain.ACTION_RULES_CHANGED);
        LocalBroadcastManager.getInstance(this).sendBroadcast(ruleset);

    } else if ("filter".equals(name)) {
        ServiceSinkhole.reload("changed " + name, this);

        // Show dialog
        if (prefs.getBoolean(name, false)) {
            LayoutInflater inflater = LayoutInflater.from(ActivitySettings.this);
            View view = inflater.inflate(R.layout.filter, null, false);
            dialogFilter = new AlertDialog.Builder(ActivitySettings.this).setView(view).setCancelable(false)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing
                        }
                    }).setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {
                            dialogFilter = null;
                        }
                    }).create();
            dialogFilter.show();
        }

    } else if ("adblock".equals(name)) {
        boolean manage = prefs.getBoolean(name, false);
        String server_url = this.getString(R.string.serverurl);
        String uid = prefs.getString("uid", "");
        if (manage) { // log in
            if (prefs.getBoolean("registered", false)) {
                // Login app
                System.out.println("uid: " + uid);
                Login login = new Login(server_url, uid, this);
                login.loginUser();
            } else {
                // Register app
                String key = this.getString(R.string.app_key);
                Register register = new Register(server_url, key, getApplicationContext());
                register.registerApp();

                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("registered", true).commit();
                Login login = new Login(server_url, uid, this);
                login.loginUser();
            }
        } else { // log out
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("logged", false).commit();
        }
    } else if ("use_hosts".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("vpn4".equals(name)) {
        String vpn4 = prefs.getString("vpn4", null);
        try {
            checkAddress(vpn4);
        } catch (Throwable ex) {
            prefs.edit().remove("vpn4").apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(vpn4))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        ServiceSinkhole.reload("changed " + name, this);
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_vpn4, prefs.getString("vpn4", "10.1.10.1")));

    } else if ("vpn6".equals(name)) {
        String vpn6 = prefs.getString("vpn6", null);
        try {
            checkAddress(vpn6);
        } catch (Throwable ex) {
            prefs.edit().remove("vpn6").apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(vpn6))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        ServiceSinkhole.reload("changed " + name, this);
        getPreferenceScreen().findPreference(name).setTitle(
                getString(R.string.setting_vpn6, prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1")));

    } else if ("dns".equals(name)) {
        String dns = prefs.getString("dns", null);
        try {
            checkAddress(dns);
        } catch (Throwable ex) {
            prefs.edit().remove("dns").apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(dns))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        ServiceSinkhole.reload("changed " + name, this);
        getPreferenceScreen().findPreference(name).setTitle(
                getString(R.string.setting_dns, prefs.getString("dns", Util.getDefaultDNS(this).get(0))));

    } 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")));

        ServiceSinkhole.setPcap(false, this);

        File pcap_file = new File(getCacheDir(), "netguard.pcap");
        if (pcap_file.exists() && !pcap_file.delete())
            Log.w(TAG, "Delete PCAP failed");

        if (prefs.getBoolean("pcap", false))
            ServiceSinkhole.setPcap(true, this);

    } else if ("show_stats".equals(name))
        ServiceSinkhole.reloadStats("changed " + name, this);

    else if ("stats_frequency".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_stats_frequency, prefs.getString(name, "1000")));

    else if ("stats_samples".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_stats_samples, prefs.getString(name, "90")));

    else if ("hosts_url".equals(name))
        getPreferenceScreen().findPreference(name)
                .setSummary(prefs.getString(name, "http://www.netguard.me/hosts"));

    else if ("loglevel".equals(name))
        ServiceSinkhole.reload("changed " + name, this);
}

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

private void synchronizeDontWantToSee(final boolean replace) {
    new Thread() {
        public void run() {
            if (!SettingConstants.UPDATING_FILTER) {
                SettingConstants.UPDATING_FILTER = true;

                Context applicationContext = getApplicationContext();

                NotificationCompat.Builder builder;

                builder = new NotificationCompat.Builder(TvBrowser.this);
                builder.setSmallIcon(R.drawable.ic_stat_notify);
                builder.setOngoing(true);
                builder.setContentTitle(getResources().getText(R.string.action_dont_want_to_see));
                builder.setContentText(getResources().getText(R.string.dont_want_to_see_notification_text));

                int notifyID = 2;

                NotificationManager notification = (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
                notification.notify(notifyID, builder.build());

                updateProgressIcon(true);

                URL documentUrl;/*  www  . j a v  a 2 s.  c  o m*/

                try {
                    documentUrl = new URL(
                            "http://android.tvbrowser.org/data/scripts/syncDown.php?type=dontWantToSee");
                    URLConnection connection = documentUrl.openConnection();

                    SharedPreferences pref = getSharedPreferences("transportation", Context.MODE_PRIVATE);

                    String car = pref.getString(SettingConstants.USER_NAME, null);
                    String bicycle = pref.getString(SettingConstants.USER_PASSWORD, null);

                    if (car != null && bicycle != null) {
                        String userpass = car + ":" + bicycle;
                        String basicAuth = "basic "
                                + Base64.encodeToString(userpass.getBytes(), Base64.NO_WRAP);

                        connection.setRequestProperty("Authorization", basicAuth);

                        BufferedReader read = new BufferedReader(new InputStreamReader(
                                new GZIPInputStream(connection.getInputStream()), "UTF-8"));

                        String line = null;

                        StringBuilder exclusionBuilder = new StringBuilder();
                        HashSet<String> exclusions = new HashSet<String>();
                        ArrayList<DontWantToSeeExclusion> exclusionList = new ArrayList<DontWantToSeeExclusion>();

                        while ((line = read.readLine()) != null) {
                            if (line.contains(";;") && line.trim().length() > 0) {
                                exclusions.add(line);
                                exclusionList.add(new DontWantToSeeExclusion(line));
                                exclusionBuilder.append(line).append("\n");
                            }
                        }

                        String key = getString(R.string.I_DONT_WANT_TO_SEE_ENTRIES);
                        SharedPreferences pref1 = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this);

                        Set<String> oldValues = pref1.getStringSet(key, null);

                        if (exclusions.size() > 0) {
                            if (!replace && oldValues != null) {
                                for (String old : oldValues) {
                                    if (!exclusions.contains(old)) {
                                        exclusions.add(old);
                                        exclusionList.add(new DontWantToSeeExclusion(old));
                                        exclusionBuilder.append(old).append("\n");
                                    }
                                }
                            }

                            Editor edit = pref1.edit();

                            edit.putStringSet(key, exclusions);
                            edit.commit();

                            DontWantToSeeExclusion[] exclusionArr = exclusionList
                                    .toArray(new DontWantToSeeExclusion[exclusionList.size()]);

                            Cursor c = getContentResolver().query(TvBrowserContentProvider.CONTENT_URI_DATA,
                                    new String[] { TvBrowserContentProvider.KEY_ID,
                                            TvBrowserContentProvider.DATA_KEY_TITLE },
                                    null, null, TvBrowserContentProvider.KEY_ID);
                            c.moveToPosition(-1);

                            builder.setProgress(c.getCount(), 0, true);
                            notification.notify(notifyID, builder.build());

                            ArrayList<ContentProviderOperation> updateValuesList = new ArrayList<ContentProviderOperation>();

                            int keyColumn = c.getColumnIndex(TvBrowserContentProvider.KEY_ID);
                            int titleColumn = c.getColumnIndex(TvBrowserContentProvider.DATA_KEY_TITLE);

                            while (c.moveToNext()) {
                                builder.setProgress(c.getCount(), c.getPosition(), false);
                                notification.notify(notifyID, builder.build());

                                String title = c.getString(titleColumn);

                                boolean filter = UiUtils.filter(getApplicationContext(), title, exclusionArr);
                                long progID = c.getLong(keyColumn);

                                ContentValues values = new ContentValues();
                                values.put(TvBrowserContentProvider.DATA_KEY_DONT_WANT_TO_SEE, filter ? 1 : 0);

                                ContentProviderOperation.Builder opBuilder = ContentProviderOperation
                                        .newUpdate(ContentUris.withAppendedId(
                                                TvBrowserContentProvider.CONTENT_URI_DATA_UPDATE, progID));
                                opBuilder.withValues(values);

                                updateValuesList.add(opBuilder.build());
                            }

                            c.close();

                            if (!updateValuesList.isEmpty()) {
                                try {
                                    getContentResolver().applyBatch(TvBrowserContentProvider.AUTHORITY,
                                            updateValuesList);
                                    UiUtils.sendDontWantToSeeChangedBroadcast(applicationContext, true);
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(),
                                                    R.string.dont_want_to_see_sync_success, Toast.LENGTH_LONG)
                                                    .show();
                                        }
                                    });
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                } catch (OperationApplicationException e) {
                                    e.printStackTrace();
                                }
                            }

                            if (!replace && exclusionBuilder.length() > 0) {
                                startSynchronizeUp(false, exclusionBuilder.toString(),
                                        "http://android.tvbrowser.org/data/scripts/syncUp.php?type=dontWantToSee",
                                        null, null);
                            }
                        } else {
                            if (!replace && oldValues != null && !oldValues.isEmpty()) {
                                for (String old : oldValues) {
                                    exclusionBuilder.append(old).append("\n");
                                }

                                startSynchronizeUp(false, exclusionBuilder.toString(),
                                        "http://android.tvbrowser.org/data/scripts/syncUp.php?type=dontWantToSee",
                                        null, null);
                            }

                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), R.string.no_dont_want_to_see_sync,
                                            Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }
                } catch (Throwable t) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), R.string.no_dont_want_to_see_sync,
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }

                notification.cancel(notifyID);
                updateProgressIcon(false);

                SettingConstants.UPDATING_FILTER = false;
            }
        }
    }.start();
}