Search in sources :

Example 1 with AtomicFile

use of com.lody.virtual.helper.utils.AtomicFile in project VirtualApp by asLody.

the class VUserManagerService method writeUserListLocked.

/*
     * Writes the user list file in this format:
     *
     * <users nextSerialNumber="3">
     *   <user id="0"></user>
     *   <user id="2"></user>
     * </users>
     */
private void writeUserListLocked() {
    FileOutputStream fos = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fos = userListFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        // XmlSerializer serializer = XmlUtils.serializerInstance();
        final XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(bos, "utf-8");
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, TAG_USERS);
        serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
        serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
        for (int i = 0; i < mUsers.size(); i++) {
            VUserInfo user = mUsers.valueAt(i);
            serializer.startTag(null, TAG_USER);
            serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
            serializer.endTag(null, TAG_USER);
        }
        serializer.endTag(null, TAG_USERS);
        serializer.endDocument();
        userListFile.finishWrite(fos);
    } catch (Exception e) {
        userListFile.failWrite(fos);
        VLog.e(LOG_TAG, "Error writing user list");
    }
}
Also used : FastXmlSerializer(com.lody.virtual.helper.utils.FastXmlSerializer) AtomicFile(com.lody.virtual.helper.utils.AtomicFile) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) FastXmlSerializer(com.lody.virtual.helper.utils.FastXmlSerializer) XmlSerializer(org.xmlpull.v1.XmlSerializer) VUserInfo(com.lody.virtual.os.VUserInfo)

Example 2 with AtomicFile

use of com.lody.virtual.helper.utils.AtomicFile in project VirtualApp by asLody.

the class VUserManagerService method removeUserStateLocked.

private void removeUserStateLocked(int userHandle) {
    // Cleanup package manager settings
    mPm.cleanUpUser(userHandle);
    // Remove this user from the list
    mUsers.remove(userHandle);
    mRemovingUserIds.remove(userHandle);
    // Remove user file
    AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml"));
    userFile.delete();
    // Update the user list
    writeUserListLocked();
    updateUserIdsLocked();
    removeDirectoryRecursive(VEnvironment.getUserSystemDirectory(userHandle));
}
Also used : AtomicFile(com.lody.virtual.helper.utils.AtomicFile) AtomicFile(com.lody.virtual.helper.utils.AtomicFile) File(java.io.File)

Example 3 with AtomicFile

use of com.lody.virtual.helper.utils.AtomicFile in project VirtualApp by asLody.

the class VUserManagerService method readUserListLocked.

private void readUserListLocked() {
    mGuestEnabled = false;
    if (!mUserListFile.exists()) {
        fallbackToSingleUserLocked();
        return;
    }
    FileInputStream fis = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fis = userListFile.openRead();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, null);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            ;
        }
        if (type != XmlPullParser.START_TAG) {
            VLog.e(LOG_TAG, "Unable to read user list");
            fallbackToSingleUserLocked();
            return;
        }
        mNextSerialNumber = -1;
        if (parser.getName().equals(TAG_USERS)) {
            String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
            if (lastSerialNumber != null) {
                mNextSerialNumber = Integer.parseInt(lastSerialNumber);
            }
            String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
            if (versionNumber != null) {
                mUserVersion = Integer.parseInt(versionNumber);
            }
        }
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
                String id = parser.getAttributeValue(null, ATTR_ID);
                VUserInfo user = readUser(Integer.parseInt(id));
                if (user != null) {
                    mUsers.put(user.id, user);
                    if (user.isGuest()) {
                        mGuestEnabled = true;
                    }
                    if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
                        mNextSerialNumber = user.id + 1;
                    }
                }
            }
        }
        updateUserIdsLocked();
        upgradeIfNecessary();
    } catch (IOException ioe) {
        fallbackToSingleUserLocked();
    } catch (XmlPullParserException pe) {
        fallbackToSingleUserLocked();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : AtomicFile(com.lody.virtual.helper.utils.AtomicFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) VUserInfo(com.lody.virtual.os.VUserInfo)

Example 4 with AtomicFile

use of com.lody.virtual.helper.utils.AtomicFile in project VirtualApp by asLody.

the class VUserManagerService method writeUserLocked.

/*
     * Writes the user file in this format:
     *
     * <user flags="20039023" id="0">
     *   <name>Primary</name>
     * </user>
     */
private void writeUserLocked(VUserInfo userInfo) {
    FileOutputStream fos = null;
    AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml"));
    try {
        fos = userFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        // XmlSerializer serializer = XmlUtils.serializerInstance();
        final XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(bos, "utf-8");
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, TAG_USER);
        serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
        serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
        serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
        serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
        serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME, Long.toString(userInfo.lastLoggedInTime));
        if (userInfo.iconPath != null) {
            serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
        }
        if (userInfo.partial) {
            serializer.attribute(null, ATTR_PARTIAL, "true");
        }
        serializer.startTag(null, TAG_NAME);
        serializer.text(userInfo.name);
        serializer.endTag(null, TAG_NAME);
        serializer.endTag(null, TAG_USER);
        serializer.endDocument();
        userFile.finishWrite(fos);
    } catch (Exception ioe) {
        VLog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
        userFile.failWrite(fos);
    }
}
Also used : FastXmlSerializer(com.lody.virtual.helper.utils.FastXmlSerializer) AtomicFile(com.lody.virtual.helper.utils.AtomicFile) FileOutputStream(java.io.FileOutputStream) AtomicFile(com.lody.virtual.helper.utils.AtomicFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) FastXmlSerializer(com.lody.virtual.helper.utils.FastXmlSerializer) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 5 with AtomicFile

use of com.lody.virtual.helper.utils.AtomicFile in project VirtualApp by asLody.

the class VUserManagerService method readUser.

private VUserInfo readUser(int id) {
    int flags = 0;
    int serialNumber = id;
    String name = null;
    String iconPath = null;
    long creationTime = 0L;
    long lastLoggedInTime = 0L;
    boolean partial = false;
    FileInputStream fis = null;
    try {
        AtomicFile userFile = new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml"));
        fis = userFile.openRead();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, null);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            ;
        }
        if (type != XmlPullParser.START_TAG) {
            VLog.e(LOG_TAG, "Unable to read user " + id);
            return null;
        }
        if (parser.getName().equals(TAG_USER)) {
            int storedId = readIntAttribute(parser, ATTR_ID, -1);
            if (storedId != id) {
                VLog.e(LOG_TAG, "User id does not match the file name");
                return null;
            }
            serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
            flags = readIntAttribute(parser, ATTR_FLAGS, 0);
            iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
            creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
            lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
            String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
            if ("true".equals(valueString)) {
                partial = true;
            }
            while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            }
            if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_NAME)) {
                type = parser.next();
                if (type == XmlPullParser.TEXT) {
                    name = parser.getText();
                }
            }
        }
        VUserInfo userInfo = new VUserInfo(id, name, iconPath, flags);
        userInfo.serialNumber = serialNumber;
        userInfo.creationTime = creationTime;
        userInfo.lastLoggedInTime = lastLoggedInTime;
        userInfo.partial = partial;
        return userInfo;
    } catch (IOException ioe) {
    } catch (XmlPullParserException pe) {
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}
Also used : AtomicFile(com.lody.virtual.helper.utils.AtomicFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) AtomicFile(com.lody.virtual.helper.utils.AtomicFile) File(java.io.File) FileInputStream(java.io.FileInputStream) VUserInfo(com.lody.virtual.os.VUserInfo)

Aggregations

AtomicFile (com.lody.virtual.helper.utils.AtomicFile)5 IOException (java.io.IOException)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)4 VUserInfo (com.lody.virtual.os.VUserInfo)3 File (java.io.File)3 FastXmlSerializer (com.lody.virtual.helper.utils.FastXmlSerializer)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 XmlSerializer (org.xmlpull.v1.XmlSerializer)2