Example usage for android.preference PreferenceCategory findPreference

List of usage examples for android.preference PreferenceCategory findPreference

Introduction

In this page you can find the example usage for android.preference PreferenceCategory findPreference.

Prototype

public Preference findPreference(CharSequence key) 

Source Link

Document

Finds a Preference based on its key.

Usage

From source file:de.grobox.blitzmail.MainActivity.java

@Override
protected void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

    // recreate send now preference
    PreferenceCategory cat = (PreferenceCategory) findPreference("pref_sending");
    if (cat.findPreference("pref_send_now") != null) {
        cat.removePreference(findPreference("pref_send_now"));
    }/*from w  w  w. j  a va 2s .  c om*/
    addSendNowPref(this);
}

From source file:com.almalence.opencam.Fragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    thiz = this;/*from   w  ww . j  a  v a  2 s .  c om*/

    String settings = getArguments().getString("type");

    ApplicationScreen.getPluginManager().loadHeaderContent(settings, this);

    if (null == getPreferenceScreen())
        return;
    for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
        initSummary(getPreferenceScreen().getPreference(i));
    }

    Preference nightPreference = findPreference("night");
    if (nightPreference != null) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        if (prefs.getBoolean("useCamera2Pref", false)) {
            getPreferenceScreen().removePreference(nightPreference);
        } else {
            Preference superPreference = findPreference("super");
            getPreferenceScreen().removePreference(superPreference);
        }
    }

    final SeekBarPreference brightnessPref = (SeekBarPreference) this.findPreference("brightnessPref");
    if (brightnessPref != null) {
        // Set seekbar summary :
        float gamma = PreferenceManager.getDefaultSharedPreferences(getActivity()).getFloat("gammaPref", 0.5f);
        brightnessPref.setSummary(
                this.getString(R.string.Pref_Super_BrightnessEnhancementValue).replace("$1", "" + gamma));
        brightnessPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            private final Map<Integer, Float> gamma_map = new HashMap<Integer, Float>() {
                {
                    put(0, 0.5f);
                    put(1, 0.55f);
                    put(2, 0.6f);
                    put(3, 0.65f);
                    put(4, 0.7f);
                }
            };

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                int radius = (Integer) newValue;
                float gamma = gamma_map.get(radius);
                PreferenceManager
                        .getDefaultSharedPreferences(Fragment.thiz.getActivity().getApplicationContext()).edit()
                        .putFloat("gammaPref", gamma).commit();
                brightnessPref.setSummary(getActivity()
                        .getString(R.string.Pref_Super_BrightnessEnhancementValue).replace("$1", "" + gamma));
                return true;
            }
        });
    }

    final CheckBoxPreference upscalePref = (CheckBoxPreference) this.findPreference("upscaleResult");
    if (upscalePref != null) {
        Size size = CameraController.getMaxCameraImageSize(CameraController.YUV);
        long resMpx = 0;
        float mpix = 0.0f;
        if (size != null) {
            resMpx = (long) ((long) size.getWidth() * (long) size.getHeight() * 2.25);
            mpix = (float) resMpx / 1000000.f;
        }

        String name = String.format("%3.1f Mpix ", mpix);

        upscalePref.setSummary(
                getActivity().getString(R.string.Pref_Super_SummaryUpscale).replace("$1", "" + name));
    }

    Preference cameraParameters = findPreference("camera_parameters");
    if (cameraParameters != null) {
        cameraParameters.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                try {
                    showCameraParameters();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return true;
            }
        });
    }

    CheckBoxPreference helpPref = (CheckBoxPreference) findPreference("showHelpPrefCommon");
    if (helpPref != null)
        helpPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                if (((CheckBoxPreference) preference).isChecked()) {
                    SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(MainScreen.getMainContext());
                    Editor prefsEditor = prefs.edit();
                    prefsEditor.putBoolean("droShowHelp", true);
                    prefsEditor.putBoolean("sequenceRemovalShowHelp", true);
                    prefsEditor.putBoolean("panoramaShowHelp", true);
                    prefsEditor.putBoolean("superShowHelp", true);
                    prefsEditor.putBoolean("groupshotRemovalShowHelp", true);
                    prefsEditor.putBoolean("objectRemovalShowHelp", true);
                    prefsEditor.putBoolean("bestShotShowHelp", true);
                    prefsEditor.commit();
                }

                return true;
            }
        });

    EditTextPreference prefix = (EditTextPreference) this
            .findPreference(getResources().getString(R.string.Preference_SavePathPrefixValue));
    EditTextPreference postfix = (EditTextPreference) this
            .findPreference(getResources().getString(R.string.Preference_SavePathPostfixValue));
    initExportName(null, null);

    if (prefix != null) {
        prefix.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                initExportName(preference, newValue);
                return true;
            }
        });
    }

    if (postfix != null) {
        postfix.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                initExportName(preference, newValue);
                return true;
            }
        });
    }

    Preference sonyPreference = findPreference(MainScreen.sSonyCamerasPref);
    if (sonyPreference != null) {
        sonyPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                boolean sonyCamerasAvailable = (Boolean) newValue;
                if (sonyCamerasAvailable) {
                    Toast.makeText(getActivity(),
                            getActivity().getString(R.string.pref_general_more_sonyCamera_available),
                            Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
    }

    ListPreference saveToPreference = (ListPreference) this
            .findPreference(getResources().getString(R.string.Preference_SaveToValue));

    // if android 5+, then remove "save to SD card" option. Because it's
    // equals to "save to custom folder" option.
    if (saveToPreference != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        CharSequence[] entries = saveToPreference.getEntries();
        CharSequence[] entriyValues = saveToPreference.getEntryValues();

        CharSequence[] newEntries = new String[2];
        CharSequence[] newEntriyValues = new String[2];

        newEntries[0] = entries[0];
        newEntries[1] = entries[2];
        newEntriyValues[0] = entriyValues[0];
        newEntriyValues[1] = entriyValues[2];

        saveToPreference.setEntries(newEntries);
        saveToPreference.setEntryValues(newEntriyValues);
    }
    if (saveToPreference != null) {

        saveToPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                int v = -1;

                int v_old = 0;

                try {
                    v = Integer.parseInt(newValue.toString());
                    v_old = Integer.parseInt(((ListPreference) preference).getValue());
                } catch (NumberFormatException e) {

                }

                if ((v == 2 || v == 1) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                        Toast.makeText(MainScreen.getInstance(),
                                MainScreen.getAppResources()
                                        .getString(R.string.pref_advanced_saving_saveToPref_CantSaveToSD),
                                Toast.LENGTH_LONG).show();

                        if (isDeviceRooted()) {
                            Intent intent = new Intent(Preferences.thiz, FolderPicker.class);

                            intent.putExtra(MainScreen.sSavePathPref, v_old);

                            Preferences.thiz.startActivity(intent);

                            return true;
                        } else
                            return false;
                    }
                }

                if (v == 2 || v == 1) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
                        startActivityForResult(intent, CHOOSE_FOLDER_CODE);
                    } else if (v != 1) {
                        Intent intent = new Intent(Preferences.thiz, FolderPicker.class);

                        intent.putExtra(MainScreen.sSavePathPref, v_old);

                        Preferences.thiz.startActivity(intent);
                    }
                }

                return true;
            }
        });
    }

    PreferenceCategory cat = (PreferenceCategory) this.findPreference("Pref_VFCommon_Preference_Category");
    if (cat != null) {
        CheckBoxPreference cp = (CheckBoxPreference) cat.findPreference("maxScreenBrightnessPref");
        if (cp != null) {
            cp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    boolean value = Boolean.parseBoolean(newValue.toString());
                    setScreenBrightness(value);
                    return true;
                }
            });
        }
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
    boolean MaxScreenBrightnessPreference = prefs.getBoolean("maxScreenBrightnessPref", false);
    setScreenBrightness(MaxScreenBrightnessPreference);
}