use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
the class RegisteredServicesCache method findOrCreateUserLocked.
@GuardedBy("mServicesLock")
private UserServices<V> findOrCreateUserLocked(int userId, boolean loadFromFileIfNew) {
UserServices<V> services = mUserServices.get(userId);
if (services == null) {
services = new UserServices<V>();
mUserServices.put(userId, services);
if (loadFromFileIfNew && mSerializerAndParser != null) {
// Check if user exists and try loading data from file
// clear existing data if there was an error during migration
UserInfo user = getUser(userId);
if (user != null) {
AtomicFile file = createFileForUser(user.id);
if (file.getBaseFile().exists()) {
if (DEBUG) {
Slog.i(TAG, String.format("Loading u%s data from %s", user.id, file));
}
InputStream is = null;
try {
is = file.openRead();
readPersistentServicesLocked(is);
} catch (Exception e) {
Log.w(TAG, "Error reading persistent services for user " + user.id, e);
} finally {
IoUtils.closeQuietly(is);
}
}
}
}
}
return services;
}
use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
the class RegisteredServicesCache method createFileForUser.
private AtomicFile createFileForUser(int userId) {
File userDir = getUserSystemDirectory(userId);
File userFile = new File(userDir, REGISTERED_SERVICES_DIR + "/" + mInterfaceName + ".xml");
return new AtomicFile(userFile);
}
use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
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 android_frameworks_base by ResurrectionRemix.
the class UsageStatsDatabase method queryUsageStats.
/**
* Find all {@link IntervalStats} for the given range and interval type.
*/
public <T> List<T> queryUsageStats(int intervalType, long beginTime, long endTime, int flags, StatCombiner<T> combiner) {
synchronized (mLock) {
if (intervalType < 0 || intervalType >= mIntervalDirs.length) {
throw new IllegalArgumentException("Bad interval type " + intervalType);
}
final TimeSparseArray<AtomicFile> intervalStats = mSortedStatFiles[intervalType];
if (endTime <= beginTime) {
if (DEBUG) {
Slog.d(TAG, "endTime(" + endTime + ") <= beginTime(" + beginTime + ")");
}
return null;
}
int startIndex = intervalStats.closestIndexOnOrBefore(beginTime);
if (startIndex < 0) {
// All the stats available have timestamps after beginTime, which means they all
// match.
startIndex = 0;
}
int endIndex = intervalStats.closestIndexOnOrBefore(endTime);
if (endIndex < 0) {
// All the stats start after this range ends, so nothing matches.
if (DEBUG) {
Slog.d(TAG, "No results for this range. All stats start after.");
}
return null;
}
if (intervalStats.keyAt(endIndex) == endTime) {
// The endTime is exclusive, so if we matched exactly take the one before.
endIndex--;
if (endIndex < 0) {
// All the stats start after this range ends, so nothing matches.
if (DEBUG) {
Slog.d(TAG, "No results for this range. All stats start after.");
}
return null;
}
}
final IntervalStats stats = new IntervalStats();
final ArrayList<T> results = new ArrayList<>();
for (int i = startIndex; i <= endIndex; i++) {
final AtomicFile f = intervalStats.valueAt(i);
if (DEBUG) {
Slog.d(TAG, "Reading stat file " + f.getBaseFile().getAbsolutePath());
}
try {
UsageStatsXml.read(f, stats, flags);
if (beginTime < stats.endTime) {
combiner.combine(stats, false, results);
}
} catch (IOException e) {
Slog.e(TAG, "Failed to read usage stats file", e);
// We continue so that we return results that are not
// corrupt.
}
}
return results;
}
}
use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
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();
}
}
Aggregations