Example usage for android.preference PreferenceScreen setPersistent

List of usage examples for android.preference PreferenceScreen setPersistent

Introduction

In this page you can find the example usage for android.preference PreferenceScreen setPersistent.

Prototype

public void setPersistent(boolean persistent) 

Source Link

Document

Sets whether this Preference is persistent.

Usage

From source file:de.ub0r.android.callmeter.ui.prefs.PreferencesPlain.java

private void loadRules() {
    new AsyncTask<Void, Void, JSONObject>() {
        @Override/*from  ww w.  ja  va 2 s .  c o  m*/
        protected JSONObject doInBackground(final Void... params) {
            try {
                String l;
                StringBuilder sb = new StringBuilder();

                File f = new File(PreferencesPlain.this.getCacheDir(), "ub0rrules.json");
                if (f.exists() && f.lastModified() + CACHE_TIMEOUT > System.currentTimeMillis()) {
                    Log.i(TAG, "found cached data: " + f.getAbsolutePath());
                    BufferedReader r = new BufferedReader(new FileReader(f));
                    while ((l = r.readLine()) != null) {
                        sb.append(l);
                    }
                    r.close();
                }
                if (sb.length() == 0) {
                    if (f.exists()) {
                        f.delete();
                    }
                    HttpURLConnection c = (HttpURLConnection) new URL(
                            "http://ub0r.de/android/callmeter/rulesets/rulesets.json").openConnection();
                    Log.i(TAG, "load new data: " + c.getURL());
                    BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream()));
                    FileWriter w = new FileWriter(f);
                    while ((l = r.readLine()) != null) {
                        sb.append(l);
                        w.write(l);
                    }
                    r.close();
                    w.close();
                }
                try {
                    return new JSONObject(sb.toString());
                } catch (JSONException e) {
                    Log.e(TAG, "JSON Error", e);
                    Log.e(TAG, "JSON: " + sb.toString());
                    return null;
                }
            } catch (IOException e) {
                Log.e(TAG, "IOError", e);
            }
            return null;
        }

        @SuppressWarnings({ "deprecation", "rawtypes" })
        @Override
        protected void onPostExecute(final JSONObject result) {
            if (result == null) {
                Toast.makeText(PreferencesPlain.this, R.string.err_export_read, Toast.LENGTH_LONG).show();
                return;
            }
            PreferenceGroup base = (PreferenceGroup) PreferencesPlain.this.findPreference("import_rules_users");
            PreferenceManager pm = base.getPreferenceManager();
            // delete old
            base.removeAll();
            base = (PreferenceGroup) PreferencesPlain.this.findPreference("import_rules_base");
            // build list
            ArrayList<String> keys = new ArrayList<String>(result.length());

            Iterator it = result.keys();
            while (it.hasNext()) {
                keys.add(it.next().toString());
            }
            Collections.sort(keys);
            keys.remove("common");
            keys.add(0, "common");

            OnPreferenceClickListener opcl = new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(final Preference preference) {
                    Intent intent = new Intent(PreferencesPlain.this, PreferencesRules.class);
                    intent.putExtra(PreferencesRules.EXTRA_JSON, result.toString());
                    intent.putExtra(PreferencesRules.EXTRA_COUNTRY, preference.getKey());
                    PreferencesPlain.this.startActivity(intent);
                    return true;
                }
            };

            for (String k : keys) {
                PreferenceScreen p = pm.createPreferenceScreen(PreferencesPlain.this);
                p.setPersistent(false);
                p.setKey(k);
                p.setTitle(k);
                p.setOnPreferenceClickListener(opcl);
                base.addPreference(p);
            }
        }

        ;
    }.execute((Void) null);
}