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

Commit 96591a36 authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Moving some utility class to IconCache to make it easier to use outside Launcher" into main

parents a01ec68f 2a359916
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -67,7 +67,6 @@
    <string name="main_process_initializer_class" translatable="false"></string>
    <string name="app_launch_tracker_class" translatable="false"></string>
    <string name="test_information_handler_class" translatable="false"></string>
    <string name="launcher_activity_logic_class" translatable="false"></string>
    <string name="model_delegate_class" translatable="false"></string>
    <string name="window_manager_proxy_class" translatable="false"></string>
    <string name="secondary_display_predictions_class" translatable="false"></string>
+0 −75
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.icons;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle;

import androidx.annotation.NonNull;

import com.android.launcher3.icons.cache.CachingLogic;

public interface ComponentWithLabel {

    ComponentName getComponent();

    UserHandle getUser();

    CharSequence getLabel(PackageManager pm);


    class ComponentCachingLogic<T extends ComponentWithLabel> implements CachingLogic<T> {

        private final PackageManager mPackageManager;
        private final boolean mAddToMemCache;

        public ComponentCachingLogic(Context context, boolean addToMemCache) {
            mPackageManager = context.getPackageManager();
            mAddToMemCache = addToMemCache;
        }

        @Override
        @NonNull
        public ComponentName getComponent(@NonNull T object) {
            return object.getComponent();
        }

        @NonNull
        @Override
        public UserHandle getUser(@NonNull T object) {
            return object.getUser();
        }

        @NonNull
        @Override
        public CharSequence getLabel(@NonNull T object) {
            return object.getLabel(mPackageManager);
        }

        @NonNull
        @Override
        public BitmapInfo loadIcon(@NonNull Context context, @NonNull T object) {
            return BitmapInfo.LOW_RES_INFO;
        }

        @Override
        public boolean addToMemCache() {
            return mAddToMemCache;
        }
    }
}
+0 −56
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.icons;

import android.content.Context;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;

import com.android.launcher3.LauncherAppState;
import com.android.launcher3.icons.BaseIconFactory.IconOptions;

/**
 * Extension of ComponentWithLabel to also support loading icons
 */
public interface ComponentWithLabelAndIcon extends ComponentWithLabel {

    /**
     * Provide an icon for this object
     */
    Drawable getFullResIcon(IconCache cache);

    class ComponentWithIconCachingLogic extends ComponentCachingLogic<ComponentWithLabelAndIcon> {

        public ComponentWithIconCachingLogic(Context context, boolean addToMemCache) {
            super(context, addToMemCache);
        }

        @NonNull
        @Override
        public BitmapInfo loadIcon(@NonNull Context context,
                @NonNull ComponentWithLabelAndIcon object) {
            Drawable d = object.getFullResIcon(LauncherAppState.getInstance(context)
                    .getIconCache());
            if (d == null) {
                return super.loadIcon(context, object);
            }
            try (LauncherIcons li = LauncherIcons.obtain(context)) {
                return li.createBadgedIconBitmap(d, new IconOptions().setUser(object.getUser()));
            }
        }
    }
}
+9 −12
Original line number Diff line number Diff line
@@ -54,9 +54,10 @@ import androidx.core.util.Pair;
import com.android.launcher3.Flags;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.Utilities;
import com.android.launcher3.icons.ComponentWithLabel.ComponentCachingLogic;
import com.android.launcher3.icons.cache.BaseIconCache;
import com.android.launcher3.icons.cache.CachedObjectCachingLogic;
import com.android.launcher3.icons.cache.CachingLogic;
import com.android.launcher3.icons.cache.LauncherActivityCachingLogic;
import com.android.launcher3.logging.FileLog;
import com.android.launcher3.model.data.AppInfo;
import com.android.launcher3.model.data.IconRequestInfo;
@@ -102,7 +103,6 @@ public class IconCache extends BaseIconCache {
    private final LauncherApps mLauncherApps;
    private final UserCache mUserManager;
    private final InstantAppResolver mInstantAppResolver;
    private final IconProvider mIconProvider;
    private final CancellableTask mCancelledTask;

    private final SparseArray<BitmapInfo> mWidgetCategoryBitmapInfos;
@@ -112,14 +112,14 @@ public class IconCache extends BaseIconCache {
    public IconCache(Context context, InvariantDeviceProfile idp, String dbFileName,
            IconProvider iconProvider) {
        super(context, dbFileName, MODEL_EXECUTOR.getLooper(),
                idp.fillResIconDpi, idp.iconBitmapSize, true /* inMemoryCache */);
        mComponentWithLabelCachingLogic = new ComponentCachingLogic(context, false);
        mLauncherActivityInfoCachingLogic = LauncherActivityCachingLogic.newInstance(context);
                idp.fillResIconDpi, idp.iconBitmapSize, true /* inMemoryCache */, iconProvider);
        mComponentWithLabelCachingLogic = new CachedObjectCachingLogic(
                context, false /* loadIcons */, false /* addToMemCache */);
        mLauncherActivityInfoCachingLogic = LauncherActivityCachingLogic.INSTANCE;
        mShortcutCachingLogic = new ShortcutCachingLogic();
        mLauncherApps = mContext.getSystemService(LauncherApps.class);
        mUserManager = UserCache.INSTANCE.get(mContext);
        mInstantAppResolver = InstantAppResolver.newInstance(mContext);
        mIconProvider = iconProvider;
        mWidgetCategoryBitmapInfos = new SparseArray<>();

        mCancelledTask = new CancellableTask(() -> null, MAIN_EXECUTOR, c -> { });
@@ -337,6 +337,9 @@ public class IconCache extends BaseIconCache {
        }
    }

    /**
     * Loads and returns the icon for the provided object without adding it to memCache
     */
    public synchronized String getTitleNoCache(ComponentWithLabel info) {
        CacheEntry entry = cacheLocked(info.getComponent(), info.getUser(), () -> info,
                mComponentWithLabelCachingLogic, false /* usePackageIcon */,
@@ -629,12 +632,6 @@ public class IconCache extends BaseIconCache {
                info.getAppLabel());
    }

    @Override
    @NonNull
    protected String getIconSystemState(String packageName) {
        return mIconProvider.getSystemStateForPackage(mSystemState, packageName);
    }

    /**
     * Interface for receiving itemInfo with high-res icon.
     */
+0 −81
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.icons;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.LauncherActivityInfo;
import android.os.Build;
import android.os.UserHandle;

import androidx.annotation.NonNull;

import com.android.launcher3.Flags;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;
import com.android.launcher3.icons.BaseIconFactory.IconOptions;
import com.android.launcher3.icons.cache.CachingLogic;
import com.android.launcher3.util.ResourceBasedOverride;

/**
 * Caching logic for LauncherActivityInfo.
 */
public class LauncherActivityCachingLogic
        implements CachingLogic<LauncherActivityInfo>, ResourceBasedOverride {

    /**
     * Creates and returns a new instance
     */
    public static LauncherActivityCachingLogic newInstance(Context context) {
        return Overrides.getObject(LauncherActivityCachingLogic.class, context,
                R.string.launcher_activity_logic_class);
    }

    @NonNull
    @Override
    public ComponentName getComponent(@NonNull LauncherActivityInfo object) {
        return object.getComponentName();
    }

    @NonNull
    @Override
    public UserHandle getUser(@NonNull LauncherActivityInfo object) {
        return object.getUser();
    }

    @NonNull
    @Override
    public CharSequence getLabel(@NonNull LauncherActivityInfo object) {
        return object.getLabel();
    }

    @NonNull
    @Override
    public BitmapInfo loadIcon(@NonNull Context context, @NonNull LauncherActivityInfo object) {
        try (LauncherIcons li = LauncherIcons.obtain(context)) {
            IconOptions iconOptions = new IconOptions().setUser(object.getUser());
            iconOptions.mIsArchived = Flags.useNewIconForArchivedApps()
                && Build.VERSION.SDK_INT >= 35
                && object.getActivityInfo().isArchived;
            return li.createBadgedIconBitmap(
                    LauncherAppState.getInstance(context)
                        .getIconProvider()
                        .getIcon(object, li.mFillResIconDpi),
                    iconOptions
            );
        }
    }
}
Loading