1 /*
2  * Copyright (C) 2020 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.systemui.car.toast;
18 
19 import android.app.ITransientNotificationCallback;
20 import android.content.Context;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.content.res.Resources;
24 import android.os.IBinder;
25 import android.util.Log;
26 
27 import androidx.annotation.Nullable;
28 
29 import com.android.systemui.R;
30 import com.android.systemui.dagger.SysUISingleton;
31 import com.android.systemui.dagger.qualifiers.Main;
32 import com.android.systemui.settings.UserTracker;
33 import com.android.systemui.statusbar.CommandQueue;
34 import com.android.systemui.toast.ToastFactory;
35 import com.android.systemui.toast.ToastLogger;
36 import com.android.systemui.toast.ToastUI;
37 
38 import java.util.Arrays;
39 import java.util.HashSet;
40 import java.util.Set;
41 
42 import javax.inject.Inject;
43 
44 /**
45  * Controls display of text toasts in AAOS.
46  */
47 @SysUISingleton
48 public class CarToastUI extends ToastUI {
49     private static final boolean DEBUG = false;
50     private static final String TAG = "CarToastUI";
51 
52     private final UserTracker mUserTracker;
53     private final PackageManager mPackageManager;
54     private final Set<String> mPackageNameAllowList;
55 
56     @Inject
CarToastUI(Context context, @Main Resources resources, CommandQueue commandQueue, ToastFactory toastFactory, ToastLogger toastLogger, UserTracker userTracker, PackageManager packageManager)57     public CarToastUI(Context context, @Main Resources resources, CommandQueue commandQueue,
58             ToastFactory toastFactory, ToastLogger toastLogger, UserTracker userTracker,
59             PackageManager packageManager) {
60         super(context, commandQueue, toastFactory, toastLogger);
61         mUserTracker = userTracker;
62         mPackageManager = packageManager;
63 
64         String[] allowList = resources.getStringArray(
65                 R.array.config_restrictedToastsPackageNameAllowList);
66         mPackageNameAllowList = new HashSet<>(Arrays.asList(allowList));
67     }
68 
69     @Override
showToast(int uid, String packageName, IBinder token, CharSequence text, IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback, int displayId)70     public void showToast(int uid, String packageName, IBinder token, CharSequence text,
71             IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback,
72             int displayId) {
73         if (!isAllowListed(packageName) && !isSystemPrivilegedOrPlatformKey(packageName)) {
74             if (DEBUG) {
75                 Log.w(TAG, packageName
76                         + " cannot show a Toast since it is not allow listed and it isn't "
77                         + "privileged.");
78             }
79             return;
80         }
81         if (DEBUG) {
82             Log.d(TAG,
83                     packageName
84                             + " is a system privileged app or has been signed with platform key.");
85         }
86 
87         super.showToast(uid, packageName, token, text, windowToken, duration, callback, displayId);
88     }
89 
isAllowListed(String packageName)90     private boolean isAllowListed(String packageName) {
91         return mPackageNameAllowList.contains(packageName);
92     }
93 
isSystemPrivilegedOrPlatformKey(String packageName)94     private boolean isSystemPrivilegedOrPlatformKey(String packageName) {
95         ApplicationInfo applicationInfo = getApplicationInfo(packageName);
96         if (applicationInfo == null) return false;
97 
98         return (applicationInfo.isSignedWithPlatformKey() || (
99                 applicationInfo.isSystemApp()
100                         && applicationInfo.isPrivilegedApp()));
101     }
102 
getApplicationInfo(String packageName)103     private ApplicationInfo getApplicationInfo(String packageName) {
104         ApplicationInfo applicationInfo = null;
105         try {
106             applicationInfo = mPackageManager.getApplicationInfoAsUser(packageName, /* flags= */ 0,
107                     mUserTracker.getUserId());
108         } catch (PackageManager.NameNotFoundException ex) {
109             Log.e(TAG, "package not found: " + packageName);
110         }
111         return applicationInfo;
112     }
113 }
114