Create an #isAppInstalled() util method

Test: robo test
Bug: 230303570
Change-Id: I39bf4129cda09b0b3e2913543e425f272d80df9d
This commit is contained in:
Tsung-Mao Fang
2022-05-06 18:19:25 +08:00
parent dc25f23cb2
commit 00d3d3086a
2 changed files with 37 additions and 2 deletions

View File

@@ -268,9 +268,9 @@ public class AppUtils {
/**
* Preload the top N icons of app entry list.
*
* @param context caller's context
* @param context caller's context
* @param appEntries AppEntry list of ApplicationsState
* @param number the number of Top N icons of the appEntries
* @param number the number of Top N icons of the appEntries
*/
public static void preloadTopIcons(Context context,
ArrayList<ApplicationsState.AppEntry> appEntries, int number) {
@@ -286,6 +286,19 @@ public class AppUtils {
}
}
/**
* Returns a boolean indicating whether this app is installed or not.
*
* @param appEntry AppEntry of ApplicationsState.
* @return true if the app is in installed state.
*/
public static boolean isAppInstalled(ApplicationsState.AppEntry appEntry) {
if (appEntry == null || appEntry.info == null) {
return false;
}
return (appEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) != 0;
}
private static void setAppEntryMounted(ApplicationsState.AppEntry appEntry, boolean mounted) {
if (appEntry.mounted != mounted) {
synchronized (appEntry) {

View File

@@ -129,6 +129,28 @@ public class AppUtilsTest {
assertThat(mAppIconCacheManager.get(APP_PACKAGE_NAME, APP_UID)).isNotNull();
}
@Test
public void isAppInstalled_noAppEntry_shouldReturnFalse() {
assertThat(AppUtils.isAppInstalled(null)).isFalse();
}
@Test
public void isAppInstalled_hasAppEntryWithInstalledFlag_shouldReturnTrue() {
final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
appEntry.info = new ApplicationInfo();
appEntry.info.flags = ApplicationInfo.FLAG_INSTALLED;
assertThat(AppUtils.isAppInstalled(appEntry)).isTrue();
}
@Test
public void isAppInstalled_hasAppEntryWithoutInstalledFlag_shouldReturnFalse() {
final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class);
appEntry.info = new ApplicationInfo();
assertThat(AppUtils.isAppInstalled(appEntry)).isFalse();
}
private ApplicationsState.AppEntry createAppEntry(ApplicationInfo appInfo, int id) {
ApplicationsState.AppEntry appEntry = new ApplicationsState.AppEntry(mContext, appInfo, id);
appEntry.label = "label";