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 package com.android.compatibility.common.deviceinfo;
17 
18 import android.util.Log;
19 
20 import com.android.compatibility.common.util.DeviceInfoStore;
21 
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.Scanner;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 
28 /**
29  * System property info collector.
30  */
31 public final class PropertyDeviceInfo extends DeviceInfo {
32 
33     private static final String LOG_TAG = "PropertyDeviceInfo";
34 
35     @Override
collectDeviceInfo(DeviceInfoStore store)36     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
37         try {
38             collectRoProperties(store);
39         } catch (IOException e) {
40             Log.w(LOG_TAG, "Failed to collect properties", e);
41         }
42     }
43 
collectRoProperties(DeviceInfoStore store)44     private void collectRoProperties(DeviceInfoStore store) throws IOException {
45         store.startArray("ro_property");
46         Pattern pattern = Pattern.compile("\\[(ro.+)\\]: \\[(.+)\\]");
47         Scanner scanner = null;
48         try {
49             Process getprop = new ProcessBuilder("getprop").start();
50             scanner = new Scanner(getprop.getInputStream());
51             while (scanner.hasNextLine()) {
52                 String line = scanner.nextLine();
53                 Matcher matcher = pattern.matcher(line);
54                 if (matcher.matches()) {
55                     String name = matcher.group(1);
56                     String value = matcher.group(2);
57 
58                     store.startGroup();
59                     store.addResult("name", name);
60                     store.addResult("value", value);
61                     store.endGroup();
62                 }
63             }
64         } finally {
65             store.endArray();
66             if (scanner != null) {
67                 scanner.close();
68             }
69         }
70     }
71 }
72