use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
the class EphemeralApplicationRegistry method writeUninstalledEphemeralAppMetadata.
private void writeUninstalledEphemeralAppMetadata(EphemeralApplicationInfo ephemeralApp, int userId) {
File appDir = getEphemeralApplicationDir(ephemeralApp.getPackageName(), userId);
if (!appDir.exists() && !appDir.mkdirs()) {
return;
}
File metadataFile = new File(appDir, EPHEMERAL_APP_METADATA_FILE);
AtomicFile destination = new AtomicFile(metadataFile);
FileOutputStream out = null;
try {
out = destination.startWrite();
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(out, StandardCharsets.UTF_8.name());
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startDocument(null, true);
serializer.startTag(null, TAG_PACKAGE);
serializer.attribute(null, ATTR_LABEL, ephemeralApp.loadLabel(mService.mContext.getPackageManager()).toString());
serializer.startTag(null, TAG_PERMS);
for (String permission : ephemeralApp.getRequestedPermissions()) {
serializer.startTag(null, TAG_PERM);
serializer.attribute(null, ATTR_NAME, permission);
if (ArrayUtils.contains(ephemeralApp.getGrantedPermissions(), permission)) {
serializer.attribute(null, ATTR_GRANTED, String.valueOf(true));
}
serializer.endTag(null, TAG_PERM);
}
serializer.endTag(null, TAG_PERMS);
serializer.endTag(null, TAG_PACKAGE);
serializer.endDocument();
destination.finishWrite(out);
} catch (Throwable t) {
Slog.wtf(LOG_TAG, "Failed to write ephemeral state, restoring backup", t);
destination.failWrite(out);
} finally {
IoUtils.closeQuietly(out);
}
}
use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
the class PackageUsage method writeInternal.
@Override
protected void writeInternal(Map<String, PackageParser.Package> packages) {
AtomicFile file = getFile();
FileOutputStream f = null;
try {
f = file.startWrite();
BufferedOutputStream out = new BufferedOutputStream(f);
FileUtils.setPermissions(file.getBaseFile().getPath(), 0640, SYSTEM_UID, PACKAGE_INFO_GID);
StringBuilder sb = new StringBuilder();
sb.append(USAGE_FILE_MAGIC_VERSION_1);
sb.append('\n');
out.write(sb.toString().getBytes(StandardCharsets.US_ASCII));
for (PackageParser.Package pkg : packages.values()) {
if (pkg.getLatestPackageUseTimeInMills() == 0L) {
continue;
}
sb.setLength(0);
sb.append(pkg.packageName);
for (long usageTimeInMillis : pkg.mLastPackageUsageTimeInMills) {
sb.append(' ');
sb.append(usageTimeInMillis);
}
sb.append('\n');
out.write(sb.toString().getBytes(StandardCharsets.US_ASCII));
}
out.flush();
file.finishWrite(f);
} catch (IOException e) {
if (f != null) {
file.failWrite(f);
}
Log.e(PackageManagerService.TAG, "Failed to write package usage times", e);
}
}
use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
the class PackageUsage method readInternal.
@Override
protected void readInternal(Map<String, PackageParser.Package> packages) {
AtomicFile file = getFile();
BufferedInputStream in = null;
try {
in = new BufferedInputStream(file.openRead());
StringBuffer sb = new StringBuffer();
String firstLine = readLine(in, sb);
if (firstLine == null) {
// Empty file. Do nothing.
} else if (USAGE_FILE_MAGIC_VERSION_1.equals(firstLine)) {
readVersion1LP(packages, in, sb);
} else {
readVersion0LP(packages, in, sb, firstLine);
}
} catch (FileNotFoundException expected) {
mIsHistoricalPackageUsageAvailable = false;
} catch (IOException e) {
Log.w(PackageManagerService.TAG, "Failed to read package usage times", e);
} finally {
IoUtils.closeQuietly(in);
}
}
use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
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);
}
}
use of android.util.AtomicFile in project android_frameworks_base by ResurrectionRemix.
the class ShortcutService method saveBaseStateLocked.
@VisibleForTesting
void saveBaseStateLocked() {
final AtomicFile file = getBaseStateFile();
if (DEBUG) {
Slog.d(TAG, "Saving to " + file.getBaseFile());
}
FileOutputStream outs = null;
try {
outs = file.startWrite();
// Write to XML
XmlSerializer out = new FastXmlSerializer();
out.setOutput(outs, StandardCharsets.UTF_8.name());
out.startDocument(null, true);
out.startTag(null, TAG_ROOT);
// Body.
writeTagValue(out, TAG_LAST_RESET_TIME, mRawLastResetTime);
// Epilogue.
out.endTag(null, TAG_ROOT);
out.endDocument();
// Close.
file.finishWrite(outs);
} catch (IOException e) {
Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e);
file.failWrite(outs);
}
}
Aggregations