From c475f22d8b520440bbecbb18261bdb27e05f936e Mon Sep 17 00:00:00 2001 From: Rohit Goyal Date: Fri, 26 Jan 2024 02:17:05 +0530 Subject: [PATCH] UI Improvement: Persist archived app icon on workspace in case app uninstallation is cancelled. Test: verified bugfix locally. Bug: 319213296 Flag: ACONFIG com.android.launcher3.enable_support_for_archiving DEVELOPMENT Change-Id: Id9ba2ea833e8360ac8aa2c765beff402cff12ae2 --- .../launcher3/util/PackageManagerHelper.java | 12 ++- .../util/PackageManagerHelperTest.java | 83 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 tests/src/com/android/launcher3/util/PackageManagerHelperTest.java diff --git a/src/com/android/launcher3/util/PackageManagerHelper.java b/src/com/android/launcher3/util/PackageManagerHelper.java index 2b5aaf5520..50d8886980 100644 --- a/src/com/android/launcher3/util/PackageManagerHelper.java +++ b/src/com/android/launcher3/util/PackageManagerHelper.java @@ -16,6 +16,8 @@ package com.android.launcher3.util; +import static com.android.launcher3.Flags.enableSupportForArchiving; + import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.content.Context; @@ -112,8 +114,7 @@ public class PackageManagerHelper { @NonNull final UserHandle user, final int flags) { try { ApplicationInfo info = mLauncherApps.getApplicationInfo(packageName, flags, user); - return (info.flags & ApplicationInfo.FLAG_INSTALLED) == 0 || !info.enabled - ? null : info; + return !isPackageInstalledOrArchived(info) || !info.enabled ? null : info; } catch (PackageManager.NameNotFoundException e) { return null; } @@ -253,4 +254,11 @@ public class PackageManagerHelper { } return 100; } + + /** Returns true in case app is installed on the device or in archived state. */ + @SuppressWarnings("NewApi") + private boolean isPackageInstalledOrArchived(ApplicationInfo info) { + return (info.flags & ApplicationInfo.FLAG_INSTALLED) != 0 || ( + enableSupportForArchiving() && info.isArchived); + } } diff --git a/tests/src/com/android/launcher3/util/PackageManagerHelperTest.java b/tests/src/com/android/launcher3/util/PackageManagerHelperTest.java new file mode 100644 index 0000000000..d1da5f4f2d --- /dev/null +++ b/tests/src/com/android/launcher3/util/PackageManagerHelperTest.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2024 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 static com.android.launcher3.Flags.FLAG_ENABLE_SUPPORT_FOR_ARCHIVING; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.LauncherApps; +import android.content.pm.PackageManager; +import android.os.UserHandle; +import android.platform.test.annotations.RequiresFlagsEnabled; +import android.platform.test.flag.junit.CheckFlagsRule; +import android.platform.test.flag.junit.DeviceFlagsValueProvider; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +/** Unit tests for {@link PackageManagerHelper}. */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public final class PackageManagerHelperTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Rule + public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); + + private static final String TEST_PACKAGE = "com.android.test.package"; + private static final int TEST_USER = 2; + + private Context mContext; + private LauncherApps mLauncherApps; + private PackageManagerHelper mPackageManagerHelper; + + @Before + public void setup() { + mContext = mock(Context.class); + mLauncherApps = mock(LauncherApps.class); + when(mContext.getSystemService(eq(LauncherApps.class))).thenReturn(mLauncherApps); + mPackageManagerHelper = new PackageManagerHelper(mContext); + } + + @Test + @RequiresFlagsEnabled(FLAG_ENABLE_SUPPORT_FOR_ARCHIVING) + public void getApplicationInfo_archivedApp_appInfoIsNotNull() + throws PackageManager.NameNotFoundException { + ApplicationInfo applicationInfo = new ApplicationInfo(); + applicationInfo.isArchived = true; + when(mLauncherApps.getApplicationInfo(TEST_PACKAGE, 0 /* flags */, + UserHandle.of(TEST_USER))) + .thenReturn(applicationInfo); + + assertThat(mPackageManagerHelper.getApplicationInfo(TEST_PACKAGE, UserHandle.of(TEST_USER), + 0 /* flags */)) + .isNotNull(); + } +}