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