Search in sources :

Example 21 with AtomicFile

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

Example 22 with AtomicFile

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

Example 23 with AtomicFile

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

Example 24 with AtomicFile

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

Example 25 with AtomicFile

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();
}
Also used : 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