• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.security;
18 
19 import android.annotation.DrawableRes;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.support.annotation.VisibleForTesting;
23 import android.util.AttributeSet;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.widget.GridLayout;
27 import android.widget.ImageButton;
28 import android.widget.TextView;
29 
30 import com.android.car.settings.R;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /**
36  * A custom view for the PIN pad.
37  */
38 public class PinPadView extends GridLayout {
39     // Number of keys in the pin pad, 0-9 plus backspace and enter keys.
40     @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
41     static final int NUM_KEYS = 12;
42 
43     @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
44     static final int[] PIN_PAD_DIGIT_KEYS = { R.id.key0, R.id.key1, R.id.key2, R.id.key3,
45             R.id.key4, R.id.key5, R.id.key6, R.id.key7, R.id.key8, R.id.key9 };
46 
47     private final List<View> mPinKeys = new ArrayList<>(NUM_KEYS);
48     private PinPadClickListener mOnClickListener;
49     private ImageButton mEnterKey;
50 
PinPadView(Context context)51     public PinPadView(Context context) {
52         super(context);
53         init();
54     }
55 
PinPadView(Context context, AttributeSet attrs)56     public PinPadView(Context context, AttributeSet attrs) {
57         super(context, attrs);
58         init();
59     }
60 
PinPadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)61     public PinPadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
62         super(context, attrs, defStyleAttr);
63         init();
64     }
65 
PinPadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)66     public PinPadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
67             int defStyleRes) {
68         super(context, attrs, defStyleAttr, defStyleRes);
69         init();
70     }
71 
72     /**
73      * Set the call back for key click.
74      *
75      * @param pinPadClickListener The call back.
76      */
setPinPadClickListener(PinPadClickListener pinPadClickListener)77     public void setPinPadClickListener(PinPadClickListener pinPadClickListener) {
78         mOnClickListener = pinPadClickListener;
79     }
80 
81     @Override
setEnabled(boolean enabled)82     public void setEnabled(boolean enabled) {
83         super.setEnabled(enabled);
84         for (View key: mPinKeys) {
85             key.setEnabled(enabled);
86         }
87     }
88 
89     /**
90      * Set the resource Id of the enter key icon.
91      *
92      * @param drawableId  The resource Id of the drawable.
93      */
setEnterKeyIcon(@rawableRes int drawableId)94     public void setEnterKeyIcon(@DrawableRes int drawableId) {
95         mEnterKey.setImageResource(drawableId);
96     }
97 
init()98     private void init() {
99         LayoutInflater inflater = LayoutInflater.from(getContext());
100         inflater.inflate(R.layout.pin_pad_view, this, true);
101 
102         for (int keyId : PIN_PAD_DIGIT_KEYS) {
103             TextView key = (TextView) findViewById(keyId);
104             String digit = key.getTag().toString();
105             key.setOnClickListener(v -> {
106                 mOnClickListener.onDigitKeyClick(digit);
107             });
108             mPinKeys.add(key);
109         }
110 
111         View backspace = findViewById(R.id.key_backspace);
112         backspace.setOnClickListener(v -> mOnClickListener.onBackspaceClick());
113         mPinKeys.add(backspace);
114 
115         mEnterKey = (ImageButton) findViewById(R.id.key_enter);
116         mEnterKey.setOnClickListener(v -> mOnClickListener.onEnterKeyClick());
117         mPinKeys.add(mEnterKey);
118     }
119 
120     /**
121      * The call back interface for onClick event in the view.
122      */
123     public interface PinPadClickListener {
124         /**
125          * One of the digit key has been clicked.
126          *
127          * @param digit A String representing a digit between 0 and 9.
128          */
onDigitKeyClick(String digit)129         void onDigitKeyClick(String digit);
130 
131         /**
132          * The backspace key has been clicked.
133          */
onBackspaceClick()134         void onBackspaceClick();
135 
136         /**
137          * The enter key has been clicked.
138          */
onEnterKeyClick()139         void onEnterKeyClick();
140     }
141 }
142