UserProfilePreference.java :  » UnTagged » tzdice » com » marquisx » tzdice » preference » Android Open Source

Android Open Source » UnTagged » tzdice 
tzdice » com » marquisx » tzdice » preference » UserProfilePreference.java
package com.marquisx.tzdice.preference;

import android.content.Context;
import android.content.Intent;
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;
import android.widget.ImageView;

import com.marquisx.tzdice.Constants;
import com.marquisx.tzdice.R;
import com.marquisx.tzdice.Setting;
import com.marquisx.tzdice.UserProfilePicGrid;

public class UserProfilePreference extends Preference
{
  private int pictureId;
  private ImageView pictureIV;
  
  // This is the constructor called by the inflater
  public UserProfilePreference(Context context, AttributeSet attrs)
  {
    super(context, attrs);

    setWidgetLayoutResource(R.layout.preference_widget_userprofile);
  }
  
  public void refresh()
  {
    pictureId = Setting.getUserProfilePicId(getContext());
    notifyChanged();
  }
  
  protected void onClick()
  {
    getContext().startActivity(new Intent(getContext(), UserProfilePicGrid.class));
  }
  
  @Override
  protected void onBindView(View view)
  {
    super.onBindView(view);

    // Set our custom views inside the layout
    pictureIV = (ImageView) view.findViewById(R.id.preference_userprofile_widget_iv);
    if (pictureIV != null)
    {
      pictureIV.setImageResource(pictureId);
    }
  }

  @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
      //TODO
      pictureId = getPersistedInt(Constants.USE_RPROFILE_PIC_ID_DEF);
    }
    else
    {
      // Set state
      int value = (Integer) defaultValue;
      pictureId = value;
      persistInt(value);
    }
  }

  @Override
  protected Parcelable onSaveInstanceState()
  {
    /*
     * 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.
     */

    final Parcelable superState = super.onSaveInstanceState();
    if (isPersistent())
    {
      // No need to save instance state since it's persistent
      return superState;
    }

    // Save the instance state
    final SavedState myState = new SavedState(superState);
    myState.pictureId = pictureId;
    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());
    pictureId = myState.pictureId;
    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 pictureId;

    public SavedState(Parcel source)
    {
      super(source);

      // Restore the click counter
      pictureId = source.readInt();
    }

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

      // Save the click counter
      dest.writeInt(pictureId);
    }

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

    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];
      }
    };
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.