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

Commit 8add5722 authored by Esteban Talavera's avatar Esteban Talavera
Browse files

Remove deprecated version of setProfileOwner that takes packageName

The ComponentName equivalent should be used instead.

Bug: 17654371
Change-Id: I7001e86ab1709b824944148a3c44af5243dacb83
parent 898de6fd
Loading
Loading
Loading
Loading
+0 −21
Original line number Diff line number Diff line
@@ -2422,27 +2422,6 @@ public class DevicePolicyManager {
        return true;
    }

    /**
     * @deprecated Use setProfileOwner(ComponentName ...)
     * @hide
     * Sets the given package as the profile owner of the given user profile. The package must
     * already be installed and there shouldn't be an existing profile owner registered for this
     * user. Also, this method must be called before the user has been used for the first time.
     * @param packageName the package name of the application to be registered as profile owner.
     * @param ownerName the human readable name of the organisation associated with this DPM.
     * @param userHandle the userId to set the profile owner for.
     * @return whether the package was successfully registered as the profile owner.
     * @throws IllegalArgumentException if packageName is null, the package isn't installed, or
     *         the user has already been set up.
     */
    public boolean setProfileOwner(String packageName, String ownerName, int userHandle)
            throws IllegalArgumentException {
        if (packageName == null) {
            throw new NullPointerException("packageName cannot be null");
        }
        return setProfileOwner(new ComponentName(packageName, ""), ownerName, userHandle);
    }

    /**
     * @hide
     * Sets the given component as the profile owner of the given user profile. The package must
+7 −32
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ import java.util.Set;
 * Stores and restores state for the Device and Profile owners. By definition there can be
 * only one device owner, but there may be a profile owner for each user.
 */
public class DeviceOwner {
class DeviceOwner {
    private static final String TAG = "DevicePolicyManagerService";

    private static final String DEVICE_OWNER_XML = "device_owner.xml";
@@ -101,16 +101,6 @@ public class DeviceOwner {
        return owner;
    }

    /**
     * @deprecated Use a component name instead of package name
     * Creates an instance of the device owner object with the profile owner set.
     */
    static DeviceOwner createWithProfileOwner(String packageName, String ownerName, int userId) {
        DeviceOwner owner = new DeviceOwner();
        owner.mProfileOwners.put(userId, new OwnerInfo(ownerName, packageName));
        return owner;
    }

    /**
     * Creates an instance of the device owner object with the profile owner set.
     */
@@ -136,13 +126,6 @@ public class DeviceOwner {
        mDeviceOwner = null;
    }

    /**
     * @deprecated
     */
    void setProfileOwner(String packageName, String ownerName, int userId) {
        mProfileOwners.put(userId, new OwnerInfo(ownerName, packageName));
    }

    void setProfileOwner(ComponentName admin, String ownerName, int userId) {
        mProfileOwners.put(userId, new OwnerInfo(ownerName, admin));
    }
@@ -151,16 +134,6 @@ public class DeviceOwner {
        mProfileOwners.remove(userId);
    }

    /**
     * @deprecated Use getProfileOwnerComponent
     * @param userId
     * @return
     */
    String getProfileOwnerPackageName(int userId) {
        OwnerInfo profileOwner = mProfileOwners.get(userId);
        return profileOwner != null ? profileOwner.packageName : null;
    }

    ComponentName getProfileOwnerComponent(int userId) {
        OwnerInfo profileOwner = mProfileOwners.get(userId);
        return profileOwner != null ? profileOwner.admin : null;
@@ -207,6 +180,7 @@ public class DeviceOwner {
        return false;
    }

    @VisibleForTesting
    void readOwnerFile() {
        try {
            InputStream input = openRead();
@@ -259,6 +233,7 @@ public class DeviceOwner {
        }
    }

    @VisibleForTesting
    void writeOwnerFile() {
        synchronized (this) {
            writeOwnerFileLocked();
@@ -329,10 +304,10 @@ public class DeviceOwner {
        }
    }

    static class OwnerInfo {
        public String name;
        public String packageName;
        public ComponentName admin;
    private static class OwnerInfo {
        public final String name;
        public final String packageName;
        public final ComponentName admin;

        public OwnerInfo(String name, String packageName) {
            this.name = name;
+2 −2
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ public class ApplicationRestrictionsTest extends AndroidTestCase {
        sDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        Settings.Secure.putInt(context.getContentResolver(),
                Settings.Secure.USER_SETUP_COMPLETE, 0);
        sDpm.setProfileOwner(context.getPackageName(), "Test", UserHandle.myUserId());
        sDpm.setProfileOwner(sAdminReceiver, "Test", UserHandle.myUserId());
        Settings.Secure.putInt(context.getContentResolver(),
                Settings.Secure.USER_SETUP_COMPLETE, 1);
        // Remove the admin if already registered. It's async, so add it back
+21 −9
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.devicepolicy;

import android.content.ComponentName;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

@@ -32,7 +33,7 @@ import java.io.ByteArrayOutputStream;
public class DeviceOwnerTest extends AndroidTestCase {

    private ByteArrayInputStream mInputStreamForTest;
    private ByteArrayOutputStream mOutputStreamForTest = new ByteArrayOutputStream();
    private final ByteArrayOutputStream mOutputStreamForTest = new ByteArrayOutputStream();

    @SmallTest
    public void testDeviceOwnerOnly() throws Exception {
@@ -46,13 +47,15 @@ public class DeviceOwnerTest extends AndroidTestCase {

        assertEquals("some.device.owner.package", in.getDeviceOwnerPackageName());
        assertEquals("owner", in.getDeviceOwnerName());
        assertNull(in.getProfileOwnerPackageName(1));
        assertNull(in.getProfileOwnerComponent(1));
    }

    @SmallTest
    public void testProfileOwnerOnly() throws Exception {
        DeviceOwner out = new DeviceOwner(null, mOutputStreamForTest);
        out.setProfileOwner("some.profile.owner.package", "some-company", 1);
        ComponentName admin = new ComponentName(
            "some.profile.owner.package", "some.profile.owner.package.Class");
        out.setProfileOwner(admin, "some-company", 1);
        out.writeOwnerFile();

        mInputStreamForTest = new ByteArrayInputStream(mOutputStreamForTest.toByteArray());
@@ -61,16 +64,24 @@ public class DeviceOwnerTest extends AndroidTestCase {

        assertNull(in.getDeviceOwnerPackageName());
        assertNull(in.getDeviceOwnerName());
        assertEquals("some.profile.owner.package", in.getProfileOwnerPackageName(1));
        assertEquals(admin, in.getProfileOwnerComponent(1));
        assertEquals("some-company", in.getProfileOwnerName(1));
    }

    @SmallTest
    public void testDeviceAndProfileOwners() throws Exception {
        DeviceOwner out = new DeviceOwner(null, mOutputStreamForTest);
        ComponentName profileAdmin = new ComponentName(
            "some.profile.owner.package", "some.profile.owner.package.Class");
        ComponentName otherProfileAdmin = new ComponentName(
            "some.other.profile.owner", "some.other.profile.owner.OtherClass");
        // Old code used package name rather than component name, so the class
        // bit could be empty.
        ComponentName legacyComponentName = new ComponentName("legacy.profile.owner.package", "");
        out.setDeviceOwner("some.device.owner.package", "owner");
        out.setProfileOwner("some.profile.owner.package", "some-company", 1);
        out.setProfileOwner("some.other.profile.owner", "some-other-company", 2);
        out.setProfileOwner(profileAdmin, "some-company", 1);
        out.setProfileOwner(otherProfileAdmin, "some-other-company", 2);
        out.setProfileOwner(legacyComponentName, "legacy-company", 3);
        out.writeOwnerFile();

        mInputStreamForTest = new ByteArrayInputStream(mOutputStreamForTest.toByteArray());
@@ -80,9 +91,10 @@ public class DeviceOwnerTest extends AndroidTestCase {

        assertEquals("some.device.owner.package", in.getDeviceOwnerPackageName());
        assertEquals("owner", in.getDeviceOwnerName());
        assertEquals("some.profile.owner.package", in.getProfileOwnerPackageName(1));
        assertEquals(profileAdmin, in.getProfileOwnerComponent(1));
        assertEquals("some-company", in.getProfileOwnerName(1));
        assertEquals("some.other.profile.owner", in.getProfileOwnerPackageName(2));
        assertEquals(otherProfileAdmin, in.getProfileOwnerComponent(2));
        assertEquals("some-other-company", in.getProfileOwnerName(2));
        assertEquals(legacyComponentName, in.getProfileOwnerComponent(3));
    }
}