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

Commit 334c7e32 authored by Geoffrey Borggaard's avatar Geoffrey Borggaard
Browse files

Add a name to the DeviceOwner

We will need to display the name of the device owner (the human
readable name of the institution) to the user.
"This device is managed by google.com"

Change-Id: Ic33b6530c19cb14a118245692697205de3911a58
parent ce65961d
Loading
Loading
Loading
Loading
+30 −1
Original line number Diff line number Diff line
@@ -1527,9 +1527,26 @@ public class DevicePolicyManager {
     */
    public boolean setDeviceOwner(String packageName) throws IllegalArgumentException,
            IllegalStateException {
        return setDeviceOwner(packageName, null);
    }

    /**
     * @hide
     * Sets the given package as the device owner. The package must already be installed and there
     * shouldn't be an existing device owner registered, for this call to succeed. Also, this
     * method must be called before the device is provisioned.
     * @param packageName the package name of the application to be registered as the device owner.
     * @param ownerName the human readable name of the institution that owns this device.
     * @return whether the package was successfully registered as the device owner.
     * @throws IllegalArgumentException if the package name is null or invalid
     * @throws IllegalStateException if a device owner is already registered or the device has
     *         already been provisioned.
     */
    public boolean setDeviceOwner(String packageName, String ownerName)
            throws IllegalArgumentException, IllegalStateException {
        if (mService != null) {
            try {
                return mService.setDeviceOwner(packageName);
                return mService.setDeviceOwner(packageName, ownerName);
            } catch (RemoteException re) {
                Log.w(TAG, "Failed to set device owner");
            }
@@ -1581,4 +1598,16 @@ public class DevicePolicyManager {
        }
        return null;
    }

    /** @hide */
    public String getDeviceOwnerName() {
        if (mService != null) {
            try {
                return mService.getDeviceOwnerName();
            } catch (RemoteException re) {
                Log.w(TAG, "Failed to get device owner");
            }
        }
        return null;
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -98,7 +98,8 @@ interface IDevicePolicyManager {
    void reportFailedPasswordAttempt(int userHandle);
    void reportSuccessfulPasswordAttempt(int userHandle);

    boolean setDeviceOwner(String packageName);
    boolean setDeviceOwner(String packageName, String ownerName);
    boolean isDeviceOwner(String packageName);
    String getDeviceOwner();
    String getDeviceOwnerName();
}
+25 −3
Original line number Diff line number Diff line
@@ -2378,7 +2378,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
    }

    @Override
    public boolean setDeviceOwner(String packageName) {
    public boolean setDeviceOwner(String packageName, String ownerName) {
        if (packageName == null
                || !DeviceOwner.isInstalled(packageName, mContext.getPackageManager())) {
            throw new IllegalArgumentException("Invalid package name " + packageName
@@ -2386,7 +2386,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        }
        synchronized (this) {
            if (mDeviceOwner == null && !isDeviceProvisioned()) {
                mDeviceOwner = new DeviceOwner(packageName);
                mDeviceOwner = new DeviceOwner(packageName, ownerName);
                mDeviceOwner.writeOwnerFile();
                return true;
            } else {
@@ -2415,6 +2415,17 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        return null;
    }

    @Override
    public String getDeviceOwnerName() {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null);
        synchronized (this) {
            if (mDeviceOwner != null) {
                return mDeviceOwner.getName();
            }
        }
        return null;
    }

    private boolean isDeviceProvisioned() {
        return Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.DEVICE_PROVISIONED, 0) > 0;
@@ -2488,15 +2499,18 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
    static class DeviceOwner {
        private static final String DEVICE_OWNER_XML = "device_owner.xml";
        private static final String TAG_DEVICE_OWNER = "device-owner";
        private static final String ATTR_NAME = "name";
        private static final String ATTR_PACKAGE = "package";
        private String mPackageName;
        private String mOwnerName;

        DeviceOwner() {
            readOwnerFile();
        }

        DeviceOwner(String packageName) {
        DeviceOwner(String packageName, String ownerName) {
            this.mPackageName = packageName;
            this.mOwnerName = ownerName;
        }

        static boolean isRegistered() {
@@ -2508,6 +2522,10 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            return mPackageName;
        }

        String getName() {
            return mOwnerName;
        }

        static boolean isInstalled(String packageName, PackageManager pm) {
            try {
                PackageInfo pi;
@@ -2539,6 +2557,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                            "Device Owner file does not start with device-owner tag: found " + tag);
                }
                mPackageName = parser.getAttributeValue(null, ATTR_PACKAGE);
                mOwnerName = parser.getAttributeValue(null, ATTR_NAME);
                input.close();
            } catch (XmlPullParserException xppe) {
                Slog.e(TAG, "Error parsing device-owner file\n" + xppe);
@@ -2563,6 +2582,9 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                out.startDocument(null, true);
                out.startTag(null, TAG_DEVICE_OWNER);
                out.attribute(null, ATTR_PACKAGE, mPackageName);
                if (mOwnerName != null) {
                    out.attribute(null, ATTR_NAME, mOwnerName);
                }
                out.endTag(null, TAG_DEVICE_OWNER);
                out.endDocument();
                out.flush();