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.Slog;
20 import android.util.Xml;
21 
22 import com.android.internal.util.XmlUtils;
23 
24 import org.xmlpull.v1.XmlPullParser;
25 import org.xmlpull.v1.XmlPullParserException;
26 
27 import java.io.*;
28 
29 public class UsageStatsXml {
30     private static final String TAG = "UsageStatsXml";
31     private static final String USAGESTATS_TAG = "usagestats";
32     private static final String VERSION_ATTR = "version";
33     static final String CHECKED_IN_SUFFIX = "-c";
34 
read(InputStream in, IntervalStats statsOut)35     public static void read(InputStream in, IntervalStats statsOut) throws IOException {
36         XmlPullParser parser = Xml.newPullParser();
37         try {
38             parser.setInput(in, "utf-8");
39             XmlUtils.beginDocument(parser, USAGESTATS_TAG);
40             String versionStr = parser.getAttributeValue(null, VERSION_ATTR);
41             try {
42                 switch (Integer.parseInt(versionStr)) {
43                     case 1:
44                         UsageStatsXmlV1.read(parser, statsOut);
45                         break;
46 
47                     default:
48                         Slog.e(TAG, "Unrecognized version " + versionStr);
49                         throw new IOException("Unrecognized version " + versionStr);
50                 }
51             } catch (NumberFormatException e) {
52                 Slog.e(TAG, "Bad version");
53                 throw new IOException(e);
54             }
55         } catch (XmlPullParserException e) {
56             Slog.e(TAG, "Failed to parse Xml", e);
57             throw new IOException(e);
58         }
59     }
60 }