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);
}
}
}
}
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();
}
}
}
}
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);
}
}
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);
}
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);
}
}
Aggregations