1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.powermodel; 18 19 import java.io.InputStream; 20 import java.io.IOException; 21 import com.android.powermodel.component.ModemBatteryStatsReader; 22 23 public class BatteryStatsReader { 24 /** 25 * Construct a reader. 26 */ BatteryStatsReader()27 public BatteryStatsReader() { 28 } 29 30 /** 31 * Parse a powermodel.xml file and return a PowerProfile object. 32 * 33 * @param stream An InputStream containing the batterystats output. 34 * 35 * @throws ParseException Thrown when the xml file can not be parsed. 36 * @throws IOException When there is a problem reading the stream. 37 */ parse(InputStream stream)38 public static ActivityReport parse(InputStream stream) throws ParseException, IOException { 39 final Parser parser = new Parser(stream); 40 return parser.parse(); 41 } 42 43 /** 44 * Implements the reading and power model logic. 45 */ 46 private static class Parser { 47 final InputStream mStream; 48 final ActivityReport mResult; 49 RawBatteryStats mBs; 50 51 /** 52 * Constructor to capture the parameters to read. 53 */ Parser(InputStream stream)54 Parser(InputStream stream) { 55 mStream = stream; 56 mResult = new ActivityReport(); 57 } 58 59 /** 60 * Read the stream, parse it, and apply the power model. 61 * Do not call this more than once. 62 */ parse()63 ActivityReport parse() throws ParseException, IOException { 64 mBs = RawBatteryStats.parse(mStream); 65 66 final ActivityReport.Builder report = new ActivityReport.Builder(); 67 68 report.addActivity(Component.MODEM, ModemBatteryStatsReader.createActivities(mBs)); 69 70 return report.build(); 71 } 72 } 73 } 74 75