Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 655be2a9 authored by Adam Connors's avatar Adam Connors
Browse files

Revert "Remove enableSystemApp methods."

We need to go back to uninstalling system apps so we can
re-install non-default system applications through Play.

This reverts commit e3dbcd138c07f2d32ac84229d0a49052cc18d424.

Change-Id: I0a7af094614c4a10800971c82e10571f7312e079
parent 1d935abd
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -5305,6 +5305,8 @@ package android.app.admin {
    method public void clearUserRestriction(android.content.ComponentName, java.lang.String);
    method public android.os.UserHandle createAndInitializeUser(android.content.ComponentName, java.lang.String, java.lang.String, android.content.ComponentName, android.os.Bundle);
    method public android.os.UserHandle createUser(android.content.ComponentName, java.lang.String);
    method public void enableSystemApp(android.content.ComponentName, java.lang.String);
    method public int enableSystemApp(android.content.ComponentName, android.content.Intent);
    method public java.lang.String[] getAccountTypesWithManagementDisabled();
    method public java.util.List<android.content.ComponentName> getActiveAdmins();
    method public android.os.Bundle getApplicationRestrictions(android.content.ComponentName, java.lang.String);
+39 −0
Original line number Diff line number Diff line
@@ -2523,6 +2523,45 @@ public class DevicePolicyManager {
        return false;
    }

    /**
     * Called by profile or device owner to re-enable a system app that was disabled by default
     * when the managed profile was created. This can only be called from a profile or device
     * owner running within a managed profile.
     *
     * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
     * @param packageName The package to be re-enabled in the current profile.
     */
    public void enableSystemApp(ComponentName admin, String packageName) {
        if (mService != null) {
            try {
                mService.enableSystemApp(admin, packageName);
            } catch (RemoteException e) {
                Log.w(TAG, "Failed to install package: " + packageName);
            }
        }
    }

    /**
     * Called by profile or device owner to re-enable system apps by intent that were disabled
     * by default when the managed profile was created. This can only be called from a profile
     * or device owner running within a managed profile.
     *
     * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
     * @param intent An intent matching the app(s) to be installed. All apps that resolve for this
     *               intent will be re-enabled in the current profile.
     * @return int The number of activities that matched the intent and were installed.
     */
    public int enableSystemApp(ComponentName admin, Intent intent) {
        if (mService != null) {
            try {
                return mService.enableSystemAppWithIntent(admin, intent);
            } catch (RemoteException e) {
                Log.w(TAG, "Failed to install packages matching filter: " + intent);
            }
        }
        return 0;
    }

    /**
     * Called by a profile owner to disable account management for a specific type of account.
     *
+3 −0
Original line number Diff line number Diff line
@@ -141,6 +141,9 @@ interface IDevicePolicyManager {
    boolean removeUser(in ComponentName who, in UserHandle userHandle);
    boolean switchUser(in ComponentName who, in UserHandle userHandle);

    void enableSystemApp(in ComponentName admin, in String packageName);
    int enableSystemAppWithIntent(in ComponentName admin, in Intent intent);

    void setAccountManagementDisabled(in ComponentName who, in String accountType, in boolean disabled);
    String[] getAccountTypesWithManagementDisabled();

+108 −0
Original line number Diff line number Diff line
@@ -3740,6 +3740,114 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        }
    }

    @Override
    public void enableSystemApp(ComponentName who, String packageName) {
        synchronized (this) {
            if (who == null) {
                throw new NullPointerException("ComponentName is null");
            }

            // This API can only be called by an active device admin,
            // so try to retrieve it to check that the caller is one.
            getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);

            int userId = UserHandle.getCallingUserId();
            long id = Binder.clearCallingIdentity();

            try {
                UserManager um = UserManager.get(mContext);
                if (!um.getUserInfo(userId).isManagedProfile()) {
                    throw new IllegalStateException(
                            "Only call this method from a managed profile.");
                }

                UserInfo primaryUser = um.getProfileParent(userId);

                if (DBG) {
                    Slog.v(LOG_TAG, "installing " + packageName + " for "
                            + userId);
                }

                IPackageManager pm = AppGlobals.getPackageManager();
                if (!isSystemApp(pm, packageName, primaryUser.id)) {
                    throw new IllegalArgumentException("Only system apps can be enabled this way.");
                }

                // Install the app.
                pm.installExistingPackageAsUser(packageName, userId);

            } catch (RemoteException re) {
                // shouldn't happen
                Slog.wtf(LOG_TAG, "Failed to install " + packageName, re);
            } finally {
                restoreCallingIdentity(id);
            }
        }
    }

    @Override
    public int enableSystemAppWithIntent(ComponentName who, Intent intent) {
        synchronized (this) {
            if (who == null) {
                throw new NullPointerException("ComponentName is null");
            }

            // This API can only be called by an active device admin,
            // so try to retrieve it to check that the caller is one.
            getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);

            int userId = UserHandle.getCallingUserId();
            long id = Binder.clearCallingIdentity();

            try {
                UserManager um = UserManager.get(mContext);
                if (!um.getUserInfo(userId).isManagedProfile()) {
                    throw new IllegalStateException(
                            "Only call this method from a managed profile.");
                }

                UserInfo primaryUser = um.getProfileParent(userId);

                IPackageManager pm = AppGlobals.getPackageManager();
                List<ResolveInfo> activitiesToEnable = pm.queryIntentActivities(intent,
                        intent.resolveTypeIfNeeded(mContext.getContentResolver()),
                        0, // no flags
                        primaryUser.id);

                if (DBG) Slog.d(LOG_TAG, "Enabling system activities: " + activitiesToEnable);
                int numberOfAppsInstalled = 0;
                if (activitiesToEnable != null) {
                    for (ResolveInfo info : activitiesToEnable) {
                        if (info.activityInfo != null) {

                            if (!isSystemApp(pm, info.activityInfo.packageName, primaryUser.id)) {
                                throw new IllegalArgumentException(
                                        "Only system apps can be enabled this way.");
                            }


                            numberOfAppsInstalled++;
                            pm.installExistingPackageAsUser(info.activityInfo.packageName, userId);
                        }
                    }
                }
                return numberOfAppsInstalled;
            } catch (RemoteException e) {
                // shouldn't happen
                Slog.wtf(LOG_TAG, "Failed to resolve intent for: " + intent);
                return 0;
            } finally {
                restoreCallingIdentity(id);
            }
        }
    }

    private boolean isSystemApp(IPackageManager pm, String packageName, int userId)
            throws RemoteException {
        ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0, userId);
        return (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0;
    }

    @Override
    public void setAccountManagementDisabled(ComponentName who, String accountType,
            boolean disabled) {