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:com.androzic.Androzic.java

/**
 * Load default and selected waypoint files.
 *//*from  w  ww  .j  a  v a2s . com*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void initializeWaypoints() {
    if (waypoints.size() > 0)
        return;

    File wptFile = new File(dataPath, "myWaypoints.wpt");
    if (wptFile.exists() && wptFile.canRead()) {
        try {
            addWaypoints(OziExplorerFiles.loadWaypointsFromFile(wptFile, charset));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // load selected waypoint sets
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        Set<String> sets = settings.getStringSet(getString(R.string.wpt_sets), new HashSet<String>());
        for (String path : sets) {
            File file = new File(path);
            try {
                if (file.exists() && file.canRead())
                    WaypointFileHelper.loadFile(file);
            } catch (Exception e) {
                // We ignore all exceptions on this stage
                e.printStackTrace();
            }
        }
    }
}

From source file:com.vonglasow.michael.satstat.ui.SettingsActivity.java

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    boolean needsLocationPerm = false;
    if (key.equals(Const.KEY_PREF_NOTIFY_FIX) || key.equals(Const.KEY_PREF_NOTIFY_SEARCH)) {
        boolean notifyFix = sharedPreferences.getBoolean(Const.KEY_PREF_NOTIFY_FIX, false);
        boolean notifySearch = sharedPreferences.getBoolean(Const.KEY_PREF_NOTIFY_SEARCH, false);
        if (!(notifyFix || notifySearch)) {
            Intent stopServiceIntent = new Intent(this, PasvLocListenerService.class);
            this.stopService(stopServiceIntent);
        } else if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            needsLocationPerm = true;/*from   www  .  ja  v a 2  s . co m*/
        }
    } else if (key.equals(Const.KEY_PREF_UPDATE_FREQ)) {
        // this piece of code is necessary because Android has no way
        // of updating the preference summary automatically. I am
        // told the absence of such functionality is a feature...
        SettingsFragment sf = (SettingsFragment) getFragmentManager().findFragmentById(android.R.id.content);
        ListPreference prefUpdateFreq = (ListPreference) sf.findPreference(Const.KEY_PREF_UPDATE_FREQ);
        final String value = sharedPreferences.getString(key, key);
        final int index = prefUpdateFreq.findIndexOfValue(value);
        if (index >= 0) {
            final String summary = (String) prefUpdateFreq.getEntries()[index];
            prefUpdateFreq.setSummary(summary);
        }
    } else if (key.equals(Const.KEY_PREF_MAP_PATH)) {
        SettingsFragment sf = (SettingsFragment) getFragmentManager().findFragmentById(android.R.id.content);
        Preference prefMapPath = sf.findPreference(Const.KEY_PREF_MAP_PATH);
        prefMapPathValue = mSharedPreferences.getString(Const.KEY_PREF_MAP_PATH, prefMapPathValue);
        prefMapPath.setSummary(prefMapPathValue);
    } else if (key.equals(Const.KEY_PREF_UPDATE_NETWORKS)) {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Set<String> updateNetworks = sharedPreferences.getStringSet(Const.KEY_PREF_UPDATE_NETWORKS,
                    new HashSet<String>());
            if (!updateNetworks.isEmpty())
                needsLocationPerm = true;
        }
    }
    if (needsLocationPerm)
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
                Const.PERM_REQUEST_LOCATION_PREF);
}

From source file:com.zhengde163.netguard.ServiceSinkhole.java

private List<Rule> getAllowedRules(List<Rule> listRule) {
    List<Rule> listAllowed = new ArrayList<>();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // Check state
    wifi = Util.isWifiActive(this);
    boolean metered = Util.isMeteredNetwork(this);
    boolean useMetered = prefs.getBoolean("use_metered", false);
    Set<String> ssidHomes = prefs.getStringSet("wifi_homes", new HashSet<String>());
    String ssidNetwork = Util.getWifiSSID(this);
    String generation = Util.getNetworkGeneration(this);
    boolean unmetered_2g = prefs.getBoolean("unmetered_2g", false);
    boolean unmetered_3g = prefs.getBoolean("unmetered_3g", false);
    boolean unmetered_4g = prefs.getBoolean("unmetered_4g", false);
    boolean roaming = Util.isRoaming(ServiceSinkhole.this);
    boolean national = prefs.getBoolean("national_roaming", false);
    boolean tethering = prefs.getBoolean("tethering", false);
    boolean filter = prefs.getBoolean("filter", false);

    // Update connected state
    last_connected = Util.isConnected(ServiceSinkhole.this);

    boolean org_metered = metered;
    boolean org_roaming = roaming;

    // Update metered state
    if (wifi && !useMetered)
        metered = false;//from   w w w.  java  2 s .co m
    if (wifi && ssidHomes.size() > 0
            && !(ssidHomes.contains(ssidNetwork) || ssidHomes.contains('"' + ssidNetwork + '"'))) {
        metered = true;
        Log.i(TAG, "!@home");
    }
    if (unmetered_2g && "2G".equals(generation))
        metered = false;
    if (unmetered_3g && "3G".equals(generation))
        metered = false;
    if (unmetered_4g && "4G".equals(generation))
        metered = false;
    last_metered = metered;

    // Update roaming state
    if (roaming && national)
        roaming = Util.isInternational(this);

    Log.i(TAG,
            "Get allowed" + " connected=" + last_connected + " wifi=" + wifi + " home="
                    + TextUtils.join(",", ssidHomes) + " network=" + ssidNetwork + " metered=" + metered + "/"
                    + org_metered + " generation=" + generation + " roaming=" + roaming + "/" + org_roaming
                    + " interactive=" + last_interactive + " tethering=" + tethering + " filter=" + filter);

    if (last_connected)
        for (Rule rule : listRule) {
            boolean blocked = (metered ? rule.other_blocked : rule.wifi_blocked);
            boolean screen = (metered ? rule.screen_other : rule.screen_wifi);
            if ((!blocked || (screen && last_interactive)) && (!metered || !(rule.roaming && roaming)))
                listAllowed.add(rule);
        }

    Log.i(TAG, "Allowed " + listAllowed.size() + " of " + listRule.size());
    return listAllowed;
}

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

private List<Rule> getAllowedRules(List<Rule> listRule) {
    List<Rule> listAllowed = new ArrayList<>();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // Check state
    boolean wifi = Util.isWifiActive(this);
    boolean metered = Util.isMeteredNetwork(this);
    boolean useMetered = prefs.getBoolean("use_metered", false);
    Set<String> ssidHomes = prefs.getStringSet("wifi_homes", new HashSet<String>());
    String ssidNetwork = Util.getWifiSSID(this);
    String generation = Util.getNetworkGeneration(this);
    boolean unmetered_2g = prefs.getBoolean("unmetered_2g", false);
    boolean unmetered_3g = prefs.getBoolean("unmetered_3g", false);
    boolean unmetered_4g = prefs.getBoolean("unmetered_4g", false);
    boolean roaming = Util.isRoaming(ServiceSinkhole.this);
    boolean national = prefs.getBoolean("national_roaming", false);
    boolean tethering = prefs.getBoolean("tethering", false);
    boolean filter = prefs.getBoolean("filter", false);

    // Update connected state
    last_connected = Util.isConnected(ServiceSinkhole.this);

    boolean org_metered = metered;
    boolean org_roaming = roaming;

    // Update metered state
    if (wifi && !useMetered)
        metered = false;/* w ww  . j a  v a2 s  . c  o  m*/
    if (wifi && ssidHomes.size() > 0
            && !(ssidHomes.contains(ssidNetwork) || ssidHomes.contains('"' + ssidNetwork + '"'))) {
        metered = true;
        Log.i(TAG, "!@home");
    }
    if (unmetered_2g && "2G".equals(generation))
        metered = false;
    if (unmetered_3g && "3G".equals(generation))
        metered = false;
    if (unmetered_4g && "4G".equals(generation))
        metered = false;
    last_metered = metered;

    boolean lockdown = isLockedDown(last_metered);

    // Update roaming state
    if (roaming && national)
        roaming = Util.isInternational(this);

    Log.i(TAG,
            "Get allowed" + " connected=" + last_connected + " wifi=" + wifi + " home="
                    + TextUtils.join(",", ssidHomes) + " network=" + ssidNetwork + " metered=" + metered + "/"
                    + org_metered + " generation=" + generation + " roaming=" + roaming + "/" + org_roaming
                    + " interactive=" + last_interactive + " tethering=" + tethering + " filter=" + filter
                    + " lockdown=" + lockdown);

    if (last_connected)
        for (Rule rule : listRule) {
            boolean blocked = (metered ? rule.other_blocked : rule.wifi_blocked);
            boolean screen = (metered ? rule.screen_other : rule.screen_wifi);
            if ((!blocked || (screen && last_interactive)) && (!metered || !(rule.roaming && roaming))
                    && (!lockdown || rule.lockdown))
                listAllowed.add(rule);
        }

    Log.i(TAG, "Allowed " + listAllowed.size() + " of " + listRule.size());
    return listAllowed;
}

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

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    final PreferenceScreen screen = getPreferenceScreen();
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    PreferenceCategory cat_options = (PreferenceCategory) screen.findPreference("category_options");
    PreferenceCategory cat_advanced = (PreferenceCategory) screen.findPreference("category_advanced_options");
    PreferenceCategory cat_backup = (PreferenceCategory) screen.findPreference("category_backup");
    PreferenceCategory cat_development = (PreferenceCategory) screen.findPreference("category_development");

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

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

    // 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;
        }/*ww  w . j  a  v  a 2s .com*/

    // Wi-Fi home
    MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) screen.findPreference("wifi_homes");
    Set<String> ssid = prefs.getStringSet("wifi_homes", 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, "-"));

    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    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);
    pref_wifi_homes.setEntries(listSSID.toArray(new CharSequence[0]));
    pref_wifi_homes.setEntryValues(listSSID.toArray(new CharSequence[0]));

    // Filtering always enabled
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        cat_advanced.removePreference(screen.findPreference("filter"));

    Preference pref_reset_usage = screen.findPreference("reset_usage");
    pref_reset_usage.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Util.areYouSure(ActivitySettings.this, R.string.setting_reset_usage, new Util.DoubtListener() {
                @Override
                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();
                }
            });
            return false;
        }
    });

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

    // VPN parameters
    screen.findPreference("vpn4")
            .setTitle(getString(R.string.setting_vpn4, prefs.getString("vpn4", "10.1.10.1")));
    screen.findPreference("vpn6")
            .setTitle(getString(R.string.setting_vpn6, prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1")));
    EditTextPreference pref_dns = (EditTextPreference) screen.findPreference("dns");
    List<String> def_dns = Util.getDefaultDNS(this);
    pref_dns.getEditText().setHint(def_dns.get(0));
    pref_dns.setTitle(getString(R.string.setting_dns, prefs.getString("dns", def_dns.get(0))));

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

    // Handle stats
    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")));

    // Handle export
    Preference pref_export = screen.findPreference("export");
    pref_export.setEnabled(getIntentCreateExport().resolveActivity(getPackageManager()) != null);
    pref_export.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivityForResult(getIntentCreateExport(), ActivitySettings.REQUEST_EXPORT);
            return true;
        }
    });

    // Handle import
    Preference pref_import = screen.findPreference("import");
    pref_import.setEnabled(getIntentOpenExport().resolveActivity(getPackageManager()) != null);
    pref_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivityForResult(getIntentOpenExport(), ActivitySettings.REQUEST_IMPORT);
            return true;
        }
    });

    // Hosts file settings
    Preference pref_block_domains = screen.findPreference("use_hosts");
    Preference pref_hosts_import = screen.findPreference("hosts_import");
    EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
    final Preference pref_hosts_download = screen.findPreference("hosts_download");

    if (Util.isPlayStoreInstall(this)) {
        cat_options.removePreference(screen.findPreference("update_check"));
        cat_advanced.removePreference(pref_block_domains);
        cat_advanced.removePreference(pref_forwarding);
        cat_backup.removePreference(pref_hosts_import);
        cat_backup.removePreference(pref_hosts_url);
        cat_backup.removePreference(pref_hosts_download);

    } else {
        pref_block_domains.setEnabled(new File(getFilesDir(), "hosts.txt").exists());

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

        // Handle hosts import
        // https://github.com/Free-Software-for-Android/AdAway/wiki/HostsSources
        pref_hosts_import.setEnabled(getIntentOpenHosts().resolveActivity(getPackageManager()) != null);
        pref_hosts_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivityForResult(getIntentOpenHosts(), ActivitySettings.REQUEST_HOSTS);
                return true;
            }
        });

        // 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) {
                final File tmp = new File(getFilesDir(), "hosts.tmp");
                final File hosts = new File(getFilesDir(), "hosts.txt");
                EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
                try {
                    new DownloadTask(ActivitySettings.this, new URL(pref_hosts_url.getText()), tmp,
                            new DownloadTask.Listener() {
                                @Override
                                public void onCompleted() {
                                    if (hosts.exists())
                                        hosts.delete();
                                    tmp.renameTo(hosts);

                                    String last = SimpleDateFormat.getDateTimeInstance()
                                            .format(new Date().getTime());
                                    prefs.edit().putString("hosts_last_download", last).apply();

                                    if (running) {
                                        getPreferenceScreen().findPreference("use_hosts").setEnabled(true);
                                        pref_hosts_download
                                                .setSummary(getString(R.string.msg_download_last, last));
                                        Toast.makeText(ActivitySettings.this, R.string.msg_downloaded,
                                                Toast.LENGTH_LONG).show();
                                    }

                                    ServiceSinkhole.reload("hosts file download", ActivitySettings.this);
                                }

                                @Override
                                public void onCancelled() {
                                    if (tmp.exists())
                                        tmp.delete();
                                }

                                @Override
                                public void onException(Throwable ex) {
                                    if (tmp.exists())
                                        tmp.delete();

                                    if (running)
                                        Toast.makeText(ActivitySettings.this, ex.getMessage(),
                                                Toast.LENGTH_LONG).show();
                                }
                            }).execute();
                } catch (MalformedURLException ex) {
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
                }
                return true;
            }
        });
    }

    // Development
    Preference pref_show_resolved = screen.findPreference("show_resolved");
    if (!(Util.isDebuggable(this) || Util.getSelfVersionName(this).contains("beta"))) {
        screen.removePreference(cat_development);
        prefs.edit().remove("loglevel").apply();
    } else if (!Util.isDebuggable(this))
        cat_development.removePreference(pref_show_resolved);

    // Show resolved
    pref_show_resolved.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivity(new Intent(ActivitySettings.this, ActivityDns.class));
            return true;
        }
    });

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

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

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    final PreferenceScreen screen = getPreferenceScreen();
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    PreferenceCategory cat_options = (PreferenceCategory) screen.findPreference("category_options");
    PreferenceCategory cat_advanced = (PreferenceCategory) screen.findPreference("category_advanced_options");
    PreferenceCategory cat_backup = (PreferenceCategory) screen.findPreference("category_backup");
    PreferenceCategory cat_development = (PreferenceCategory) screen.findPreference("category_development");

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

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

    // 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;
        }// w w w  .  j a  va2s . c  o m

    // Wi-Fi home
    MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) screen.findPreference("wifi_homes");
    Set<String> ssid = prefs.getStringSet("wifi_homes", 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, "-"));

    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    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);
    pref_wifi_homes.setEntries(listSSID.toArray(new CharSequence[0]));
    pref_wifi_homes.setEntryValues(listSSID.toArray(new CharSequence[0]));

    // Filtering always enabled
    //        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
    //            cat_advanced.removePreference(screen.findPreference("filter"));

    Preference pref_reset_usage = screen.findPreference("reset_usage");
    pref_reset_usage.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Util.areYouSure(ActivitySettings.this, R.string.setting_reset_usage, new Util.DoubtListener() {
                @Override
                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();
                }
            });
            return false;
        }
    });

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

    // VPN parameters
    screen.findPreference("vpn4")
            .setTitle(getString(R.string.setting_vpn4, prefs.getString("vpn4", "10.1.10.1")));
    screen.findPreference("vpn6")
            .setTitle(getString(R.string.setting_vpn6, prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1")));
    EditTextPreference pref_dns = (EditTextPreference) screen.findPreference("dns");
    List<String> def_dns = Util.getDefaultDNS(this);
    pref_dns.getEditText().setHint(def_dns.get(0));
    pref_dns.setTitle(getString(R.string.setting_dns, prefs.getString("dns", def_dns.get(0))));

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

    // Handle stats
    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")));

    // Handle export
    Preference pref_export = screen.findPreference("export");
    pref_export.setEnabled(getIntentCreateExport().resolveActivity(getPackageManager()) != null);
    pref_export.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivityForResult(getIntentCreateExport(), ActivitySettings.REQUEST_EXPORT);
            return true;
        }
    });

    // Handle import
    Preference pref_import = screen.findPreference("import");
    pref_import.setEnabled(getIntentOpenExport().resolveActivity(getPackageManager()) != null);
    pref_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivityForResult(getIntentOpenExport(), ActivitySettings.REQUEST_IMPORT);
            return true;
        }
    });

    // Hosts file settings
    Preference pref_block_domains = screen.findPreference("use_hosts");
    Preference pref_hosts_import = screen.findPreference("hosts_import");
    EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
    final Preference pref_hosts_download = screen.findPreference("hosts_download");

    if (Util.isPlayStoreInstall(this)) {
        cat_options.removePreference(screen.findPreference("update_check"));
        cat_advanced.removePreference(pref_block_domains);
        cat_advanced.removePreference(pref_forwarding);
        cat_backup.removePreference(pref_hosts_import);
        cat_backup.removePreference(pref_hosts_url);
        cat_backup.removePreference(pref_hosts_download);

    } else {
        try {

            AssetManager am = getAssets();

            InputStream inputStream = getResources().openRawResource(R.raw.hosts);
            File hfile = createFileFromInputStream(inputStream);

            pref_block_domains.setEnabled(hfile.exists());
        } catch (Exception e) {
            e.printStackTrace();
        }

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

        // Handle hosts import
        // https://github.com/Free-Software-for-Android/AdAway/wiki/HostsSources
        pref_hosts_import.setEnabled(getIntentOpenHosts().resolveActivity(getPackageManager()) != null);
        pref_hosts_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivityForResult(getIntentOpenHosts(), ActivitySettings.REQUEST_HOSTS);
                return true;
            }
        });

        // 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) {
                final File tmp = new File(getFilesDir(), "hosts.tmp");
                final File hosts = new File(getFilesDir(), "host/hosts.txt");
                EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
                try {
                    new DownloadTask(ActivitySettings.this, new URL(pref_hosts_url.getText()), tmp,
                            new DownloadTask.Listener() {
                                @Override
                                public void onCompleted() {
                                    if (hosts.exists())
                                        hosts.delete();
                                    tmp.renameTo(hosts);

                                    String last = SimpleDateFormat.getDateTimeInstance()
                                            .format(new Date().getTime());
                                    prefs.edit().putString("hosts_last_download", last).apply();

                                    if (running) {
                                        getPreferenceScreen().findPreference("use_hosts").setEnabled(true);
                                        pref_hosts_download
                                                .setSummary(getString(R.string.msg_download_last, last));
                                        Toast.makeText(ActivitySettings.this, R.string.msg_downloaded,
                                                Toast.LENGTH_LONG).show();
                                    }

                                    ServiceSinkhole.reload("hosts file download", ActivitySettings.this);
                                }

                                @Override
                                public void onCancelled() {
                                    if (tmp.exists())
                                        tmp.delete();
                                }

                                @Override
                                public void onException(Throwable ex) {
                                    if (tmp.exists())
                                        tmp.delete();

                                    if (running)
                                        Toast.makeText(ActivitySettings.this, ex.getMessage(),
                                                Toast.LENGTH_LONG).show();
                                }
                            }).execute();
                } catch (MalformedURLException ex) {
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
                }
                return true;
            }
        });
    }

    // Development
    Preference pref_show_resolved = screen.findPreference("show_resolved");
    if (!(Util.isDebuggable(this) || Util.getSelfVersionName(this).contains("beta"))) {
        screen.removePreference(cat_development);
        prefs.edit().remove("loglevel").apply();
    } else if (!Util.isDebuggable(this))
        cat_development.removePreference(pref_show_resolved);

    // Show resolved
    pref_show_resolved.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivity(new Intent(ActivitySettings.this, ActivityDns.class));
            return true;
        }
    });

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

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

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    final PreferenceScreen screen = getPreferenceScreen();
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    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");
    PreferenceGroup cat_stats = (PreferenceGroup) ((PreferenceGroup) screen.findPreference("screen_stats"))
            .findPreference("category_stats");
    PreferenceGroup cat_backup = (PreferenceGroup) ((PreferenceGroup) screen.findPreference("screen_backup"))
            .findPreference("category_backup");

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

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

    // 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;
        }//  w  ww  .java 2s.  c o  m

    // Wi-Fi home
    MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) screen.findPreference("wifi_homes");
    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)));
    else
        pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, "-"));

    WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    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);
    for (String ssid : ssids)
        if (!listSSID.contains(ssid))
            listSSID.add(ssid);
    pref_wifi_homes.setEntries(listSSID.toArray(new CharSequence[0]));
    pref_wifi_homes.setEntryValues(listSSID.toArray(new CharSequence[0]));

    Preference pref_reset_usage = screen.findPreference("reset_usage");
    pref_reset_usage.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Util.areYouSure(ActivitySettings.this, R.string.setting_reset_usage, new Util.DoubtListener() {
                @Override
                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();
                        }
                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }
            });
            return false;
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        TwoStatePreference pref_reload_onconnectivity = (TwoStatePreference) screen
                .findPreference("reload_onconnectivity");
        pref_reload_onconnectivity.setChecked(true);
        pref_reload_onconnectivity.setEnabled(false);
    }

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

    boolean can = Util.canFilter(this);
    TwoStatePreference pref_log_app = (TwoStatePreference) screen.findPreference("log_app");
    TwoStatePreference pref_filter = (TwoStatePreference) screen.findPreference("filter");
    pref_log_app.setEnabled(can);
    pref_filter.setEnabled(can);
    if (!can) {
        pref_log_app.setSummary(R.string.msg_unavailable);
        pref_filter.setSummary(R.string.msg_unavailable);
    }

    // VPN parameters
    screen.findPreference("vpn4")
            .setTitle(getString(R.string.setting_vpn4, prefs.getString("vpn4", "10.1.10.1")));
    screen.findPreference("vpn6")
            .setTitle(getString(R.string.setting_vpn6, prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1")));
    EditTextPreference pref_dns1 = (EditTextPreference) screen.findPreference("dns");
    EditTextPreference pref_dns2 = (EditTextPreference) screen.findPreference("dns2");
    EditTextPreference pref_ttl = (EditTextPreference) screen.findPreference("ttl");
    List<String> def_dns = Util.getDefaultDNS(this);
    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))));
    pref_ttl.setTitle(getString(R.string.setting_ttl, prefs.getString("ttl", "259200")));

    // 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", "-")));
    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", "")) ? "-" : "*****"));

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

    // Watchdog
    screen.findPreference("watchdog")
            .setTitle(getString(R.string.setting_watchdog, prefs.getString("watchdog", "0")));

    // Show resolved
    Preference pref_show_resolved = screen.findPreference("show_resolved");
    if (Util.isPlayStoreInstall(this))
        cat_advanced.removePreference(pref_show_resolved);
    else
        pref_show_resolved.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivity(new Intent(ActivitySettings.this, ActivityDns.class));
                return true;
            }
        });

    // Handle stats
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        cat_stats.removePreference(screen.findPreference("show_top"));
    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")));

    // Handle export
    Preference pref_export = screen.findPreference("export");
    pref_export.setEnabled(getIntentCreateExport().resolveActivity(getPackageManager()) != null);
    pref_export.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivityForResult(getIntentCreateExport(), ActivitySettings.REQUEST_EXPORT);
            return true;
        }
    });

    // Handle import
    Preference pref_import = screen.findPreference("import");
    pref_import.setEnabled(getIntentOpenExport().resolveActivity(getPackageManager()) != null);
    pref_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivityForResult(getIntentOpenExport(), ActivitySettings.REQUEST_IMPORT);
            return true;
        }
    });

    // Hosts file settings
    Preference pref_block_domains = screen.findPreference("use_hosts");
    EditTextPreference pref_rcode = (EditTextPreference) screen.findPreference("rcode");
    Preference pref_hosts_import = screen.findPreference("hosts_import");
    EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
    final Preference pref_hosts_download = screen.findPreference("hosts_download");

    pref_rcode.setTitle(getString(R.string.setting_rcode, prefs.getString("rcode", "3")));

    if (Util.isPlayStoreInstall(this)) {
        Log.i(TAG, "Play store install");
        cat_options.removePreference(screen.findPreference("update_check"));
        cat_advanced.removePreference(pref_block_domains);
        cat_advanced.removePreference(pref_rcode);
        cat_advanced.removePreference(pref_forwarding);
        cat_backup.removePreference(pref_hosts_import);
        cat_backup.removePreference(pref_hosts_url);
        cat_backup.removePreference(pref_hosts_download);

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

        // Handle hosts import
        // https://github.com/Free-Software-for-Android/AdAway/wiki/HostsSources
        pref_hosts_import.setEnabled(getIntentOpenHosts().resolveActivity(getPackageManager()) != null);
        pref_hosts_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivityForResult(getIntentOpenHosts(), ActivitySettings.REQUEST_HOSTS);
                return true;
            }
        });

        // 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) {
                final File tmp = new File(getFilesDir(), "hosts.tmp");
                final File hosts = new File(getFilesDir(), "hosts.txt");
                EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
                try {
                    new DownloadTask(ActivitySettings.this, new URL(pref_hosts_url.getText()), tmp,
                            new DownloadTask.Listener() {
                                @Override
                                public void onCompleted() {
                                    if (hosts.exists())
                                        hosts.delete();
                                    tmp.renameTo(hosts);

                                    String last = SimpleDateFormat.getDateTimeInstance()
                                            .format(new Date().getTime());
                                    prefs.edit().putString("hosts_last_download", last).apply();

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

                                    ServiceSinkhole.reload("hosts file download", ActivitySettings.this, false);
                                }

                                @Override
                                public void onCancelled() {
                                    if (tmp.exists())
                                        tmp.delete();
                                }

                                @Override
                                public void onException(Throwable ex) {
                                    if (tmp.exists())
                                        tmp.delete();

                                    if (running)
                                        Toast.makeText(ActivitySettings.this, ex.getMessage(),
                                                Toast.LENGTH_LONG).show();
                                }
                            }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                } catch (MalformedURLException ex) {
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
                }
                return true;
            }
        });
    }

    // Development
    if (!Util.isDebuggable(this))
        screen.removePreference(screen.findPreference("screen_development"));

    // 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");
    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);
    updateTechnicalInfo();

    markPro(screen.findPreference("theme"), ActivityPro.SKU_THEME);
    markPro(screen.findPreference("install"), ActivityPro.SKU_NOTIFY);
    markPro(screen.findPreference("show_stats"), ActivityPro.SKU_SPEED);
}

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

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    final PreferenceScreen screen = getPreferenceScreen();
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    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");
    PreferenceGroup cat_stats = (PreferenceGroup) ((PreferenceGroup) screen.findPreference("screen_stats"))
            .findPreference("category_stats");
    PreferenceGroup cat_backup = (PreferenceGroup) ((PreferenceGroup) screen.findPreference("screen_backup"))
            .findPreference("category_backup");

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

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

    // 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;
        }//ww w.  j av a2  s .c o m

    // Wi-Fi home
    MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) screen.findPreference("wifi_homes");
    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)));
    else
        pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, "-"));

    WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    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);
    for (String ssid : ssids)
        if (!listSSID.contains(ssid))
            listSSID.add(ssid);
    pref_wifi_homes.setEntries(listSSID.toArray(new CharSequence[0]));
    pref_wifi_homes.setEntryValues(listSSID.toArray(new CharSequence[0]));

    Preference pref_reset_usage = screen.findPreference("reset_usage");
    pref_reset_usage.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Util.areYouSure(ActivitySettings.this, R.string.setting_reset_usage, new Util.DoubtListener() {
                @Override
                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();
                }
            });
            return false;
        }
    });

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

    // VPN parameters
    screen.findPreference("vpn4")
            .setTitle(getString(R.string.setting_vpn4, prefs.getString("vpn4", "10.1.10.1")));
    screen.findPreference("vpn6")
            .setTitle(getString(R.string.setting_vpn6, prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1")));
    EditTextPreference pref_dns1 = (EditTextPreference) screen.findPreference("dns");
    EditTextPreference pref_dns2 = (EditTextPreference) screen.findPreference("dns2");
    EditTextPreference pref_ttl = (EditTextPreference) screen.findPreference("ttl");
    List<String> def_dns = Util.getDefaultDNS(this);
    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))));
    pref_ttl.setTitle(getString(R.string.setting_ttl, prefs.getString("ttl", "259200")));

    // 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", "-")));
    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", "")) ? "-" : "*****"));

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

    // Watchdog
    screen.findPreference("watchdog")
            .setTitle(getString(R.string.setting_watchdog, prefs.getString("watchdog", "0")));

    // Show resolved
    Preference pref_show_resolved = screen.findPreference("show_resolved");
    if (Util.isPlayStoreInstall(this))
        cat_advanced.removePreference(pref_show_resolved);
    else
        pref_show_resolved.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivity(new Intent(ActivitySettings.this, ActivityDns.class));
                return true;
            }
        });

    // Handle stats
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        cat_stats.removePreference(screen.findPreference("show_top"));
    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")));

    // Handle export
    Preference pref_export = screen.findPreference("export");
    pref_export.setEnabled(getIntentCreateExport().resolveActivity(getPackageManager()) != null);
    pref_export.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivityForResult(getIntentCreateExport(), ActivitySettings.REQUEST_EXPORT);
            return true;
        }
    });

    // Handle import
    Preference pref_import = screen.findPreference("import");
    pref_import.setEnabled(getIntentOpenExport().resolveActivity(getPackageManager()) != null);
    pref_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            startActivityForResult(getIntentOpenExport(), ActivitySettings.REQUEST_IMPORT);
            return true;
        }
    });

    // Hosts file settings
    Preference pref_block_domains = screen.findPreference("use_hosts");
    EditTextPreference pref_rcode = (EditTextPreference) screen.findPreference("rcode");
    Preference pref_hosts_import = screen.findPreference("hosts_import");
    EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
    final Preference pref_hosts_download = screen.findPreference("hosts_download");

    pref_rcode.setTitle(getString(R.string.setting_rcode, prefs.getString("rcode", "3")));

    if (Util.isPlayStoreInstall(this)) {
        Log.i(TAG, "Play store install");
        cat_options.removePreference(screen.findPreference("update_check"));
        cat_advanced.removePreference(pref_block_domains);
        cat_advanced.removePreference(pref_rcode);
        cat_advanced.removePreference(pref_forwarding);
        cat_backup.removePreference(pref_hosts_import);
        cat_backup.removePreference(pref_hosts_url);
        cat_backup.removePreference(pref_hosts_download);

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

        // Handle hosts import
        // https://github.com/Free-Software-for-Android/AdAway/wiki/HostsSources
        pref_hosts_import.setEnabled(getIntentOpenHosts().resolveActivity(getPackageManager()) != null);
        pref_hosts_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivityForResult(getIntentOpenHosts(), ActivitySettings.REQUEST_HOSTS);
                return true;
            }
        });

        // 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) {
                final File tmp = new File(getFilesDir(), "hosts.tmp");
                final File hosts = new File(getFilesDir(), "hosts.txt");
                EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
                try {
                    new DownloadTask(ActivitySettings.this, new URL(pref_hosts_url.getText()), tmp,
                            new DownloadTask.Listener() {
                                @Override
                                public void onCompleted() {
                                    if (hosts.exists())
                                        hosts.delete();
                                    tmp.renameTo(hosts);

                                    String last = SimpleDateFormat.getDateTimeInstance()
                                            .format(new Date().getTime());
                                    prefs.edit().putString("hosts_last_download", last).apply();

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

                                    ServiceSinkhole.reload("hosts file download", ActivitySettings.this);
                                }

                                @Override
                                public void onCancelled() {
                                    if (tmp.exists())
                                        tmp.delete();
                                }

                                @Override
                                public void onException(Throwable ex) {
                                    if (tmp.exists())
                                        tmp.delete();

                                    if (running)
                                        Toast.makeText(ActivitySettings.this, ex.getMessage(),
                                                Toast.LENGTH_LONG).show();
                                }
                            }).execute();
                } catch (MalformedURLException ex) {
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
                }
                return true;
            }
        });
    }

    // Development
    if (!Util.isDebuggable(this))
        screen.removePreference(screen.findPreference("screen_development"));

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

    //        markPro(screen.findPreference("theme"), ActivityPro.SKU_THEME);
    //        markPro(screen.findPreference("install"), ActivityPro.SKU_NOTIFY);
    //        markPro(screen.findPreference("show_stats"), ActivityPro.SKU_SPEED);
}

From source file:com.android.launcher2.Workspace.java

void removeItems(final ArrayList<String> packages) {
    final HashSet<String> packageNames = new HashSet<String>();
    packageNames.addAll(packages);//from   w  w  w .  j a  v a2 s .c o  m

    ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts();
    for (final CellLayout layoutParent : cellLayouts) {
        final ViewGroup layout = layoutParent.getShortcutsAndWidgets();

        // Avoid ANRs by treating each screen separately
        post(new Runnable() {
            public void run() {
                final ArrayList<View> childrenToRemove = new ArrayList<View>();
                childrenToRemove.clear();

                int childCount = layout.getChildCount();
                for (int j = 0; j < childCount; j++) {
                    final View view = layout.getChildAt(j);
                    Object tag = view.getTag();

                    if (tag instanceof ShortcutInfo) {
                        final ShortcutInfo info = (ShortcutInfo) tag;
                        final Intent intent = info.intent;
                        final ComponentName name = intent.getComponent();

                        if (name != null) {
                            if (packageNames.contains(name.getPackageName())) {
                                LauncherModel.deleteItemFromDatabase(mLauncher, info);
                                childrenToRemove.add(view);
                            }
                        }
                    } else if (tag instanceof FolderInfo) {
                        final FolderInfo info = (FolderInfo) tag;
                        final ArrayList<ShortcutInfo> contents = info.contents;
                        final int contentsCount = contents.size();
                        final ArrayList<ShortcutInfo> appsToRemoveFromFolder = new ArrayList<ShortcutInfo>();

                        for (int k = 0; k < contentsCount; k++) {
                            final ShortcutInfo appInfo = contents.get(k);
                            final Intent intent = appInfo.intent;
                            final ComponentName name = intent.getComponent();

                            if (name != null) {
                                if (packageNames.contains(name.getPackageName())) {
                                    appsToRemoveFromFolder.add(appInfo);
                                }
                            }
                        }
                        for (ShortcutInfo item : appsToRemoveFromFolder) {
                            info.remove(item);
                            LauncherModel.deleteItemFromDatabase(mLauncher, item);
                        }
                    } else if (tag instanceof LauncherAppWidgetInfo) {
                        final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
                        final ComponentName provider = info.providerName;
                        if (provider != null) {
                            if (packageNames.contains(provider.getPackageName())) {
                                LauncherModel.deleteItemFromDatabase(mLauncher, info);
                                childrenToRemove.add(view);
                            }
                        }
                    }
                }

                childCount = childrenToRemove.size();
                for (int j = 0; j < childCount; j++) {
                    View child = childrenToRemove.get(j);
                    // Note: We can not remove the view directly from CellLayoutChildren as this
                    // does not re-mark the spaces as unoccupied.
                    layoutParent.removeViewInLayout(child);
                    if (child instanceof DropTarget) {
                        mDragController.removeDropTarget((DropTarget) child);
                    }
                }

                if (childCount > 0) {
                    layout.requestLayout();
                    layout.invalidate();
                }
            }
        });
    }

    // Clean up new-apps animation list
    final Context context = getContext();
    post(new Runnable() {
        @Override
        public void run() {
            String spKey = LauncherApplication.getSharedPreferencesKey();
            SharedPreferences sp = context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
            Set<String> newApps = sp.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null);

            // Remove all queued items that match the same package
            if (newApps != null) {
                synchronized (newApps) {
                    Iterator<String> iter = newApps.iterator();
                    while (iter.hasNext()) {
                        try {
                            Intent intent = Intent.parseUri(iter.next(), 0);
                            String pn = ItemInfo.getPackageName(intent);
                            if (packageNames.contains(pn)) {
                                iter.remove();
                            }

                            // It is possible that we've queued an item to be loaded, yet it has
                            // not been added to the workspace, so remove those items as well.
                            ArrayList<ItemInfo> shortcuts;
                            shortcuts = LauncherModel.getWorkspaceShortcutItemInfosWithIntent(intent);
                            for (ItemInfo info : shortcuts) {
                                LauncherModel.deleteItemFromDatabase(context, info);
                            }
                        } catch (URISyntaxException e) {
                        }
                    }
                }
            }
        }
    });
}

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

@Override
@TargetApi(Build.VERSION_CODES.M)/*from  w  w w .j ava2s  . 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;
        }
    }

    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 ("tethering".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 ("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(), "adblocker.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.adblocker.me/hosts"));

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