1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 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.googlecode.android_scripting.widget; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.LayoutInflater; 22 import android.widget.FrameLayout; 23 24 import com.googlecode.android_scripting.R; 25 26 /** 27 * A view for selecting the a duration using days, hours, minutes, and seconds. 28 */ 29 public class DurationPicker extends FrameLayout { 30 31 private int mCurrentDay = 0; // 0-99 32 private int mCurrentHour = 0; // 0-23 33 private int mCurrentMinute = 0; // 0-59 34 private int mCurrentSecond = 0; // 0-59 35 36 private final NumberPicker mDayPicker; 37 private final NumberPicker mHourPicker; 38 private final NumberPicker mMinutePicker; 39 private final NumberPicker mSecondPicker; 40 DurationPicker(Context context)41 public DurationPicker(Context context) { 42 this(context, null); 43 } 44 DurationPicker(Context context, AttributeSet attrs)45 public DurationPicker(Context context, AttributeSet attrs) { 46 this(context, attrs, 0); 47 } 48 DurationPicker(Context context, AttributeSet attrs, int defStyle)49 public DurationPicker(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 LayoutInflater inflater = 52 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 53 inflater.inflate(R.layout.duration_picker, this, true); 54 55 mDayPicker = (NumberPicker) findViewById(R.id.day); 56 mDayPicker.setRange(0, 99); 57 mDayPicker.setSpeed(100); 58 // mHourPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER); 59 mDayPicker.setOnChangeListener(new NumberPicker.OnChangedListener() { 60 public void onChanged(NumberPicker spinner, int oldVal, int newVal) { 61 mCurrentDay = newVal; 62 } 63 }); 64 65 mHourPicker = (NumberPicker) findViewById(R.id.hour); 66 mHourPicker.setRange(0, 23); 67 mHourPicker.setSpeed(100); 68 // mHourPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER); 69 mHourPicker.setOnChangeListener(new NumberPicker.OnChangedListener() { 70 public void onChanged(NumberPicker spinner, int oldVal, int newVal) { 71 mCurrentHour = newVal; 72 } 73 }); 74 75 mMinutePicker = (NumberPicker) findViewById(R.id.minute); 76 mMinutePicker.setRange(0, 59); 77 mMinutePicker.setSpeed(100); 78 mMinutePicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER); 79 mMinutePicker.setOnChangeListener(new NumberPicker.OnChangedListener() { 80 public void onChanged(NumberPicker spinner, int oldVal, int newVal) { 81 mCurrentMinute = newVal; 82 } 83 }); 84 85 mSecondPicker = (NumberPicker) findViewById(R.id.second); 86 mSecondPicker.setRange(0, 59); 87 mSecondPicker.setSpeed(100); 88 mSecondPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER); 89 mSecondPicker.setOnChangeListener(new NumberPicker.OnChangedListener() { 90 public void onChanged(NumberPicker spinner, int oldVal, int newVal) { 91 mCurrentSecond = newVal; 92 } 93 }); 94 } 95 96 @Override setEnabled(boolean enabled)97 public void setEnabled(boolean enabled) { 98 super.setEnabled(enabled); 99 mDayPicker.setEnabled(enabled); 100 mHourPicker.setEnabled(enabled); 101 mMinutePicker.setEnabled(enabled); 102 mSecondPicker.setEnabled(enabled); 103 } 104 105 /** 106 * Returns the current day. 107 */ getCurrentDay()108 public Integer getCurrentDay() { 109 return mCurrentDay; 110 } 111 112 /** 113 * Set the current hour. 114 */ setCurrentDay(Integer currentDay)115 public void setCurrentDay(Integer currentDay) { 116 mCurrentDay = currentDay; 117 updateDayDisplay(); 118 } 119 120 /** 121 * Returns the current hour. 122 */ getCurrentHour()123 public Integer getCurrentHour() { 124 return mCurrentHour; 125 } 126 127 /** 128 * Set the current hour. 129 */ setCurrentHour(Integer currentHour)130 public void setCurrentHour(Integer currentHour) { 131 mCurrentHour = currentHour; 132 updateHourDisplay(); 133 } 134 135 /** 136 * Returns the current minute. 137 */ getCurrentMinute()138 public Integer getCurrentMinute() { 139 return mCurrentMinute; 140 } 141 142 /** 143 * Set the current minute. 144 */ setCurrentMinute(Integer currentMinute)145 public void setCurrentMinute(Integer currentMinute) { 146 mCurrentMinute = currentMinute; 147 updateMinuteDisplay(); 148 } 149 150 /** 151 * Returns the current second. 152 */ getCurrentSecond()153 public Integer getCurrentSecond() { 154 return mCurrentSecond; 155 } 156 157 /** 158 * Set the current minute. 159 */ setCurrentSecond(Integer currentSecond)160 public void setCurrentSecond(Integer currentSecond) { 161 mCurrentSecond = currentSecond; 162 updateSecondDisplay(); 163 } 164 165 /** 166 * Set the state of the spinners appropriate to the current day. 167 */ updateDayDisplay()168 private void updateDayDisplay() { 169 int currentDay = mCurrentDay; 170 mDayPicker.setCurrent(currentDay); 171 } 172 173 /** 174 * Set the state of the spinners appropriate to the current hour. 175 */ updateHourDisplay()176 private void updateHourDisplay() { 177 int currentHour = mCurrentHour; 178 mHourPicker.setCurrent(currentHour); 179 } 180 181 /** 182 * Set the state of the spinners appropriate to the current minute. 183 */ updateMinuteDisplay()184 private void updateMinuteDisplay() { 185 mMinutePicker.setCurrent(mCurrentMinute); 186 } 187 188 /** 189 * Set the state of the spinners appropriate to the current minute. 190 */ updateSecondDisplay()191 private void updateSecondDisplay() { 192 mSecondPicker.setCurrent(mCurrentSecond); 193 } 194 195 /** 196 * Returns the duration in seconds. 197 */ getDuration()198 public double getDuration() { 199 // The text views may still have focus so clear theirs focus which will trigger the on focus 200 // changed and any typed values to be pulled. 201 mDayPicker.clearFocus(); 202 mHourPicker.clearFocus(); 203 mMinutePicker.clearFocus(); 204 mSecondPicker.clearFocus(); 205 return (((((mCurrentDay * 24l + mCurrentHour) * 60) + mCurrentMinute) * 60) + mCurrentSecond); 206 } 207 208 /** 209 * Sets the duration in milliseconds. 210 * 211 * @return 212 */ setDuration(long duration)213 public void setDuration(long duration) { 214 double seconds = duration / 1000; 215 double minutes = seconds / 60; 216 seconds = seconds % 60; 217 double hours = minutes / 60; 218 minutes = minutes % 60; 219 double days = hours / 24; 220 hours = hours % 24; 221 222 setCurrentDay((int) days); 223 setCurrentHour((int) hours); 224 setCurrentMinute((int) minutes); 225 setCurrentSecond((int) seconds); 226 } 227 } 228