FileDocCategorySizeDatePackage
UsageStatsXml.javaAPI DocAndroid 5.1 API4528Thu Mar 12 22:22:42 GMT 2015com.android.server.usage

UsageStatsXml

public class UsageStatsXml extends Object
Copyright (C) 2014 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Fields Summary
private static final String
TAG
private static final int
CURRENT_VERSION
private static final String
USAGESTATS_TAG
private static final String
VERSION_ATTR
static final String
CHECKED_IN_SUFFIX
Constructors Summary
Methods Summary
public static longparseBeginTime(android.util.AtomicFile file)


         
        return parseBeginTime(file.getBaseFile());
    
public static longparseBeginTime(java.io.File file)

        String name = file.getName();

        // Eat as many occurrences of -c as possible. This is due to a bug where -c
        // would be appended more than once to a checked-in file, causing a crash
        // on boot when indexing files since Long.parseLong() will puke on anything but
        // a number.
        while (name.endsWith(CHECKED_IN_SUFFIX)) {
            name = name.substring(0, name.length() - CHECKED_IN_SUFFIX.length());
        }
        return Long.parseLong(name);
    
public static voidread(android.util.AtomicFile file, IntervalStats statsOut)

        try {
            FileInputStream in = file.openRead();
            try {
                statsOut.beginTime = parseBeginTime(file);
                read(in, statsOut);
                statsOut.lastTimeSaved = file.getLastModifiedTime();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    // Empty
                }
            }
        } catch (FileNotFoundException e) {
            Slog.e(TAG, "UsageStats Xml", e);
            throw e;
        }
    
private static voidread(java.io.InputStream in, IntervalStats statsOut)

        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(in, "utf-8");
            XmlUtils.beginDocument(parser, USAGESTATS_TAG);
            String versionStr = parser.getAttributeValue(null, VERSION_ATTR);
            try {
                switch (Integer.parseInt(versionStr)) {
                    case 1:
                        UsageStatsXmlV1.read(parser, statsOut);
                        break;

                    default:
                        Slog.e(TAG, "Unrecognized version " + versionStr);
                        throw new IOException("Unrecognized version " + versionStr);
                }
            } catch (NumberFormatException e) {
                Slog.e(TAG, "Bad version");
                throw new IOException(e);
            }
        } catch (XmlPullParserException e) {
            Slog.e(TAG, "Failed to parse Xml", e);
            throw new IOException(e);
        }
    
public static voidwrite(android.util.AtomicFile file, IntervalStats stats)

        FileOutputStream fos = file.startWrite();
        try {
            write(fos, stats);
            file.finishWrite(fos);
            fos = null;
        } finally {
            // When fos is null (successful write), this will no-op
            file.failWrite(fos);
        }
    
private static voidwrite(java.io.OutputStream out, IntervalStats stats)

        FastXmlSerializer xml = new FastXmlSerializer();
        xml.setOutput(out, "utf-8");
        xml.startDocument("utf-8", true);
        xml.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        xml.startTag(null, USAGESTATS_TAG);
        xml.attribute(null, VERSION_ATTR, Integer.toString(CURRENT_VERSION));

        UsageStatsXmlV1.write(xml, stats);

        xml.endTag(null, USAGESTATS_TAG);
        xml.endDocument();