1 /*
2  * Copyright (C) 2023 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.server.contentprotection;
18 
19 import android.Manifest;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.content.Context;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageInfo;
25 import android.content.pm.PackageManager;
26 import android.content.pm.PackageManager.NameNotFoundException;
27 import android.content.pm.PackageManager.PackageInfoFlags;
28 import android.util.Slog;
29 
30 import java.util.Arrays;
31 
32 /**
33  * Basic package manager for content protection using content capture.
34  *
35  * @hide
36  */
37 public class ContentProtectionPackageManager {
38 
39     private static final String TAG = "ContentProtectionPackageManager";
40 
41     private static final PackageInfoFlags PACKAGE_INFO_FLAGS =
42             PackageInfoFlags.of(PackageManager.GET_PERMISSIONS);
43 
44     @NonNull private final PackageManager mPackageManager;
45 
ContentProtectionPackageManager(@onNull Context context)46     public ContentProtectionPackageManager(@NonNull Context context) {
47         mPackageManager = context.getPackageManager();
48     }
49 
50     @Nullable
getPackageInfo(@onNull String packageName)51     public PackageInfo getPackageInfo(@NonNull String packageName) {
52         try {
53             return mPackageManager.getPackageInfo(packageName, PACKAGE_INFO_FLAGS);
54         } catch (NameNotFoundException ex) {
55             Slog.w(TAG, "Package info not found for: " + packageName, ex);
56             return null;
57         }
58     }
59 
isSystemApp(@onNull PackageInfo packageInfo)60     public boolean isSystemApp(@NonNull PackageInfo packageInfo) {
61         return packageInfo.applicationInfo != null && isSystemApp(packageInfo.applicationInfo);
62     }
63 
isSystemApp(@onNull ApplicationInfo applicationInfo)64     private boolean isSystemApp(@NonNull ApplicationInfo applicationInfo) {
65         return (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
66     }
67 
isUpdatedSystemApp(@onNull PackageInfo packageInfo)68     public boolean isUpdatedSystemApp(@NonNull PackageInfo packageInfo) {
69         return packageInfo.applicationInfo != null
70                 && isUpdatedSystemApp(packageInfo.applicationInfo);
71     }
72 
isUpdatedSystemApp(@onNull ApplicationInfo applicationInfo)73     private boolean isUpdatedSystemApp(@NonNull ApplicationInfo applicationInfo) {
74         return (applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
75     }
76 
hasRequestedInternetPermissions(@onNull PackageInfo packageInfo)77     public boolean hasRequestedInternetPermissions(@NonNull PackageInfo packageInfo) {
78         return packageInfo.requestedPermissions != null
79                 && Arrays.asList(packageInfo.requestedPermissions)
80                         .contains(Manifest.permission.INTERNET);
81     }
82 }
83