Search in sources :

Example 36 with AtomicFile

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

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 37 with AtomicFile

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

the class UsageStatsDatabase method pruneFilesOlderThan.

private static void pruneFilesOlderThan(File dir, long expiryTime) {
    File[] files = dir.listFiles();
    if (files != null) {
        for (File f : files) {
            String path = f.getPath();
            if (path.endsWith(BAK_SUFFIX)) {
                f = new File(path.substring(0, path.length() - BAK_SUFFIX.length()));
            }
            long beginTime;
            try {
                beginTime = UsageStatsXml.parseBeginTime(f);
            } catch (IOException e) {
                beginTime = 0;
            }
            if (beginTime < expiryTime) {
                new AtomicFile(f).delete();
            }
        }
    }
}
Also used : AtomicFile(android.util.AtomicFile) IOException(java.io.IOException) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 38 with AtomicFile

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

the class FingerprintsUserState method doWriteState.

private void doWriteState() {
    AtomicFile destination = new AtomicFile(mFile);
    ArrayList<Fingerprint> fingerprints;
    synchronized (this) {
        fingerprints = getCopy(mFingerprints);
    }
    FileOutputStream out = null;
    try {
        out = destination.startWrite();
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(out, "utf-8");
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startDocument(null, true);
        serializer.startTag(null, TAG_FINGERPRINTS);
        final int count = fingerprints.size();
        for (int i = 0; i < count; i++) {
            Fingerprint fp = fingerprints.get(i);
            serializer.startTag(null, TAG_FINGERPRINT);
            serializer.attribute(null, ATTR_FINGER_ID, Integer.toString(fp.getFingerId()));
            serializer.attribute(null, ATTR_NAME, fp.getName().toString());
            serializer.attribute(null, ATTR_GROUP_ID, Integer.toString(fp.getGroupId()));
            serializer.attribute(null, ATTR_DEVICE_ID, Long.toString(fp.getDeviceId()));
            serializer.endTag(null, TAG_FINGERPRINT);
        }
        serializer.endTag(null, TAG_FINGERPRINTS);
        serializer.endDocument();
        destination.finishWrite(out);
    // Any error while writing is fatal.
    } catch (Throwable t) {
        Slog.wtf(TAG, "Failed to write settings, restoring backup", t);
        destination.failWrite(out);
        throw new IllegalStateException("Failed to write fingerprints", t);
    } finally {
        IoUtils.closeQuietly(out);
    }
}
Also used : Fingerprint(android.hardware.fingerprint.Fingerprint) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) Fingerprint(android.hardware.fingerprint.Fingerprint) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 39 with AtomicFile

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

the class AbstractStatsBase method getFile.

protected AtomicFile getFile() {
    File dataDir = Environment.getDataDirectory();
    File systemDir = new File(dataDir, "system");
    File fname = new File(systemDir, mFileName);
    return new AtomicFile(fname);
}
Also used : AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 40 with AtomicFile

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

the class EphemeralApplicationRegistry method parseMetadataFile.

private static UninstalledEphemeralAppState parseMetadataFile(File metadataFile) {
    if (!metadataFile.exists()) {
        return null;
    }
    FileInputStream in;
    try {
        in = new AtomicFile(metadataFile).openRead();
    } catch (FileNotFoundException fnfe) {
        Slog.i(LOG_TAG, "No ephemeral metadata file");
        return null;
    }
    final File ephemeralDir = metadataFile.getParentFile();
    final long timestamp = metadataFile.lastModified();
    final String packageName = ephemeralDir.getName();
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, StandardCharsets.UTF_8.name());
        return new UninstalledEphemeralAppState(parseMetadata(parser, packageName), timestamp);
    } catch (XmlPullParserException | IOException e) {
        throw new IllegalStateException("Failed parsing ephemeral" + " metadata file: " + metadataFile, 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) AtomicFile(android.util.AtomicFile) File(java.io.File) 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