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)67 public void showToast(int uid, String packageName, IBinder token, CharSequence text, 68 IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback) { 69 if (!isAllowListed(packageName) && !isSystemPrivilegedOrPlatformKey(packageName)) { 70 if (DEBUG) { 71 Log.w(TAG, packageName 72 + " cannot show a Toast since it is not allow listed and it isn't " 73 + "privileged."); 74 } 75 return; 76 } 77 if (DEBUG) { 78 Log.d(TAG, 79 packageName 80 + " is a system privileged app or has been signed with platform key."); 81 } 82 83 super.showToast(uid, packageName, token, text, windowToken, duration, callback); 84 } 85 isAllowListed(String packageName)86 private boolean isAllowListed(String packageName) { 87 return mPackageNameAllowList.contains(packageName); 88 } 89 isSystemPrivilegedOrPlatformKey(String packageName)90 private boolean isSystemPrivilegedOrPlatformKey(String packageName) { 91 ApplicationInfo applicationInfo = getApplicationInfo(packageName); 92 if (applicationInfo == null) return false; 93 94 return (applicationInfo.isSignedWithPlatformKey() || ( 95 applicationInfo.isSystemApp() 96 && applicationInfo.isPrivilegedApp())); 97 } 98 getApplicationInfo(String packageName)99 private ApplicationInfo getApplicationInfo(String packageName) { 100 ApplicationInfo applicationInfo = null; 101 try { 102 applicationInfo = mPackageManager.getApplicationInfoAsUser(packageName, /* flags= */ 0, 103 ActivityManager.getCurrentUser()); 104 } catch (PackageManager.NameNotFoundException ex) { 105 Log.e(TAG, "package not found: " + packageName); 106 } 107 return applicationInfo; 108 } 109 } 110