Search in sources :

Example 51 with AtomicFile

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;
}
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 52 with AtomicFile

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

Example 53 with AtomicFile

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

Example 54 with AtomicFile

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

Example 55 with AtomicFile

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

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