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 package com.android.loganalysis.parser;
17 
18 import com.android.loganalysis.item.LocationDumpsItem;
19 import com.android.loganalysis.util.NumberFormattingUtil;
20 
21 import java.util.List;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 
25 /**
26  * A {@link IParser} to handle the parsing of location request information
27  */
28 public class LocationServiceParser implements IParser {
29     /**
30      * Match a valid line such as:
31      * "Interval effective/min/max 1/0/0[s] Duration: 140[minutes] [com.google.android.gms,
32      * PRIORITY_NO_POWER, UserLocationProducer] Num requests: 2 Active: true"
33      */
34     private static final Pattern LOCATION_PAT = Pattern.compile(
35             "^\\s*Interval effective/min/max (\\d+)/(\\d+)/(\\d+)\\[s\\] Duration: (\\d+)"
36             + "\\[minutes\\]\\s*\\[([\\w.]+), (\\w+)(,.*)?\\] Num requests: \\d+ Active: \\w*");
37 
38     private LocationDumpsItem mItem = new LocationDumpsItem();
39 
40     /**
41      * {@inheritDoc}
42      */
43     @Override
parse(List<String> lines)44     public LocationDumpsItem parse(List<String> lines) {
45         Matcher m = null;
46         for (String line : lines) {
47             m = LOCATION_PAT.matcher(line);
48             if (m.matches()) {
49                 mItem.addLocationClient(m.group(5), NumberFormattingUtil.parseIntOrZero(m.group(1)),
50                         NumberFormattingUtil.parseIntOrZero(m.group(2)),
51                         NumberFormattingUtil.parseIntOrZero(m.group(3)), m.group(6),
52                         NumberFormattingUtil.parseIntOrZero(m.group(4)));
53                 continue;
54             }
55         }
56         return mItem;
57     }
58 
59     /**
60      * Get the {@link LocationDumpsItem}.
61      * <p>
62      * Exposed for unit testing.
63      * </p>
64      */
getItem()65     LocationDumpsItem getItem() {
66         return mItem;
67     }
68 }
69