Search in sources :

Example 81 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

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;
}
Also used : AtomicFile(android.util.AtomicFile) InputStream(java.io.InputStream) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) GuardedBy(com.android.internal.annotations.GuardedBy)

Example 82 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class ProcessStatsService method getStatsOverTime.

public ParcelFileDescriptor getStatsOverTime(long minTime) {
    mAm.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS, null);
    Parcel current = Parcel.obtain();
    long curTime;
    synchronized (mAm) {
        long now = SystemClock.uptimeMillis();
        mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
        mProcessStats.mTimePeriodEndUptime = now;
        mProcessStats.writeToParcel(current, now, 0);
        curTime = mProcessStats.mTimePeriodEndRealtime - mProcessStats.mTimePeriodStartRealtime;
    }
    mWriteLock.lock();
    try {
        if (curTime < minTime) {
            // Need to add in older stats to reach desired time.
            ArrayList<String> files = getCommittedFiles(0, false, true);
            if (files != null && files.size() > 0) {
                current.setDataPosition(0);
                ProcessStats stats = ProcessStats.CREATOR.createFromParcel(current);
                current.recycle();
                int i = files.size() - 1;
                while (i >= 0 && (stats.mTimePeriodEndRealtime - stats.mTimePeriodStartRealtime) < minTime) {
                    AtomicFile file = new AtomicFile(new File(files.get(i)));
                    i--;
                    ProcessStats moreStats = new ProcessStats(false);
                    readLocked(moreStats, file);
                    if (moreStats.mReadError == null) {
                        stats.add(moreStats);
                        StringBuilder sb = new StringBuilder();
                        sb.append("Added stats: ");
                        sb.append(moreStats.mTimePeriodStartClockStr);
                        sb.append(", over ");
                        TimeUtils.formatDuration(moreStats.mTimePeriodEndRealtime - moreStats.mTimePeriodStartRealtime, sb);
                        Slog.i(TAG, sb.toString());
                    } else {
                        Slog.w(TAG, "Failure reading " + files.get(i + 1) + "; " + moreStats.mReadError);
                        continue;
                    }
                }
                current = Parcel.obtain();
                stats.writeToParcel(current, 0);
            }
        }
        final byte[] outData = current.marshall();
        current.recycle();
        final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
        Thread thr = new Thread("ProcessStats pipe output") {

            public void run() {
                FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(fds[1]);
                try {
                    fout.write(outData);
                    fout.close();
                } catch (IOException e) {
                    Slog.w(TAG, "Failure writing pipe", e);
                }
            }
        };
        thr.start();
        return fds[0];
    } catch (IOException e) {
        Slog.w(TAG, "Failed building output pipe", e);
    } finally {
        mWriteLock.unlock();
    }
    return null;
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) Parcel(android.os.Parcel) IOException(java.io.IOException) BackgroundThread(com.android.internal.os.BackgroundThread) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 83 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class ShortcutService method saveUserLocked.

private void saveUserLocked(@UserIdInt int userId) {
    final File path = getUserFile(userId);
    if (DEBUG) {
        Slog.d(TAG, "Saving to " + path);
    }
    path.getParentFile().mkdirs();
    final AtomicFile file = new AtomicFile(path);
    FileOutputStream os = null;
    try {
        os = file.startWrite();
        saveUserInternalLocked(userId, os, /* forBackup= */
        false);
        file.finishWrite(os);
        // Remove all dangling bitmap files.
        cleanupDanglingBitmapDirectoriesLocked(userId);
    } catch (XmlPullParserException | IOException e) {
        Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e);
        file.failWrite(os);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile)

Example 84 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

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)

Example 85 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class ShortcutService method getBaseStateFile.

/** Return the base state file name */
private AtomicFile getBaseStateFile() {
    final File path = new File(injectSystemDataPath(), FILENAME_BASE_STATE);
    path.mkdirs();
    return new AtomicFile(path);
}
Also used : AtomicFile(android.util.AtomicFile) File(java.io.File) AtomicFile(android.util.AtomicFile)

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