1 /* 2 * Copyright (c) 2016, 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.car.dialer.ui.dialpad; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.util.AttributeSet; 22 import android.widget.FrameLayout; 23 import android.widget.ImageView; 24 import android.widget.TextView; 25 26 import com.android.car.dialer.R; 27 28 /** 29 * A View that represents a single button on the keypad. This View display a number above letters 30 * or an image. 31 */ 32 public class KeypadButton extends FrameLayout { 33 private static final int INVALID_IMAGE_RES = -1; 34 35 private String mNumberText; 36 private String mLetterText; 37 private int mImageRes = INVALID_IMAGE_RES; 38 KeypadButton(Context context)39 public KeypadButton(Context context) { 40 super(context); 41 init(context, null); 42 } 43 KeypadButton(Context context, AttributeSet attrs)44 public KeypadButton(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 init(context, attrs); 47 } 48 KeypadButton(Context context, AttributeSet attrs, int defStyleAttrs)49 public KeypadButton(Context context, AttributeSet attrs, int defStyleAttrs) { 50 super(context, attrs, defStyleAttrs); 51 init(context, attrs); 52 } 53 KeypadButton(Context context, AttributeSet attrs, int defStyleAttrs, int defStyleRes)54 public KeypadButton(Context context, AttributeSet attrs, int defStyleAttrs, int defStyleRes) { 55 super(context, attrs, defStyleAttrs, defStyleRes); 56 init(context, attrs); 57 } 58 init(Context context, AttributeSet attrs)59 private void init(Context context, AttributeSet attrs) { 60 inflate(context, R.layout.keypad_button, this /* root */); 61 62 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.KeypadButton); 63 64 try { 65 mNumberText = ta.getString(R.styleable.KeypadButton_numberText); 66 mLetterText = ta.getString(R.styleable.KeypadButton_letterText); 67 mImageRes = ta.getResourceId(R.styleable.KeypadButton_image, INVALID_IMAGE_RES); 68 } finally { 69 if (ta != null) { 70 ta.recycle(); 71 } 72 } 73 } 74 75 @Override onFinishInflate()76 public void onFinishInflate() { 77 super.onFinishInflate(); 78 79 // Using null check instead of a TextUtils.isEmpty() check so that an empty number/letter 80 // can be used to keep the positioning of non-empty numbers/letters consistent. 81 if (mNumberText != null) { 82 TextView numberTextView = (TextView) findViewById(R.id.keypad_number); 83 numberTextView.setText(mNumberText); 84 numberTextView.setVisibility(VISIBLE); 85 } 86 87 if (mLetterText != null) { 88 TextView letterTextView = (TextView) findViewById(R.id.keypad_letters); 89 letterTextView.setText(mLetterText); 90 letterTextView.setVisibility(VISIBLE); 91 } 92 93 if (mImageRes != INVALID_IMAGE_RES) { 94 ImageView imageView = (ImageView) findViewById(R.id.keypad_image); 95 imageView.setImageResource(mImageRes); 96 imageView.setVisibility(VISIBLE); 97 } 98 } 99 } 100