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

Commit 773fd7b3 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Generalize userproperties to support for more user types in iconCache

Bug: 305062259
Flag: None
Test: Presubmit
Change-Id: Ibdd5f25760d34470f3b05733674ac1206c7dcb7e
parent 1049b455
Loading
Loading
Loading
Loading
+35 −29
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ import android.graphics.drawable.DrawableWrapper;
import android.graphics.drawable.InsetDrawable;
import android.os.Build;
import android.os.UserHandle;
import android.util.SparseBooleanArray;
import android.util.SparseArray;

import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
@@ -42,6 +42,7 @@ import androidx.annotation.Nullable;

import com.android.launcher3.icons.BitmapInfo.Extender;
import com.android.launcher3.util.FlagOp;
import com.android.launcher3.util.UserIconInfo;

import java.lang.annotation.Retention;
import java.util.Objects;
@@ -71,7 +72,7 @@ public class BaseIconFactory implements AutoCloseable {
    private final Rect mOldBounds = new Rect();

    @NonNull
    private final SparseBooleanArray mIsUserBadged = new SparseBooleanArray();
    private final SparseArray<UserIconInfo> mCachedUserInfo = new SparseArray<>();

    @NonNull
    protected final Context mContext;
@@ -257,27 +258,32 @@ public class BaseIconFactory implements AutoCloseable {
                op = op.addFlag(FLAG_INSTANT);
            }

            if (options.mUserHandle != null) {
                int key = options.mUserHandle.hashCode();
                boolean isBadged;
                int index;
                if ((index = mIsUserBadged.indexOfKey(key)) >= 0) {
                    isBadged = mIsUserBadged.valueAt(index);
                } else {
                    // Check packageManager if the provided user needs a badge
                    NoopDrawable d = new NoopDrawable();
                    isBadged = (d != mPm.getUserBadgedIcon(d, options.mUserHandle));
                    mIsUserBadged.put(key, isBadged);
            UserIconInfo info = options.mUserIconInfo;
            if (info == null && options.mUserHandle != null) {
                info = getUserInfo(options.mUserHandle);
            }
                // Set the clone profile badge flag in case it is present.
                op = op.setFlag(FLAG_CLONE, isBadged && options.mIsCloneProfile);
                // Set the Work profile badge for all other cases.
                op = op.setFlag(FLAG_WORK, isBadged && !options.mIsCloneProfile);
            if (info != null) {
                op = op.setFlag(FLAG_WORK, info.isWork());
                op = op.setFlag(FLAG_CLONE, info.isCloned());
            }
        }
        return op;
    }

    @NonNull
    protected UserIconInfo getUserInfo(@NonNull UserHandle user) {
        int key = user.hashCode();
        UserIconInfo info = mCachedUserInfo.get(key);
        if (info == null) {
            // Simple check to check if the provided user is work profile or not based on badging
            NoopDrawable d = new NoopDrawable();
            boolean isWork = (d != mPm.getUserBadgedIcon(d, user));
            info = new UserIconInfo(user, isWork ? UserIconInfo.TYPE_WORK : UserIconInfo.TYPE_MAIN);
            mCachedUserInfo.put(key, info);
        }
        return info;
    }

    @NonNull
    public Bitmap getWhiteShadowLayer() {
        if (mWhiteShadowLayer == null) {
@@ -505,12 +511,12 @@ public class BaseIconFactory implements AutoCloseable {

        boolean mIsInstantApp;

        boolean mIsCloneProfile;

        @BitmapGenerationMode
        int mGenerationMode = MODE_WITH_SHADOW;

        @Nullable UserHandle mUserHandle;
        @Nullable
        UserIconInfo mUserIconInfo;

        @ColorInt
        @Nullable Integer mExtractedColor;
@@ -533,6 +539,15 @@ public class BaseIconFactory implements AutoCloseable {
            return this;
        }

        /**
         * User for this icon, in case of badging
         */
        @NonNull
        public IconOptions setUser(@Nullable final UserIconInfo user) {
            mUserIconInfo = user;
            return this;
        }

        /**
         * If this icon represents an instant app
         */
@@ -560,15 +575,6 @@ public class BaseIconFactory implements AutoCloseable {
            mGenerationMode = generationMode;
            return this;
        }

        /**
         * Used to determine the badge type for this icon.
         */
        @NonNull
        public IconOptions setIsCloneProfile(boolean isCloneProfile) {
            mIsCloneProfile = isCloneProfile;
            return this;
        }
    }

    /**
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 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.launcher3.util;

import android.os.UserHandle;

import androidx.annotation.IntDef;

/**
 * Data class which stores various properties of a {@link android.os.UserHandle}
 * which affects rendering
 */
public class UserIconInfo {

    public static final int TYPE_MAIN = 0;
    public static final int TYPE_WORK = 1;
    public static final int TYPE_CLONED = 2;

    @IntDef({TYPE_MAIN, TYPE_WORK, TYPE_CLONED})
    public @interface UserType { }

    public final UserHandle user;
    @UserType
    public final int type;

    public final long userSerial;

    public UserIconInfo(UserHandle user, @UserType int type) {
        this(user, type, 0);
    }

    public UserIconInfo(UserHandle user, @UserType int type, long userSerial) {
        this.user = user;
        this.type = type;
        this.userSerial = userSerial;
    }

    public boolean isMain() {
        return type == TYPE_MAIN;
    }

    public boolean isWork() {
        return type == TYPE_WORK;
    }

    public boolean isCloned() {
        return type == TYPE_CLONED;
    }
}