Home | History | Annotate | Download | only in parser
      1 /*
      2  * Copyright (C) 2015 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.loganalysis.parser;
     18 
     19 import com.android.loganalysis.item.BatteryDischargeStatsInfoItem;
     20 import com.android.loganalysis.item.BatteryStatsDetailedInfoItem;
     21 import com.android.loganalysis.item.DumpsysBatteryStatsItem;
     22 import com.android.loganalysis.item.BatteryStatsSummaryInfoItem;
     23 
     24 import java.util.List;
     25 
     26 
     27 /**
     28  * A {@link IParser} to parse the battery stats section of the bugreport
     29  */
     30 public class DumpsysBatteryStatsParser extends AbstractSectionParser {
     31 
     32     private static final String SUMMARY_INFO_SECTION_REGEX =
     33             "Battery History \\(\\d+% used, \\d+(KB)? used of \\d+KB, \\d+ strings using "
     34             + "\\d+(KB)?\\):$";
     35     private static final String DISCHARGE_STATS_INFO_SECTION_REGEX = "^Discharge step durations:$";
     36     private static final String DETAILED_INFO_SECTION_REGEX = "^Statistics since last charge:$";
     37 
     38     // We are not using this sections and will be ignored.
     39     private static final String NOOP_SECTION_REGEX =
     40         "^(Statistics since last unplugged:|Daily stats:)$";
     41 
     42     private BatteryStatsSummaryInfoParser mSummaryParser = new BatteryStatsSummaryInfoParser();
     43     private BatteryStatsDetailedInfoParser mDetailedParser = new BatteryStatsDetailedInfoParser();
     44     private BatteryDischargeStatsInfoParser mDischargeStepsParser = new
     45         BatteryDischargeStatsInfoParser();
     46 
     47     private DumpsysBatteryStatsItem mDumpsysBatteryStatsItem = null;
     48     private boolean mParsedInput = false;
     49 
     50     /**
     51      * {@inheritDoc}
     52      *
     53      * @return The {@link DumpsysBatteryStatsItem}
     54      */
     55     @Override
     56     public DumpsysBatteryStatsItem parse(List<String> lines) {
     57         setup();
     58         for (String line : lines) {
     59             if (!mParsedInput && !"".equals(line.trim())) {
     60                 mParsedInput = true;
     61             }
     62             parseLine(line);
     63         }
     64         commit();
     65         return mDumpsysBatteryStatsItem;
     66     }
     67 
     68     /**
     69      * Sets up the parser by adding the section parsers.
     70      */
     71     protected void setup() {
     72         addSectionParser(mSummaryParser, SUMMARY_INFO_SECTION_REGEX);
     73         addSectionParser(mDetailedParser, DETAILED_INFO_SECTION_REGEX);
     74         addSectionParser(mDischargeStepsParser, DISCHARGE_STATS_INFO_SECTION_REGEX);
     75         addSectionParser(new NoopParser(), NOOP_SECTION_REGEX);
     76     }
     77 
     78     /**
     79      * {@inheritDoc}
     80      */
     81     @Override
     82     protected void commit() {
     83         // signal EOF
     84         super.commit();
     85         if (mParsedInput) {
     86             if (mDumpsysBatteryStatsItem == null) {
     87                 mDumpsysBatteryStatsItem = new DumpsysBatteryStatsItem();
     88             }
     89         }
     90 
     91         if (mDumpsysBatteryStatsItem != null) {
     92             mDumpsysBatteryStatsItem.setBatteryStatsSummarytem(
     93                 (BatteryStatsSummaryInfoItem) getSection(mSummaryParser));
     94             mDumpsysBatteryStatsItem.setDetailedBatteryStatsItem(
     95                 (BatteryStatsDetailedInfoItem) getSection(mDetailedParser));
     96             mDumpsysBatteryStatsItem.setBatteryDischargeStatsItem(
     97                 (BatteryDischargeStatsInfoItem) getSection(mDischargeStepsParser));
     98         }
     99     }
    100 }
    101