113b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati/* 213b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * Copyright (C) 2014 The Android Open Source Project 313b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * 413b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * Licensed under the Apache License, Version 2.0 (the "License"); 513b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * you may not use this file except in compliance with the License. 613b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * You may obtain a copy of the License at 713b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * 813b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * http://www.apache.org/licenses/LICENSE-2.0 913b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * 1013b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * Unless required by applicable law or agreed to in writing, software 1113b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * distributed under the License is distributed on an "AS IS" BASIS, 1213b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1313b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * See the License for the specific language governing permissions and 1413b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * limitations under the License 1513b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati */ 1613b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 1713b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapatipackage com.android.phone.common.dialpad; 1813b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 1913b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapatiimport android.content.Context; 2013b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapatiimport android.graphics.Canvas; 2113b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapatiimport android.graphics.Paint; 2213b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapatiimport android.graphics.Rect; 2313b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapatiimport android.util.AttributeSet; 2413b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapatiimport android.widget.TextView; 2513b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 2613b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati/** 2713b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * This is a custom text view intended only for rendering the numerals (and star and pound) on the 2813b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * dialpad. TextView has built in top/bottom padding to help account for ascenders/descenders. 2913b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * 3013b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * Since vertical space is at a premium on the dialpad, particularly if the font size is scaled to 3113b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * a larger default, for the dialpad we use this class to more precisely render characters according 3213b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * to the precise amount of space they need. 3313b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati */ 3413b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapatipublic class DialpadTextView extends TextView { 3513b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati private Rect mTextBounds = new Rect(); 3613b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati private String mTextStr; 3713b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 3813b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati public DialpadTextView(Context context, AttributeSet attrs) { 3913b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati super(context, attrs); 4013b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati } 4113b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 4213b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati /** 4313b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * Draw the text to fit within the height/width which have been specified during measurement. 4413b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati */ 4513b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati @Override 4613b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati public void draw(Canvas canvas) { 4713b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati Paint paint = getPaint(); 4813b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 4913b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati // Without this, the draw does not respect the style's specified text color. 5013b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati paint.setColor(getCurrentTextColor()); 5113b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 5213b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati // The text bounds values are relative and can be negative,, so rather than specifying a 5313b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati // standard origin such as 0, 0, we need to use negative of the left/top bounds. 5413b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati // For example, the bounds may be: Left: 11, Right: 37, Top: -77, Bottom: 0 5513b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati canvas.drawText(mTextStr, -mTextBounds.left, -mTextBounds.top, paint); 5613b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati } 5713b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 5813b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati /** 5913b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * Calculate the pixel-accurate bounds of the text when rendered, and use that to specify the 6013b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati * height and width. 6113b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati */ 6213b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati @Override 6313b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 6413b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati super.onMeasure(widthMeasureSpec, heightMeasureSpec); 6513b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati mTextStr = getText().toString(); 6613b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati getPaint().getTextBounds(mTextStr, 0, mTextStr.length(), mTextBounds); 6713b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati 6813b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati int width = resolveSize(mTextBounds.width(), widthMeasureSpec); 6913b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati int height = resolveSize(mTextBounds.height(), heightMeasureSpec); 7013b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati setMeasuredDimension(width, height); 7113b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati } 7213b8948c0cc543f62a68fa32c4692ba16a2e93c6Sai Cheemalapati} 73