1 /*
2  * Copyright (C) 2016 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.LocationDumpsItem;
20 import com.android.loganalysis.item.ActivityServiceItem;
21 
22 import java.util.List;
23 
24 /**
25  * A {@link IParser} to parse the activity service dump section of the bugreport
26  */
27 public class ActivityServiceParser extends AbstractSectionParser {
28 
29     private static final String LOCATION_SECTION_REGEX =
30             "^\\s*SERVICE com.google.android.gms/"
31             + "com.google.android.location.internal.GoogleLocationManagerService \\w+ pid=\\d+";
32 
33     private static final String NOOP_SECTION_REGEX = "^\\s*SERVICE .*/.*";
34 
35     private LocationServiceParser mLocationParser = new LocationServiceParser();
36 
37     private ActivityServiceItem mActivityServiceItem= null;
38     private boolean mParsedInput = false;
39 
40     /**
41      * {@inheritDoc}
42      *
43      * @return The {@link ActivityServiceItem}
44      */
45     @Override
parse(List<String> lines)46     public ActivityServiceItem parse(List<String> lines) {
47         setup();
48         for (String line : lines) {
49             if (!mParsedInput && !"".equals(line.trim())) {
50                 mParsedInput = true;
51             }
52             parseLine(line);
53         }
54         commit();
55         return mActivityServiceItem;
56     }
57 
58     /**
59      * Sets up the parser by adding the section parsers.
60      */
setup()61     protected void setup() {
62         addSectionParser(mLocationParser, LOCATION_SECTION_REGEX);
63         addSectionParser(new NoopParser(), NOOP_SECTION_REGEX);
64     }
65 
66     /**
67      * {@inheritDoc}
68      */
69     @Override
commit()70     protected void commit() {
71         // signal EOF
72         super.commit();
73         if (mParsedInput) {
74             if (mActivityServiceItem == null) {
75                 mActivityServiceItem = new ActivityServiceItem();
76             }
77         }
78 
79         if (mActivityServiceItem != null) {
80             mActivityServiceItem.setLocationDumps(
81                     (LocationDumpsItem) getSection(mLocationParser));
82         }
83     }
84 }
85