Search in sources :

Example 31 with AtomicFile

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

the class UserManagerService method readUserListLocked.

private void readUserListLocked() {
    mGuestEnabled = false;
    if (!mUserListFile.exists()) {
        fallbackToSingleUserLocked();
        return;
    }
    FileInputStream fis = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fis = userListFile.openRead();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, null);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            ;
        }
        if (type != XmlPullParser.START_TAG) {
            Slog.e(LOG_TAG, "Unable to read user list");
            fallbackToSingleUserLocked();
            return;
        }
        mNextSerialNumber = -1;
        if (parser.getName().equals(TAG_USERS)) {
            String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
            if (lastSerialNumber != null) {
                mNextSerialNumber = Integer.parseInt(lastSerialNumber);
            }
            String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
            if (versionNumber != null) {
                mUserVersion = Integer.parseInt(versionNumber);
            }
        }
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
                String id = parser.getAttributeValue(null, ATTR_ID);
                UserInfo user = readUserLocked(Integer.parseInt(id));
                if (user != null) {
                    mUsers.put(user.id, user);
                    if (user.isGuest()) {
                        mGuestEnabled = true;
                    }
                    if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) {
                        mNextSerialNumber = user.id + 1;
                    }
                }
            }
        }
        updateUserIdsLocked();
        upgradeIfNecessary();
    } catch (IOException ioe) {
        fallbackToSingleUserLocked();
    } catch (XmlPullParserException pe) {
        fallbackToSingleUserLocked();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : AtomicFile(android.util.AtomicFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) UserInfo(android.content.pm.UserInfo) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 32 with AtomicFile

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

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

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

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)

Example 34 with AtomicFile

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

the class ProcessStatsService method updateFile.

private void updateFile() {
    mFile = new AtomicFile(new File(mBaseDir, STATE_FILE_PREFIX + mProcessStats.mTimePeriodStartClockStr + STATE_FILE_SUFFIX));
    mLastWriteTime = SystemClock.uptimeMillis();
}
Also used : AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 35 with AtomicFile

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

the class SettingsState method doWriteState.

private void doWriteState() {
    if (DEBUG_PERSISTENCE) {
        Slog.i(LOG_TAG, "[PERSIST START]");
    }
    AtomicFile destination = new AtomicFile(mStatePersistFile);
    final int version;
    final ArrayMap<String, Setting> settings;
    synchronized (mLock) {
        version = mVersion;
        settings = new ArrayMap<>(mSettings);
        mDirty = false;
        mWriteScheduled = false;
    }
    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_SETTINGS);
        serializer.attribute(null, ATTR_VERSION, String.valueOf(version));
        final int settingCount = settings.size();
        for (int i = 0; i < settingCount; i++) {
            Setting setting = settings.valueAt(i);
            writeSingleSetting(mVersion, serializer, setting.getId(), setting.getName(), setting.getValue(), setting.getPackageName());
            if (DEBUG_PERSISTENCE) {
                Slog.i(LOG_TAG, "[PERSISTED]" + setting.getName() + "=" + setting.getValue());
            }
        }
        serializer.endTag(null, TAG_SETTINGS);
        serializer.endDocument();
        destination.finishWrite(out);
        synchronized (mLock) {
            addHistoricalOperationLocked(HISTORICAL_OPERATION_PERSIST, null);
        }
        if (DEBUG_PERSISTENCE) {
            Slog.i(LOG_TAG, "[PERSIST END]");
        }
    } catch (Throwable t) {
        Slog.wtf(LOG_TAG, "Failed to write settings, restoring backup", t);
        destination.failWrite(out);
    } finally {
        IoUtils.closeQuietly(out);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer)

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