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.content.pm.PackageInfo;
19 import android.content.pm.PackageManager;
20 import android.os.Bundle;
21 
22 import com.android.compatibility.common.deviceinfo.DeviceInfo;
23 import com.android.compatibility.common.util.DeviceInfoStore;
24 
25 /**
26  * PackageDeviceInfo collector.
27  */
28 public class PackageDeviceInfo extends DeviceInfo {
29 
30     private static final String PACKAGE = "package";
31     private static final String NAME = "name";
32     private static final String VERSION_NAME = "version_name";
33     private static final String SYSTEM_PRIV = "system_priv";
34     private static final String PRIV_APP_DIR = "/system/priv-app";
35 
36     @Override
collectDeviceInfo(DeviceInfoStore store)37     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
38         PackageManager pm = getContext().getPackageManager();
39         store.startArray(PACKAGE);
40         for (PackageInfo pkg : pm.getInstalledPackages(0)) {
41             store.startGroup();
42             store.addResult(NAME, pkg.packageName);
43             store.addResult(VERSION_NAME, pkg.versionName);
44 
45             if (pkg.applicationInfo != null) {
46                 String dir = pkg.applicationInfo.sourceDir;
47                 store.addResult(SYSTEM_PRIV, dir != null && dir.startsWith(PRIV_APP_DIR));
48             }
49             store.endGroup();
50         }
51         store.endArray(); // Package
52     }
53 }
54