use of android.util.AtomicFile in project platform_frameworks_base by android.
the class AppIdleHistory method writeScreenOnTimeLocked.
private void writeScreenOnTimeLocked() {
AtomicFile screenOnTimeFile = new AtomicFile(getScreenOnTimeFile());
FileOutputStream fos = null;
try {
fos = screenOnTimeFile.startWrite();
fos.write((Long.toString(mScreenOnDuration) + "\n" + Long.toString(mElapsedDuration) + "\n").getBytes());
screenOnTimeFile.finishWrite(fos);
} catch (IOException ioe) {
screenOnTimeFile.failWrite(fos);
}
}
use of android.util.AtomicFile in project platform_frameworks_base by android.
the class UsageStatsDatabase method putUsageStats.
/**
* Update the stats in the database. They may not be written to disk immediately.
*/
public void putUsageStats(int intervalType, IntervalStats stats) throws IOException {
if (stats == null)
return;
synchronized (mLock) {
if (intervalType < 0 || intervalType >= mIntervalDirs.length) {
throw new IllegalArgumentException("Bad interval type " + intervalType);
}
AtomicFile f = mSortedStatFiles[intervalType].get(stats.beginTime);
if (f == null) {
f = new AtomicFile(new File(mIntervalDirs[intervalType], Long.toString(stats.beginTime)));
mSortedStatFiles[intervalType].put(stats.beginTime, f);
}
UsageStatsXml.write(f, stats);
stats.lastTimeSaved = f.getLastModifiedTime();
}
}
use of android.util.AtomicFile in project platform_frameworks_base by android.
the class UsageStatsDatabase method onTimeChanged.
public void onTimeChanged(long timeDiffMillis) {
synchronized (mLock) {
StringBuilder logBuilder = new StringBuilder();
logBuilder.append("Time changed by ");
TimeUtils.formatDuration(timeDiffMillis, logBuilder);
logBuilder.append(".");
int filesDeleted = 0;
int filesMoved = 0;
for (TimeSparseArray<AtomicFile> files : mSortedStatFiles) {
final int fileCount = files.size();
for (int i = 0; i < fileCount; i++) {
final AtomicFile file = files.valueAt(i);
final long newTime = files.keyAt(i) + timeDiffMillis;
if (newTime < 0) {
filesDeleted++;
file.delete();
} else {
try {
file.openRead().close();
} catch (IOException e) {
// Ignore, this is just to make sure there are no backups.
}
String newName = Long.toString(newTime);
if (file.getBaseFile().getName().endsWith(CHECKED_IN_SUFFIX)) {
newName = newName + CHECKED_IN_SUFFIX;
}
final File newFile = new File(file.getBaseFile().getParentFile(), newName);
filesMoved++;
file.getBaseFile().renameTo(newFile);
}
}
files.clear();
}
logBuilder.append(" files deleted: ").append(filesDeleted);
logBuilder.append(" files moved: ").append(filesMoved);
Slog.i(TAG, logBuilder.toString());
// Now re-index the new files.
indexFilesLocked();
}
}
use of android.util.AtomicFile in project platform_frameworks_base by android.
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 platform_frameworks_base by android.
the class ProcessStatsService method writeStateLocked.
public void writeStateLocked(boolean sync, final boolean commit) {
synchronized (mPendingWriteLock) {
long now = SystemClock.uptimeMillis();
if (mPendingWrite == null || !mPendingWriteCommitted) {
mPendingWrite = Parcel.obtain();
mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
mProcessStats.mTimePeriodEndUptime = now;
if (commit) {
mProcessStats.mFlags |= ProcessStats.FLAG_COMPLETE;
}
mProcessStats.writeToParcel(mPendingWrite, 0);
mPendingWriteFile = new AtomicFile(mFile.getBaseFile());
mPendingWriteCommitted = commit;
}
if (commit) {
mProcessStats.resetSafely();
updateFile();
}
mLastWriteTime = SystemClock.uptimeMillis();
Slog.i(TAG, "Prepared write state in " + (SystemClock.uptimeMillis() - now) + "ms");
if (!sync) {
BackgroundThread.getHandler().post(new Runnable() {
@Override
public void run() {
performWriteState();
}
});
return;
}
}
performWriteState();
}
Aggregations