summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/biometrics/UdfpsDialogMeasureAdapter.java
blob: 59a9ad6124f0875a48ad641e4ebb5e3150456d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.biometrics;

import android.annotation.IdRes;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.graphics.Insets;
import android.graphics.Rect;
import android.hardware.biometrics.SensorLocationInternal;
import android.hardware.fingerprint.FingerprintSensorPropertiesInternal;
import android.os.Build;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.WindowMetrics;
import android.widget.FrameLayout;

import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.R;

/**
 * Adapter that remeasures an auth dialog view to ensure that it matches the location of a physical
 * under-display fingerprint sensor (UDFPS).
 */
public class UdfpsDialogMeasureAdapter {
    private static final String TAG = "UdfpsDialogMeasurementAdapter";
    private static final boolean DEBUG = Build.IS_USERDEBUG || Build.IS_ENG;

    @NonNull private final ViewGroup mView;
    @NonNull private final FingerprintSensorPropertiesInternal mSensorProps;
    @Nullable private WindowManager mWindowManager;
    private int mBottomSpacerHeight;

    public UdfpsDialogMeasureAdapter(
            @NonNull ViewGroup view, @NonNull FingerprintSensorPropertiesInternal sensorProps) {
        mView = view;
        mSensorProps = sensorProps;
        mWindowManager = mView.getContext().getSystemService(WindowManager.class);
    }

    @NonNull
    FingerprintSensorPropertiesInternal getSensorProps() {
        return mSensorProps;
    }

    @NonNull
    AuthDialog.LayoutParams onMeasureInternal(
            int width, int height, @NonNull AuthDialog.LayoutParams layoutParams,
            float scaleFactor) {

        final int displayRotation = mView.getDisplay().getRotation();
        switch (displayRotation) {
            case Surface.ROTATION_0:
                return onMeasureInternalPortrait(width, height, scaleFactor);
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                return onMeasureInternalLandscape(width, height, scaleFactor);
            default:
                Log.e(TAG, "Unsupported display rotation: " + displayRotation);
                return layoutParams;
        }
    }

    /**
     * @return the actual (and possibly negative) bottom spacer height. If negative, this indicates
     * that the UDFPS sensor is too low. Our current xml and custom measurement logic is very hard
     * too cleanly support this case. So, let's have the onLayout code translate the sensor location
     * instead.
     */
    int getBottomSpacerHeight() {
        return mBottomSpacerHeight;
    }

    /**
     * @return sensor diameter size as scaleFactor
     */
    public int getSensorDiameter(float scaleFactor) {
        return (int) (scaleFactor * mSensorProps.getLocation().sensorRadius * 2);
    }

    @NonNull
    private AuthDialog.LayoutParams onMeasureInternalPortrait(int width, int height,
            float scaleFactor) {
        final WindowMetrics windowMetrics = mWindowManager.getMaximumWindowMetrics();

        // Figure out where the bottom of the sensor anim should be.
        final int textIndicatorHeight = getViewHeightPx(R.id.indicator);
        final int buttonBarHeight = getViewHeightPx(R.id.button_bar);
        final int dialogMargin = getDialogMarginPx();
        final int displayHeight = getMaximumWindowBounds(windowMetrics).height();
        mBottomSpacerHeight = calculateBottomSpacerHeightForPortrait(
                mSensorProps, displayHeight, textIndicatorHeight, buttonBarHeight,
                dialogMargin, scaleFactor);

        // Go through each of the children and do the custom measurement.
        int totalHeight = 0;
        final int numChildren = mView.getChildCount();
        final int sensorDiameter = getSensorDiameter(scaleFactor);
        for (int i = 0; i < numChildren; i++) {
            final View child = mView.getChildAt(i);
            if (child.getId() == R.id.biometric_icon_frame) {
                final FrameLayout iconFrame = (FrameLayout) child;
                final View icon = iconFrame.getChildAt(0);
                // Create a frame that's exactly the height of the sensor circle.
                iconFrame.measure(
                        MeasureSpec.makeMeasureSpec(
                                child.getLayoutParams().width, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(sensorDiameter, MeasureSpec.EXACTLY));

                // Ensure that the icon is never larger than the sensor.
                icon.measure(
                        MeasureSpec.makeMeasureSpec(sensorDiameter, MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(sensorDiameter, MeasureSpec.AT_MOST));
            } else if (child.getId() == R.id.space_above_icon) {
                child.measure(
                        MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(
                                child.getLayoutParams().height, MeasureSpec.EXACTLY));
            } else if (child.getId() == R.id.button_bar) {
                child.measure(
                        MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(child.getLayoutParams().height,
                                MeasureSpec.EXACTLY));
            } else if (child.getId() == R.id.space_below_icon) {
                // Set the spacer height so the fingerprint icon is on the physical sensor area
                final int clampedSpacerHeight = Math.max(mBottomSpacerHeight, 0);
                child.measure(
                        MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(clampedSpacerHeight, MeasureSpec.EXACTLY));
            } else if (child.getId() == R.id.description) {
                //skip description view and compute later
                continue;
            } else {
                child.measure(
                        MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
            }

            if (child.getVisibility() != View.GONE) {
                totalHeight += child.getMeasuredHeight();
            }
        }

        //re-calculate the height of description
        View description = mView.findViewById(R.id.description);
        if (description != null && description.getVisibility() != View.GONE) {
            totalHeight += measureDescription(description, displayHeight, width, totalHeight);
        }

        return new AuthDialog.LayoutParams(width, totalHeight);
    }

    private int measureDescription(View description, int displayHeight, int currWidth,
                                   int currHeight) {
        //description view should be measured in AuthBiometricFingerprintView#onMeasureInternal
        //so we could getMeasuredHeight in onMeasureInternalPortrait directly.
        int newHeight = description.getMeasuredHeight() + currHeight;
        int limit = (int) (displayHeight * 0.75);
        if (newHeight > limit) {
            description.measure(
                    MeasureSpec.makeMeasureSpec(currWidth, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(limit - currHeight, MeasureSpec.EXACTLY));
        }
        return description.getMeasuredHeight();
    }

    @NonNull
    private AuthDialog.LayoutParams onMeasureInternalLandscape(int width, int height,
            float scaleFactor) {
        final WindowMetrics windowMetrics = mWindowManager.getMaximumWindowMetrics();

        // Find the spacer height needed to vertically align the icon with the sensor.
        final int titleHeight = getViewHeightPx(R.id.title);
        final int subtitleHeight = getViewHeightPx(R.id.subtitle);
        final int descriptionHeight = getViewHeightPx(R.id.description);
        final int topSpacerHeight = getViewHeightPx(R.id.space_above_icon);
        final int textIndicatorHeight = getViewHeightPx(R.id.indicator);
        final int buttonBarHeight = getViewHeightPx(R.id.button_bar);

        final int bottomSpacerHeight = calculateBottomSpacerHeightForLandscape(titleHeight,
                subtitleHeight, descriptionHeight, topSpacerHeight, textIndicatorHeight,
                buttonBarHeight);

        // Find the spacer width needed to horizontally align the icon with the sensor.
        final int displayWidth = getMaximumWindowBounds(windowMetrics).width();
        final int dialogMargin = getDialogMarginPx();
        final int horizontalSpacerWidth = calculateHorizontalSpacerWidthForLandscape(
                mSensorProps, displayWidth, dialogMargin, scaleFactor);

        final int sensorDiameter = getSensorDiameter(scaleFactor);
        final int remeasuredWidth = sensorDiameter + 2 * horizontalSpacerWidth;

        int remeasuredHeight = 0;
        final int numChildren = mView.getChildCount();
        for (int i = 0; i < numChildren; i++) {
            final View child = mView.getChildAt(i);
            if (child.getId() == R.id.biometric_icon_frame) {
                final FrameLayout iconFrame = (FrameLayout) child;
                final View icon = iconFrame.getChildAt(0);
                // Create a frame that's exactly the height of the sensor circle.
                iconFrame.measure(
                        MeasureSpec.makeMeasureSpec(remeasuredWidth, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(sensorDiameter, MeasureSpec.EXACTLY));

                // Ensure that the icon is never larger than the sensor.
                icon.measure(
                        MeasureSpec.makeMeasureSpec(sensorDiameter, MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(sensorDiameter, MeasureSpec.AT_MOST));
            } else if (child.getId() == R.id.space_above_icon) {
                // Adjust the width and height of the top spacer if necessary.
                final int newTopSpacerHeight = child.getLayoutParams().height
                        - Math.min(bottomSpacerHeight, 0);
                child.measure(
                        MeasureSpec.makeMeasureSpec(remeasuredWidth, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(newTopSpacerHeight, MeasureSpec.EXACTLY));
            } else if (child.getId() == R.id.button_bar) {
                // Adjust the width of the button bar while preserving its height.
                child.measure(
                        MeasureSpec.makeMeasureSpec(remeasuredWidth, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(
                                child.getLayoutParams().height, MeasureSpec.EXACTLY));
            } else if (child.getId() == R.id.space_below_icon) {
                // Adjust the bottom spacer height to align the fingerprint icon with the sensor.
                final int newBottomSpacerHeight = Math.max(bottomSpacerHeight, 0);
                child.measure(
                        MeasureSpec.makeMeasureSpec(remeasuredWidth, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(newBottomSpacerHeight, MeasureSpec.EXACTLY));
            } else {
                // Use the remeasured width for all other child views.
                child.measure(
                        MeasureSpec.makeMeasureSpec(remeasuredWidth, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
            }

            if (child.getVisibility() != View.GONE) {
                remeasuredHeight += child.getMeasuredHeight();
            }
        }

        return new AuthDialog.LayoutParams(remeasuredWidth, remeasuredHeight);
    }

    private int getViewHeightPx(@IdRes int viewId) {
        final View view = mView.findViewById(viewId);
        return view != null && view.getVisibility() != View.GONE ? view.getMeasuredHeight() : 0;
    }

    private int getDialogMarginPx() {
        return mView.getResources().getDimensionPixelSize(R.dimen.biometric_dialog_border_padding);
    }

    @NonNull
    private static Rect getMaximumWindowBounds(@Nullable WindowMetrics windowMetrics) {
        return windowMetrics != null ? windowMetrics.getBounds() : new Rect();
    }

    /**
     * For devices in portrait orientation where the sensor is too high up, calculates the amount of
     * padding necessary to center the biometric icon within the sensor's physical location.
     */
    @VisibleForTesting
    static int calculateBottomSpacerHeightForPortrait(
            @NonNull FingerprintSensorPropertiesInternal sensorProperties, int displayHeightPx,
            int textIndicatorHeightPx, int buttonBarHeightPx, int dialogMarginPx,
            float scaleFactor) {
        final SensorLocationInternal location = sensorProperties.getLocation();
        final int sensorDistanceFromBottom = displayHeightPx
                - (int) (scaleFactor * location.sensorLocationY)
                - (int) (scaleFactor * location.sensorRadius);

        final int spacerHeight = sensorDistanceFromBottom
                - textIndicatorHeightPx
                - buttonBarHeightPx
                - dialogMarginPx;

        if (DEBUG) {
            Log.d(TAG, "Display height: " + displayHeightPx
                    + ", Distance from bottom: " + sensorDistanceFromBottom
                    + ", Bottom margin: " + dialogMarginPx
                    + ", Bottom spacer height (portrait): " + spacerHeight
                    + ", Scale Factor: " + scaleFactor);
        }

        return spacerHeight;
    }

    /**
     * For devices in landscape orientation where the sensor is too high up, calculates the amount
     * of padding necessary to center the biometric icon within the sensor's physical location.
     */
    @VisibleForTesting
    static int calculateBottomSpacerHeightForLandscape(int titleHeightPx, int subtitleHeightPx,
            int descriptionHeightPx, int topSpacerHeightPx, int textIndicatorHeightPx,
            int buttonBarHeightPx) {

        final int dialogHeightAboveIcon = titleHeightPx
                + subtitleHeightPx
                + descriptionHeightPx
                + topSpacerHeightPx;

        final int dialogHeightBelowIcon = textIndicatorHeightPx + buttonBarHeightPx;

        final int bottomSpacerHeight = dialogHeightAboveIcon
                - dialogHeightBelowIcon;

        if (DEBUG) {
            Log.d(TAG, "Title height: " + titleHeightPx
                    + ", Subtitle height: " + subtitleHeightPx
                    + ", Description height: " + descriptionHeightPx
                    + ", Top spacer height: " + topSpacerHeightPx
                    + ", Text indicator height: " + textIndicatorHeightPx
                    + ", Button bar height: " + buttonBarHeightPx
                    + ", Bottom spacer height (landscape): " + bottomSpacerHeight);
        }

        return bottomSpacerHeight;
    }

    /**
     * For devices in landscape orientation where the sensor is too left/right, calculates the
     * amount of padding necessary to center the biometric icon within the sensor's physical
     * location.
     */
    @VisibleForTesting
    static int calculateHorizontalSpacerWidthForLandscape(
            @NonNull FingerprintSensorPropertiesInternal sensorProperties, int displayWidthPx,
            int dialogMarginPx, float scaleFactor) {
        final SensorLocationInternal location = sensorProperties.getLocation();
        final int sensorDistanceFromEdge = displayWidthPx
                - (int) (scaleFactor * location.sensorLocationY)
                - (int) (scaleFactor * location.sensorRadius);

        final int horizontalPadding = sensorDistanceFromEdge
                - dialogMarginPx;

        if (DEBUG) {
            Log.d(TAG, "Display width: " + displayWidthPx
                    + ", Distance from edge: " + sensorDistanceFromEdge
                    + ", Dialog margin: " + dialogMarginPx
                    + ", Horizontal spacer width (landscape): " + horizontalPadding
                    + ", Scale Factor: " + scaleFactor);
        }

        return horizontalPadding;
    }
}