1 /*
2  * Copyright (C) 2021 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.systemui.biometrics;
18 
19 import android.content.Context;
20 import android.graphics.ColorFilter;
21 import android.graphics.Paint;
22 import android.graphics.Rect;
23 import android.graphics.RectF;
24 import android.graphics.drawable.Drawable;
25 import android.graphics.drawable.ShapeDrawable;
26 import android.graphics.drawable.shapes.PathShape;
27 import android.util.PathParser;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 
32 import com.android.systemui.R;
33 
34 /**
35  * Abstract base class for drawable displayed when the finger is not touching the
36  * sensor area.
37  */
38 public abstract class UdfpsDrawable extends Drawable {
39     static final float DEFAULT_STROKE_WIDTH = 3f;
40 
41     @NonNull final Context mContext;
42     @NonNull final ShapeDrawable mFingerprintDrawable;
43     private final Paint mPaint;
44     private boolean mIlluminationShowing;
45 
46     int mAlpha = 255; // 0 - 255
UdfpsDrawable(@onNull Context context)47     public UdfpsDrawable(@NonNull Context context) {
48         mContext = context;
49         final String fpPath = context.getResources().getString(R.string.config_udfpsIcon);
50         mFingerprintDrawable = new ShapeDrawable(
51                 new PathShape(PathParser.createPathFromPathData(fpPath), 72, 72));
52         mFingerprintDrawable.mutate();
53 
54         mPaint = mFingerprintDrawable.getPaint();
55         mPaint.setStyle(Paint.Style.STROKE);
56         mPaint.setStrokeCap(Paint.Cap.ROUND);
57         setStrokeWidth(DEFAULT_STROKE_WIDTH);
58     }
59 
setStrokeWidth(float strokeWidth)60     void setStrokeWidth(float strokeWidth) {
61         mPaint.setStrokeWidth(strokeWidth);
62         invalidateSelf();
63     }
64 
65     /**
66      * @param sensorRect the rect coordinates for the sensor area
67      */
onSensorRectUpdated(@onNull RectF sensorRect)68     public void onSensorRectUpdated(@NonNull RectF sensorRect) {
69         final int margin =  (int) sensorRect.height() / 8;
70 
71         final Rect bounds = new Rect((int) sensorRect.left + margin,
72                 (int) sensorRect.top + margin,
73                 (int) sensorRect.right - margin,
74                 (int) sensorRect.bottom - margin);
75         updateFingerprintIconBounds(bounds);
76     }
77 
78     /**
79      * Bounds for the fingerprint icon
80      */
updateFingerprintIconBounds(@onNull Rect bounds)81     protected void updateFingerprintIconBounds(@NonNull Rect bounds) {
82         mFingerprintDrawable.setBounds(bounds);
83         invalidateSelf();
84     }
85 
86     @Override
setAlpha(int alpha)87     public void setAlpha(int alpha) {
88         mAlpha = alpha;
89         mFingerprintDrawable.setAlpha(mAlpha);
90         invalidateSelf();
91     }
92 
isIlluminationShowing()93     boolean isIlluminationShowing() {
94         return mIlluminationShowing;
95     }
96 
setIlluminationShowing(boolean showing)97     void setIlluminationShowing(boolean showing) {
98         if (mIlluminationShowing == showing) {
99             return;
100         }
101         mIlluminationShowing = showing;
102         invalidateSelf();
103     }
104 
105     @Override
setColorFilter(@ullable ColorFilter colorFilter)106     public void setColorFilter(@Nullable ColorFilter colorFilter) {
107     }
108 
109     @Override
getOpacity()110     public int getOpacity() {
111         return 0;
112     }
113 }
114