Search in sources :

Example 41 with AtomicFile

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);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) AtomicFile(android.util.AtomicFile) File(java.io.File) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 42 with AtomicFile

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);
    }
}
Also used : AtomicFile(android.util.AtomicFile) PackageParser(android.content.pm.PackageParser) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 43 with AtomicFile

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);
    }
}
Also used : AtomicFile(android.util.AtomicFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 44 with AtomicFile

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);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile) FileInputStream(java.io.FileInputStream) Nullable(android.annotation.Nullable)

Example 45 with AtomicFile

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);
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

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