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