Numpad time picker saves instance state, used in edit alarm screen
This commit is contained in:
parent
c768559acc
commit
b395722386
@ -8,8 +8,6 @@ import android.support.v4.app.Fragment;
|
|||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
import android.support.v4.app.FragmentPagerAdapter;
|
import android.support.v4.app.FragmentPagerAdapter;
|
||||||
import android.support.v4.view.ViewPager;
|
import android.support.v4.view.ViewPager;
|
||||||
import android.text.format.DateFormat;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -17,8 +15,7 @@ import android.view.ViewGroup;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.philliphsu.clock2.alarms.AlarmsFragment;
|
import com.philliphsu.clock2.alarms.AlarmsFragment;
|
||||||
import com.philliphsu.clock2.editalarm.NumpadTimePickerDialog;
|
import com.philliphsu.clock2.editalarm.EditAlarmActivity;
|
||||||
import com.philliphsu.clock2.editalarm.OnTimeSetListener;
|
|
||||||
import com.philliphsu.clock2.settings.SettingsActivity;
|
import com.philliphsu.clock2.settings.SettingsActivity;
|
||||||
|
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
@ -58,14 +55,6 @@ public class MainActivity extends BaseActivity {
|
|||||||
mFab.setOnClickListener(new View.OnClickListener() {
|
mFab.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
NumpadTimePickerDialog tpd = NumpadTimePickerDialog.newInstance(new OnTimeSetListener() {
|
|
||||||
@Override
|
|
||||||
public void onTimeSet(ViewGroup viewGroup, int hourOfDay, int minute) {
|
|
||||||
Log.i(TAG, "Time set: " + String.format("%02d:%02d", hourOfDay, minute));
|
|
||||||
}
|
|
||||||
}, 0, 0, DateFormat.is24HourFormat(MainActivity.this));
|
|
||||||
tpd.show(getFragmentManager(), "tag");
|
|
||||||
/*
|
|
||||||
Intent intent = new Intent(MainActivity.this, EditAlarmActivity.class);
|
Intent intent = new Intent(MainActivity.this, EditAlarmActivity.class);
|
||||||
// Call Fragment#startActivityForResult() instead of Activity#startActivityForResult()
|
// Call Fragment#startActivityForResult() instead of Activity#startActivityForResult()
|
||||||
// because we want the result to be handled in the Fragment, not in this Activity.
|
// because we want the result to be handled in the Fragment, not in this Activity.
|
||||||
@ -73,7 +62,6 @@ public class MainActivity extends BaseActivity {
|
|||||||
// Fragment's onActivityResult() will NOT be called.
|
// Fragment's onActivityResult() will NOT be called.
|
||||||
mSectionsPagerAdapter.getCurrentFragment()
|
mSectionsPagerAdapter.getCurrentFragment()
|
||||||
.startActivityForResult(intent, AlarmsFragment.REQUEST_CREATE_ALARM);
|
.startActivityForResult(intent, AlarmsFragment.REQUEST_CREATE_ALARM);
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,6 +36,7 @@ import com.philliphsu.clock2.model.AlarmLoader;
|
|||||||
import com.philliphsu.clock2.ringtone.RingtoneActivity;
|
import com.philliphsu.clock2.ringtone.RingtoneActivity;
|
||||||
import com.philliphsu.clock2.util.AlarmController;
|
import com.philliphsu.clock2.util.AlarmController;
|
||||||
import com.philliphsu.clock2.util.AlarmUtils;
|
import com.philliphsu.clock2.util.AlarmUtils;
|
||||||
|
import com.philliphsu.clock2.util.DateFormatUtils;
|
||||||
import com.philliphsu.clock2.util.LocalBroadcastHelper;
|
import com.philliphsu.clock2.util.LocalBroadcastHelper;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -50,7 +51,6 @@ import static android.view.View.GONE;
|
|||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
import static com.philliphsu.clock2.DaysOfWeek.SATURDAY;
|
import static com.philliphsu.clock2.DaysOfWeek.SATURDAY;
|
||||||
import static com.philliphsu.clock2.DaysOfWeek.SUNDAY;
|
import static com.philliphsu.clock2.DaysOfWeek.SUNDAY;
|
||||||
import static com.philliphsu.clock2.util.KeyboardUtils.hideKeyboard;
|
|
||||||
import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,17 +59,19 @@ import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
|||||||
* The class would have the API for editing the alarm, so move all
|
* The class would have the API for editing the alarm, so move all
|
||||||
* the relevant helper methods from here to there.
|
* the relevant helper methods from here to there.
|
||||||
*/
|
*/
|
||||||
public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyListener,
|
public class EditAlarmActivity extends BaseActivity implements
|
||||||
|
AlarmNumpad.KeyListener, // TODO: Deprecated, remove
|
||||||
EditAlarmContract.View, // TODO: Remove @Override from the methods
|
EditAlarmContract.View, // TODO: Remove @Override from the methods
|
||||||
AlarmUtilsHelper,
|
AlarmUtilsHelper,
|
||||||
SharedPreferencesHelper,
|
SharedPreferencesHelper,
|
||||||
LoaderManager.LoaderCallbacks<Alarm>,
|
LoaderManager.LoaderCallbacks<Alarm>,
|
||||||
OnTimeSetListener {
|
NumpadTimePicker.OnTimeSetListener {
|
||||||
private static final String TAG = "EditAlarmActivity";
|
private static final String TAG = "EditAlarmActivity";
|
||||||
public static final String EXTRA_ALARM_ID = "com.philliphsu.clock2.editalarm.extra.ALARM_ID";
|
public static final String EXTRA_ALARM_ID = "com.philliphsu.clock2.editalarm.extra.ALARM_ID";
|
||||||
public static final String EXTRA_MODIFIED_ALARM = "com.philliphsu.clock2.editalarm.extra.MODIFIED_ALARM";
|
public static final String EXTRA_MODIFIED_ALARM = "com.philliphsu.clock2.editalarm.extra.MODIFIED_ALARM";
|
||||||
public static final String EXTRA_IS_DELETING = "com.philliphsu.clock2.editalarm.extra.IS_DELETING";
|
public static final String EXTRA_IS_DELETING = "com.philliphsu.clock2.editalarm.extra.IS_DELETING";
|
||||||
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
||||||
|
private static final String TAG_TIME_PICKER = "time_picker";
|
||||||
|
|
||||||
private static final int REQUEST_PICK_RINGTONE = 0;
|
private static final int REQUEST_PICK_RINGTONE = 0;
|
||||||
private static final int ID_MENU_ITEM = 0;
|
private static final int ID_MENU_ITEM = 0;
|
||||||
@ -77,12 +79,14 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
private long mOldAlarmId;
|
private long mOldAlarmId;
|
||||||
private Uri mSelectedRingtoneUri;
|
private Uri mSelectedRingtoneUri;
|
||||||
private Alarm mOldAlarm;
|
private Alarm mOldAlarm;
|
||||||
|
private int mSelectedHourOfDay = -1;
|
||||||
|
private int mSelctedMinute = -1;
|
||||||
|
|
||||||
@Bind(R.id.main_content) CoordinatorLayout mMainContent;
|
@Bind(R.id.main_content) CoordinatorLayout mMainContent;
|
||||||
@Bind(R.id.save) Button mSave;
|
@Bind(R.id.save) Button mSave;
|
||||||
@Bind(R.id.delete) Button mDelete;
|
@Bind(R.id.delete) Button mDelete;
|
||||||
@Bind(R.id.on_off) SwitchCompat mSwitch;
|
@Bind(R.id.on_off) SwitchCompat mSwitch;
|
||||||
@Bind(R.id.input_time) EditText mTimeText;
|
@Bind(R.id.input_time) TextView mTimeText;
|
||||||
@Bind({R.id.day0, R.id.day1, R.id.day2, R.id.day3, R.id.day4, R.id.day5, R.id.day6})
|
@Bind({R.id.day0, R.id.day1, R.id.day2, R.id.day3, R.id.day4, R.id.day5, R.id.day6})
|
||||||
ToggleButton[] mDays;
|
ToggleButton[] mDays;
|
||||||
@Bind(R.id.label) EditText mLabel;
|
@Bind(R.id.label) EditText mLabel;
|
||||||
@ -92,7 +96,9 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTimeSet(ViewGroup viewGroup, int hourOfDay, int minute) {
|
public void onTimeSet(ViewGroup viewGroup, int hourOfDay, int minute) {
|
||||||
Log.i(TAG, "Time set: " + String.format("%02d:%02d", hourOfDay, minute));
|
mSelectedHourOfDay = hourOfDay;
|
||||||
|
mSelctedMinute = minute;
|
||||||
|
showTimeText(DateFormatUtils.formatTime(this, hourOfDay, minute));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -114,6 +120,19 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
setTimeTextHint(); // TODO: private access
|
setTimeTextHint(); // TODO: private access
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
// Was the time picker in our backstack? It could have been if it was showing
|
||||||
|
// and the device had rotated.
|
||||||
|
NumpadTimePickerDialog picker = (NumpadTimePickerDialog)
|
||||||
|
getFragmentManager().findFragmentByTag(TAG_TIME_PICKER);
|
||||||
|
if (picker != null) {
|
||||||
|
// Restore the callback
|
||||||
|
picker.setOnTimeSetListener(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||||
// TODO: Snooze menu item when alarm is ringing.
|
// TODO: Snooze menu item when alarm is ringing.
|
||||||
@ -206,6 +225,7 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
showTimeTextPostBackspace("");
|
showTimeTextPostBackspace("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* // TODO: remove
|
||||||
@OnTouch(R.id.input_time)
|
@OnTouch(R.id.input_time)
|
||||||
boolean touch(MotionEvent event) {
|
boolean touch(MotionEvent event) {
|
||||||
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
|
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
|
||||||
@ -220,6 +240,7 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
@OnClick(R.id.ringtone)
|
@OnClick(R.id.ringtone)
|
||||||
void ringtone() {
|
void ringtone() {
|
||||||
@ -336,6 +357,12 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OnClick(R.id.input_time)
|
||||||
|
void openTimePicker() {
|
||||||
|
NumpadTimePickerDialog picker = NumpadTimePickerDialog.newInstance(EditAlarmActivity.this);
|
||||||
|
picker.show(getFragmentManager(), TAG_TIME_PICKER);
|
||||||
|
}
|
||||||
|
|
||||||
private void setWeekDaysText() {
|
private void setWeekDaysText() {
|
||||||
for (int i = 0; i < mDays.length; i++) {
|
for (int i = 0; i < mDays.length; i++) {
|
||||||
int weekDay = DaysOfWeek.getInstance(this).weekDayAt(i);
|
int weekDay = DaysOfWeek.getInstance(this).weekDayAt(i);
|
||||||
@ -488,13 +515,14 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
} else {
|
} else {
|
||||||
mTimeText.setText(formattedInput);
|
mTimeText.setText(formattedInput);
|
||||||
}
|
}
|
||||||
mTimeText.setSelection(mTimeText.length());
|
//TODO:delete mTimeText.setSelection(mTimeText.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated // TODO: Remove
|
||||||
@Override
|
@Override
|
||||||
public void showTimeTextPostBackspace(String newStr) {
|
public void showTimeTextPostBackspace(String newStr) {
|
||||||
mTimeText.setText(newStr);
|
mTimeText.setText(newStr);
|
||||||
mTimeText.setSelection(mTimeText.length());
|
//TODO:delete mTimeText.setSelection(mTimeText.length());
|
||||||
if (!mNumpad.checkTimeValid() && mSwitch.isChecked()) {
|
if (!mNumpad.checkTimeValid() && mSwitch.isChecked()) {
|
||||||
mSwitch.setChecked(false);
|
mSwitch.setChecked(false);
|
||||||
}
|
}
|
||||||
@ -519,7 +547,7 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
if (focused) {
|
if (focused) {
|
||||||
mTimeText.requestFocus();
|
mTimeText.requestFocus();
|
||||||
// Move cursor to end
|
// Move cursor to end
|
||||||
mTimeText.setSelection(mTimeText.length());
|
//TODO:delete mTimeText.setSelection(mTimeText.length());
|
||||||
} else {
|
} else {
|
||||||
mTimeText.clearFocus(); // TODO: not cleared! focus needs to go to a neighboring view.
|
mTimeText.clearFocus(); // TODO: not cleared! focus needs to go to a neighboring view.
|
||||||
}
|
}
|
||||||
@ -581,7 +609,8 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
// TODO default values
|
// TODO default values
|
||||||
showTimeTextFocused(true);
|
showTimeTextFocused(true);
|
||||||
showRingtone(""); // gets default ringtone
|
showRingtone(""); // gets default ringtone
|
||||||
showNumpad(true);
|
// TODO: Show the dialog instead
|
||||||
|
//showNumpad(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -90,6 +90,15 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic
|
|||||||
return mInput[index];
|
return mInput[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a defensive copy of the internal array of inputted digits
|
||||||
|
*/
|
||||||
|
protected final int[] getDigits() {
|
||||||
|
int[] digits = new int[mInput.length];
|
||||||
|
System.arraycopy(mInput, 0, digits, 0, mInput.length);
|
||||||
|
return digits;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the number of digits inputted
|
* @return the number of digits inputted
|
||||||
*/
|
*/
|
||||||
@ -184,10 +193,14 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic
|
|||||||
* with the String value of those digits.
|
* with the String value of those digits.
|
||||||
*/
|
*/
|
||||||
protected final void insertDigits(int... digits) {
|
protected final void insertDigits(int... digits) {
|
||||||
|
if (digits == null)
|
||||||
|
return;
|
||||||
String newDigits = "";
|
String newDigits = "";
|
||||||
for (int d : digits) {
|
for (int d : digits) {
|
||||||
if (mCount == mInput.length)
|
if (mCount == mInput.length)
|
||||||
break;
|
break;
|
||||||
|
if (d == UNMODIFIED)
|
||||||
|
continue;
|
||||||
mInput[mCount++] = d;
|
mInput[mCount++] = d;
|
||||||
newDigits += d;
|
newDigits += d;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ import butterknife.OnClick;
|
|||||||
/**
|
/**
|
||||||
* Created by Phillip Hsu on 7/12/2016.
|
* Created by Phillip Hsu on 7/12/2016.
|
||||||
*/
|
*/
|
||||||
public class NumpadTimePicker extends GridLayoutNumpad {
|
public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
|
||||||
// Time can be represented with maximum of 4 digits
|
// Time can be represented with maximum of 4 digits
|
||||||
private static final int MAX_DIGITS = 4;
|
private static final int MAX_DIGITS = 4;
|
||||||
|
|
||||||
@ -101,8 +101,7 @@ public class NumpadTimePicker extends GridLayoutNumpad {
|
|||||||
mFormattedInput.delete(mFormattedInput.indexOf(" "), len);
|
mFormattedInput.delete(mFormattedInput.indexOf(" "), len);
|
||||||
// No digit was actually deleted, but we have to notify the
|
// No digit was actually deleted, but we have to notify the
|
||||||
// listener to update its output.
|
// listener to update its output.
|
||||||
/*TOneverDO: remove super*/super.onDigitDeleted(
|
/*TOneverDO: remove super*/super.onDigitDeleted(mFormattedInput.toString());
|
||||||
mFormattedInput.toString());
|
|
||||||
updateNumpadStates();
|
updateNumpadStates();
|
||||||
} else {
|
} else {
|
||||||
super.delete();
|
super.delete();
|
||||||
@ -110,9 +109,10 @@ public class NumpadTimePicker extends GridLayoutNumpad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the hour of day (0-23) regardless of clock system */
|
/** Returns the hour of day (0-23) regardless of clock system */
|
||||||
public int getHours() {
|
@Override
|
||||||
|
public int hourOfDay() {
|
||||||
if (!checkTimeValid())
|
if (!checkTimeValid())
|
||||||
throw new IllegalStateException("Cannot call getHours() until legal time inputted");
|
throw new IllegalStateException("Cannot call hourOfDay() until legal time inputted");
|
||||||
int hours = count() < 4 ? valueAt(0) : valueAt(0) * 10 + valueAt(1);
|
int hours = count() < 4 ? valueAt(0) : valueAt(0) * 10 + valueAt(1);
|
||||||
if (hours == 12) {
|
if (hours == 12) {
|
||||||
switch (mAmPmState) {
|
switch (mAmPmState) {
|
||||||
@ -130,9 +130,10 @@ public class NumpadTimePicker extends GridLayoutNumpad {
|
|||||||
return hours + (mAmPmState == PM ? 12 : 0);
|
return hours + (mAmPmState == PM ? 12 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMinutes() {
|
@Override
|
||||||
|
public int minute() {
|
||||||
if (!checkTimeValid())
|
if (!checkTimeValid())
|
||||||
throw new IllegalStateException("Cannot call getMinutes() until legal time inputted");
|
throw new IllegalStateException("Cannot call minute() until legal time inputted");
|
||||||
return count() < 4 ? valueAt(1) * 10 + valueAt(2) : valueAt(2) * 10 + valueAt(3);
|
return count() < 4 ? valueAt(1) * 10 + valueAt(2) : valueAt(2) * 10 + valueAt(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,6 +180,7 @@ public class NumpadTimePicker extends GridLayoutNumpad {
|
|||||||
amPmState = HRS_24;
|
amPmState = HRS_24;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// Convert the hour and minutes into text form, so that
|
// Convert the hour and minutes into text form, so that
|
||||||
// we can read each digit individually.
|
// we can read each digit individually.
|
||||||
// Only if on 24-hour clock, zero-pad single digit hours.
|
// Only if on 24-hour clock, zero-pad single digit hours.
|
||||||
@ -192,8 +194,14 @@ public class NumpadTimePicker extends GridLayoutNumpad {
|
|||||||
for (int i = 0; i < textDigits.length(); i++) {
|
for (int i = 0; i < textDigits.length(); i++) {
|
||||||
digits[i] = Character.digit(textDigits.charAt(i), BASE_10);
|
digits[i] = Character.digit(textDigits.charAt(i), BASE_10);
|
||||||
}
|
}
|
||||||
|
|
||||||
insertDigits(digits);
|
insertDigits(digits);
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (is24HourFormat() || hours > 9) {
|
||||||
|
insertDigits(hours / 10, hours % 10, minutes / 10, minutes % 10);
|
||||||
|
} else {
|
||||||
|
insertDigits(hours, minutes / 10, minutes % 10);
|
||||||
|
}
|
||||||
|
|
||||||
mAmPmState = amPmState;
|
mAmPmState = amPmState;
|
||||||
if (mAmPmState != HRS_24) {
|
if (mAmPmState != HRS_24) {
|
||||||
@ -236,15 +244,17 @@ public class NumpadTimePicker extends GridLayoutNumpad {
|
|||||||
mFormattedInput.append(' ').append(altBtn.getText());
|
mFormattedInput.append(' ').append(altBtn.getText());
|
||||||
mAmPmState = mAltButtons[0] == altBtn ? AM : PM;
|
mAmPmState = mAltButtons[0] == altBtn ? AM : PM;
|
||||||
// Digits will be shown for you on insert, but not AM/PM
|
// Digits will be shown for you on insert, but not AM/PM
|
||||||
/*TOneverDO: remove super*/super.onDigitInserted(
|
/*TOneverDO: remove super*/super.onDigitInserted(mFormattedInput.toString());
|
||||||
mFormattedInput.toString());
|
|
||||||
} else {
|
} else {
|
||||||
CharSequence text = altBtn.getText();
|
CharSequence text = altBtn.getText();
|
||||||
int[] digits = new int[text.length() - 1];
|
int[] digits = new int[text.length() - 1];
|
||||||
// charAt(0) is the colon, so skip i = 0.
|
// charAt(0) is the colon, so skip i = 0.
|
||||||
// We are only interested in storing the digits.
|
// We are only interested in storing the digits.
|
||||||
for (int i = 1; i < text.length(); i++) {
|
for (int i = 1; i < text.length(); i++) {
|
||||||
digits[i] = Character.digit(text.charAt(i), BASE_10);
|
// The array and the text do not have the same lengths,
|
||||||
|
// so the iterator value does not correspond to the
|
||||||
|
// array index directly
|
||||||
|
digits[i - 1] = Character.digit(text.charAt(i), BASE_10);
|
||||||
}
|
}
|
||||||
// Colon is added for you
|
// Colon is added for you
|
||||||
insertDigits(digits);
|
insertDigits(digits);
|
||||||
|
|||||||
@ -28,12 +28,17 @@ public class NumpadTimePickerDialog extends DialogFragment
|
|||||||
private static final String KEY_HOUR_OF_DAY = "hour_of_day";
|
private static final String KEY_HOUR_OF_DAY = "hour_of_day";
|
||||||
private static final String KEY_MINUTE = "minute";
|
private static final String KEY_MINUTE = "minute";
|
||||||
private static final String KEY_IS_24_HOUR_VIEW = "is_24_hour_view";
|
private static final String KEY_IS_24_HOUR_VIEW = "is_24_hour_view";
|
||||||
|
private static final String KEY_DIGITS_INPUTTED = "digits_inputted";
|
||||||
|
|
||||||
private OnTimeSetListener mCallback;
|
private TimePicker.OnTimeSetListener mCallback;
|
||||||
|
|
||||||
private int mInitialHourOfDay;
|
private int mInitialHourOfDay;
|
||||||
private int mInitialMinute;
|
private int mInitialMinute;
|
||||||
private boolean mIs24HourMode;
|
private boolean mIs24HourMode;
|
||||||
|
/**
|
||||||
|
* The digits stored in the numpad from the last time onSaveInstanceState() was called
|
||||||
|
*/
|
||||||
|
private int[] mInputtedDigits;
|
||||||
|
|
||||||
@Bind(R.id.backspace) ImageButton mBackspace;
|
@Bind(R.id.backspace) ImageButton mBackspace;
|
||||||
@Bind(R.id.input) EditText mInputField;
|
@Bind(R.id.input) EditText mInputField;
|
||||||
@ -46,14 +51,23 @@ public class NumpadTimePickerDialog extends DialogFragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: We don't need to pass in an initial hour and minute for a new instance.
|
// TODO: We don't need to pass in an initial hour and minute for a new instance.
|
||||||
public static NumpadTimePickerDialog newInstance(OnTimeSetListener callback,
|
// TODO: Delete is24HourMode?
|
||||||
|
@Deprecated
|
||||||
|
public static NumpadTimePickerDialog newInstance(TimePicker.OnTimeSetListener callback,
|
||||||
int hourOfDay, int minute, boolean is24HourMode) {
|
int hourOfDay, int minute, boolean is24HourMode) {
|
||||||
NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
|
NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
|
||||||
ret.initialize(callback, hourOfDay, minute, is24HourMode);
|
ret.initialize(callback, hourOfDay, minute, is24HourMode);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(OnTimeSetListener callback,
|
public static NumpadTimePickerDialog newInstance(TimePicker.OnTimeSetListener callback) {
|
||||||
|
NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
|
||||||
|
ret.setOnTimeSetListener(callback);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
public void initialize(TimePicker.OnTimeSetListener callback,
|
||||||
int hourOfDay, int minute, boolean is24HourMode) {
|
int hourOfDay, int minute, boolean is24HourMode) {
|
||||||
mCallback = callback;
|
mCallback = callback;
|
||||||
mInitialHourOfDay = hourOfDay;
|
mInitialHourOfDay = hourOfDay;
|
||||||
@ -61,18 +75,15 @@ public class NumpadTimePickerDialog extends DialogFragment
|
|||||||
mIs24HourMode = is24HourMode;
|
mIs24HourMode = is24HourMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOnTimeSetListener(OnTimeSetListener callback) {
|
public void setOnTimeSetListener(TimePicker.OnTimeSetListener callback) {
|
||||||
mCallback = callback;
|
mCallback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
if (savedInstanceState != null && savedInstanceState.containsKey(KEY_HOUR_OF_DAY)
|
if (savedInstanceState != null) {
|
||||||
&& savedInstanceState.containsKey(KEY_MINUTE)
|
mInputtedDigits = savedInstanceState.getIntArray(KEY_DIGITS_INPUTTED);
|
||||||
&& savedInstanceState.containsKey(KEY_IS_24_HOUR_VIEW)) {
|
|
||||||
mInitialHourOfDay = savedInstanceState.getInt(KEY_HOUR_OF_DAY);
|
|
||||||
mInitialMinute = savedInstanceState.getInt(KEY_MINUTE);
|
|
||||||
mIs24HourMode = savedInstanceState.getBoolean(KEY_IS_24_HOUR_VIEW);
|
mIs24HourMode = savedInstanceState.getBoolean(KEY_IS_24_HOUR_VIEW);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,12 +92,14 @@ public class NumpadTimePickerDialog extends DialogFragment
|
|||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
|
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
|
||||||
|
|
||||||
View view = inflater.inflate(R.layout.dialog_time_picker_numpad, container, false);
|
View view = inflater.inflate(R.layout.dialog_time_picker_numpad, container, false);
|
||||||
ButterKnife.bind(this, view);
|
ButterKnife.bind(this, view);
|
||||||
mNumpad.setOnInputChangeListener(this);
|
|
||||||
|
|
||||||
//mNumpad.setTime(mInitialHourOfDay, mInitialMinute);
|
mNumpad.setOnInputChangeListener(this);
|
||||||
// TODO: Write numpad method set24HourMode() and use mIs24HourMode
|
mNumpad.insertDigits(mInputtedDigits); // TOneverDO: before mNumpad.setOnInputChangeListener(this);
|
||||||
|
// TODO: Disabled color
|
||||||
|
updateInputText(""); // Primarily to disable 'OK'
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
@ -94,8 +107,7 @@ public class NumpadTimePickerDialog extends DialogFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
if (mNumpad != null) {
|
if (mNumpad != null) {
|
||||||
outState.putInt(KEY_HOUR_OF_DAY, mNumpad.getHours());
|
outState.putIntArray(KEY_DIGITS_INPUTTED, mNumpad.getDigits());
|
||||||
outState.putInt(KEY_MINUTE, mNumpad.getMinutes());
|
|
||||||
outState.putBoolean(KEY_IS_24_HOUR_VIEW, mIs24HourMode);
|
outState.putBoolean(KEY_IS_24_HOUR_VIEW, mIs24HourMode);
|
||||||
//outState.putBoolean(KEY_DARK_THEME, mThemeDark);
|
//outState.putBoolean(KEY_DARK_THEME, mThemeDark);
|
||||||
}
|
}
|
||||||
@ -125,7 +137,7 @@ public class NumpadTimePickerDialog extends DialogFragment
|
|||||||
void ok() {
|
void ok() {
|
||||||
if (!mNumpad.checkTimeValid())
|
if (!mNumpad.checkTimeValid())
|
||||||
return;
|
return;
|
||||||
mCallback.onTimeSet(mNumpad, mNumpad.getHours(), mNumpad.getMinutes());
|
mCallback.onTimeSet(mNumpad, mNumpad.hourOfDay(), mNumpad.minute());
|
||||||
dismiss();
|
dismiss();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,5 +154,6 @@ public class NumpadTimePickerDialog extends DialogFragment
|
|||||||
|
|
||||||
private void updateInputText(String inputText) {
|
private void updateInputText(String inputText) {
|
||||||
mInputField.setText(inputText);
|
mInputField.setText(inputText);
|
||||||
|
mOkButton.setEnabled(mNumpad.checkTimeValid());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,18 +0,0 @@
|
|||||||
package com.philliphsu.clock2.editalarm;
|
|
||||||
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Phillip Hsu on 7/12/2016.
|
|
||||||
*
|
|
||||||
* The callback interface used to indicate the user is done filling in
|
|
||||||
* the time (they clicked on the 'Set' button).
|
|
||||||
*/
|
|
||||||
public interface OnTimeSetListener {
|
|
||||||
/**
|
|
||||||
* @param viewGroup The view associated with this listener.
|
|
||||||
* @param hourOfDay The hour that was set.
|
|
||||||
* @param minute The minute that was set.
|
|
||||||
*/
|
|
||||||
void onTimeSet(ViewGroup viewGroup, int hourOfDay, int minute);
|
|
||||||
}
|
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
package com.philliphsu.clock2.editalarm;
|
||||||
|
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Phillip Hsu on 7/12/2016.
|
||||||
|
*
|
||||||
|
* The callback interface used to indicate the user is done filling in
|
||||||
|
* the time (they clicked on the 'Set' button).
|
||||||
|
*/
|
||||||
|
public interface TimePicker {
|
||||||
|
int hourOfDay();
|
||||||
|
int minute();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The callback interface used to indicate the user is done filling in
|
||||||
|
* the time (they clicked on the 'Set' button).
|
||||||
|
*/
|
||||||
|
interface OnTimeSetListener {
|
||||||
|
/**
|
||||||
|
* @param viewGroup The view associated with this listener.
|
||||||
|
* @param hourOfDay The hour that was set.
|
||||||
|
* @param minute The minute that was set.
|
||||||
|
*/
|
||||||
|
void onTimeSet(ViewGroup viewGroup, int hourOfDay, int minute);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@ package com.philliphsu.clock2.util;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import static android.text.format.DateFormat.getTimeFormat;
|
import static android.text.format.DateFormat.getTimeFormat;
|
||||||
@ -16,4 +17,11 @@ public final class DateFormatUtils {
|
|||||||
public static String formatTime(Context context, long millis) {
|
public static String formatTime(Context context, long millis) {
|
||||||
return getTimeFormat(context).format(new Date(millis));
|
return getTimeFormat(context).format(new Date(millis));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String formatTime(Context context, int hourOfDay, int minute) {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
|
||||||
|
cal.set(Calendar.MINUTE, minute);
|
||||||
|
return formatTime(context, cal.getTimeInMillis());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -85,17 +85,14 @@
|
|||||||
android:layout_alignParentEnd="true"
|
android:layout_alignParentEnd="true"
|
||||||
android:layout_centerVertical="true"/>
|
android:layout_centerVertical="true"/>
|
||||||
|
|
||||||
<!-- TODO: Find out how to get a touch ripple; foreground attr didn't work -->
|
<TextView
|
||||||
<EditText
|
|
||||||
android:id="@+id/input_time"
|
android:id="@+id/input_time"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:foreground="?selectableItemBackground"
|
android:background="?selectableItemBackground"
|
||||||
android:background="@android:color/transparent"
|
|
||||||
android:hint="@string/default_alarm_time_12h"
|
android:hint="@string/default_alarm_time_12h"
|
||||||
android:textSize="56sp"
|
android:textSize="56sp"
|
||||||
android:layout_toStartOf="@id/on_off"
|
android:layout_toStartOf="@id/on_off"/>
|
||||||
android:inputType="time"/>
|
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
||||||
|
|||||||
@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
<ImageButton
|
<ImageButton
|
||||||
android:id="@+id/backspace"
|
android:id="@+id/backspace"
|
||||||
android:layout_width="48dp"
|
android:layout_width="72dp"
|
||||||
android:layout_height="48dp"
|
android:layout_height="72dp"
|
||||||
android:src="@drawable/ic_backspace_24dp"
|
android:src="@drawable/ic_backspace_24dp"
|
||||||
android:background="?selectableItemBackground"
|
android:background="?selectableItemBackground"
|
||||||
android:layout_alignParentEnd="true"
|
android:layout_alignParentEnd="true"
|
||||||
@ -15,13 +15,14 @@
|
|||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/input"
|
android:id="@+id/input"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="48dp"
|
android:layout_height="72dp"
|
||||||
android:layout_centerHorizontal="true"
|
android:layout_centerHorizontal="true"
|
||||||
android:layout_alignParentTop="true"
|
android:layout_alignParentTop="true"
|
||||||
android:background="@android:color/transparent"
|
android:background="@android:color/transparent"
|
||||||
android:textSize="28sp"
|
android:textSize="40sp"
|
||||||
android:focusable="false"
|
android:focusable="false"
|
||||||
android:focusableInTouchMode="false"/>
|
android:focusableInTouchMode="false"
|
||||||
|
android:cursorVisible="true"/>
|
||||||
|
|
||||||
<View
|
<View
|
||||||
android:id="@+id/divider"
|
android:id="@+id/divider"
|
||||||
@ -30,19 +31,28 @@
|
|||||||
android:background="?android:listDivider"
|
android:background="?android:listDivider"
|
||||||
android:layout_below="@id/input"/>
|
android:layout_below="@id/input"/>
|
||||||
|
|
||||||
|
<!-- TODO: Your height attr is being ignored because
|
||||||
|
you constrained this below and above the header and
|
||||||
|
footer views -->
|
||||||
|
<com.philliphsu.clock2.editalarm.NumpadTimePicker
|
||||||
|
android:id="@+id/number_grid"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="270dp"
|
||||||
|
android:layout_below="@id/divider"/>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/actions"
|
android:id="@+id/actions"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
android:layout_alignParentBottom="true">
|
android:layout_below="@id/number_grid">
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/cancel"
|
android:id="@+id/cancel"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:text="Cancel"
|
android:text="CANCEL"
|
||||||
style="@style/Widget.AppCompat.Button.Borderless.Colored"/>
|
style="@style/Widget.AppCompat.Button.Borderless.Colored"/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
@ -50,16 +60,9 @@
|
|||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:text="Ok"
|
android:text="OK"
|
||||||
style="@style/Widget.AppCompat.Button.Borderless.Colored"/>
|
style="@style/Widget.AppCompat.Button.Borderless.Colored"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<com.philliphsu.clock2.editalarm.NumpadTimePicker
|
|
||||||
android:id="@+id/number_grid"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:layout_below="@id/divider"
|
|
||||||
android:layout_above="@id/actions"/>
|
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
@ -25,7 +25,7 @@
|
|||||||
<item name="android:layout_width">0dp</item>
|
<item name="android:layout_width">0dp</item>
|
||||||
<item name="android:layout_height">0dp</item>
|
<item name="android:layout_height">0dp</item>
|
||||||
<item name="android:background">?android:attr/selectableItemBackground</item>
|
<item name="android:background">?android:attr/selectableItemBackground</item>
|
||||||
<item name="android:textSize">20sp</item>
|
<item name="android:textSize">28sp</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user