DO Disclosure: add UI that lists apps that were managed by owner:

- had permissions granted by admin
- were installed by owner via policy

Bug: 32692748
Test: m RunSettingsRoboTests
Change-Id: I365e2f8f351671e68f83cceb7c0ca241d7a5a588
Merged-In: I365e2f8f351671e68f83cceb7c0ca241d7a5a588
(cherry picked from commit 60b2960cbb)
This commit is contained in:
Denis Kuznetsov
2017-04-27 10:44:21 +02:00
parent b21808bd6b
commit 7db69b7a26
22 changed files with 1544 additions and 186 deletions

View File

@@ -0,0 +1,69 @@
/*
* 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.applications;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.AsyncTask;
import android.os.UserHandle;
import android.os.UserManager;
import java.util.ArrayList;
import java.util.List;
/**
* Lists apps for current user that fit some criteria specified by includeInCount method
* implementation.
* This class is similar to {@link AppCounter} class, but but builds actual list of apps instead
* of just counting them.
*/
public abstract class AppLister extends AsyncTask<Void, Void, List<UserAppInfo>> {
protected final PackageManagerWrapper mPm;
protected final UserManager mUm;
public AppLister(PackageManagerWrapper packageManager, UserManager userManager) {
mPm = packageManager;
mUm = userManager;
}
@Override
protected List<UserAppInfo> doInBackground(Void... params) {
final List<UserAppInfo> result = new ArrayList<>();
for (UserInfo user : mUm.getProfiles(UserHandle.myUserId())) {
final List<ApplicationInfo> list =
mPm.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| (user.isAdmin() ? PackageManager.MATCH_ANY_USER : 0),
user.id);
for (ApplicationInfo info : list) {
if (includeInCount(info)) {
result.add(new UserAppInfo(user, info));
}
}
}
return result;
}
@Override
protected void onPostExecute(List<UserAppInfo> list) {
onAppListBuilt(list);
}
protected abstract void onAppListBuilt(List<UserAppInfo> list);
protected abstract boolean includeInCount(ApplicationInfo info);
}

View File

@@ -31,7 +31,6 @@ import com.android.settings.enterprise.DevicePolicyManagerWrapper;
public abstract class AppWithAdminGrantedPermissionsCounter extends AppCounter {
private final String[] mPermissions;
private final PackageManagerWrapper mPackageManager;
private final IPackageManagerWrapper mPackageManagerService;
private final DevicePolicyManagerWrapper mDevicePolicyManager;
@@ -40,18 +39,24 @@ public abstract class AppWithAdminGrantedPermissionsCounter extends AppCounter {
DevicePolicyManagerWrapper devicePolicyManager) {
super(context, packageManager);
mPermissions = permissions;
mPackageManager = packageManager;
mPackageManagerService = packageManagerService;
mDevicePolicyManager = devicePolicyManager;
}
@Override
protected boolean includeInCount(ApplicationInfo info) {
return includeInCount(mPermissions, mDevicePolicyManager, mPm, mPackageManagerService,
info);
}
public static boolean includeInCount(String[] permissions,
DevicePolicyManagerWrapper devicePolicyManager, PackageManagerWrapper packageManager,
IPackageManagerWrapper packageManagerService, ApplicationInfo info) {
if (info.targetSdkVersion >= Build.VERSION_CODES.M) {
// The app uses run-time permissions. Check whether one or more of the permissions were
// granted by enterprise policy.
for (final String permission : mPermissions) {
if (mDevicePolicyManager.getPermissionGrantState(null /* admin */, info.packageName,
for (final String permission : permissions) {
if (devicePolicyManager.getPermissionGrantState(null /* admin */, info.packageName,
permission) == DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED) {
return true;
}
@@ -61,14 +66,14 @@ public abstract class AppWithAdminGrantedPermissionsCounter extends AppCounter {
// The app uses install-time permissions. Check whether the app requested one or more of the
// permissions and was installed by enterprise policy, implicitly granting permissions.
if (mPackageManager.getInstallReason(info.packageName,
if (packageManager.getInstallReason(info.packageName,
new UserHandle(UserHandle.getUserId(info.uid)))
!= PackageManager.INSTALL_REASON_POLICY) {
return false;
}
try {
for (final String permission : mPermissions) {
if (mPackageManagerService.checkUidPermission(permission, info.uid)
for (final String permission : permissions) {
if (packageManagerService.checkUidPermission(permission, info.uid)
== PackageManager.PERMISSION_GRANTED) {
return true;
}

View File

@@ -0,0 +1,46 @@
/*
* 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.applications;
import android.content.pm.ApplicationInfo;
import android.os.UserManager;
import com.android.settings.enterprise.DevicePolicyManagerWrapper;
/**
* Lists installed apps across all users that have been granted one or more specific permissions by
* the admin.
*/
public abstract class AppWithAdminGrantedPermissionsLister extends AppLister {
private final String[] mPermissions;
private final IPackageManagerWrapper mPackageManagerService;
private final DevicePolicyManagerWrapper mDevicePolicyManager;
public AppWithAdminGrantedPermissionsLister(String[] permissions,
PackageManagerWrapper packageManager, IPackageManagerWrapper packageManagerService,
DevicePolicyManagerWrapper devicePolicyManager, UserManager userManager) {
super(packageManager, userManager);
mPermissions = permissions;
mPackageManagerService = packageManagerService;
mDevicePolicyManager = devicePolicyManager;
}
@Override
protected boolean includeInCount(ApplicationInfo info) {
return AppWithAdminGrantedPermissionsCounter.includeInCount(mPermissions,
mDevicePolicyManager, mPm, mPackageManagerService, info);
}
}

View File

@@ -22,6 +22,7 @@ import android.app.Fragment;
import android.content.Intent;
import android.view.View;
import java.util.List;
import java.util.Set;
public interface ApplicationFeatureProvider {
@@ -48,6 +49,14 @@ public interface ApplicationFeatureProvider {
*/
void calculateNumberOfPolicyInstalledApps(boolean async, NumberOfAppsCallback callback);
/**
* Asynchronously builds the list of apps installed on the device via policy in the current user
* and all its managed profiles.
*
* @param callback The callback to invoke with the result
*/
void listPolicyInstalledApps(ListOfAppsCallback callback);
/**
* Asynchronously calculates the total number of apps installed in the current user and all its
* managed profiles that have been granted one or more of the given permissions by the admin.
@@ -60,6 +69,16 @@ public interface ApplicationFeatureProvider {
void calculateNumberOfAppsWithAdminGrantedPermissions(String[] permissions, boolean async,
NumberOfAppsCallback callback);
/**
* Asynchronously builds the list of apps installed in the current user and all its
* managed profiles that have been granted one or more of the given permissions by the admin.
*
* @param permissions Only consider apps that have been granted one or more of these permissions
* by the admin, either at run-time or install-time
* @param callback The callback to invoke with the result
*/
void listAppsWithAdminGrantedPermissions(String[] permissions, ListOfAppsCallback callback);
/**
* Return the persistent preferred activities configured by the admin for the current user and
* all its managed profiles. A persistent preferred activity is an activity that the admin
@@ -79,6 +98,13 @@ public interface ApplicationFeatureProvider {
void onNumberOfAppsResult(int num);
}
/**
* Callback that receives the list of packages installed on the device.
*/
interface ListOfAppsCallback {
void onListOfAppsResult(List<UserAppInfo> result);
}
public static class PersistentPreferredActivityInfo {
public final String packageName;
public final int userId;

View File

@@ -73,6 +73,13 @@ public class ApplicationFeatureProviderImpl implements ApplicationFeatureProvide
}
}
@Override
public void listPolicyInstalledApps(ListOfAppsCallback callback) {
final CurrentUserPolicyInstalledAppLister lister =
new CurrentUserPolicyInstalledAppLister(mPm, mUm, callback);
lister.execute();
}
@Override
public void calculateNumberOfAppsWithAdminGrantedPermissions(String[] permissions,
boolean async, NumberOfAppsCallback callback) {
@@ -86,6 +93,15 @@ public class ApplicationFeatureProviderImpl implements ApplicationFeatureProvide
}
}
@Override
public void listAppsWithAdminGrantedPermissions(String[] permissions,
ListOfAppsCallback callback) {
final CurrentUserAppWithAdminGrantedPermissionsLister lister =
new CurrentUserAppWithAdminGrantedPermissionsLister(permissions, mPm, mPms, mDpm,
mUm, callback);
lister.execute();
}
@Override
public Set<PersistentPreferredActivityInfo> findPersistentPreferredActivities(
Intent[] intents) {
@@ -152,4 +168,39 @@ public class ApplicationFeatureProviderImpl implements ApplicationFeatureProvide
mCallback.onNumberOfAppsResult(num);
}
}
private static class CurrentUserPolicyInstalledAppLister extends InstalledAppLister {
private ListOfAppsCallback mCallback;
CurrentUserPolicyInstalledAppLister(PackageManagerWrapper packageManager,
UserManager userManager, ListOfAppsCallback callback) {
super(packageManager, userManager);
mCallback = callback;
}
@Override
protected void onAppListBuilt(List<UserAppInfo> list) {
mCallback.onListOfAppsResult(list);
}
}
private static class CurrentUserAppWithAdminGrantedPermissionsLister extends
AppWithAdminGrantedPermissionsLister {
private ListOfAppsCallback mCallback;
CurrentUserAppWithAdminGrantedPermissionsLister(String[] permissions,
PackageManagerWrapper packageManager, IPackageManagerWrapper packageManagerService,
DevicePolicyManagerWrapper devicePolicyManager, UserManager userManager,
ListOfAppsCallback callback) {
super(permissions, packageManager, packageManagerService, devicePolicyManager,
userManager);
mCallback = callback;
}
@Override
protected void onAppListBuilt(List<UserAppInfo> list) {
mCallback.onListOfAppsResult(list);
}
}
}

View File

@@ -31,21 +31,24 @@ public abstract class InstalledAppCounter extends AppCounter {
public static final int IGNORE_INSTALL_REASON = -1;
private final int mInstallReason;
private final PackageManagerWrapper mPackageManager;
public InstalledAppCounter(Context context, int installReason,
PackageManagerWrapper packageManager) {
super(context, packageManager);
mInstallReason = installReason;
mPackageManager = packageManager;
}
@Override
protected boolean includeInCount(ApplicationInfo info) {
return includeInCount(mInstallReason, mPm, info);
}
public static boolean includeInCount(int installReason, PackageManagerWrapper pm,
ApplicationInfo info) {
final int userId = UserHandle.getUserId(info.uid);
if (mInstallReason != IGNORE_INSTALL_REASON
&& mPackageManager.getInstallReason(info.packageName,
new UserHandle(userId)) != mInstallReason) {
if (installReason != IGNORE_INSTALL_REASON
&& pm.getInstallReason(info.packageName,
new UserHandle(userId)) != installReason) {
return false;
}
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
@@ -57,7 +60,7 @@ public abstract class InstalledAppCounter extends AppCounter {
Intent launchIntent = new Intent(Intent.ACTION_MAIN, null)
.addCategory(Intent.CATEGORY_LAUNCHER)
.setPackage(info.packageName);
List<ResolveInfo> intents = mPm.queryIntentActivitiesAsUser(
List<ResolveInfo> intents = pm.queryIntentActivitiesAsUser(
launchIntent,
PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.MATCH_DIRECT_BOOT_AWARE

View File

@@ -0,0 +1,34 @@
/*
* 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.applications;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.UserManager;
public abstract class InstalledAppLister extends AppLister {
public InstalledAppLister(PackageManagerWrapper packageManager,
UserManager userManager) {
super(packageManager, userManager);
}
@Override
protected boolean includeInCount(ApplicationInfo info) {
return InstalledAppCounter.includeInCount(PackageManager.INSTALL_REASON_POLICY, mPm, info);
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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.applications;
import android.content.pm.ApplicationInfo;
import android.content.pm.UserInfo;
import android.text.TextUtils;
import java.util.Objects;
/**
* Simple class for bringing together information about application and user for which it was
* installed.
*/
public class UserAppInfo {
public final UserInfo userInfo;
public final ApplicationInfo appInfo;
public UserAppInfo(UserInfo mUserInfo, ApplicationInfo mAppInfo) {
this.userInfo = mUserInfo;
this.appInfo = mAppInfo;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
final UserAppInfo that = (UserAppInfo) other;
return that.userInfo.id == userInfo.id && TextUtils.equals(that.appInfo.packageName,
appInfo.packageName);
}
@Override
public int hashCode() {
return Objects.hash(userInfo.id, appInfo.packageName);
}
}

View File

@@ -16,7 +16,6 @@
package com.android.settings.enterprise;
import android.content.Context;
import android.content.Intent;
import android.support.v7.preference.Preference;
import com.android.settings.R;
@@ -32,6 +31,7 @@ public abstract class AdminGrantedPermissionsPreferenceControllerBase
private final String mPermissionGroup;
private final ApplicationFeatureProvider mFeatureProvider;
private final boolean mAsync;
private boolean mHasApps;
public AdminGrantedPermissionsPreferenceControllerBase(Context context, Lifecycle lifecycle,
boolean async, String[] permissions, String permissionGroup) {
@@ -41,6 +41,7 @@ public abstract class AdminGrantedPermissionsPreferenceControllerBase
mFeatureProvider = FeatureFactory.getFactory(context)
.getApplicationFeatureProvider(context);
mAsync = async;
mHasApps = false;
}
@Override
@@ -50,11 +51,13 @@ public abstract class AdminGrantedPermissionsPreferenceControllerBase
(num) -> {
if (num == 0) {
preference.setVisible(false);
mHasApps = false;
} else {
preference.setVisible(true);
preference.setSummary(mContext.getResources().getQuantityString(
R.plurals.enterprise_privacy_number_packages_lower_bound,
num, num));
mHasApps = true;
}
});
}
@@ -76,7 +79,8 @@ public abstract class AdminGrantedPermissionsPreferenceControllerBase
final Boolean[] haveAppsWithAdminGrantedPermissions = { null };
mFeatureProvider.calculateNumberOfAppsWithAdminGrantedPermissions(mPermissions,
false /* async */, (num) -> haveAppsWithAdminGrantedPermissions[0] = num > 0);
return haveAppsWithAdminGrantedPermissions[0];
mHasApps = haveAppsWithAdminGrantedPermissions[0];
return mHasApps;
}
@Override
@@ -84,9 +88,9 @@ public abstract class AdminGrantedPermissionsPreferenceControllerBase
if (!getPreferenceKey().equals(preference.getKey())) {
return false;
}
final Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS)
.putExtra(Intent.EXTRA_PERMISSION_NAME, mPermissionGroup);
mContext.startActivity(intent);
return true;
if (!mHasApps) {
return false;
}
return super.handlePreferenceTreeClick(preference);
}
}

View File

@@ -0,0 +1,110 @@
/*
* 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.Manifest;
import android.content.Context;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.applications.ApplicationFeatureProvider;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.overlay.FeatureFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Base fragment for displaying a list of applications on a device.
* Inner static classes are concrete implementations.
*/
public abstract class ApplicationListFragment extends DashboardFragment
implements ApplicationListPreferenceController.ApplicationListBuilder {
static final String TAG = "EnterprisePrivacySettings";
@Override
public int getMetricsCategory() {
return MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.app_list_disclosure_settings;
}
@Override
protected List<PreferenceController> getPreferenceControllers(Context context) {
final List controllers = new ArrayList<>();
ApplicationListPreferenceController controller = new ApplicationListPreferenceController(
context, this, context.getPackageManager(), this);
controllers.add(controller);
return controllers;
}
private abstract static class AdminGrantedPermission extends ApplicationListFragment {
private final String[] mPermissions;
public AdminGrantedPermission(String[] permissions) {
mPermissions = permissions;
}
@Override
public void buildApplicationList(Context context,
ApplicationFeatureProvider.ListOfAppsCallback callback) {
FeatureFactory.getFactory(context).getApplicationFeatureProvider(context)
.listAppsWithAdminGrantedPermissions(mPermissions, callback);
}
}
public static class AdminGrantedPermissionCamera extends AdminGrantedPermission {
public AdminGrantedPermissionCamera() {
super(new String[] {Manifest.permission.CAMERA});
}
}
public static class AdminGrantedPermissionLocation extends AdminGrantedPermission {
public AdminGrantedPermissionLocation() {
super(new String[] {Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION});
}
}
public static class AdminGrantedPermissionMicrophone extends AdminGrantedPermission {
public AdminGrantedPermissionMicrophone() {
super(new String[] {Manifest.permission.RECORD_AUDIO});
}
}
public static class EnterpriseInstalledPackages extends ApplicationListFragment {
public EnterpriseInstalledPackages() {
}
@Override
public void buildApplicationList(Context context,
ApplicationFeatureProvider.ListOfAppsCallback callback) {
FeatureFactory.getFactory(context).getApplicationFeatureProvider(context).
listPolicyInstalledApps(callback);
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.applications.ApplicationFeatureProvider;
import com.android.settings.applications.UserAppInfo;
import com.android.settings.core.PreferenceController;
import java.util.List;
/**
* PreferenceController that builds a dynamic list of applications provided by
* {@link ApplicationListBuilder} instance.
*/
public class ApplicationListPreferenceController extends PreferenceController
implements ApplicationFeatureProvider.ListOfAppsCallback {
private final PackageManager mPm;
private SettingsPreferenceFragment mParent;
public ApplicationListPreferenceController(Context context, ApplicationListBuilder builder,
PackageManager packageManager, SettingsPreferenceFragment parent) {
super(context);
mPm = packageManager;
mParent = parent;
builder.buildApplicationList(context, this);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return null;
}
@Override
public void onListOfAppsResult(List<UserAppInfo> result) {
final PreferenceScreen screen = mParent.getPreferenceScreen();
if (screen == null) {
return;
}
final Context prefContext = mParent.getPreferenceManager().getContext();
for (int position = 0; position < result.size(); position++) {
final UserAppInfo item = result.get(position);
final Preference preference = new Preference(prefContext);
preference.setLayoutResource(R.layout.preference_app);
preference.setTitle(item.appInfo.loadLabel(mPm));
preference.setIcon(item.appInfo.loadIcon(mPm));
preference.setOrder(position);
preference.setSelectable(false);
screen.addPreference(preference);
}
}
/**
* Simple interface for building application list within {
* @link ApplicationListPreferenceController}
*/
public interface ApplicationListBuilder {
void buildApplicationList(Context context,
ApplicationFeatureProvider.ListOfAppsCallback callback);
}
}