Loading src/com/android/settings/applications/InstalledAppDetails.java +34 −0 Original line number Diff line number Diff line Loading @@ -53,6 +53,7 @@ import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; import android.service.notification.NotificationListenerService.Ranking; import android.support.annotation.VisibleForTesting; import android.support.v7.preference.Preference; import android.support.v7.preference.Preference.OnPreferenceClickListener; import android.support.v7.preference.PreferenceCategory; Loading Loading @@ -461,6 +462,8 @@ public class InstalledAppDetails extends AppInfoBase showIt = false; } else if (mUserManager.getUsers().size() < 2) { showIt = false; } else if (PackageUtil.countPackageInUsers(mPm, mUserManager, mPackageName) < 2) { showIt = false; } menu.findItem(UNINSTALL_ALL_USERS_MENU).setVisible(showIt); mUpdatedSysApp = (mAppEntry.info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0; Loading Loading @@ -1164,6 +1167,37 @@ public class InstalledAppDetails extends AppInfoBase } } /** * Elicit this class for testing. Test cannot be done in robolectric because it * invokes the new API. */ @VisibleForTesting public static class PackageUtil { /** * Count how many users in device have installed package {@paramref packageName} */ public static int countPackageInUsers(PackageManager packageManager, UserManager userManager, String packageName) { final List<UserInfo> userInfos = userManager.getUsers(true); int count = 0; for (final UserInfo userInfo : userInfos) { try { // Use this API to check whether user has this package final ApplicationInfo info = packageManager.getApplicationInfoAsUser( packageName, PackageManager.GET_META_DATA, userInfo.id); if ((info.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { count++; } } catch(NameNotFoundException e) { Log.e(TAG, "Package: " + packageName + " not found for user: " + userInfo.id); } } return count; } } private static class DisableChanger extends AsyncTask<Object, Object, Object> { final PackageManager mPm; final WeakReference<InstalledAppDetails> mActivity; Loading tests/app/src/com/android/settings/applications/PackageUtilTest.java 0 → 100644 +93 −0 Original line number Diff line number Diff line /* * Copyright (C) 2016 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.settings.applications; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.UserInfo; import android.os.UserManager; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.util.ArrayList; import java.util.List; import static junit.framework.Assert.assertEquals; import static org.mockito.Mockito.when; @RunWith(AndroidJUnit4.class) @SmallTest public class PackageUtilTest { private static final String ALL_USERS_APP_NAME = "com.google.allusers.app"; private static final String ONE_USER_APP_NAME = "com.google.oneuser.app"; private static final int USER1_ID = 1; private static final int USER2_ID = 11; @Mock private PackageManager mMockPackageManager; @Mock private UserManager mMockUserManager; private InstalledAppDetails.PackageUtil mPackageUtil; private List<UserInfo> mUserInfos; @Before public void setUp() throws PackageManager.NameNotFoundException { MockitoAnnotations.initMocks(this); mUserInfos = new ArrayList<>(); mUserInfos.add(new UserInfo(USER1_ID, "lei", 0)); mUserInfos.add(new UserInfo(USER2_ID, "yue", 0)); when(mMockUserManager.getUsers(true)).thenReturn(mUserInfos); ApplicationInfo usersApp = new ApplicationInfo(); usersApp.flags = ApplicationInfo.FLAG_INSTALLED; when(mMockPackageManager.getApplicationInfoAsUser( ALL_USERS_APP_NAME, PackageManager.GET_META_DATA, USER1_ID)) .thenReturn(usersApp); when(mMockPackageManager.getApplicationInfoAsUser( ALL_USERS_APP_NAME, PackageManager.GET_META_DATA, USER2_ID)) .thenReturn(usersApp); when(mMockPackageManager.getApplicationInfoAsUser( ONE_USER_APP_NAME, PackageManager.GET_META_DATA, USER1_ID)) .thenReturn(usersApp); when(mMockPackageManager.getApplicationInfoAsUser( ONE_USER_APP_NAME, PackageManager.GET_META_DATA, USER2_ID)) .thenThrow(new PackageManager.NameNotFoundException()); mPackageUtil = new InstalledAppDetails.PackageUtil(); } @Test public void testCountPackageInUsers_twoUsersInstalled_returnTwo() { assertEquals(2, mPackageUtil.countPackageInUsers( mMockPackageManager, mMockUserManager, ALL_USERS_APP_NAME)); } @Test public void testCountPackageInUsers_oneUsersInstalled_returnOne() { assertEquals(1, mPackageUtil.countPackageInUsers( mMockPackageManager, mMockUserManager, ONE_USER_APP_NAME)); } } Loading
src/com/android/settings/applications/InstalledAppDetails.java +34 −0 Original line number Diff line number Diff line Loading @@ -53,6 +53,7 @@ import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; import android.service.notification.NotificationListenerService.Ranking; import android.support.annotation.VisibleForTesting; import android.support.v7.preference.Preference; import android.support.v7.preference.Preference.OnPreferenceClickListener; import android.support.v7.preference.PreferenceCategory; Loading Loading @@ -461,6 +462,8 @@ public class InstalledAppDetails extends AppInfoBase showIt = false; } else if (mUserManager.getUsers().size() < 2) { showIt = false; } else if (PackageUtil.countPackageInUsers(mPm, mUserManager, mPackageName) < 2) { showIt = false; } menu.findItem(UNINSTALL_ALL_USERS_MENU).setVisible(showIt); mUpdatedSysApp = (mAppEntry.info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0; Loading Loading @@ -1164,6 +1167,37 @@ public class InstalledAppDetails extends AppInfoBase } } /** * Elicit this class for testing. Test cannot be done in robolectric because it * invokes the new API. */ @VisibleForTesting public static class PackageUtil { /** * Count how many users in device have installed package {@paramref packageName} */ public static int countPackageInUsers(PackageManager packageManager, UserManager userManager, String packageName) { final List<UserInfo> userInfos = userManager.getUsers(true); int count = 0; for (final UserInfo userInfo : userInfos) { try { // Use this API to check whether user has this package final ApplicationInfo info = packageManager.getApplicationInfoAsUser( packageName, PackageManager.GET_META_DATA, userInfo.id); if ((info.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { count++; } } catch(NameNotFoundException e) { Log.e(TAG, "Package: " + packageName + " not found for user: " + userInfo.id); } } return count; } } private static class DisableChanger extends AsyncTask<Object, Object, Object> { final PackageManager mPm; final WeakReference<InstalledAppDetails> mActivity; Loading
tests/app/src/com/android/settings/applications/PackageUtilTest.java 0 → 100644 +93 −0 Original line number Diff line number Diff line /* * Copyright (C) 2016 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.settings.applications; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.UserInfo; import android.os.UserManager; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.util.ArrayList; import java.util.List; import static junit.framework.Assert.assertEquals; import static org.mockito.Mockito.when; @RunWith(AndroidJUnit4.class) @SmallTest public class PackageUtilTest { private static final String ALL_USERS_APP_NAME = "com.google.allusers.app"; private static final String ONE_USER_APP_NAME = "com.google.oneuser.app"; private static final int USER1_ID = 1; private static final int USER2_ID = 11; @Mock private PackageManager mMockPackageManager; @Mock private UserManager mMockUserManager; private InstalledAppDetails.PackageUtil mPackageUtil; private List<UserInfo> mUserInfos; @Before public void setUp() throws PackageManager.NameNotFoundException { MockitoAnnotations.initMocks(this); mUserInfos = new ArrayList<>(); mUserInfos.add(new UserInfo(USER1_ID, "lei", 0)); mUserInfos.add(new UserInfo(USER2_ID, "yue", 0)); when(mMockUserManager.getUsers(true)).thenReturn(mUserInfos); ApplicationInfo usersApp = new ApplicationInfo(); usersApp.flags = ApplicationInfo.FLAG_INSTALLED; when(mMockPackageManager.getApplicationInfoAsUser( ALL_USERS_APP_NAME, PackageManager.GET_META_DATA, USER1_ID)) .thenReturn(usersApp); when(mMockPackageManager.getApplicationInfoAsUser( ALL_USERS_APP_NAME, PackageManager.GET_META_DATA, USER2_ID)) .thenReturn(usersApp); when(mMockPackageManager.getApplicationInfoAsUser( ONE_USER_APP_NAME, PackageManager.GET_META_DATA, USER1_ID)) .thenReturn(usersApp); when(mMockPackageManager.getApplicationInfoAsUser( ONE_USER_APP_NAME, PackageManager.GET_META_DATA, USER2_ID)) .thenThrow(new PackageManager.NameNotFoundException()); mPackageUtil = new InstalledAppDetails.PackageUtil(); } @Test public void testCountPackageInUsers_twoUsersInstalled_returnTwo() { assertEquals(2, mPackageUtil.countPackageInUsers( mMockPackageManager, mMockUserManager, ALL_USERS_APP_NAME)); } @Test public void testCountPackageInUsers_oneUsersInstalled_returnOne() { assertEquals(1, mPackageUtil.countPackageInUsers( mMockPackageManager, mMockUserManager, ONE_USER_APP_NAME)); } }