@Override
  protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult) {
      Editor editor = getEditor();
      editor.putString("display_color_calibration", DisplayColorCalibration.getCurColors());
      editor.commit();
    } else if (mOriginalColors != null) {
      DisplayColorCalibration.setColors(mOriginalColors);
    }
  }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      if (fromUser) {
        mCurrentColors[mIndex] = String.valueOf(progress);
        DisplayColorCalibration.setColors(TextUtils.join(" ", mCurrentColors));
      }

      int min = DisplayColorCalibration.getMinValue();
      int max = DisplayColorCalibration.getMaxValue();
      int percent = Math.round(100F * progress / (max - min));
      mValue.setText(String.format("%d%%", percent));
    }
    public ColorSeekBar(SeekBar seekBar, TextView value, int index) {
      mSeekBar = seekBar;
      mValue = value;
      mIndex = index;

      mSeekBar.setMax(DisplayColorCalibration.getMaxValue());
      mSeekBar.setOnSeekBarChangeListener(this);
    }
 public static boolean isSupported() {
   try {
     return DisplayColorCalibration.isSupported();
   } catch (NoClassDefFoundError e) {
     // Hardware abstraction framework isn't installed
     return false;
   }
 }
  public static void restore(Context context) {
    if (!isSupported()) {
      return;
    }

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String value = prefs.getString("display_color_calibration", null);

    if (value != null) {
      DisplayColorCalibration.setColors(value);
    }
  }
  @Override
  protected void onBindDialogView(View view) {
    super.onBindDialogView(view);

    mOriginalColors = DisplayColorCalibration.getCurColors();
    mCurrentColors = mOriginalColors.split(" ");

    for (int i = 0; i < SEEKBAR_ID.length; i++) {
      SeekBar seekBar = (SeekBar) view.findViewById(SEEKBAR_ID[i]);
      TextView value = (TextView) view.findViewById(SEEKBAR_VALUE_ID[i]);
      mSeekBars[i] = new ColorSeekBar(seekBar, value, i);
      mSeekBars[i].setValueFromString(mCurrentColors[i]);
    }
  }
  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    if (state == null || !state.getClass().equals(SavedState.class)) {
      // Didn't save state for us in onSaveInstanceState
      super.onRestoreInstanceState(state);
      return;
    }

    SavedState myState = (SavedState) state;
    super.onRestoreInstanceState(myState.getSuperState());
    mOriginalColors = myState.originalColors;
    mCurrentColors = myState.currentColors;
    for (int i = 0; i < mSeekBars.length; i++) {
      mSeekBars[i].setValueFromString(mCurrentColors[i]);
    }
    DisplayColorCalibration.setColors(TextUtils.join(" ", mCurrentColors));
  }
  @Override
  protected Parcelable onSaveInstanceState() {
    final Parcelable superState = super.onSaveInstanceState();
    if (getDialog() == null || !getDialog().isShowing()) {
      return superState;
    }

    // Save the dialog state
    final SavedState myState = new SavedState(superState);
    myState.currentColors = mCurrentColors;
    myState.originalColors = mOriginalColors;

    // Restore the old state when the activity or dialog is being paused
    DisplayColorCalibration.setColors(mOriginalColors);
    mOriginalColors = null;

    return myState;
  }