|
| 1 | +/* |
| 2 | + * Copyright (C) 2007 The Android Open Source Project |
| 3 | + * Copyright (C) 2013 Ivan Kovac [email protected] |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.simform.videoimageeditor.ikovac.timepickerwithseconds; |
| 18 | + |
| 19 | +import android.app.AlertDialog; |
| 20 | +import android.content.Context; |
| 21 | +import android.content.DialogInterface; |
| 22 | +import android.content.DialogInterface.OnClickListener; |
| 23 | +import android.os.Bundle; |
| 24 | +import android.text.format.DateFormat; |
| 25 | +import android.view.LayoutInflater; |
| 26 | +import android.view.View; |
| 27 | +import android.view.Window; |
| 28 | +import com.simform.videoimageeditor.R; |
| 29 | +import java.util.Calendar; |
| 30 | + |
| 31 | +/** |
| 32 | + * A dialog that prompts the user for the time of day using a {@link TimePicker}. |
| 33 | + */ |
| 34 | +public class MyTimePickerDialog extends AlertDialog implements OnClickListener, |
| 35 | + TimePicker.OnTimeChangedListener { |
| 36 | + |
| 37 | + /** |
| 38 | + * The callback interface used to indicate the user is done filling in |
| 39 | + * the time (they clicked on the 'Set' button). |
| 40 | + */ |
| 41 | + public interface OnTimeSetListener { |
| 42 | + |
| 43 | + /** |
| 44 | + * @param view The view associated with this listener. |
| 45 | + * @param hourOfDay The hour that was set. |
| 46 | + * @param minute The minute that was set. |
| 47 | + */ |
| 48 | + void onTimeSet(TimePicker view, int hourOfDay, int minute, int seconds); |
| 49 | + } |
| 50 | + |
| 51 | + private static final String HOUR = "hour"; |
| 52 | + private static final String MINUTE = "minute"; |
| 53 | + private static final String SECONDS = "seconds"; |
| 54 | + private static final String IS_24_HOUR = "is24hour"; |
| 55 | + |
| 56 | + private final TimePicker mTimePicker; |
| 57 | + private final OnTimeSetListener mCallback; |
| 58 | + private final Calendar mCalendar; |
| 59 | + private final java.text.DateFormat mDateFormat; |
| 60 | + |
| 61 | + int mInitialHourOfDay; |
| 62 | + int mInitialMinute; |
| 63 | + int mInitialSeconds; |
| 64 | + boolean mIs24HourView; |
| 65 | + |
| 66 | + /** |
| 67 | + * @param context Parent. |
| 68 | + * @param callBack How parent is notified. |
| 69 | + * @param hourOfDay The initial hour. |
| 70 | + * @param minute The initial minute. |
| 71 | + * @param is24HourView Whether this is a 24 hour view, or AM/PM. |
| 72 | + */ |
| 73 | + public MyTimePickerDialog(Context context, |
| 74 | + OnTimeSetListener callBack, |
| 75 | + int hourOfDay, int minute, int seconds, boolean is24HourView) { |
| 76 | + |
| 77 | + this(context, 0, |
| 78 | + callBack, hourOfDay, minute, seconds, is24HourView); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param context Parent. |
| 83 | + * @param theme the theme to apply to this dialog |
| 84 | + * @param callBack How parent is notified. |
| 85 | + * @param hourOfDay The initial hour. |
| 86 | + * @param minute The initial minute. |
| 87 | + * @param is24HourView Whether this is a 24 hour view, or AM/PM. |
| 88 | + */ |
| 89 | + public MyTimePickerDialog(Context context, |
| 90 | + int theme, |
| 91 | + OnTimeSetListener callBack, |
| 92 | + int hourOfDay, int minute, int seconds, boolean is24HourView) { |
| 93 | + super(context, theme); |
| 94 | + requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 95 | + mCallback = callBack; |
| 96 | + mInitialHourOfDay = hourOfDay; |
| 97 | + mInitialMinute = minute; |
| 98 | + mInitialSeconds = seconds; |
| 99 | + mIs24HourView = is24HourView; |
| 100 | + |
| 101 | + mDateFormat = DateFormat.getTimeFormat(context); |
| 102 | + mCalendar = Calendar.getInstance(); |
| 103 | + updateTitle(mInitialHourOfDay, mInitialMinute, mInitialSeconds); |
| 104 | + |
| 105 | + setButton(context.getText(R.string.time_set), this); |
| 106 | + setButton2(context.getText(R.string.cancel), (OnClickListener) null); |
| 107 | + |
| 108 | + LayoutInflater inflater = |
| 109 | + (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 110 | + View view = inflater.inflate(R.layout.time_picker_dialog, null); |
| 111 | + setView(view); |
| 112 | + mTimePicker = (TimePicker) view.findViewById(R.id.timePicker); |
| 113 | + |
| 114 | + // initialize state |
| 115 | + mTimePicker.setCurrentHour(mInitialHourOfDay); |
| 116 | + mTimePicker.setCurrentMinute(mInitialMinute); |
| 117 | + mTimePicker.setCurrentSecond(mInitialSeconds); |
| 118 | + mTimePicker.setIs24HourView(mIs24HourView); |
| 119 | + mTimePicker.setOnTimeChangedListener(this); |
| 120 | + } |
| 121 | + |
| 122 | + public void onClick(DialogInterface dialog, int which) { |
| 123 | + if (mCallback != null) { |
| 124 | + mTimePicker.clearFocus(); |
| 125 | + mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(), |
| 126 | + mTimePicker.getCurrentMinute(), mTimePicker.getCurrentSeconds()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public void onTimeChanged(TimePicker view, int hourOfDay, int minute, int seconds) { |
| 131 | + updateTitle(hourOfDay, minute, seconds); |
| 132 | + } |
| 133 | + |
| 134 | + public void updateTime(int hourOfDay, int minuteOfHour, int seconds) { |
| 135 | + mTimePicker.setCurrentHour(hourOfDay); |
| 136 | + mTimePicker.setCurrentMinute(minuteOfHour); |
| 137 | + mTimePicker.setCurrentSecond(seconds); |
| 138 | + } |
| 139 | + |
| 140 | + private void updateTitle(int hour, int minute, int seconds) { |
| 141 | + String sHour = String.format("%02d", hour); |
| 142 | + String sMin = String.format("%02d", minute); |
| 143 | + String sSec = String.format("%02d", seconds); |
| 144 | + setTitle(sHour + ":" + sMin + ":" + sSec); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Bundle onSaveInstanceState() { |
| 149 | + Bundle state = super.onSaveInstanceState(); |
| 150 | + state.putInt(HOUR, mTimePicker.getCurrentHour()); |
| 151 | + state.putInt(MINUTE, mTimePicker.getCurrentMinute()); |
| 152 | + state.putInt(SECONDS, mTimePicker.getCurrentSeconds()); |
| 153 | + state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView()); |
| 154 | + return state; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void onRestoreInstanceState(Bundle savedInstanceState) { |
| 159 | + super.onRestoreInstanceState(savedInstanceState); |
| 160 | + int hour = savedInstanceState.getInt(HOUR); |
| 161 | + int minute = savedInstanceState.getInt(MINUTE); |
| 162 | + int seconds = savedInstanceState.getInt(SECONDS); |
| 163 | + mTimePicker.setCurrentHour(hour); |
| 164 | + mTimePicker.setCurrentMinute(minute); |
| 165 | + mTimePicker.setCurrentSecond(seconds); |
| 166 | + mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR)); |
| 167 | + mTimePicker.setOnTimeChangedListener(this); |
| 168 | + updateTitle(hour, minute, seconds); |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | +} |
0 commit comments