Android Open Source - ParticlePlay Color Picker Preference






From Project

Back to project page ParticlePlay.

License

The source code is released under:

GNU General Public License

If you think the Android project ParticlePlay listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.droidinteractive.colorpicker.widget;
/*//from  ww w .j  a  v  a2  s .c  om
 * Copyright (c) 2010 Ragdoll Games
 * Copyright (c) 2010-2014 Droid Interactive
 * Copyright (c) 2010-2014 IDKJava Team
 * 
 * This file is part of Particle Play.
 * 
 * Particle Play is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Particle Play is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Particle Play. If not, see <http://www.gnu.org/licenses/>.
 */
import com.droidinteractive.colorpicker.ColorPickerDialog;
import com.droidinteractive.particleplay.R;


import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;


public class ColorPickerPreference extends Preference {
  int value;

  public ColorPickerPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setWidgetLayoutResource(R.layout.colorpicker_pref_widget);
  }

  @Override protected void onBindView(View view) {
    super.onBindView(view);

    // Set our custom views inside the layout
    final View kotak = view.findViewById(R.id.colorpicker_pref_widget_kotak);
    if (kotak != null) {
      kotak.setBackgroundColor(value);
    }
  }

  @Override protected void onClick() {
    new ColorPickerDialog(getContext(), value, new ColorPickerDialog.OnColorPickerListener() {
      @Override public void onOk(ColorPickerDialog dialog, int color) {
        if (!callChangeListener(color)) return; // They don't want the value to be set
        value = color;
        persistInt(value);
        notifyChanged();
      }

      @Override public void onCancel(ColorPickerDialog dialog) {
        // nothing to do
      }
    }).show();
  }

  public void forceSetValue(int value) {
    this.value = value;
    persistInt(value);
    notifyChanged();
  }

  @Override protected Object onGetDefaultValue(TypedArray a, int index) {
    // This preference type's value type is Integer, so we read the default value from the attributes as an Integer.
    return a.getInteger(index, 0);
  }

  @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    if (restoreValue) { // Restore state
      value = getPersistedInt(value);
    } else { // Set state
      int value = (Integer) defaultValue;
      this.value = value;
      persistInt(value);
    }
  }

  /*
   * Suppose a client uses this preference type without persisting. We
   * must save the instance state so it is able to, for example, survive
   * orientation changes.
   */
  @Override protected Parcelable onSaveInstanceState() {
    final Parcelable superState = super.onSaveInstanceState();
    if (isPersistent()) return superState; // No need to save instance state since it's persistent

    final SavedState myState = new SavedState(superState);
    myState.value = value;
    return myState;
  }

  @Override protected void onRestoreInstanceState(Parcelable state) {
    if (!state.getClass().equals(SavedState.class)) {
      // Didn't save state for us in onSaveInstanceState
      super.onRestoreInstanceState(state);
      return;
    }

    // Restore the instance state
    SavedState myState = (SavedState) state;
    super.onRestoreInstanceState(myState.getSuperState());
    this.value = myState.value;
    notifyChanged();
  }

  /**
   * SavedState, a subclass of {@link BaseSavedState}, will store the state
   * of MyPreference, a subclass of Preference.
   * <p>
   * It is important to always call through to super methods.
   */
  private static class SavedState extends BaseSavedState {
    int value;

    public SavedState(Parcel source) {
      super(source);
      value = source.readInt();
    }

    @Override public void writeToParcel(Parcel dest, int flags) {
      super.writeToParcel(dest, flags);
      dest.writeInt(value);
    }

    public SavedState(Parcelable superState) {
      super(superState);
    }

    @SuppressWarnings("unused") public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
      public SavedState createFromParcel(Parcel in) {
        return new SavedState(in);
      }

      public SavedState[] newArray(int size) {
        return new SavedState[size];
      }
    };
  }
}




Java Source Code List

com.droidinteractive.colorpicker.ColorPickerBox.java
com.droidinteractive.colorpicker.ColorPickerDialog.java
com.droidinteractive.colorpicker.widget.ColorPickerPreferenceWidgetView.java
com.droidinteractive.colorpicker.widget.ColorPickerPreference.java
com.droidinteractive.particleplay.FixMeActivity.java
com.droidinteractive.particleplay.Globals.java
com.droidinteractive.particleplay.LoadStateActivity.java
com.droidinteractive.particleplay.MainActivity.java
com.droidinteractive.particleplay.MenuActivity.java
com.droidinteractive.particleplay.SaveStateActivity.java
com.droidinteractive.particleplay.SplashActivity.java
com.droidinteractive.particleplay.custom.CustomElementActivity.java
com.droidinteractive.particleplay.custom.CustomElementAdvancedActivity.java
com.droidinteractive.particleplay.custom.CustomElementBasicActivity.java
com.droidinteractive.particleplay.custom.CustomElementManagerActivity.java
com.droidinteractive.particleplay.custom.CustomElementManager.java
com.droidinteractive.particleplay.custom.CustomElement.java
com.droidinteractive.particleplay.game.ActionItem.java
com.droidinteractive.particleplay.game.Control.java
com.droidinteractive.particleplay.game.CustomElementManager.java
com.droidinteractive.particleplay.game.FileManager.java
com.droidinteractive.particleplay.game.MenuBar.java
com.droidinteractive.particleplay.game.PopupWindows.java
com.droidinteractive.particleplay.game.QuickAction.java
com.droidinteractive.particleplay.game.SandView.java
com.droidinteractive.particleplay.game.SaveManager.java
com.droidinteractive.particleplay.game.Temperature.java
com.droidinteractive.particleplay.preferences.AlphaPatternDrawable.java
com.droidinteractive.particleplay.preferences.ColorPickerDialogPref.java
com.droidinteractive.particleplay.preferences.ColorPickerPanelView.java
com.droidinteractive.particleplay.preferences.ColorPickerPreference.java
com.droidinteractive.particleplay.preferences.ColorPickerView.java
com.droidinteractive.particleplay.preferences.PreferencesActivity.java
com.droidinteractive.particleplay.preferences.Preferences.java
com.droidinteractive.particleplay.preferences.SeekBarPreference.java
com.droidinteractive.slidingdrawer.OnDrawerCloseListener.java
com.droidinteractive.slidingdrawer.OnDrawerOpenListener.java
com.droidinteractive.slidingdrawer.OnDrawerScrollListener.java
com.droidinteractive.slidingdrawer.SlidingDrawer.java