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

Commit c4698283 authored by Dave Mankoff's avatar Dave Mankoff
Browse files

Cache icon drawables.

There's only a few of these. They currently get reloaded every time
the view changes states. This loading also happens in an on-predraw
listener. Cache them instead.

Fixes: 145666518
Test: atest SystemUITests
Change-Id: I5727459d395f1e347db42d91b313913c079ff3be
parent 27bb2092
Loading
Loading
Loading
Loading
+33 −19
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.statusbar.phone;
import android.annotation.IntDef;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.Animatable2;
import android.graphics.drawable.AnimatedVectorDrawable;
@@ -27,6 +28,7 @@ import android.os.Trace;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.ViewTreeObserver.OnPreDrawListener;

import com.android.internal.graphics.ColorUtils;
@@ -53,7 +55,7 @@ public class LockIcon extends KeyguardAffordanceView {
    private boolean mPulsing;
    private boolean mDozing;
    private boolean mKeyguardJustShown;
    private boolean mScanningFace;
    private final SparseArray<Drawable> mDrawableCache = new SparseArray<>();

    private final OnPreDrawListener mOnPreDrawListener = new OnPreDrawListener() {
        @Override
@@ -61,23 +63,15 @@ public class LockIcon extends KeyguardAffordanceView {
            getViewTreeObserver().removeOnPreDrawListener(this);

            int newState = mStateProvider.getState();
            @LockAnimIndex final int lockAnimIndex =
                    getAnimationIndexForTransition(mOldState, newState, mPulsing, mDozing,
                            mKeyguardJustShown);

            boolean isAnim = lockAnimIndex != -1;
            int iconRes = isAnim ? getThemedAnimationResId(lockAnimIndex)
                    : LockIcon.getIconForState(newState);

            Drawable icon = getResources().getDrawable(iconRes);

            Drawable icon = getIcon(newState);
            setImageDrawable(icon, false);
            if (mScanningFace) {

            if (newState == STATE_SCANNING_FACE) {
                announceForAccessibility(getResources().getString(
                        R.string.accessibility_scanning_face));
            }

            if (isAnim && icon instanceof AnimatedVectorDrawable) {
            if (icon instanceof AnimatedVectorDrawable) {
                final AnimatedVectorDrawable animation = (AnimatedVectorDrawable) icon;
                animation.forceAnimationOnUI();
                animation.clearAnimationCallbacks();
@@ -87,7 +81,7 @@ public class LockIcon extends KeyguardAffordanceView {
                            public void onAnimationEnd(Drawable drawable) {
                                if (getDrawable() == animation
                                        && newState == mStateProvider.getState()
                                        && lockAnimIndex == LockIcon.SCANNING) {
                                        && newState == STATE_SCANNING_FACE) {
                                    animation.start();
                                } else {
                                    Trace.endAsyncSection("LockIcon#Animation", newState);
@@ -110,6 +104,12 @@ public class LockIcon extends KeyguardAffordanceView {
        mStateProvider = stateProvider;
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawableCache.clear();
    }

    /**
     * Update the icon visibility
     * @return true if the visibility changed
@@ -135,13 +135,11 @@ public class LockIcon extends KeyguardAffordanceView {
        return false;
    }

    void update(int oldState, boolean pulsing, boolean dozing,
            boolean keyguardJustShown, boolean scanningFace) {
    void update(int oldState, boolean pulsing, boolean dozing, boolean keyguardJustShown) {
        mOldState = oldState;
        mPulsing = pulsing;
        mDozing = dozing;
        mKeyguardJustShown = keyguardJustShown;
        mScanningFace = scanningFace;

        getViewTreeObserver().addOnPreDrawListener(mOnPreDrawListener);
    }
@@ -151,7 +149,8 @@ public class LockIcon extends KeyguardAffordanceView {
        updateDarkTint();
    }

    void setIconColor(int iconColor) {
    void onThemeChange(int iconColor) {
        mDrawableCache.clear();
        mIconColor = iconColor;
        updateDarkTint();
    }
@@ -161,6 +160,21 @@ public class LockIcon extends KeyguardAffordanceView {
        setImageTintList(ColorStateList.valueOf(color));
    }

    private Drawable getIcon(int newState) {
        @LockAnimIndex final int lockAnimIndex =
                getAnimationIndexForTransition(mOldState, newState, mPulsing, mDozing,
                        mKeyguardJustShown);

        boolean isAnim = lockAnimIndex != -1;
        int iconRes = isAnim ? getThemedAnimationResId(lockAnimIndex) : getIconForState(newState);

        if (!mDrawableCache.contains(iconRes)) {
            mDrawableCache.put(iconRes, getResources().getDrawable(iconRes));
        }

        return mDrawableCache.get(iconRes);
    }

    static int getIconForState(int state) {
        int iconRes;
        switch (state) {
@@ -206,7 +220,7 @@ public class LockIcon extends KeyguardAffordanceView {
    @IntDef({ERROR, UNLOCK, LOCK, SCANNING})
    @interface LockAnimIndex {}
    static final int ERROR = 0, UNLOCK = 1, LOCK = 2, SCANNING = 3;
    static final int[][] LOCK_ANIM_RES_IDS = new int[][] {
    private static final int[][] LOCK_ANIM_RES_IDS = new int[][] {
            {
                    R.anim.lock_to_error,
                    R.anim.lock_unlock,
+3 −7
Original line number Diff line number Diff line
@@ -75,7 +75,6 @@ public class LockscreenLockIconController {
    private final HeadsUpManagerPhone mHeadsUpManagerPhone;
    private boolean mKeyguardShowing;
    private boolean mKeyguardJustShown;
    private boolean mIsFaceUnlockState;
    private boolean mBlockUpdates;
    private boolean mPulsing;
    private boolean mDozing;
@@ -150,7 +149,7 @@ public class LockscreenLockIconController {
                    null, new int[]{ R.attr.wallpaperTextColor }, 0, 0);
            int iconColor = typedArray.getColor(0, Color.WHITE);
            typedArray.recycle();
            mLockIcon.setIconColor(iconColor);
            mLockIcon.onThemeChange(iconColor);
        }

        @Override
@@ -299,7 +298,7 @@ public class LockscreenLockIconController {
                        info.addAction(unlock);
                        info.setHintText(mResources.getString(
                                R.string.accessibility_waiting_for_fingerprint));
                    } else if (mIsFaceUnlockState) {
                    } else if (getState() == STATE_SCANNING_FACE) {
                        //Avoid 'button' to be spoken for scanning face
                        info.setClassName(LockIcon.class.getName());
                        info.setContentDescription(mResources.getString(
@@ -458,15 +457,12 @@ public class LockscreenLockIconController {

    private void update(boolean force) {
        int state = getState();
        mIsFaceUnlockState = state == STATE_SCANNING_FACE;

        boolean shouldUpdate = mLastState != state || force;
        if (mBlockUpdates && canBlockUpdates()) {
            shouldUpdate = false;
        }
        if (shouldUpdate && mLockIcon != null) {
            mLockIcon.update(
                    mLastState, mPulsing, mDozing, mKeyguardJustShown, mIsFaceUnlockState);
            mLockIcon.update(mLastState, mPulsing, mDozing, mKeyguardJustShown);
        }
        mLastState = state;
        mKeyguardJustShown = false;