Search in sources :

Example 71 with AtomicFile

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

the class NetworkStatsCollection method readLegacyNetwork.

@Deprecated
public void readLegacyNetwork(File file) throws IOException {
    final AtomicFile inputFile = new AtomicFile(file);
    DataInputStream in = null;
    try {
        in = new DataInputStream(new BufferedInputStream(inputFile.openRead()));
        // verify file magic header intact
        final int magic = in.readInt();
        if (magic != FILE_MAGIC) {
            throw new ProtocolException("unexpected magic: " + magic);
        }
        final int version = in.readInt();
        switch(version) {
            case VERSION_NETWORK_INIT:
                {
                    // network := size *(NetworkIdentitySet NetworkStatsHistory)
                    final int size = in.readInt();
                    for (int i = 0; i < size; i++) {
                        final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                        final NetworkStatsHistory history = new NetworkStatsHistory(in);
                        final Key key = new Key(ident, UID_ALL, SET_ALL, TAG_NONE);
                        recordHistory(key, history);
                    }
                    break;
                }
            default:
                {
                    throw new ProtocolException("unexpected version: " + version);
                }
        }
    } catch (FileNotFoundException e) {
    // missing stats is okay, probably first boot
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : ProtocolException(java.net.ProtocolException) AtomicFile(android.util.AtomicFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) NetworkStatsHistory(android.net.NetworkStatsHistory) DataInputStream(java.io.DataInputStream)

Example 72 with AtomicFile

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

the class NetworkStatsCollection method readLegacyUid.

@Deprecated
public void readLegacyUid(File file, boolean onlyTags) throws IOException {
    final AtomicFile inputFile = new AtomicFile(file);
    DataInputStream in = null;
    try {
        in = new DataInputStream(new BufferedInputStream(inputFile.openRead()));
        // verify file magic header intact
        final int magic = in.readInt();
        if (magic != FILE_MAGIC) {
            throw new ProtocolException("unexpected magic: " + magic);
        }
        final int version = in.readInt();
        switch(version) {
            case VERSION_UID_INIT:
                {
                    // mapping into NetworkIdentitySet.
                    break;
                }
            case VERSION_UID_WITH_IDENT:
                {
                    // for a short time.
                    break;
                }
            case VERSION_UID_WITH_TAG:
            case VERSION_UID_WITH_SET:
                {
                    // uid := size *(NetworkIdentitySet size *(uid set tag NetworkStatsHistory))
                    final int identSize = in.readInt();
                    for (int i = 0; i < identSize; i++) {
                        final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                        final int size = in.readInt();
                        for (int j = 0; j < size; j++) {
                            final int uid = in.readInt();
                            final int set = (version >= VERSION_UID_WITH_SET) ? in.readInt() : SET_DEFAULT;
                            final int tag = in.readInt();
                            final Key key = new Key(ident, uid, set, tag);
                            final NetworkStatsHistory history = new NetworkStatsHistory(in);
                            if ((tag == TAG_NONE) != onlyTags) {
                                recordHistory(key, history);
                            }
                        }
                    }
                    break;
                }
            default:
                {
                    throw new ProtocolException("unexpected version: " + version);
                }
        }
    } catch (FileNotFoundException e) {
    // missing stats is okay, probably first boot
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : ProtocolException(java.net.ProtocolException) AtomicFile(android.util.AtomicFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) NetworkStatsHistory(android.net.NetworkStatsHistory) DataInputStream(java.io.DataInputStream)

Example 73 with AtomicFile

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

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

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

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

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

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)

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