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.content.res.Configuration; 21 import android.os.Bundle; 22 import android.util.AttributeSet; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.widget.Button; 26 import android.widget.ImageButton; 27 import android.widget.LinearLayout; 28 29 import com.android.deskclock.timer.TimerView; 30 31 32 public class TimerSetupView extends LinearLayout implements Button.OnClickListener, 33 Button.OnLongClickListener{ 34 35 protected int mInputSize = 5; 36 37 protected final Button mNumbers [] = new Button [10]; 38 protected int mInput [] = new int [mInputSize]; 39 protected int mInputPointer = -1; 40 protected Button mLeft, mRight; 41 protected Button mStart; 42 protected ImageButton mDelete; 43 protected TimerView mEnteredTime; 44 protected final Context mContext; 45 46 public TimerSetupView(Context context) { 47 this(context, null); 48 } 49 50 public TimerSetupView(Context context, AttributeSet attrs) { 51 super(context, attrs); 52 mContext = context; 53 LayoutInflater layoutInflater = 54 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 55 layoutInflater.inflate(R.layout.time_setup_view, this); 56 } 57 58 @Override 59 protected void onFinishInflate() { 60 super.onFinishInflate(); 61 62 View v1 = findViewById(R.id.first); 63 View v2 = findViewById(R.id.second); 64 View v3 = findViewById(R.id.third); 65 View v4 = findViewById(R.id.fourth); 66 mEnteredTime = (TimerView)findViewById(R.id.timer_time_text); 67 mDelete = (ImageButton)findViewById(R.id.delete); 68 mDelete.setOnClickListener(this); 69 mDelete.setOnLongClickListener(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 @Override 97 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 98 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 99 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 100 setMarginsAfterMeasure(); 101 } 102 } 103 104 /** 105 * To properly center the TimerView across from the dial pad, append a bottom margin that 106 * matches the measured height of the start button that is below the dial pad. 107 */ 108 protected void setMarginsAfterMeasure() { 109 View timerStart = findViewById(R.id.timer_start); 110 View timerDisplay = findViewById(R.id.timer_time_display); 111 if (timerStart != null && timerDisplay != null) { 112 MarginLayoutParams marginLayoutParams = 113 (MarginLayoutParams) timerDisplay.getLayoutParams(); 114 marginLayoutParams.bottomMargin = timerStart.getMeasuredHeight(); 115 } 116 } 117 118 public void registerStartButton(Button start) { 119 mStart = start; 120 } 121 122 public void updateStartButton() { 123 boolean enabled = mInputPointer != -1; 124 if (mStart != null) { 125 mStart.setEnabled(enabled); 126 } 127 } 128 129 public void updateDeleteButton() { 130 boolean enabled = mInputPointer != -1; 131 if (mDelete != null) { 132 mDelete.setEnabled(enabled); 133 } 134 } 135 136 @Override 137 public void onClick(View v) { 138 doOnClick(v); 139 updateStartButton(); 140 updateDeleteButton(); 141 } 142 143 protected void doOnClick(View v) { 144 145 Integer val = (Integer) v.getTag(R.id.numbers_key); 146 // A number was pressed 147 if (val != null) { 148 // pressing "0" as the first digit does nothing 149 if (mInputPointer == -1 && val == 0) { 150 return; 151 } 152 if (mInputPointer < mInputSize - 1) { 153 for (int i = mInputPointer; i >= 0; i--) { 154 mInput[i+1] = mInput[i]; 155 } 156 mInputPointer++; 157 mInput [0] = val; 158 updateTime(); 159 } 160 return; 161 } 162 163 // other keys 164 if (v == mDelete) { 165 if (mInputPointer >= 0) { 166 for (int i = 0; i < mInputPointer; i++) { 167 mInput[i] = mInput[i + 1]; 168 } 169 mInput[mInputPointer] = 0; 170 mInputPointer--; 171 updateTime(); 172 } 173 } 174 } 175 176 @Override 177 public boolean onLongClick(View v) { 178 if (v == mDelete) { 179 reset(); 180 updateStartButton(); 181 updateDeleteButton(); 182 return true; 183 } 184 return false; 185 } 186 187 protected void updateTime() { 188 mEnteredTime.setTime(mInput[4], mInput[3], mInput[2], 189 mInput[1] * 10 + mInput[0]); 190 } 191 192 public void reset() { 193 for (int i = 0; i < mInputSize; i ++) { 194 mInput[i] = 0; 195 } 196 mInputPointer = -1; 197 updateTime(); 198 } 199 200 public int getTime() { 201 return mInput[4] * 3600 + mInput[3] * 600 + mInput[2] * 60 + mInput[1] * 10 + mInput[0]; 202 } 203 204 public void saveEntryState(Bundle outState, String key) { 205 outState.putIntArray(key, mInput); 206 } 207 208 public void restoreEntryState(Bundle inState, String key) { 209 int[] input = inState.getIntArray(key); 210 if (input != null && mInputSize == input.length) { 211 for (int i = 0; i < mInputSize; i++) { 212 mInput[i] = input[i]; 213 if (mInput[i] != 0) { 214 mInputPointer = i; 215 } 216 } 217 updateTime(); 218 } 219 } 220 221 protected void setLeftRightEnabled(boolean enabled) { 222 mLeft.setEnabled(enabled); 223 mRight.setEnabled(enabled); 224 if (!enabled) { 225 mLeft.setContentDescription(null); 226 mRight.setContentDescription(null); 227 } 228 } 229 } 230