Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcel;
Expand Down Expand Up @@ -40,6 +41,10 @@ public class HmsPicker extends LinearLayout implements Button.OnClickListener, B
private int mDeleteDrawableSrcResId;
private int mTheme = -1;

private int mSign;
public static final int SIGN_POSITIVE = 0;
public static final int SIGN_NEGATIVE = 1;

/**
* Instantiates an HmsPicker object
*
Expand All @@ -53,7 +58,7 @@ public HmsPicker(Context context) {
* Instantiates an HmsPicker object
*
* @param context the Context required for creation
* @param attrs additional attributes that define custom colors, selectors, and backgrounds.
* @param attrs additional attributes that define custom colors, selectors, and backgrounds.
*/
public HmsPicker(Context context, AttributeSet attrs) {
super(context, attrs);
Expand Down Expand Up @@ -125,6 +130,10 @@ private void restyleViews() {
if (mEnteredHms != null) {
mEnteredHms.setTheme(mTheme);
}
if (mLeft != null) {
mLeft.setTextColor(mTextColor);
mLeft.setBackgroundResource(mKeyBackgroundResId);
}
}

@Override
Expand Down Expand Up @@ -155,7 +164,7 @@ protected void onFinishInflate() {
mLeft = (Button) v4.findViewById(R.id.key_left);
mNumbers[0] = (Button) v4.findViewById(R.id.key_middle);
mRight = (Button) v4.findViewById(R.id.key_right);
setLeftRightEnabled(false);
setRightEnabled(false);

for (int i = 0; i < 10; i++) {
mNumbers[i].setOnClickListener(this);
Expand All @@ -164,6 +173,10 @@ protected void onFinishInflate() {
}
updateHms();

Resources res = mContext.getResources();
mLeft.setText(res.getString(R.string.number_picker_plus_minus));
mLeft.setOnClickListener(this);

mHoursLabel = (TextView) findViewById(R.id.hours_label);
mMinutesLabel = (TextView) findViewById(R.id.minutes_label);
mSecondsLabel = (TextView) findViewById(R.id.seconds_label);
Expand Down Expand Up @@ -203,10 +216,20 @@ protected void doOnClick(View v) {
mInput[mInputPointer] = 0;
mInputPointer--;
}
} else if (v == mLeft) {
onLeftClicked();
}
updateKeypad();
}

private void onLeftClicked() {
if (isNegative()) {
mSign = SIGN_POSITIVE;
} else {
mSign = SIGN_NEGATIVE;
}
}

@Override
public boolean onLongClick(View v) {
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
Expand Down Expand Up @@ -243,13 +266,13 @@ private void updateKeypad() {

/**
* Update the time displayed in the picker:
*
* <p/>
* Put "-" in digits that was not entered by passing -1
*
* <p/>
* Hide digit by passing -2 (for highest hours digit only);
*/
protected void updateHms() {
mEnteredHms.setTime(mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]);
mEnteredHms.setTime(isNegative(), mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]);
}

private void addClickedNumber(int val) {
Expand Down Expand Up @@ -317,10 +340,21 @@ public int getSeconds() {
return mInput[1] * 10 + mInput[0];
}

/**
* Using View.GONE, View.VISIBILE, or View.INVISIBLE, set the visibility of the plus/minus indicator
*
* @param visibility an int using Android's View.* convention
*/
public void setPlusMinusVisibility(int visibility) {
if (mLeft != null) {
mLeft.setVisibility(visibility);
}
}

/**
* Set the current hours, minutes, and seconds on the picker.
*
* @param hours the input hours value
* @param hours the input hours value
* @param minutes the input minutes value
* @param seconds the input seconds value
*/
Expand All @@ -331,7 +365,7 @@ public void setTime(int hours, int minutes, int seconds) {
mInput[1] = seconds / 10;
mInput[0] = seconds % 10;

for (int i=4; i>=0; i--) {
for (int i = 4; i >= 0; i--) {
if (mInput[i] > 0) {
mInputPointer = i;
break;
Expand All @@ -342,7 +376,7 @@ public void setTime(int hours, int minutes, int seconds) {
}


@Override
@Override
public Parcelable onSaveInstanceState() {
final Parcelable parcel = super.onSaveInstanceState();
final SavedState state = new SavedState(parcel);
Expand Down Expand Up @@ -433,12 +467,14 @@ public void restoreEntryState(Bundle inState, String key) {
}
}

protected void setLeftRightEnabled(boolean enabled) {
mLeft.setEnabled(enabled);
protected void setRightEnabled(boolean enabled) {
mRight.setEnabled(enabled);
if (!enabled) {
mLeft.setContentDescription(null);
mRight.setContentDescription(null);
}
}

public boolean isNegative() {
return mSign == SIGN_NEGATIVE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,26 @@ public class HmsPickerBuilder {
private Fragment targetFragment;
private int mReference;
private Vector<HmsPickerDialogHandler> mHmsPickerDialogHandlers = new Vector<HmsPickerDialogHandler>();
private Vector<HmsPickerDialogFragment.HmsPickerDialogHandlerV2> mHmsPickerDialogHandlerV2s = new Vector<HmsPickerDialogFragment.HmsPickerDialogHandlerV2>();
private int mHours;
private int mMinutes;
private int mSeconds;
private Integer plusMinusVisibility;

/**
* Set the visibility of the +/- button. This takes an int corresponding to Android's View.VISIBLE, View.INVISIBLE,
* or View.GONE. When using View.INVISIBLE, the +/- button will still be present in the layout but be
* non-clickable. When set to View.GONE, the +/- button will disappear entirely, and the "0" button will occupy its
* space.
*
* @param plusMinusVisibility an int corresponding to View.VISIBLE, View.INVISIBLE, or View.GONE
* @return the current Builder object
*/
public HmsPickerBuilder setPlusMinusVisibility(int plusMinusVisibility) {
this.plusMinusVisibility = plusMinusVisibility;
return this;
}


/**
* Attach a FragmentManager. This is required for creation of the Fragment.
Expand Down Expand Up @@ -68,6 +85,32 @@ public HmsPickerBuilder setReference(int reference) {
return this;
}

/**
* @param handler an Object implementing the appropriate Picker Handler
* @return the current Builder object
* @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status
* <p/>
* Attach universal objects as additional handlers for notification when the Picker is set. For most use cases, this
* method is not necessary as attachment to an Activity or Fragment is done automatically. If, however, you would
* like additional objects to subscribe to this Picker being set, attach Handlers here.
*/
public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler) {
this.mHmsPickerDialogHandlers.add(handler);
return this;
}

/**
* @param handler the Object to remove
* @return the current Builder object
* @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status
* <p/>
* Remove objects previously added as handlers.
*/
public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogHandler handler) {
this.mHmsPickerDialogHandlers.remove(handler);
return this;
}

/**
* Attach universal objects as additional handlers for notification when the Picker is set. For most use cases, this
* method is not necessary as attachment to an Activity or Fragment is done automatically. If, however, you would
Expand All @@ -76,8 +119,8 @@ public HmsPickerBuilder setReference(int reference) {
* @param handler an Object implementing the appropriate Picker Handler
* @return the current Builder object
*/
public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler) {
this.mHmsPickerDialogHandlers.add(handler);
public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogFragment.HmsPickerDialogHandlerV2 handler) {
this.mHmsPickerDialogHandlerV2s.add(handler);
return this;
}

Expand All @@ -87,15 +130,15 @@ public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler
* @param handler the Object to remove
* @return the current Builder object
*/
public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogHandler handler) {
this.mHmsPickerDialogHandlers.remove(handler);
public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogFragment.HmsPickerDialogHandlerV2 handler) {
this.mHmsPickerDialogHandlerV2s.remove(handler);
return this;
}

/**
* Set some initial values for the picker
*
* @param hours the initial hours value
* @param hours the initial hours value
* @param minutes the initial minutes value
* @param seconds the initial seconds value
* @return the current Builder object
Expand Down Expand Up @@ -147,11 +190,12 @@ public void show() {
}
ft.addToBackStack(null);

final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId);
final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId, plusMinusVisibility);
if (targetFragment != null) {
fragment.setTargetFragment(targetFragment, 0);
}
fragment.setHmsPickerDialogHandlers(mHmsPickerDialogHandlers);
fragment.setHmsPickerDialogHandlersV2(mHmsPickerDialogHandlerV2s);

if ((mHours | mMinutes | mSeconds) != 0) {
fragment.setTime(mHours, mMinutes, mSeconds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ public class HmsPickerDialogFragment extends DialogFragment {

private static final String REFERENCE_KEY = "HmsPickerDialogFragment_ReferenceKey";
private static final String THEME_RES_ID_KEY = "HmsPickerDialogFragment_ThemeResIdKey";
private static final String PLUS_MINUS_VISIBILITY_KEY = "HmsPickerDialogFragment_PlusMinusVisibilityKey";

private HmsPicker mPicker;

private int mReference = -1;
private int mTheme = -1;
private ColorStateList mTextColor;
private int mDialogBackgroundResId;
@Deprecated
private Vector<HmsPickerDialogHandler> mHmsPickerDialogHandlers = new Vector<HmsPickerDialogHandler>();
private Vector<HmsPickerDialogHandlerV2> mHmsPickerDialogHandlerV2s = new Vector<HmsPickerDialogHandlerV2>();
private int mHours;
private int mMinutes;
private int mSeconds;
private int mPlusMinusVisibility = View.INVISIBLE;

/**
* Create an instance of the Picker (used internally)
Expand All @@ -41,11 +45,14 @@ public class HmsPickerDialogFragment extends DialogFragment {
* @param themeResId the style resource ID for theming
* @return a Picker!
*/
public static HmsPickerDialogFragment newInstance(int reference, int themeResId) {
public static HmsPickerDialogFragment newInstance(int reference, int themeResId, Integer plusMinusVisibility) {
final HmsPickerDialogFragment frag = new HmsPickerDialogFragment();
Bundle args = new Bundle();
args.putInt(REFERENCE_KEY, reference);
args.putInt(THEME_RES_ID_KEY, themeResId);
if (plusMinusVisibility != null) {
args.putInt(PLUS_MINUS_VISIBILITY_KEY, plusMinusVisibility);
}
frag.setArguments(args);
return frag;
}
Expand All @@ -66,6 +73,9 @@ public void onCreate(Bundle savedInstanceState) {
if (args != null && args.containsKey(THEME_RES_ID_KEY)) {
mTheme = args.getInt(THEME_RES_ID_KEY);
}
if (args != null && args.containsKey(PLUS_MINUS_VISIBILITY_KEY)) {
mPlusMinusVisibility = args.getInt(PLUS_MINUS_VISIBILITY_KEY);
}

setStyle(DialogFragment.STYLE_NO_TITLE, 0);

Expand Down Expand Up @@ -104,8 +114,13 @@ public void onClick(View view) {
for (HmsPickerDialogHandler handler : mHmsPickerDialogHandlers) {
handler.onDialogHmsSet(mReference, mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
}
for (HmsPickerDialogHandlerV2 handler : mHmsPickerDialogHandlerV2s) {
handler.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
}

final Activity activity = getActivity();
final Fragment fragment = getTargetFragment();

if (activity instanceof HmsPickerDialogHandler) {
final HmsPickerDialogHandler act =
(HmsPickerDialogHandler) activity;
Expand All @@ -115,6 +130,17 @@ public void onClick(View view) {
(HmsPickerDialogHandler) fragment;
frag.onDialogHmsSet(mReference, mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
}

if (activity instanceof HmsPickerDialogHandlerV2) {
final HmsPickerDialogHandlerV2 act =
(HmsPickerDialogHandlerV2) activity;
act.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
} else if (fragment instanceof HmsPickerDialogHandlerV2) {
final HmsPickerDialogHandlerV2 frag =
(HmsPickerDialogHandlerV2) fragment;
frag.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
}

dismiss();
}
});
Expand All @@ -123,29 +149,47 @@ public void onClick(View view) {
mPicker.setSetButton(doneButton);
mPicker.setTime(mHours, mMinutes, mSeconds);
mPicker.setTheme(mTheme);
mPicker.setPlusMinusVisibility(mPlusMinusVisibility);

getDialog().getWindow().setBackgroundDrawableResource(mDialogBackgroundResId);

return view;
}

public interface HmsPickerDialogHandlerV2 {

void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds);
}

/**
* @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status
* This interface allows objects to register for the Picker's set action.
*/
@Deprecated
public interface HmsPickerDialogHandler {

@Deprecated
void onDialogHmsSet(int reference, int hours, int minutes, int seconds);
}

/**
* Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment.
*
* @param handlers a Vector of handlers
* @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status
* Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment.
*/
@Deprecated
public void setHmsPickerDialogHandlers(Vector<HmsPickerDialogHandler> handlers) {
mHmsPickerDialogHandlers = handlers;
}

/**
* @param handlers a Vector of handlers
* Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment.
*/
public void setHmsPickerDialogHandlersV2(Vector<HmsPickerDialogHandlerV2> handlers) {
mHmsPickerDialogHandlerV2s = handlers;
}

public void setTime(int hours, int minutes, int seconds) {
this.mHours = hours;
this.mMinutes = minutes;
Expand Down
Loading