1 /*
2  * Copyright (C) 2023 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.keyguard;
18 
19 import static com.android.systemui.bouncer.shared.constants.KeyguardBouncerConstants.ColorId.PIN_SHAPES;
20 
21 import android.content.Context;
22 import android.graphics.drawable.AnimatedVectorDrawable;
23 import android.graphics.drawable.Drawable;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.widget.ImageView;
27 import android.widget.LinearLayout;
28 
29 import androidx.core.graphics.drawable.DrawableCompat;
30 
31 import com.android.settingslib.Utils;
32 import com.android.systemui.res.R;
33 
34 /**
35  * This class contains implementation for methods that will be used when user has set a
36  * six digit pin on their device
37  */
38 public class PinShapeHintingView extends LinearLayout implements PinShapeInput {
39 
40     private int mPinLength;
41     private int mDotDiameter;
42     private int mColor = Utils.getColorAttr(getContext(), PIN_SHAPES)
43             .getDefaultColor();
44     private int mPosition = 0;
45     private static final int DEFAULT_PIN_LENGTH = 6;
46     private PinShapeAdapter mPinShapeAdapter;
47 
PinShapeHintingView(Context context, AttributeSet attrs)48     public PinShapeHintingView(Context context, AttributeSet attrs) {
49         super(context, attrs);
50         mPinShapeAdapter = new PinShapeAdapter(context);
51         mPinLength = DEFAULT_PIN_LENGTH;
52         mDotDiameter = context.getResources().getDimensionPixelSize(R.dimen.password_shape_size);
53 
54         for (int i = 0; i < mPinLength; i++) {
55             ImageView pinDot = new ImageView(context, attrs);
56             LayoutParams layoutParams = new LayoutParams(mDotDiameter, mDotDiameter);
57             pinDot.setLayoutParams(layoutParams);
58             pinDot.setImageResource(R.drawable.pin_dot_avd);
59             if (pinDot.getDrawable() != null) {
60                 Drawable drawable = DrawableCompat.wrap(pinDot.getDrawable());
61                 DrawableCompat.setTint(drawable, mColor);
62             }
63             addView(pinDot);
64         }
65     }
66 
67     @Override
append()68     public void append() {
69         if (mPosition == DEFAULT_PIN_LENGTH) {
70             return;
71         }
72         setAnimatedDrawable(mPosition, mPinShapeAdapter.getShape(mPosition));
73         mPosition++;
74     }
75 
76     @Override
delete()77     public void delete() {
78         if (mPosition == 0) {
79             return;
80         }
81         mPosition--;
82         setAnimatedDrawable(mPosition, R.drawable.pin_dot_delete_avd);
83     }
84 
85     @Override
setDrawColor(int color)86     public void setDrawColor(int color) {
87         this.mColor = color;
88     }
89 
90     @Override
reset()91     public void reset() {
92         int size = mPosition;
93         for (int i = 0; i < size; i++) {
94             delete();
95         }
96         mPosition = 0;
97     }
98 
99     @Override
getView()100     public View getView() {
101         return this;
102     }
103 
setAnimatedDrawable(int position, int drawableResId)104     private void setAnimatedDrawable(int position, int drawableResId) {
105         ImageView pinDot = (ImageView) getChildAt(position);
106         pinDot.setImageResource(drawableResId);
107         if (pinDot.getDrawable() != null) {
108             Drawable drawable = DrawableCompat.wrap(pinDot.getDrawable());
109             DrawableCompat.setTint(drawable, mColor);
110         }
111         if (pinDot.getDrawable() instanceof AnimatedVectorDrawable) {
112             ((AnimatedVectorDrawable) pinDot.getDrawable()).start();
113         }
114     }
115 }
116