Merge "Add number of enterprise-installed apps to Privacy Settings page"
This commit is contained in:
committed by
Android (Google) Code Review
commit
c3fdc3e813
@@ -8015,6 +8015,11 @@
|
|||||||
<string name="enterprise_privacy_always_on_vpn_work">Always-on VPN turned on in your work profile</string>
|
<string name="enterprise_privacy_always_on_vpn_work">Always-on VPN turned on in your work profile</string>
|
||||||
<!-- Label explaining that a global HTTP proxy was set by the admin. [CHAR LIMIT=NONE] -->
|
<!-- Label explaining that a global HTTP proxy was set by the admin. [CHAR LIMIT=NONE] -->
|
||||||
<string name="enterprise_privacy_global_http_proxy">Global HTTP proxy set</string>
|
<string name="enterprise_privacy_global_http_proxy">Global HTTP proxy set</string>
|
||||||
|
<!-- Label indicating how many apps were installed on the device by the admin. [CHAR LIMIT=NONE] -->
|
||||||
|
<plurals name="enterprise_privacy_number_enterprise_installed_packages">
|
||||||
|
<item quantity="one"><xliff:g id="count">%d</xliff:g> app installed by your admin</item>
|
||||||
|
<item quantity="other"><xliff:g id="count">%d</xliff:g> apps installed by your admin</item>
|
||||||
|
</plurals>
|
||||||
|
|
||||||
<!-- Preference label for the Photos & Videos storage section. [CHAR LIMIT=50] -->
|
<!-- Preference label for the Photos & Videos storage section. [CHAR LIMIT=50] -->
|
||||||
<string name="storage_photos_videos">Photos & Videos</string>
|
<string name="storage_photos_videos">Photos & Videos</string>
|
||||||
|
@@ -73,6 +73,10 @@
|
|||||||
android:title="@string/enterprise_privacy_global_http_proxy"
|
android:title="@string/enterprise_privacy_global_http_proxy"
|
||||||
settings:allowDividerBelow="true"
|
settings:allowDividerBelow="true"
|
||||||
settings:multiLine="true"/>
|
settings:multiLine="true"/>
|
||||||
|
<com.android.settings.DividerPreference
|
||||||
|
android:key="number_enterprise_installed_packages"
|
||||||
|
settings:allowDividerBelow="true"
|
||||||
|
settings:multiLine="true"/>
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
<PreferenceCategory android:title="@string/enterprise_privacy_device_access_category">
|
<PreferenceCategory android:title="@string/enterprise_privacy_device_access_category">
|
||||||
|
@@ -26,11 +26,21 @@ public interface ApplicationFeatureProvider {
|
|||||||
*/
|
*/
|
||||||
AppHeaderController newAppHeaderController(Fragment fragment, View appHeader);
|
AppHeaderController newAppHeaderController(Fragment fragment, View appHeader);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count all installed packages, irrespective of install reason.
|
||||||
|
*/
|
||||||
|
public static final int IGNORE_INSTALL_REASON = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asynchronously calculates the total number of apps installed on the device, across all users
|
* Asynchronously calculates the total number of apps installed on the device, across all users
|
||||||
* and managed profiles.
|
* and managed profiles.
|
||||||
|
*
|
||||||
|
* @param installReason Only consider packages with this install reason; may be any install
|
||||||
|
* reason defined in {@link android.content.pm.PackageManager} or
|
||||||
|
* {@link #IGNORE_INSTALL_REASON} to count all packages, irrespective of install reason.
|
||||||
|
* @param callback The callback to invoke with the result
|
||||||
*/
|
*/
|
||||||
void calculateNumberOfInstalledApps(NumberOfInstalledAppsCallback callback);
|
void calculateNumberOfInstalledApps(int installReason, NumberOfInstalledAppsCallback callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback that receives the total number of packages installed on the device.
|
* Callback that receives the total number of packages installed on the device.
|
||||||
|
@@ -42,15 +42,16 @@ public class ApplicationFeatureProviderImpl implements ApplicationFeatureProvide
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void calculateNumberOfInstalledApps(NumberOfInstalledAppsCallback callback) {
|
public void calculateNumberOfInstalledApps(int installReason,
|
||||||
new AllUserInstalledAppCounter(callback).execute();
|
NumberOfInstalledAppsCallback callback) {
|
||||||
|
new AllUserInstalledAppCounter(installReason, callback).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AllUserInstalledAppCounter extends InstalledAppCounter {
|
private class AllUserInstalledAppCounter extends InstalledAppCounter {
|
||||||
private NumberOfInstalledAppsCallback mCallback;
|
private NumberOfInstalledAppsCallback mCallback;
|
||||||
|
|
||||||
AllUserInstalledAppCounter(NumberOfInstalledAppsCallback callback) {
|
AllUserInstalledAppCounter(int installReason, NumberOfInstalledAppsCallback callback) {
|
||||||
super(mContext, ApplicationFeatureProviderImpl.this.mPm);
|
super(mContext, installReason, ApplicationFeatureProviderImpl.this.mPm);
|
||||||
mCallback = callback;
|
mCallback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,12 +25,24 @@ import java.util.List;
|
|||||||
|
|
||||||
public abstract class InstalledAppCounter extends AppCounter {
|
public abstract class InstalledAppCounter extends AppCounter {
|
||||||
|
|
||||||
public InstalledAppCounter(Context context, PackageManagerWrapper packageManager) {
|
private final int mInstallReason;
|
||||||
|
private final PackageManagerWrapper mPackageManager;
|
||||||
|
|
||||||
|
public InstalledAppCounter(Context context, int installReason,
|
||||||
|
PackageManagerWrapper packageManager) {
|
||||||
super(context, packageManager);
|
super(context, packageManager);
|
||||||
|
mInstallReason = installReason;
|
||||||
|
mPackageManager = packageManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean includeInCount(ApplicationInfo info) {
|
protected boolean includeInCount(ApplicationInfo info) {
|
||||||
|
final int userId = UserHandle.getUserId(info.uid);
|
||||||
|
if (mInstallReason != ApplicationFeatureProvider.IGNORE_INSTALL_REASON
|
||||||
|
&& mPackageManager.getInstallReason(info.packageName,
|
||||||
|
new UserHandle(userId)) != mInstallReason) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
|
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -40,7 +52,6 @@ public abstract class InstalledAppCounter extends AppCounter {
|
|||||||
Intent launchIntent = new Intent(Intent.ACTION_MAIN, null)
|
Intent launchIntent = new Intent(Intent.ACTION_MAIN, null)
|
||||||
.addCategory(Intent.CATEGORY_LAUNCHER)
|
.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||||
.setPackage(info.packageName);
|
.setPackage(info.packageName);
|
||||||
int userId = UserHandle.getUserId(info.uid);
|
|
||||||
List<ResolveInfo> intents = mPm.queryIntentActivitiesAsUser(
|
List<ResolveInfo> intents = mPm.queryIntentActivitiesAsUser(
|
||||||
launchIntent,
|
launchIntent,
|
||||||
PackageManager.GET_DISABLED_COMPONENTS
|
PackageManager.GET_DISABLED_COMPONENTS
|
||||||
|
@@ -1252,7 +1252,7 @@ public class ManageApplications extends InstrumentedPreferenceFragment
|
|||||||
@Override
|
@Override
|
||||||
public void setListening(boolean listening) {
|
public void setListening(boolean listening) {
|
||||||
if (listening) {
|
if (listening) {
|
||||||
new InstalledAppCounter(mContext,
|
new InstalledAppCounter(mContext, ApplicationFeatureProvider.IGNORE_INSTALL_REASON,
|
||||||
new PackageManagerWrapperImpl(mContext.getPackageManager())) {
|
new PackageManagerWrapperImpl(mContext.getPackageManager())) {
|
||||||
@Override
|
@Override
|
||||||
protected void onCountComplete(int num) {
|
protected void onCountComplete(int num) {
|
||||||
|
@@ -20,6 +20,7 @@ import android.content.Intent;
|
|||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -56,4 +57,12 @@ public interface PackageManagerWrapper {
|
|||||||
* @see android.content.pm.PackageManager#queryIntentActivitiesAsUser
|
* @see android.content.pm.PackageManager#queryIntentActivitiesAsUser
|
||||||
*/
|
*/
|
||||||
List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId);
|
List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls {@code PackageManager.getInstallReason()}.
|
||||||
|
*
|
||||||
|
* @see android.content.pm.PackageManager#getInstallReason
|
||||||
|
*/
|
||||||
|
int getInstallReason(String packageName, UserHandle user);
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,7 @@ import android.content.Intent;
|
|||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -50,4 +51,9 @@ public class PackageManagerWrapperImpl implements PackageManagerWrapper {
|
|||||||
public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId) {
|
public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId) {
|
||||||
return mPm.queryIntentActivitiesAsUser(intent, flags, userId);
|
return mPm.queryIntentActivitiesAsUser(intent, flags, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInstallReason(String packageName, UserHandle user) {
|
||||||
|
return mPm.getInstallReason(packageName, user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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.enterprise;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.applications.ApplicationFeatureProvider;
|
||||||
|
import com.android.settings.core.PreferenceController;
|
||||||
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
|
|
||||||
|
public class EnterpriseInstalledPackagesPreferenceController extends PreferenceController {
|
||||||
|
|
||||||
|
private static final String KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES
|
||||||
|
= "number_enterprise_installed_packages";
|
||||||
|
private final ApplicationFeatureProvider mFeatureProvider;
|
||||||
|
|
||||||
|
public EnterpriseInstalledPackagesPreferenceController(Context context) {
|
||||||
|
super(context);
|
||||||
|
mFeatureProvider = FeatureFactory.getFactory(context)
|
||||||
|
.getApplicationFeatureProvider(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateState(Preference preference) {
|
||||||
|
mFeatureProvider.calculateNumberOfInstalledApps(
|
||||||
|
PackageManager.INSTALL_REASON_POLICY,
|
||||||
|
(num) -> {
|
||||||
|
if (num == 0) {
|
||||||
|
preference.setVisible(false);
|
||||||
|
} else {
|
||||||
|
preference.setVisible(true);
|
||||||
|
preference.setTitle(mContext.getResources().getQuantityString(
|
||||||
|
R.plurals.enterprise_privacy_number_enterprise_installed_packages,
|
||||||
|
num, num));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAvailable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPreferenceKey() {
|
||||||
|
return KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES;
|
||||||
|
}
|
||||||
|
}
|
@@ -64,6 +64,7 @@ public class EnterprisePrivacySettings extends DashboardFragment {
|
|||||||
controllers.add(new AlwaysOnVpnPrimaryUserPreferenceController(context));
|
controllers.add(new AlwaysOnVpnPrimaryUserPreferenceController(context));
|
||||||
controllers.add(new AlwaysOnVpnManagedProfilePreferenceController(context));
|
controllers.add(new AlwaysOnVpnManagedProfilePreferenceController(context));
|
||||||
controllers.add(new GlobalHttpProxyPreferenceController(context));
|
controllers.add(new GlobalHttpProxyPreferenceController(context));
|
||||||
|
controllers.add(new EnterpriseInstalledPackagesPreferenceController(context));
|
||||||
return controllers;
|
return controllers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -36,12 +36,10 @@ public class InstalledPackagesPreferenceController extends PreferenceController
|
|||||||
@Override
|
@Override
|
||||||
public void updateState(Preference preference) {
|
public void updateState(Preference preference) {
|
||||||
mFeatureProvider.calculateNumberOfInstalledApps(
|
mFeatureProvider.calculateNumberOfInstalledApps(
|
||||||
new ApplicationFeatureProvider.NumberOfInstalledAppsCallback() {
|
ApplicationFeatureProvider.IGNORE_INSTALL_REASON,
|
||||||
@Override
|
(num) -> {
|
||||||
public void onNumberOfInstalledAppsResult(int num) {
|
preference.setTitle(mContext.getResources().getQuantityString(
|
||||||
preference.setTitle(mContext.getResources().getQuantityString(
|
R.plurals.enterprise_privacy_number_installed_packages, num, num));
|
||||||
R.plurals.enterprise_privacy_number_installed_packages, num, num));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -19,6 +19,7 @@ package com.android.settings.applications;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.UserInfo;
|
import android.content.pm.UserInfo;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
|
||||||
import com.android.settings.SettingsRobolectricTestRunner;
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
@@ -49,6 +50,9 @@ public final class ApplicationFeatureProviderImplTest {
|
|||||||
private final int MAIN_USER_ID = 0;
|
private final int MAIN_USER_ID = 0;
|
||||||
private final int MANAGED_PROFILE_ID = 10;
|
private final int MANAGED_PROFILE_ID = 10;
|
||||||
|
|
||||||
|
private final String APP_1 = "app1";
|
||||||
|
private final String APP_2 = "app2";
|
||||||
|
|
||||||
private @Mock UserManager mUserManager;
|
private @Mock UserManager mUserManager;
|
||||||
private @Mock Context mContext;
|
private @Mock Context mContext;
|
||||||
private @Mock PackageManagerWrapper mPackageManager;
|
private @Mock PackageManagerWrapper mPackageManager;
|
||||||
@@ -78,22 +82,32 @@ public final class ApplicationFeatureProviderImplTest {
|
|||||||
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
|
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
|
||||||
| PackageManager.MATCH_ANY_USER,
|
| PackageManager.MATCH_ANY_USER,
|
||||||
MAIN_USER_ID)).thenReturn(Arrays.asList(
|
MAIN_USER_ID)).thenReturn(Arrays.asList(
|
||||||
ApplicationTestUtils.buildInfo(MAIN_USER_ID, "app1", 0 /* flags */)));
|
ApplicationTestUtils.buildInfo(MAIN_USER_ID, APP_1, 0 /* flags */)));
|
||||||
|
when(mPackageManager.getInstallReason(APP_1, new UserHandle(MAIN_USER_ID)))
|
||||||
|
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
|
||||||
|
|
||||||
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
|
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
|
||||||
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
|
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
|
||||||
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
|
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
|
||||||
ApplicationTestUtils.buildInfo(MANAGED_PROFILE_ID, "app2", 0 /* flags */)));
|
ApplicationTestUtils.buildInfo(MANAGED_PROFILE_ID, APP_2, 0 /* flags */)));
|
||||||
|
when(mPackageManager.getInstallReason(APP_2, new UserHandle(MANAGED_PROFILE_ID)))
|
||||||
|
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
|
||||||
|
|
||||||
mProvider.calculateNumberOfInstalledApps(
|
// Count all installed apps.
|
||||||
new ApplicationFeatureProvider.NumberOfInstalledAppsCallback() {
|
mProvider.calculateNumberOfInstalledApps(ApplicationFeatureProvider.IGNORE_INSTALL_REASON,
|
||||||
@Override
|
(num) -> {
|
||||||
public void onNumberOfInstalledAppsResult(int num) {
|
numberOfInstalledApps[0] = num;
|
||||||
numberOfInstalledApps[0] = num;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
ShadowApplication.runBackgroundTasks();
|
ShadowApplication.runBackgroundTasks();
|
||||||
|
|
||||||
assertThat(numberOfInstalledApps[0]).isEqualTo(2);
|
assertThat(numberOfInstalledApps[0]).isEqualTo(2);
|
||||||
|
|
||||||
|
// Count apps with specific install reason only.
|
||||||
|
numberOfInstalledApps[0] = null;
|
||||||
|
mProvider.calculateNumberOfInstalledApps(PackageManager.INSTALL_REASON_POLICY,
|
||||||
|
(num) -> {
|
||||||
|
numberOfInstalledApps[0] = num;
|
||||||
|
});
|
||||||
|
ShadowApplication.runBackgroundTasks();
|
||||||
|
assertThat(numberOfInstalledApps[0]).isEqualTo(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,6 +22,7 @@ import android.content.pm.ApplicationInfo;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.content.pm.UserInfo;
|
import android.content.pm.UserInfo;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
|
||||||
import com.android.settings.SettingsRobolectricTestRunner;
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
@@ -60,6 +61,13 @@ import static org.mockito.Mockito.when;
|
|||||||
shadows = {ShadowUserManager.class})
|
shadows = {ShadowUserManager.class})
|
||||||
public final class InstalledAppCounterTest {
|
public final class InstalledAppCounterTest {
|
||||||
|
|
||||||
|
private final String APP_1 = "app1";
|
||||||
|
private final String APP_2 = "app2";
|
||||||
|
private final String APP_3 = "app3";
|
||||||
|
private final String APP_4 = "app4";
|
||||||
|
private final String APP_5 = "app5";
|
||||||
|
private final String APP_6 = "app6";
|
||||||
|
|
||||||
private final int MAIN_USER_ID = 0;
|
private final int MAIN_USER_ID = 0;
|
||||||
private final int MANAGED_PROFILE_ID = 10;
|
private final int MANAGED_PROFILE_ID = 10;
|
||||||
|
|
||||||
@@ -101,14 +109,25 @@ public final class InstalledAppCounterTest {
|
|||||||
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
|
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
|
||||||
| PackageManager.MATCH_ANY_USER,
|
| PackageManager.MATCH_ANY_USER,
|
||||||
MAIN_USER_ID)).thenReturn(Arrays.asList(
|
MAIN_USER_ID)).thenReturn(Arrays.asList(
|
||||||
buildInfo(MAIN_USER_ID, "app1", ApplicationInfo.FLAG_UPDATED_SYSTEM_APP),
|
buildInfo(MAIN_USER_ID, APP_1, ApplicationInfo.FLAG_UPDATED_SYSTEM_APP),
|
||||||
buildInfo(MAIN_USER_ID, "app2", 0 /* flags */),
|
buildInfo(MAIN_USER_ID, APP_2, 0 /* flags */),
|
||||||
buildInfo(MAIN_USER_ID, "app3", ApplicationInfo.FLAG_SYSTEM),
|
buildInfo(MAIN_USER_ID, APP_3, ApplicationInfo.FLAG_SYSTEM),
|
||||||
buildInfo(MAIN_USER_ID, "app4", ApplicationInfo.FLAG_SYSTEM)));
|
buildInfo(MAIN_USER_ID, APP_4, ApplicationInfo.FLAG_SYSTEM)));
|
||||||
// For system apps, InstalledAppCounter checks whether they handle the default launcher
|
// For system apps, InstalledAppCounter checks whether they handle the default launcher
|
||||||
// intent to decide whether to include them in the count of installed apps or not.
|
// intent to decide whether to include them in the count of installed apps or not.
|
||||||
expectQueryIntentActivities(MAIN_USER_ID, "app3", true /* launchable */);
|
expectQueryIntentActivities(MAIN_USER_ID, APP_3, true /* launchable */);
|
||||||
expectQueryIntentActivities(MAIN_USER_ID, "app4", false /* launchable */);
|
expectQueryIntentActivities(MAIN_USER_ID, APP_4, false /* launchable */);
|
||||||
|
|
||||||
|
// app1, app3 and app4 are installed by enterprise policy.
|
||||||
|
final UserHandle mainUser = new UserHandle(MAIN_USER_ID);
|
||||||
|
when(mPackageManager.getInstallReason(APP_1, mainUser))
|
||||||
|
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
|
||||||
|
when(mPackageManager.getInstallReason(APP_2, mainUser))
|
||||||
|
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
|
||||||
|
when(mPackageManager.getInstallReason(APP_3, mainUser))
|
||||||
|
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
|
||||||
|
when(mPackageManager.getInstallReason(APP_4, mainUser))
|
||||||
|
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
|
||||||
|
|
||||||
// The second user has four apps installed:
|
// The second user has four apps installed:
|
||||||
// * app5 is a user-installed app. It should be counted.
|
// * app5 is a user-installed app. It should be counted.
|
||||||
@@ -116,12 +135,21 @@ public final class InstalledAppCounterTest {
|
|||||||
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
|
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
|
||||||
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
|
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
|
||||||
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
|
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
|
||||||
buildInfo(MANAGED_PROFILE_ID, "app5", 0 /* flags */),
|
buildInfo(MANAGED_PROFILE_ID, APP_5, 0 /* flags */),
|
||||||
buildInfo(MANAGED_PROFILE_ID, "app6", ApplicationInfo.FLAG_SYSTEM)));
|
buildInfo(MANAGED_PROFILE_ID, APP_6, ApplicationInfo.FLAG_SYSTEM)));
|
||||||
expectQueryIntentActivities(MANAGED_PROFILE_ID, "app6", true /* launchable */);
|
expectQueryIntentActivities(MANAGED_PROFILE_ID, APP_6, true /* launchable */);
|
||||||
|
|
||||||
// Count the number of apps installed. Wait for the background task to finish.
|
// app5 is installed by enterprise policy.
|
||||||
(new InstalledAppCounterTestable()).execute();
|
final UserHandle managedProfileUser = new UserHandle(MANAGED_PROFILE_ID);
|
||||||
|
when(mPackageManager.getInstallReason(APP_5, managedProfileUser))
|
||||||
|
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
|
||||||
|
when(mPackageManager.getInstallReason(APP_6, managedProfileUser))
|
||||||
|
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
|
||||||
|
|
||||||
|
// Count the number of all apps installed, irrespective of install reason. Wait for the
|
||||||
|
// background task to finish.
|
||||||
|
(new InstalledAppCounterTestable(ApplicationFeatureProvider.IGNORE_INSTALL_REASON))
|
||||||
|
.execute();
|
||||||
ShadowApplication.runBackgroundTasks();
|
ShadowApplication.runBackgroundTasks();
|
||||||
|
|
||||||
assertThat(mInstalledAppCount).isEqualTo(5);
|
assertThat(mInstalledAppCount).isEqualTo(5);
|
||||||
@@ -134,11 +162,19 @@ public final class InstalledAppCounterTest {
|
|||||||
verify(mPackageManager, atLeast(0)).queryIntentActivitiesAsUser(anyObject(), anyInt(),
|
verify(mPackageManager, atLeast(0)).queryIntentActivitiesAsUser(anyObject(), anyInt(),
|
||||||
anyInt());
|
anyInt());
|
||||||
verifyNoMoreInteractions(mPackageManager);
|
verifyNoMoreInteractions(mPackageManager);
|
||||||
|
|
||||||
|
// Count once more, considering apps installed by enterprise policy only. Wait for the
|
||||||
|
// background task to finish.
|
||||||
|
mInstalledAppCount = 0;
|
||||||
|
(new InstalledAppCounterTestable(PackageManager.INSTALL_REASON_POLICY)).execute();
|
||||||
|
ShadowApplication.runBackgroundTasks();
|
||||||
|
|
||||||
|
assertThat(mInstalledAppCount).isEqualTo(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class InstalledAppCounterTestable extends InstalledAppCounter {
|
private class InstalledAppCounterTestable extends InstalledAppCounter {
|
||||||
public InstalledAppCounterTestable() {
|
public InstalledAppCounterTestable(int installReason) {
|
||||||
super(mContext, mPackageManager);
|
super(mContext, installReason, mPackageManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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.enterprise;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.applications.ApplicationFeatureProvider;
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static org.mockito.Mockito.anyObject;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.eq;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link EnterpriseInstalledPackagesPreferenceController}.
|
||||||
|
*/
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public final class EnterpriseInstalledPackagesPreferenceControllerTest {
|
||||||
|
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private Context mContext;
|
||||||
|
private FakeFeatureFactory mFeatureFactory;
|
||||||
|
|
||||||
|
private EnterpriseInstalledPackagesPreferenceController mController;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
FakeFeatureFactory.setupForTest(mContext);
|
||||||
|
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
|
||||||
|
mController = new EnterpriseInstalledPackagesPreferenceController(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setNumberOfEnterpriseInstalledPackages(int number) {
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
public Object answer(InvocationOnMock invocation) {
|
||||||
|
((ApplicationFeatureProvider.NumberOfInstalledAppsCallback)
|
||||||
|
invocation.getArguments()[1]).onNumberOfInstalledAppsResult(number);
|
||||||
|
return null;
|
||||||
|
}}).when(mFeatureFactory.applicationFeatureProvider)
|
||||||
|
.calculateNumberOfInstalledApps(eq(PackageManager.INSTALL_REASON_POLICY),
|
||||||
|
anyObject());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateState() {
|
||||||
|
final Preference preference = new Preference(mContext, null, 0, 0);
|
||||||
|
preference.setVisible(true);
|
||||||
|
|
||||||
|
setNumberOfEnterpriseInstalledPackages(20);
|
||||||
|
when(mContext.getResources().getQuantityString(
|
||||||
|
R.plurals.enterprise_privacy_number_enterprise_installed_packages, 20, 20))
|
||||||
|
.thenReturn("20 packages");
|
||||||
|
mController.updateState(preference);
|
||||||
|
assertThat(preference.getTitle()).isEqualTo("20 packages");
|
||||||
|
assertThat(preference.isVisible()).isTrue();
|
||||||
|
|
||||||
|
setNumberOfEnterpriseInstalledPackages(0);
|
||||||
|
mController.updateState(preference);
|
||||||
|
assertThat(preference.isVisible()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsAvailable() {
|
||||||
|
assertThat(mController.isAvailable()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHandlePreferenceTreeClick() {
|
||||||
|
assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
|
||||||
|
.isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetPreferenceKey() {
|
||||||
|
assertThat(mController.getPreferenceKey())
|
||||||
|
.isEqualTo("number_enterprise_installed_packages");
|
||||||
|
}
|
||||||
|
}
|
@@ -73,7 +73,7 @@ public final class EnterprisePrivacySettingsTest {
|
|||||||
final List<PreferenceController> controllers = mSettings.getPreferenceControllers(
|
final List<PreferenceController> controllers = mSettings.getPreferenceControllers(
|
||||||
ShadowApplication.getInstance().getApplicationContext());
|
ShadowApplication.getInstance().getApplicationContext());
|
||||||
assertThat(controllers).isNotNull();
|
assertThat(controllers).isNotNull();
|
||||||
assertThat(controllers.size()).isEqualTo(7);
|
assertThat(controllers.size()).isEqualTo(8);
|
||||||
assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class);
|
assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class);
|
||||||
assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class);
|
assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class);
|
||||||
assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class);
|
assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class);
|
||||||
@@ -83,6 +83,7 @@ public final class EnterprisePrivacySettingsTest {
|
|||||||
assertThat(controllers.get(5)).isInstanceOf(
|
assertThat(controllers.get(5)).isInstanceOf(
|
||||||
AlwaysOnVpnManagedProfilePreferenceController.class);
|
AlwaysOnVpnManagedProfilePreferenceController.class);
|
||||||
assertThat(controllers.get(6)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
|
assertThat(controllers.get(6)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
|
||||||
|
assertThat(controllers.get(7)).isInstanceOf(
|
||||||
|
EnterpriseInstalledPackagesPreferenceController.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -40,6 +40,7 @@ import org.robolectric.annotation.Config;
|
|||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.mockito.Mockito.anyObject;
|
import static org.mockito.Mockito.anyObject;
|
||||||
import static org.mockito.Mockito.doAnswer;
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,10 +70,11 @@ public final class InstalledPackagesPreferenceControllerTest {
|
|||||||
doAnswer(new Answer() {
|
doAnswer(new Answer() {
|
||||||
public Object answer(InvocationOnMock invocation) {
|
public Object answer(InvocationOnMock invocation) {
|
||||||
((ApplicationFeatureProvider.NumberOfInstalledAppsCallback)
|
((ApplicationFeatureProvider.NumberOfInstalledAppsCallback)
|
||||||
invocation.getArguments()[0]).onNumberOfInstalledAppsResult(20);
|
invocation.getArguments()[1]).onNumberOfInstalledAppsResult(20);
|
||||||
return null;
|
return null;
|
||||||
}}).when(mFeatureFactory.applicationFeatureProvider)
|
}}).when(mFeatureFactory.applicationFeatureProvider)
|
||||||
.calculateNumberOfInstalledApps(anyObject());
|
.calculateNumberOfInstalledApps(
|
||||||
|
eq(ApplicationFeatureProvider.IGNORE_INSTALL_REASON), anyObject());
|
||||||
when(mContext.getResources().getQuantityString(
|
when(mContext.getResources().getQuantityString(
|
||||||
R.plurals.enterprise_privacy_number_installed_packages, 20, 20))
|
R.plurals.enterprise_privacy_number_installed_packages, 20, 20))
|
||||||
.thenReturn("20 packages");
|
.thenReturn("20 packages");
|
||||||
|
Reference in New Issue
Block a user