Back to project page ExpertAndroid.
The source code is released under:
MIT License
If you think the Android project ExpertAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.iuriio.demos.expertandroid.ch2durationcontrol; //w w w. j a v a 2 s.com import android.app.Activity; import android.app.DialogFragment; import android.app.FragmentManager; import android.content.Context; import android.content.res.TypedArray; import android.os.Parcel; import android.os.Parcelable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Calendar; public class DurationControl extends LinearLayout implements View.OnClickListener { private Button fromButton; private Button toButton; private Calendar fromDate; private Calendar toDate; private int durationUnits; public DurationControl(Context context) { super(context); this.initialize(context); } public DurationControl(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DurationControl(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.DurationControl, defStyle, 0); this.durationUnits = t.getInt(R.styleable.DurationControl_durationUnits, this.durationUnits); t.recycle(); this.initialize(context); } private void initialize(Context context) { LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.duration_view_layout, this); // Load controls. this.fromButton = (Button) this.findViewById(R.id.fromButton); this.fromButton.setOnClickListener(this); this.toButton = (Button) this.findViewById(R.id.toButton); this.toButton.setOnClickListener(this); // Enable state saving. this.setSaveEnabled(true); } private FragmentManager getFragmentManager() { final Context context = this.getContext(); if (context instanceof Activity) { return ((Activity) context).getFragmentManager(); } throw new RuntimeException("Activity context expected instead"); } /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { DialogFragment newFragment = null; switch (v.getId()) { case R.id.fromButton: newFragment = new DatePickerFragment(this, R.id.fromButton); newFragment.show(this.getFragmentManager(), "com.iuriio.demos.tags.datePicker"); break; case R.id.toButton: newFragment = new DatePickerFragment(this, R.id.toButton); newFragment.show(this.getFragmentManager(), "com.iuriio.demos.tags.datePicker"); break; } } public void onDateSet(int buttonId, int year, int monthOfYear, int dayOfMonth) { final Calendar calendar = this.getDate(year, monthOfYear, dayOfMonth); switch (buttonId) { case R.id.fromButton: this.setFromDate(calendar); break; case R.id.toButton: this.setToDate(calendar); break; } } private void setToDate(Calendar calendar) { if (calendar == null) { return; } this.toDate = calendar; TextView toText = (TextView) this.findViewById(R.id.toDate); toText.setText(this.getDateString(calendar)); } private void setFromDate(Calendar calendar) { if (calendar == null) { return; } this.fromDate = calendar; TextView fromText = (TextView) this.findViewById(R.id.fromDate); fromText.setText(this.getDateString(calendar)); } private String getDateString(Calendar calendar) { if (calendar == null) { return "null"; } final SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy"); format.setLenient(false); String result = format.format(calendar.getTime()); return result; } private Calendar getDate(int year, int month, int day) { final Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day); return calendar; } /** * Hook allowing a view to re-apply a representation of its internal state that had previously * been generated by {@link #onSaveInstanceState}. This function will never be called with a * null state. * * @param state The frozen state that had previously been returned by * {@link #onSaveInstanceState}. * @see #onSaveInstanceState() * @see #restoreHierarchyState(android.util.SparseArray) * @see #dispatchRestoreInstanceState(android.util.SparseArray) */ @Override protected void onRestoreInstanceState(Parcelable state) { if (!(state instanceof SavedState)) { super.onRestoreInstanceState(state); return; } SavedState ss = (SavedState) state; super.onRestoreInstanceState(ss.getSuperState()); this.setFromDate(ss.fromDate); this.setToDate(ss.toDate); } /** * Hook allowing a view to generate a representation of its internal state * that can later be used to create a new instance with that same state. * This state should only contain information that is not persistent or can * not be reconstructed later. For example, you will never store your * current position on screen because that will be computed again when a * new instance of the view is placed in its view hierarchy. * <p/> * Some examples of things you may store here: the current cursor position * in a text view (but usually not the text itself since that is stored in a * content provider or other persistent storage), the currently selected * item in a list view. * * @return Returns a Parcelable object containing the view's current dynamic * state, or null if there is nothing interesting to save. The * default implementation returns null. * @see #onRestoreInstanceState(android.os.Parcelable) * @see #saveHierarchyState(android.util.SparseArray) * @see #dispatchSaveInstanceState(android.util.SparseArray) * @see #setSaveEnabled(boolean) */ @Override protected Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); return new SavedState(superState, this.fromDate, this.toDate); } public static class SavedState extends BaseSavedState { public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { /** * Create a new instance of the Parcelable class, instantiating it * from the given Parcel whose data had previously been written by * {@link android.os.Parcelable#writeToParcel Parcelable.writeToParcel()}. * * @param source The Parcel to read the object's data from. * @return Returns a new instance of the Parcelable class. */ @Override public SavedState createFromParcel(Parcel source) { return new SavedState(source); } /** * Create a new array of the Parcelable class. * * @param size Size of the array. * @return Returns an array of the Parcelable class, with every entry * initialized to null. */ @Override public SavedState[] newArray(int size) { return new SavedState[size]; } }; private Calendar fromDate; private Calendar toDate; /** * Constructor called by derived classes when creating their SavedState objects * * @param superState The state of the superclass of this view */ public SavedState(Parcelable superState) { super(superState); } public SavedState(Parcelable superState, Calendar fromDate, Calendar toDate) { super(superState); this.fromDate = fromDate; this.toDate = toDate; } /** * Constructor used when reading from a parcel. Reads the state of the superclass. * * @param source Source to restore saved state from. */ public SavedState(Parcel source) { super(source); long fromDateValue = source.readLong(); if (fromDateValue == -1) { this.fromDate = null; } else { this.fromDate = Calendar.getInstance(); this.fromDate.setTimeInMillis(fromDateValue); } long toDateValue = source.readLong(); if (toDateValue == -1) { this.toDate = null; } else { this.toDate = Calendar.getInstance(); this.toDate.setTimeInMillis(toDateValue); } } @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); if (this.fromDate != null) { dest.writeLong(this.fromDate.getTimeInMillis()); } else { dest.writeLong(-1L); } if (this.toDate != null) { dest.writeLong(this.toDate.getTimeInMillis()); } else { dest.writeLong(-1L); } } } }