@Override
  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {

    String key = preference.getKey();

    Log.w(TAG, "key: " + key);

    if (key.equals(DeviceSettings.KEY_USE_ACCELEROMETER_CALIBRATION)) {
      Utils.writeValue(FILE_ACCELEROMETER_CALIB, ((CheckBoxPreference) preference).isChecked());
      getPreferenceScreen()
          .findPreference(DeviceSettings.KEY_CALIBRATE_ACCELEROMETER)
          .setEnabled(((CheckBoxPreference) preference).isChecked());
    } else if (key.equals(DeviceSettings.KEY_CALIBRATE_ACCELEROMETER)) {
      // when calibration data utilization is disabled and enabled back,
      // calibration is done at the same time by driver
      Utils.writeValue(FILE_ACCELEROMETER_CALIB, "0");
      Utils.writeValue(FILE_ACCELEROMETER_CALIB, "1");
      Utils.showDialog(
          getActivity(),
          getString(R.string.accelerometer_dialog_head),
          getString(R.string.accelerometer_dialog_message),
          false);
    } else if (key.equals(DeviceSettings.KEY_DISABLE_BLN)) {
      Utils.writeValue(FILE_BLN, !((CheckBoxPreference) preference).isChecked());
      getPreferenceScreen()
          .findPreference(DeviceSettings.KEY_DISABLE_BLN_BLINK)
          .setEnabled(!((CheckBoxPreference) preference).isChecked());
      getPreferenceScreen()
          .findPreference(DeviceSettings.KEY_BLN_ONDELAY)
          .setEnabled(!((CheckBoxPreference) preference).isChecked());
      getPreferenceScreen()
          .findPreference(DeviceSettings.KEY_BLN_OFFDELAY)
          .setEnabled(!((CheckBoxPreference) preference).isChecked());
    } else if (key.equals(DeviceSettings.KEY_DISABLE_BLN_BLINK)) {
      Utils.writeValue(
          FILE_BLN_BLINK, (((CheckBoxPreference) preference).isChecked() ? "off" : "on"));
      getPreferenceScreen()
          .findPreference(DeviceSettings.KEY_BLN_ONDELAY)
          .setEnabled(!((CheckBoxPreference) preference).isChecked());
      getPreferenceScreen()
          .findPreference(DeviceSettings.KEY_BLN_OFFDELAY)
          .setEnabled(!((CheckBoxPreference) preference).isChecked());
    } else if (key.equals(DeviceSettings.KEY_BURNING_LED)) {
      Utils.writeValue(FILE_BURNING_LED, ((CheckBoxPreference) preference).isChecked());
    } else if (key.equals(DeviceSettings.KEY_ENABLE_VOLTAGE)) {
      Utils.writeValue(
          FILE_VOLTAGE1,
          "set_volt=" + (((CheckBoxPreference) preference).isChecked() ? "1" : "0 "));
      if (Utils.isJanice()) {
        Utils.writeValue(
            FILE_VOLTAGE2,
            "set_volt=" + (((CheckBoxPreference) preference).isChecked() ? "1" : "0 "));
      }
    }

    return true;
  }
  public static void restore(Context context) {
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

    Utils.writeValue(
        FILE_BURNING_LED,
        sharedPrefs.getBoolean(DeviceSettings.KEY_BURNING_LED, false) ? "1" : "0");

    Utils.writeValue(
        FILE_BLN, sharedPrefs.getBoolean(DeviceSettings.KEY_DISABLE_BLN, false) ? "0" : "1");

    Utils.writeValue(
        FILE_BLN_BLINK,
        sharedPrefs.getBoolean(DeviceSettings.KEY_DISABLE_BLN_BLINK, false) ? "off" : "on");

    boolean accelerometerCalib =
        sharedPrefs.getBoolean(DeviceSettings.KEY_USE_ACCELEROMETER_CALIBRATION, true);
    if (!accelerometerCalib) Utils.writeValue(FILE_ACCELEROMETER_CALIB, "0");

    Utils.writeValue(
        FILE_VOLTAGE1,
        "set_volt="
            + (sharedPrefs.getBoolean(DeviceSettings.KEY_ENABLE_VOLTAGE, false) ? "1" : "0"));

    if (Utils.isJanice())
      Utils.writeValue(
          FILE_VOLTAGE2,
          "set_volt="
              + (sharedPrefs.getBoolean(DeviceSettings.KEY_ENABLE_VOLTAGE, false) ? "1" : "0"));
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.advanced_preferences);

    getPreferenceScreen()
        .findPreference(DeviceSettings.KEY_CALIBRATE_ACCELEROMETER)
        .setEnabled(
            ((CheckBoxPreference) findPreference("use_accelerometer_calibration")).isChecked());

    getActivity().getActionBar().setTitle(getResources().getString(R.string.advanced_name));
    getActivity().getActionBar().setIcon(getResources().getDrawable(R.drawable.advanced_icon));

    // Compatibility check for janice (BLN)
    if (Utils.isJanice()) {
      getPreferenceScreen().removePreference(findPreference(DeviceSettings.KEY_BACKLIGHT));
    }
  }