1 /* 2 * Copyright (C) 2008 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.android.deskclock; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.util.AttributeSet; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.widget.Button; 25 import android.widget.ImageButton; 26 import android.widget.LinearLayout; 27 28 import com.android.deskclock.timer.TimerView; 29 30 31 public class TimerSetupView extends LinearLayout implements Button.OnClickListener, 32 Button.OnLongClickListener{ 33 34 protected int mInputSize = 5; 35 36 protected final Button mNumbers [] = new Button [10]; 37 protected int mInput [] = new int [mInputSize]; 38 protected int mInputPointer = -1; 39 protected Button mLeft, mRight; 40 protected Button mStart; 41 protected ImageButton mDelete; 42 protected TimerView mEnteredTime; 43 protected final Context mContext; 44 TimerSetupView(Context context)45 public TimerSetupView(Context context) { 46 this(context, null); 47 } 48 TimerSetupView(Context context, AttributeSet attrs)49 public TimerSetupView(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 mContext = context; 52 LayoutInflater layoutInflater = 53 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 54 layoutInflater.inflate(getLayoutId(), this); 55 } 56 getLayoutId()57 protected int getLayoutId() { 58 return R.layout.time_setup_view; 59 } 60 61 @Override onFinishInflate()62 protected void onFinishInflate() { 63 super.onFinishInflate(); 64 65 View v1 = findViewById(R.id.first); 66 View v2 = findViewById(R.id.second); 67 View v3 = findViewById(R.id.third); 68 View v4 = findViewById(R.id.fourth); 69 mEnteredTime = (TimerView)findViewById(R.id.timer_time_text); 70 mDelete = (ImageButton)findViewById(R.id.delete); 71 mDelete.setOnClickListener(this); 72 mDelete.setOnLongClickListener(this); 73 74 mNumbers[1] = (Button)v1.findViewById(R.id.key_left); 75 mNumbers[2] = (Button)v1.findViewById(R.id.key_middle); 76 mNumbers[3] = (Button)v1.findViewById(R.id.key_right); 77 78 mNumbers[4] = (Button)v2.findViewById(R.id.key_left); 79 mNumbers[5] = (Button)v2.findViewById(R.id.key_middle); 80 mNumbers[6] = (Button)v2.findViewById(R.id.key_right); 81 82 mNumbers[7] = (Button)v3.findViewById(R.id.key_left); 83 mNumbers[8] = (Button)v3.findViewById(R.id.key_middle); 84 mNumbers[9] = (Button)v3.findViewById(R.id.key_right); 85 86 mLeft = (Button)v4.findViewById(R.id.key_left); 87 mNumbers[0] = (Button)v4.findViewById(R.id.key_middle); 88 mRight = (Button)v4.findViewById(R.id.key_right); 89 setLeftRightEnabled(false); 90 91 for (int i = 0; i < 10; i++) { 92 mNumbers[i].setOnClickListener(this); 93 mNumbers [i].setText(String.format("%d",i)); 94 mNumbers [i].setTag(R.id.numbers_key,new Integer(i)); 95 } 96 updateTime(); 97 } 98 registerStartButton(Button start)99 public void registerStartButton(Button start) { 100 mStart = start; 101 } 102 updateStartButton()103 public void updateStartButton() { 104 boolean enabled = mInputPointer != -1; 105 if (mStart != null) { 106 mStart.setEnabled(enabled); 107 } 108 } 109 updateDeleteButton()110 public void updateDeleteButton() { 111 boolean enabled = mInputPointer != -1; 112 if (mDelete != null) { 113 mDelete.setEnabled(enabled); 114 } 115 } 116 117 @Override onClick(View v)118 public void onClick(View v) { 119 doOnClick(v); 120 updateStartButton(); 121 updateDeleteButton(); 122 } 123 doOnClick(View v)124 protected void doOnClick(View v) { 125 126 Integer val = (Integer) v.getTag(R.id.numbers_key); 127 // A number was pressed 128 if (val != null) { 129 // pressing "0" as the first digit does nothing 130 if (mInputPointer == -1 && val == 0) { 131 return; 132 } 133 if (mInputPointer < mInputSize - 1) { 134 for (int i = mInputPointer; i >= 0; i--) { 135 mInput[i+1] = mInput[i]; 136 } 137 mInputPointer++; 138 mInput [0] = val; 139 updateTime(); 140 } 141 return; 142 } 143 144 // other keys 145 if (v == mDelete) { 146 if (mInputPointer >= 0) { 147 for (int i = 0; i < mInputPointer; i++) { 148 mInput[i] = mInput[i + 1]; 149 } 150 mInput[mInputPointer] = 0; 151 mInputPointer--; 152 updateTime(); 153 } 154 } 155 } 156 157 @Override onLongClick(View v)158 public boolean onLongClick(View v) { 159 if (v == mDelete) { 160 reset(); 161 updateStartButton(); 162 updateDeleteButton(); 163 return true; 164 } 165 return false; 166 } 167 updateTime()168 protected void updateTime() { 169 mEnteredTime.setTime(-1, mInput[4], mInput[3], mInput[2], 170 mInput[1] * 10 + mInput[0]); 171 } 172 reset()173 public void reset() { 174 for (int i = 0; i < mInputSize; i ++) { 175 mInput[i] = 0; 176 } 177 mInputPointer = -1; 178 updateTime(); 179 } 180 getTime()181 public int getTime() { 182 return mInput[4] * 3600 + mInput[3] * 600 + mInput[2] * 60 + mInput[1] * 10 + mInput[0]; 183 } 184 saveEntryState(Bundle outState, String key)185 public void saveEntryState(Bundle outState, String key) { 186 outState.putIntArray(key, mInput); 187 } 188 restoreEntryState(Bundle inState, String key)189 public void restoreEntryState(Bundle inState, String key) { 190 int[] input = inState.getIntArray(key); 191 if (input != null && mInputSize == input.length) { 192 for (int i = 0; i < mInputSize; i++) { 193 mInput[i] = input[i]; 194 if (mInput[i] != 0) { 195 mInputPointer = i; 196 } 197 } 198 updateTime(); 199 } 200 } 201 setLeftRightEnabled(boolean enabled)202 protected void setLeftRightEnabled(boolean enabled) { 203 mLeft.setEnabled(enabled); 204 mRight.setEnabled(enabled); 205 if (!enabled) { 206 mLeft.setContentDescription(null); 207 mRight.setContentDescription(null); 208 } 209 } 210 } 211