Search in sources :

Example 6 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by ParanoidAndroid.

the class UserManagerService method writeUserLocked.

/*
     * Writes the user file in this format:
     *
     * <user flags="20039023" id="0">
     *   <name>Primary</name>
     * </user>
     */
private void writeUserLocked(UserInfo 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);
        Bundle restrictions = mUserRestrictions.get(userInfo.id);
        if (restrictions != null) {
            serializer.startTag(null, TAG_RESTRICTIONS);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS);
            writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER);
            serializer.endTag(null, TAG_RESTRICTIONS);
        }
        serializer.endTag(null, TAG_USER);
        serializer.endDocument();
        userFile.finishWrite(fos);
    } catch (Exception ioe) {
        Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe);
        userFile.failWrite(fos);
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) Bundle(android.os.Bundle) FileOutputStream(java.io.FileOutputStream) AtomicFile(android.util.AtomicFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 7 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by ParanoidAndroid.

the class UserManagerService 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++) {
            UserInfo 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);
        Slog.e(LOG_TAG, "Error writing user list");
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) UserInfo(android.content.pm.UserInfo) BufferedOutputStream(java.io.BufferedOutputStream) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 8 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by ParanoidAndroid.

the class UserManagerService 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) {
            Slog.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);
                UserInfo user = readUserLocked(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) {
            }
        }
    }
}
Also used : AtomicFile(android.util.AtomicFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) UserInfo(android.content.pm.UserInfo) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 9 with AtomicFile

use of android.util.AtomicFile in project platform_frameworks_base by android.

the class SettingsState method readStateSyncLocked.

private void readStateSyncLocked() {
    FileInputStream in;
    if (!mStatePersistFile.exists()) {
        Slog.i(LOG_TAG, "No settings state " + mStatePersistFile);
        addHistoricalOperationLocked(HISTORICAL_OPERATION_INITIALIZE, null);
        return;
    }
    try {
        in = new AtomicFile(mStatePersistFile).openRead();
    } catch (FileNotFoundException fnfe) {
        String message = "No settings state " + mStatePersistFile;
        Slog.wtf(LOG_TAG, message);
        Slog.i(LOG_TAG, message);
        return;
    }
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, StandardCharsets.UTF_8.name());
        parseStateLocked(parser);
    } catch (XmlPullParserException | IOException e) {
        String message = "Failed parsing settings file: " + mStatePersistFile;
        Slog.wtf(LOG_TAG, message);
        throw new IllegalStateException(message, e);
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileNotFoundException(java.io.FileNotFoundException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 10 with AtomicFile

use of android.util.AtomicFile in project platform_frameworks_base by android.

the class AppWidgetServiceImpl method getSavedStateFile.

private static AtomicFile getSavedStateFile(int userId) {
    File dir = Environment.getUserSystemDirectory(userId);
    File settingsFile = getStateFile(userId);
    if (!settingsFile.exists() && userId == UserHandle.USER_SYSTEM) {
        if (!dir.exists()) {
            dir.mkdirs();
        }
        // Migrate old data
        File oldFile = new File("/data/system/" + STATE_FILENAME);
        // Method doesn't throw an exception on failure. Ignore any errors
        // in moving the file (like non-existence)
        oldFile.renameTo(settingsFile);
    }
    return new AtomicFile(settingsFile);
}
Also used : AtomicFile(android.util.AtomicFile) File(java.io.File) AtomicFile(android.util.AtomicFile)

Aggregations

AtomicFile (android.util.AtomicFile)231 IOException (java.io.IOException)135 File (java.io.File)106 FileOutputStream (java.io.FileOutputStream)73 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)70 FileNotFoundException (java.io.FileNotFoundException)61 FileInputStream (java.io.FileInputStream)39 XmlSerializer (org.xmlpull.v1.XmlSerializer)35 XmlPullParser (org.xmlpull.v1.XmlPullParser)33 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)25 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)24 BufferedOutputStream (java.io.BufferedOutputStream)19 BufferedInputStream (java.io.BufferedInputStream)16 UserInfo (android.content.pm.UserInfo)15 Bundle (android.os.Bundle)15 RemoteException (android.os.RemoteException)15 ArrayList (java.util.ArrayList)15 InputStream (java.io.InputStream)14 NetworkStatsHistory (android.net.NetworkStatsHistory)12 ErrnoException (android.system.ErrnoException)12