Search in sources :

Example 56 with AtomicFile

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

the class AppIdleHistory method readAppIdleTimesLocked.

private void readAppIdleTimesLocked(int userId, ArrayMap<String, PackageHistory> userHistory) {
    FileInputStream fis = null;
    try {
        AtomicFile appIdleFile = new AtomicFile(getUserFile(userId));
        fis = appIdleFile.openRead();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, StandardCharsets.UTF_8.name());
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
        // Skip
        }
        if (type != XmlPullParser.START_TAG) {
            Slog.e(TAG, "Unable to read app idle file for user " + userId);
            return;
        }
        if (!parser.getName().equals(TAG_PACKAGES)) {
            return;
        }
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type == XmlPullParser.START_TAG) {
                final String name = parser.getName();
                if (name.equals(TAG_PACKAGE)) {
                    final String packageName = parser.getAttributeValue(null, ATTR_NAME);
                    PackageHistory packageHistory = new PackageHistory();
                    packageHistory.lastUsedElapsedTime = Long.parseLong(parser.getAttributeValue(null, ATTR_ELAPSED_IDLE));
                    packageHistory.lastUsedScreenTime = Long.parseLong(parser.getAttributeValue(null, ATTR_SCREEN_IDLE));
                    userHistory.put(packageName, packageHistory);
                }
            }
        }
    } catch (IOException | XmlPullParserException e) {
        Slog.e(TAG, "Unable to read app idle file for user " + userId);
    } finally {
        IoUtils.closeQuietly(fis);
    }
}
Also used : AtomicFile(android.util.AtomicFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 57 with AtomicFile

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

the class UserManagerServiceTest method testWriteReadApplicationRestrictions.

public void testWriteReadApplicationRestrictions() throws IOException {
    AtomicFile atomicFile = new AtomicFile(restrictionsFile);
    Bundle bundle = createBundle();
    UserManagerService.writeApplicationRestrictionsLP(bundle, atomicFile);
    assertTrue(atomicFile.getBaseFile().exists());
    String s = FileUtils.readTextFile(restrictionsFile, 10000, "");
    System.out.println("restrictionsFile: " + s);
    bundle = UserManagerService.readApplicationRestrictionsLP(atomicFile);
    System.out.println("readApplicationRestrictionsLocked bundle: " + bundle);
    assertBundle(bundle);
}
Also used : AtomicFile(android.util.AtomicFile) Bundle(android.os.Bundle)

Example 58 with AtomicFile

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

the class AppWidgetServiceImpl method loadGroupStateLocked.

// only call from initialization -- it assumes that the data structures are all empty
private void loadGroupStateLocked(int[] profileIds) {
    // We can bind the widgets to host and providers only after
    // reading the host and providers for all users since a widget
    // can have a host and a provider in different users.
    List<LoadedWidgetState> loadedWidgets = new ArrayList<>();
    int version = 0;
    final int profileIdCount = profileIds.length;
    for (int i = 0; i < profileIdCount; i++) {
        final int profileId = profileIds[i];
        // No file written for this user - nothing to do.
        AtomicFile file = getSavedStateFile(profileId);
        try {
            FileInputStream stream = file.openRead();
            version = readProfileStateFromFileLocked(stream, profileId, loadedWidgets);
            IoUtils.closeQuietly(stream);
        } catch (FileNotFoundException e) {
            Slog.w(TAG, "Failed to read state: " + e);
        }
    }
    if (version >= 0) {
        // Hooke'm up...
        bindLoadedWidgetsLocked(loadedWidgets);
        // upgrade the database if needed
        performUpgradeLocked(version);
    } else {
        // failed reading, clean up
        Slog.w(TAG, "Failed to read state, clearing widgets and hosts.");
        clearWidgetsLocked();
        mHosts.clear();
        final int N = mProviders.size();
        for (int i = 0; i < N; i++) {
            mProviders.get(i).widgets.clear();
        }
    }
}
Also used : AtomicFile(android.util.AtomicFile) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Point(android.graphics.Point) FileInputStream(java.io.FileInputStream)

Example 59 with AtomicFile

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

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 60 with AtomicFile

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

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)

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