Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9476fc29 authored by skxu's avatar skxu
Browse files

Add config for save dialog height percent

Make max percent of screen that the save dialog can take up in height
a device config.

Fixes: 231754479
Test: b/232835792
Change-Id: Id05b7c45323a2d481a00c5dc536b81ed6d31f56c
parent a9235c32
Loading
Loading
Loading
Loading
+63 −9
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@ package com.android.server.autofill.ui;
import static com.android.server.autofill.Helper.sDebug;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.provider.DeviceConfig;
import android.util.AttributeSet;
import android.util.Slog;
import android.util.TypedValue;
@@ -34,24 +36,59 @@ public class CustomScrollView extends ScrollView {

    private static final String TAG = "CustomScrollView";

    /**
     * Sets the max percent of screen that the autofill save dialog can take up in height
     * when the device is in portrait orientation.
     *
     * @hide
     */
    public static final String DEVICE_CONFIG_SAVE_DIALOG_PORTRAIT_BODY_HEIGHT_MAX_PERCENT =
            "autofill_save_dialog_portrait_body_height_max_percent";

    /**
     * Sets the max percent of screen that the autofill save dialog can take up in height
     * when the device is in landscape orientation.
     *
     * @hide
     */
    public static final String DEVICE_CONFIG_SAVE_DIALOG_LANDSCAPE_BODY_HEIGHT_MAX_PERCENT =
            "autofill_save_dialog_landscape_body_height_max_percent";

    private int mWidth = -1;
    private int mHeight = -1;
    private int mMaxPortraitBodyHeightPercent = 20;
    private int mMaxLandscapeBodyHeightPercent = 20;

    public CustomScrollView(Context context) {
        super(context);
        setMaxBodyHeightPercent();
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMaxBodyHeightPercent();
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setMaxBodyHeightPercent();
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setMaxBodyHeightPercent();
    }

    private void setMaxBodyHeightPercent() {
        mMaxPortraitBodyHeightPercent = DeviceConfig.getInt(
                DeviceConfig.NAMESPACE_AUTOFILL,
                DEVICE_CONFIG_SAVE_DIALOG_PORTRAIT_BODY_HEIGHT_MAX_PERCENT,
                mMaxPortraitBodyHeightPercent);
        mMaxLandscapeBodyHeightPercent = DeviceConfig.getInt(
                DeviceConfig.NAMESPACE_AUTOFILL,
                DEVICE_CONFIG_SAVE_DIALOG_LANDSCAPE_BODY_HEIGHT_MAX_PERCENT,
                mMaxLandscapeBodyHeightPercent);
    }

    @Override
@@ -72,20 +109,37 @@ public class CustomScrollView extends ScrollView {
    private void calculateDimensions() {
        if (mHeight != -1) return;

        final TypedValue typedValue = new TypedValue();
        final Point point = new Point();
        final Context context = getContext();
        context.getDisplayNoVerify().getSize(point);
        context.getTheme().resolveAttribute(R.attr.autofillSaveCustomSubtitleMaxHeight,
                typedValue, true);
        final View child = getChildAt(0);
        final int childHeight = child.getMeasuredHeight();
        final int maxHeight = (int) typedValue.getFraction(point.y, point.y);

        mHeight = Math.min(childHeight, maxHeight);
        final View content = getChildAt(0);
        final int contentHeight = content.getMeasuredHeight();
        int displayHeight = point.y;

        int configBasedMaxHeight = (getResources().getConfiguration().orientation
                    == Configuration.ORIENTATION_LANDSCAPE)
                ? (int) (mMaxLandscapeBodyHeightPercent * displayHeight / 100)
                : (int) (mMaxPortraitBodyHeightPercent * displayHeight / 100);
        mHeight = configBasedMaxHeight > 0
                ? Math.min(contentHeight, configBasedMaxHeight)
                : Math.min(contentHeight, getAttrBasedMaxHeight(context, displayHeight));

        if (sDebug) {
            Slog.d(TAG, "calculateDimensions(): maxHeight=" + maxHeight
                    + ", childHeight=" + childHeight + ", w=" + mWidth + ", h=" + mHeight);
            Slog.d(TAG, "calculateDimensions():"
                    + " mMaxPortraitBodyHeightPercent=" + mMaxPortraitBodyHeightPercent
                    + ", mMaxLandscapeBodyHeightPercent=" + mMaxLandscapeBodyHeightPercent
                    + ", configBasedMaxHeight=" + configBasedMaxHeight
                    + ", attrBasedMaxHeight=" + getAttrBasedMaxHeight(context, displayHeight)
                    + ", contentHeight=" + contentHeight
                    + ", w=" + mWidth + ", h=" + mHeight);
        }
    }

    private int getAttrBasedMaxHeight(Context context, int displayHeight) {
        final TypedValue maxHeightAttrTypedValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.autofillSaveCustomSubtitleMaxHeight,
                maxHeightAttrTypedValue, true);
        return (int) maxHeightAttrTypedValue.getFraction(displayHeight, displayHeight);
    }
}