• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ActivityManager;
20 import android.app.ITransientNotificationCallback;
21 import android.content.Context;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager;
24 import android.content.res.Resources;
25 import android.os.IBinder;
26 import android.util.Log;
27 
28 import androidx.annotation.Nullable;
29 
30 import com.android.systemui.R;
31 import com.android.systemui.dagger.SysUISingleton;
32 import com.android.systemui.dagger.qualifiers.Main;
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 PackageManager mPackageManager;
53     private final Set<String> mPackageNameAllowList;
54 
55     @Inject
CarToastUI(Context context, @Main Resources resources, CommandQueue commandQueue, ToastFactory toastFactory, ToastLogger toastLogger, PackageManager packageManager)56     public CarToastUI(Context context, @Main Resources resources, CommandQueue commandQueue,
57             ToastFactory toastFactory, ToastLogger toastLogger, PackageManager packageManager) {
58         super(context, commandQueue, toastFactory, toastLogger);
59         mPackageManager = packageManager;
60 
61         String[] allowList = resources.getStringArray(
62                 R.array.config_restrictedToastsPackageNameAllowList);
63         mPackageNameAllowList = new HashSet<>(Arrays.asList(allowList));
64     }
65 
66     @Override
showToast(int uid, String packageName, IBinder token, CharSequence text, IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback, int displayId)67     public void showToast(int uid, String packageName, IBinder token, CharSequence text,
68             IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback,
69             int displayId) {
70         if (!isAllowListed(packageName) && !isSystemPrivilegedOrPlatformKey(packageName)) {
71             if (DEBUG) {
72                 Log.w(TAG, packageName
73                         + " cannot show a Toast since it is not allow listed and it isn't "
74                         + "privileged.");
75             }
76             return;
77         }
78         if (DEBUG) {
79             Log.d(TAG,
80                     packageName
81                             + " is a system privileged app or has been signed with platform key.");
82         }
83 
84         super.showToast(uid, packageName, token, text, windowToken, duration, callback, displayId);
85     }
86 
isAllowListed(String packageName)87     private boolean isAllowListed(String packageName) {
88         return mPackageNameAllowList.contains(packageName);
89     }
90 
isSystemPrivilegedOrPlatformKey(String packageName)91     private boolean isSystemPrivilegedOrPlatformKey(String packageName) {
92         ApplicationInfo applicationInfo = getApplicationInfo(packageName);
93         if (applicationInfo == null) return false;
94 
95         return (applicationInfo.isSignedWithPlatformKey() || (
96                 applicationInfo.isSystemApp()
97                         && applicationInfo.isPrivilegedApp()));
98     }
99 
getApplicationInfo(String packageName)100     private ApplicationInfo getApplicationInfo(String packageName) {
101         ApplicationInfo applicationInfo = null;
102         try {
103             applicationInfo = mPackageManager.getApplicationInfoAsUser(packageName, /* flags= */ 0,
104                     ActivityManager.getCurrentUser());
105         } catch (PackageManager.NameNotFoundException ex) {
106             Log.e(TAG, "package not found: " + packageName);
107         }
108         return applicationInfo;
109     }
110 }
111