Search in sources :

Example 16 with AtomicFile

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

the class ShortcutService method getBaseStateFile.

/** Return the base state file name */
private AtomicFile getBaseStateFile() {
    final File path = new File(injectSystemDataPath(), FILENAME_BASE_STATE);
    path.mkdirs();
    return new AtomicFile(path);
}
Also used : AtomicFile(android.util.AtomicFile) File(java.io.File) AtomicFile(android.util.AtomicFile)

Example 17 with AtomicFile

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

the class ShortcutService method loadUserLocked.

@Nullable
private ShortcutUser loadUserLocked(@UserIdInt int userId) {
    final File path = getUserFile(userId);
    if (DEBUG) {
        Slog.d(TAG, "Loading from " + path);
    }
    final AtomicFile file = new AtomicFile(path);
    final FileInputStream in;
    try {
        in = file.openRead();
    } catch (FileNotFoundException e) {
        if (DEBUG) {
            Slog.d(TAG, "Not found " + path);
        }
        return null;
    }
    try {
        final ShortcutUser ret = loadUserInternal(userId, in, /* forBackup= */
        false);
        return ret;
    } catch (IOException | XmlPullParserException | InvalidFileFormatException e) {
        Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e);
        return null;
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile) FileInputStream(java.io.FileInputStream) Nullable(android.annotation.Nullable)

Example 18 with AtomicFile

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

the class UserManagerService method writeApplicationRestrictionsLP.

private void writeApplicationRestrictionsLP(String packageName, Bundle restrictions, int userId) {
    AtomicFile restrictionsFile = new AtomicFile(new File(Environment.getUserSystemDirectory(userId), packageToRestrictionsFileName(packageName)));
    writeApplicationRestrictionsLP(restrictions, restrictionsFile);
}
Also used : AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 19 with AtomicFile

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

the class UsageStatsDatabase method init.

/**
     * Initialize any directories required and index what stats are available.
     */
public void init(long currentTimeMillis) {
    synchronized (mLock) {
        for (File f : mIntervalDirs) {
            f.mkdirs();
            if (!f.exists()) {
                throw new IllegalStateException("Failed to create directory " + f.getAbsolutePath());
            }
        }
        checkVersionAndBuildLocked();
        indexFilesLocked();
        // Delete files that are in the future.
        for (TimeSparseArray<AtomicFile> files : mSortedStatFiles) {
            final int startIndex = files.closestIndexOnOrAfter(currentTimeMillis);
            if (startIndex < 0) {
                continue;
            }
            final int fileCount = files.size();
            for (int i = startIndex; i < fileCount; i++) {
                files.valueAt(i).delete();
            }
            // will cause a gc in the SparseArray and mess up the order.
            for (int i = startIndex; i < fileCount; i++) {
                files.removeAt(i);
            }
        }
    }
}
Also used : AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 20 with AtomicFile

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

the class AppIdleHistory method readAppIdleTimesLocked.

private void readAppIdleTimesLocked(int userId, ArrayMap<String, PackageHistory> userHistory) {
    FileInputStream fis = null;
    try {
        AtomicFile appIdleFile = new AtomicFile(getUserFile(userId));
        fis = appIdleFile.openRead();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, StandardCharsets.UTF_8.name());
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
        // Skip
        }
        if (type != XmlPullParser.START_TAG) {
            Slog.e(TAG, "Unable to read app idle file for user " + userId);
            return;
        }
        if (!parser.getName().equals(TAG_PACKAGES)) {
            return;
        }
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type == XmlPullParser.START_TAG) {
                final String name = parser.getName();
                if (name.equals(TAG_PACKAGE)) {
                    final String packageName = parser.getAttributeValue(null, ATTR_NAME);
                    PackageHistory packageHistory = new PackageHistory();
                    packageHistory.lastUsedElapsedTime = Long.parseLong(parser.getAttributeValue(null, ATTR_ELAPSED_IDLE));
                    packageHistory.lastUsedScreenTime = Long.parseLong(parser.getAttributeValue(null, ATTR_SCREEN_IDLE));
                    userHistory.put(packageName, packageHistory);
                }
            }
        }
    } catch (IOException | XmlPullParserException e) {
        Slog.e(TAG, "Unable to read app idle file for user " + userId);
    } finally {
        IoUtils.closeQuietly(fis);
    }
}
Also used : AtomicFile(android.util.AtomicFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

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