FileDocCategorySizeDatePackage
DeviceOwner.javaAPI DocAndroid 5.1 API12702Thu Mar 12 22:22:42 GMT 2015com.android.server.devicepolicy

DeviceOwner

public class DeviceOwner extends Object
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.

Fields Summary
private static final String
TAG
private static final String
DEVICE_OWNER_XML
private static final String
TAG_DEVICE_OWNER
private static final String
TAG_PROFILE_OWNER
private static final String
ATTR_NAME
private static final String
ATTR_PACKAGE
private static final String
ATTR_COMPONENT_NAME
private static final String
ATTR_USERID
private android.util.AtomicFile
fileForWriting
private InputStream
mInputStreamForTest
private OutputStream
mOutputStreamForTest
private OwnerInfo
mDeviceOwner
private final HashMap
mProfileOwners
Constructors Summary
private DeviceOwner()


    // Private default constructor.
      
    
DeviceOwner(InputStream in, OutputStream out)

        mInputStreamForTest = in;
        mOutputStreamForTest = out;
    
Methods Summary
voidclearDeviceOwner()

        mDeviceOwner = null;
    
static com.android.server.devicepolicy.DeviceOwnercreateWithDeviceOwner(java.lang.String packageName, java.lang.String ownerName)
Creates an instance of the device owner object with the device owner set.

        DeviceOwner owner = new DeviceOwner();
        owner.mDeviceOwner = new OwnerInfo(ownerName, packageName);
        return owner;
    
static com.android.server.devicepolicy.DeviceOwnercreateWithProfileOwner(java.lang.String packageName, java.lang.String ownerName, int userId)

deprecated
Use a component name instead of package name Creates an instance of the device owner object with the profile owner set.

        DeviceOwner owner = new DeviceOwner();
        owner.mProfileOwners.put(userId, new OwnerInfo(ownerName, packageName));
        return owner;
    
static com.android.server.devicepolicy.DeviceOwnercreateWithProfileOwner(android.content.ComponentName admin, java.lang.String ownerName, int userId)
Creates an instance of the device owner object with the profile owner set.

        DeviceOwner owner = new DeviceOwner();
        owner.mProfileOwners.put(userId, new OwnerInfo(ownerName, admin));
        return owner;
    
private voidfinishWrite(java.io.OutputStream stream)

        if (fileForWriting != null) {
            fileForWriting.finishWrite((FileOutputStream) stream);
        }
    
java.lang.StringgetDeviceOwnerName()

        return mDeviceOwner != null ? mDeviceOwner.name : null;
    
java.lang.StringgetDeviceOwnerPackageName()

        return mDeviceOwner != null ? mDeviceOwner.packageName : null;
    
android.content.ComponentNamegetProfileOwnerComponent(int userId)

        OwnerInfo profileOwner = mProfileOwners.get(userId);
        return profileOwner != null ? profileOwner.admin : null;
    
java.util.SetgetProfileOwnerKeys()

        return mProfileOwners.keySet();
    
java.lang.StringgetProfileOwnerName(int userId)

        OwnerInfo profileOwner = mProfileOwners.get(userId);
        return profileOwner != null ? profileOwner.name : null;
    
java.lang.StringgetProfileOwnerPackageName(int userId)

deprecated
Use getProfileOwnerComponent
param
userId
return

        OwnerInfo profileOwner = mProfileOwners.get(userId);
        return profileOwner != null ? profileOwner.packageName : null;
    
booleanhasDeviceOwner()

        return mDeviceOwner != null;
    
static booleanisInstalled(java.lang.String packageName, android.content.pm.PackageManager pm)

        try {
            PackageInfo pi;
            if ((pi = pm.getPackageInfo(packageName, 0)) != null) {
                if ((pi.applicationInfo.flags) != 0) {
                    return true;
                }
            }
        } catch (NameNotFoundException nnfe) {
            Slog.w(TAG, "Device Owner package " + packageName + " not installed.");
        }
        return false;
    
static booleanisInstalledForUser(java.lang.String packageName, int userHandle)

        try {
            PackageInfo pi = (AppGlobals.getPackageManager())
                    .getPackageInfo(packageName, 0, userHandle);
            if (pi != null && pi.applicationInfo.flags != 0) {
                return true;
            }
        } catch (RemoteException re) {
            throw new RuntimeException("Package manager has died", re);
        }

        return false;
    
static com.android.server.devicepolicy.DeviceOwnerload()
Loads the device owner state from disk.

        DeviceOwner owner = new DeviceOwner();
        if (new File(Environment.getSystemSecureDirectory(), DEVICE_OWNER_XML).exists()) {
            owner.readOwnerFile();
            return owner;
        } else {
            return null;
        }
    
private java.io.InputStreamopenRead()

        if (mInputStreamForTest != null) {
            return mInputStreamForTest;
        }

        return new AtomicFile(new File(Environment.getSystemSecureDirectory(),
                DEVICE_OWNER_XML)).openRead();
    
voidreadOwnerFile()

        try {
            InputStream input = openRead();
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(input, null);
            int type;
            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT) {
                if (type!=XmlPullParser.START_TAG) {
                    continue;
                }

                String tag = parser.getName();
                if (tag.equals(TAG_DEVICE_OWNER)) {
                    String name = parser.getAttributeValue(null, ATTR_NAME);
                    String packageName = parser.getAttributeValue(null, ATTR_PACKAGE);
                    mDeviceOwner = new OwnerInfo(name, packageName);
                } else if (tag.equals(TAG_PROFILE_OWNER)) {
                    String profileOwnerPackageName = parser.getAttributeValue(null, ATTR_PACKAGE);
                    String profileOwnerName = parser.getAttributeValue(null, ATTR_NAME);
                    String profileOwnerComponentStr =
                            parser.getAttributeValue(null, ATTR_COMPONENT_NAME);
                    int userId = Integer.parseInt(parser.getAttributeValue(null, ATTR_USERID));
                    OwnerInfo profileOwnerInfo = null;
                    if (profileOwnerComponentStr != null) {
                        ComponentName admin = ComponentName.unflattenFromString(
                                profileOwnerComponentStr);
                        if (admin != null) {
                            profileOwnerInfo = new OwnerInfo(profileOwnerName, admin);
                        } else {
                            // This shouldn't happen but switch from package name -> component name
                            // might have written bad device owner files. b/17652534
                            Slog.e(TAG, "Error parsing device-owner file. Bad component name " +
                                    profileOwnerComponentStr);
                        }
                    }
                    if (profileOwnerInfo == null) {
                        profileOwnerInfo = new OwnerInfo(profileOwnerName, profileOwnerPackageName);
                    }
                    mProfileOwners.put(userId, profileOwnerInfo);
                } else {
                    throw new XmlPullParserException(
                            "Unexpected tag in device owner file: " + tag);
                }
            }
            input.close();
        } catch (XmlPullParserException xppe) {
            Slog.e(TAG, "Error parsing device-owner file\n" + xppe);
        } catch (IOException ioe) {
            Slog.e(TAG, "IO Exception when reading device-owner file\n" + ioe);
        }
    
voidremoveProfileOwner(int userId)

        mProfileOwners.remove(userId);
    
voidsetDeviceOwner(java.lang.String packageName, java.lang.String ownerName)

        mDeviceOwner = new OwnerInfo(ownerName, packageName);
    
voidsetProfileOwner(java.lang.String packageName, java.lang.String ownerName, int userId)

deprecated

        mProfileOwners.put(userId, new OwnerInfo(ownerName, packageName));
    
voidsetProfileOwner(android.content.ComponentName admin, java.lang.String ownerName, int userId)

        mProfileOwners.put(userId, new OwnerInfo(ownerName, admin));
    
private java.io.OutputStreamstartWrite()

        if (mOutputStreamForTest != null) {
            return mOutputStreamForTest;
        }

        fileForWriting = new AtomicFile(new File(Environment.getSystemSecureDirectory(),
                DEVICE_OWNER_XML));
        return fileForWriting.startWrite();
    
voidwriteOwnerFile()

        synchronized (this) {
            writeOwnerFileLocked();
        }
    
private voidwriteOwnerFileLocked()

        try {
            OutputStream outputStream = startWrite();
            XmlSerializer out = new FastXmlSerializer();
            out.setOutput(outputStream, "utf-8");
            out.startDocument(null, true);

            // Write device owner tag
            if (mDeviceOwner != null) {
                out.startTag(null, TAG_DEVICE_OWNER);
                out.attribute(null, ATTR_PACKAGE, mDeviceOwner.packageName);
                if (mDeviceOwner.name != null) {
                    out.attribute(null, ATTR_NAME, mDeviceOwner.name);
                }
                out.endTag(null, TAG_DEVICE_OWNER);
            }

            // Write profile owner tags
            if (mProfileOwners.size() > 0) {
                for (HashMap.Entry<Integer, OwnerInfo> owner : mProfileOwners.entrySet()) {
                    out.startTag(null, TAG_PROFILE_OWNER);
                    OwnerInfo ownerInfo = owner.getValue();
                    out.attribute(null, ATTR_PACKAGE, ownerInfo.packageName);
                    out.attribute(null, ATTR_NAME, ownerInfo.name);
                    out.attribute(null, ATTR_USERID, Integer.toString(owner.getKey()));
                    if (ownerInfo.admin != null) {
                        out.attribute(null, ATTR_COMPONENT_NAME, ownerInfo.admin.flattenToString());
                    }
                    out.endTag(null, TAG_PROFILE_OWNER);
                }
            }
            out.endDocument();
            out.flush();
            finishWrite(outputStream);
        } catch (IOException ioe) {
            Slog.e(TAG, "IO Exception when writing device-owner file\n" + ioe);
        }