1 /**
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 
17 package com.android.server.usage;
18 
19 import android.util.AtomicFile;
20 import android.util.Slog;
21 import android.util.Xml;
22 import android.util.proto.ProtoInputStream;
23 import android.util.proto.ProtoOutputStream;
24 
25 import com.android.internal.util.FastXmlSerializer;
26 import com.android.internal.util.XmlUtils;
27 import org.xmlpull.v1.XmlPullParser;
28 import org.xmlpull.v1.XmlPullParserException;
29 
30 import java.io.*;
31 
32 public class UsageStatsXml {
33     private static final String TAG = "UsageStatsXml";
34     private static final int CURRENT_VERSION = 1;
35     private static final String USAGESTATS_TAG = "usagestats";
36     private static final String VERSION_ATTR = "version";
37     static final String CHECKED_IN_SUFFIX = "-c";
38 
read(InputStream in, IntervalStats statsOut)39     public static void read(InputStream in, IntervalStats statsOut) throws IOException {
40         XmlPullParser parser = Xml.newPullParser();
41         try {
42             parser.setInput(in, "utf-8");
43             XmlUtils.beginDocument(parser, USAGESTATS_TAG);
44             String versionStr = parser.getAttributeValue(null, VERSION_ATTR);
45             try {
46                 switch (Integer.parseInt(versionStr)) {
47                     case 1:
48                         UsageStatsXmlV1.read(parser, statsOut);
49                         break;
50 
51                     default:
52                         Slog.e(TAG, "Unrecognized version " + versionStr);
53                         throw new IOException("Unrecognized version " + versionStr);
54                 }
55             } catch (NumberFormatException e) {
56                 Slog.e(TAG, "Bad version");
57                 throw new IOException(e);
58             }
59         } catch (XmlPullParserException e) {
60             Slog.e(TAG, "Failed to parse Xml", e);
61             throw new IOException(e);
62         }
63     }
64 
write(OutputStream out, IntervalStats stats)65     public static void write(OutputStream out, IntervalStats stats) throws IOException {
66         FastXmlSerializer xml = new FastXmlSerializer();
67         xml.setOutput(out, "utf-8");
68         xml.startDocument("utf-8", true);
69         xml.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
70         xml.startTag(null, USAGESTATS_TAG);
71         xml.attribute(null, VERSION_ATTR, Integer.toString(CURRENT_VERSION));
72 
73         UsageStatsXmlV1.write(xml, stats);
74 
75         xml.endTag(null, USAGESTATS_TAG);
76         xml.endDocument();
77     }
78 }