• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.ddmlib;
18 
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 
22 /**
23  * A receiver able to parse the result of the execution of
24  * {@link #GETPROP_COMMAND} on a device.
25  */
26 final class GetPropReceiver extends MultiLineReceiver {
27     final static String GETPROP_COMMAND = "getprop"; //$NON-NLS-1$
28 
29     private final static Pattern GETPROP_PATTERN = Pattern.compile("^\\[([^]]+)\\]\\:\\s*\\[(.*)\\]$"); //$NON-NLS-1$
30 
31     /** indicates if we need to read the first */
32     private Device mDevice = null;
33 
34     /**
35      * Creates the receiver with the device the receiver will modify.
36      * @param device The device to modify
37      */
GetPropReceiver(Device device)38     public GetPropReceiver(Device device) {
39         mDevice = device;
40     }
41 
42     @Override
processNewLines(String[] lines)43     public void processNewLines(String[] lines) {
44         // We receive an array of lines. We're expecting
45         // to have the build info in the first line, and the build
46         // date in the 2nd line. There seems to be an empty line
47         // after all that.
48 
49         for (String line : lines) {
50             if (line.length() == 0 || line.startsWith("#")) {
51                 continue;
52             }
53 
54             Matcher m = GETPROP_PATTERN.matcher(line);
55             if (m.matches()) {
56                 String label = m.group(1);
57                 String value = m.group(2);
58 
59                 if (label.length() > 0) {
60                     mDevice.addProperty(label, value);
61                 }
62             }
63         }
64     }
65 
66     @Override
isCancelled()67     public boolean isCancelled() {
68         return false;
69     }
70 
71     @Override
done()72     public void done() {
73         mDevice.update(Device.CHANGE_BUILD_INFO);
74     }
75 }
76