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.DeviceInfoActivity;
23 
24 /**
25  * PackageDeviceInfo collector.
26  */
27 public class PackageDeviceInfo extends DeviceInfoActivity {
28 
29     private static final String PACKAGE = "package";
30     private static final String NAME = "name";
31     private static final String VERSION_NAME = "version_name";
32     private static final String SYSTEM_PRIV = "system_priv";
33     private static final String PRIV_APP_DIR = "/system/priv-app";
34 
35     @Override
collectDeviceInfo()36     protected void collectDeviceInfo() {
37         PackageManager pm = this.getPackageManager();
38         startArray(PACKAGE);
39         for (PackageInfo pkg : pm.getInstalledPackages(0)) {
40             startGroup();
41             addResult(NAME, pkg.packageName);
42             addResult(VERSION_NAME, pkg.versionName);
43 
44             String dir = pkg.applicationInfo.sourceDir;
45             addResult(SYSTEM_PRIV, dir != null && dir.startsWith(PRIV_APP_DIR));
46             endGroup();
47         }
48         endArray(); // Package
49     }
50 }
51