• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.car.notification;
18 
19 import android.car.userlib.CarUserManagerHelper;
20 import android.content.Context;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.service.notification.StatusBarNotification;
24 import android.util.Log;
25 
26 public class NotificationUtils {
27     private static final String TAG = "NotificationUtils";
28 
NotificationUtils()29     private NotificationUtils() {
30     }
31 
32     /**
33      * Validates if the notification posted by the application meets at least one of the below
34      * conditions.
35      *
36      * <ul>
37      * <li>application is signed with platform key.
38      * <li>application is a system and privileged app.
39      * </ul>
40      */
isSystemPrivilegedOrPlatformKey(Context context, StatusBarNotification statusBarNotification)41     public static boolean isSystemPrivilegedOrPlatformKey(Context context,
42             StatusBarNotification statusBarNotification) {
43         return isSystemPrivilegedOrPlatformKeyInner(context, statusBarNotification,
44                 /* checkForPrivilegedApp= */ true);
45     }
46 
47     /**
48      * Validates if the notification posted by the application meets at least one of the below
49      * conditions.
50      *
51      * <ul>
52      * <li>application is signed with platform key.
53      * <li>application is a system app.
54      * </ul>
55      */
isSystemOrPlatformKey(Context context, StatusBarNotification statusBarNotification)56     public static boolean isSystemOrPlatformKey(Context context,
57             StatusBarNotification statusBarNotification) {
58         return isSystemPrivilegedOrPlatformKeyInner(context, statusBarNotification,
59                 /* checkForPrivilegedApp= */ false);
60     }
61 
62     /**
63      * Validates if the notification posted by the application is a system application.
64      */
isSystemApp(Context context, StatusBarNotification statusBarNotification)65     public static boolean isSystemApp(Context context,
66             StatusBarNotification statusBarNotification) {
67         PackageManager packageManager = context.getPackageManager();
68         CarUserManagerHelper carUserManagerHelper = new CarUserManagerHelper(context);
69         PackageInfo packageInfo = null;
70         try {
71             packageInfo = packageManager.getPackageInfoAsUser(
72                     statusBarNotification.getPackageName(), /* flags= */ 0,
73                     carUserManagerHelper.getCurrentForegroundUserId());
74         } catch (PackageManager.NameNotFoundException ex) {
75             Log.e(TAG, "package not found: " + statusBarNotification.getPackageName());
76         }
77         if (packageInfo == null) return false;
78 
79         return packageInfo.applicationInfo.isSystemApp();
80     }
81 
82     /**
83      * Validates if the notification posted by the application is signed with platform key.
84      */
isSignedWithPlatformKey(Context context, StatusBarNotification statusBarNotification)85     public static boolean isSignedWithPlatformKey(Context context,
86             StatusBarNotification statusBarNotification) {
87         PackageManager packageManager = context.getPackageManager();
88         CarUserManagerHelper carUserManagerHelper = new CarUserManagerHelper(context);
89         PackageInfo packageInfo = null;
90         try {
91             packageInfo = packageManager.getPackageInfoAsUser(
92                     statusBarNotification.getPackageName(), /* flags= */ 0,
93                     carUserManagerHelper.getCurrentForegroundUserId());
94         } catch (PackageManager.NameNotFoundException ex) {
95             Log.e(TAG, "package not found: " + statusBarNotification.getPackageName());
96         }
97         if (packageInfo == null) return false;
98 
99         return packageInfo.applicationInfo.isSignedWithPlatformKey();
100     }
101 
isSystemPrivilegedOrPlatformKeyInner(Context context, StatusBarNotification statusBarNotification, boolean checkForPrivilegedApp)102     private static boolean isSystemPrivilegedOrPlatformKeyInner(Context context,
103             StatusBarNotification statusBarNotification, boolean checkForPrivilegedApp) {
104         PackageManager packageManager = context.getPackageManager();
105         CarUserManagerHelper carUserManagerHelper = new CarUserManagerHelper(context);
106         PackageInfo packageInfo = null;
107         try {
108             packageInfo = packageManager.getPackageInfoAsUser(
109                     statusBarNotification.getPackageName(), /* flags= */ 0,
110                     carUserManagerHelper.getCurrentForegroundUserId());
111         } catch (PackageManager.NameNotFoundException ex) {
112             Log.e(TAG, "package not found: " + statusBarNotification.getPackageName());
113         }
114         if (packageInfo == null) return false;
115 
116         // Only include the privilegedApp check if the caller wants this check.
117         boolean isPrivilegedApp =
118                 (!checkForPrivilegedApp) || packageInfo.applicationInfo.isPrivilegedApp();
119 
120         return (packageInfo.applicationInfo.isSignedWithPlatformKey() ||
121                 (packageInfo.applicationInfo.isSystemApp()
122                         && isPrivilegedApp));
123     }
124 }
125