Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.wm; |
| 18 | |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 20 | import static android.app.PendingIntent.FLAG_CANCEL_CURRENT; |
| 21 | import static android.content.Context.NOTIFICATION_SERVICE; |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 22 | import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK; |
| 23 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
Bernardo Rufino | 8fefb94 | 2019-11-25 19:51:09 +0000 | [diff] [blame] | 24 | import static android.provider.Settings.ACTION_MANAGE_APP_OVERLAY_PERMISSION; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 25 | |
| 26 | import android.app.Notification; |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 27 | import android.app.NotificationChannel; |
Wale Ogunwale | face3bb | 2017-06-01 10:22:36 -0700 | [diff] [blame] | 28 | import android.app.NotificationChannelGroup; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 29 | import android.app.NotificationManager; |
| 30 | import android.app.PendingIntent; |
| 31 | import android.content.Context; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 32 | import android.content.Intent; |
| 33 | import android.content.pm.ApplicationInfo; |
| 34 | import android.content.pm.PackageManager; |
| 35 | import android.graphics.Bitmap; |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 36 | import android.graphics.drawable.Drawable; |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 37 | import android.net.Uri; |
Julia Reynolds | 3c7de11 | 2018-03-28 09:33:13 -0400 | [diff] [blame] | 38 | import android.os.Bundle; |
| 39 | |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 40 | import com.android.internal.R; |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 41 | import com.android.server.policy.IconUtilities; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 42 | |
| 43 | /** Displays an ongoing notification for a process displaying an alert window */ |
| 44 | class AlertWindowNotification { |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 45 | private static final String CHANNEL_PREFIX = "com.android.server.wm.AlertWindowNotification - "; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 46 | private static final int NOTIFICATION_ID = 0; |
| 47 | |
| 48 | private static int sNextRequestCode = 0; |
Wale Ogunwale | face3bb | 2017-06-01 10:22:36 -0700 | [diff] [blame] | 49 | private static NotificationChannelGroup sChannelGroup; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 50 | private final int mRequestCode; |
| 51 | private final WindowManagerService mService; |
| 52 | private String mNotificationTag; |
| 53 | private final NotificationManager mNotificationManager; |
| 54 | private final String mPackageName; |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 55 | private boolean mPosted; |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 56 | private IconUtilities mIconUtilities; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 57 | |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 58 | AlertWindowNotification(WindowManagerService service, String packageName) { |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 59 | mService = service; |
| 60 | mPackageName = packageName; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 61 | mNotificationManager = |
| 62 | (NotificationManager) mService.mContext.getSystemService(NOTIFICATION_SERVICE); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 63 | mNotificationTag = CHANNEL_PREFIX + mPackageName; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 64 | mRequestCode = sNextRequestCode++; |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 65 | mIconUtilities = new IconUtilities(mService.mContext); |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 66 | } |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 67 | |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 68 | void post() { |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 69 | // We can't create/post the notification while the window manager lock is held since it will |
| 70 | // end up calling into activity manager. So, we post a message to do it later. |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 71 | mService.mH.post(this::onPostNotification); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** Cancels the notification */ |
Wale Ogunwale | 6c8f2e4 | 2018-02-01 09:07:34 -0800 | [diff] [blame] | 75 | void cancel(boolean deleteChannel) { |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 76 | // We can't call into NotificationManager with WM lock held since it might call into AM. |
| 77 | // So, we post a message to do it later. |
Wale Ogunwale | 6c8f2e4 | 2018-02-01 09:07:34 -0800 | [diff] [blame] | 78 | mService.mH.post(() -> onCancelNotification(deleteChannel)); |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** Don't call with the window manager lock held! */ |
Wale Ogunwale | 6c8f2e4 | 2018-02-01 09:07:34 -0800 | [diff] [blame] | 82 | private void onCancelNotification(boolean deleteChannel) { |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 83 | if (!mPosted) { |
| 84 | // Notification isn't currently posted... |
| 85 | return; |
| 86 | } |
| 87 | mPosted = false; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 88 | mNotificationManager.cancel(mNotificationTag, NOTIFICATION_ID); |
Wale Ogunwale | 6c8f2e4 | 2018-02-01 09:07:34 -0800 | [diff] [blame] | 89 | if (deleteChannel) { |
| 90 | mNotificationManager.deleteNotificationChannel(mNotificationTag); |
| 91 | } |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** Don't call with the window manager lock held! */ |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 95 | private void onPostNotification() { |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 96 | if (mPosted) { |
| 97 | // Notification already posted... |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 98 | return; |
| 99 | } |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 100 | mPosted = true; |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 101 | |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 102 | final Context context = mService.mContext; |
| 103 | final PackageManager pm = context.getPackageManager(); |
| 104 | final ApplicationInfo aInfo = getApplicationInfo(pm, mPackageName); |
| 105 | final String appName = (aInfo != null) |
| 106 | ? pm.getApplicationLabel(aInfo).toString() : mPackageName; |
| 107 | |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 108 | createNotificationChannel(context, appName); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 109 | |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 110 | final String message = context.getString(R.string.alert_windows_notification_message, |
| 111 | appName); |
Julia Reynolds | 3c7de11 | 2018-03-28 09:33:13 -0400 | [diff] [blame] | 112 | Bundle extras = new Bundle(); |
| 113 | extras.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[] {mPackageName}); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 114 | final Notification.Builder builder = new Notification.Builder(context, mNotificationTag) |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 115 | .setOngoing(true) |
| 116 | .setContentTitle( |
| 117 | context.getString(R.string.alert_windows_notification_title, appName)) |
| 118 | .setContentText(message) |
| 119 | .setSmallIcon(R.drawable.alert_window_layer) |
| 120 | .setColor(context.getColor(R.color.system_notification_accent_color)) |
| 121 | .setStyle(new Notification.BigTextStyle().bigText(message)) |
| 122 | .setLocalOnly(true) |
Julia Reynolds | 3c7de11 | 2018-03-28 09:33:13 -0400 | [diff] [blame] | 123 | .addExtras(extras) |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 124 | .setContentIntent(getContentIntent(context, mPackageName)); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 125 | |
| 126 | if (aInfo != null) { |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 127 | final Drawable drawable = pm.getApplicationIcon(aInfo); |
| 128 | if (drawable != null) { |
| 129 | final Bitmap bitmap = mIconUtilities.createIconBitmap(drawable); |
| 130 | builder.setLargeIcon(bitmap); |
| 131 | } |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 134 | mNotificationManager.notify(mNotificationTag, NOTIFICATION_ID, builder.build()); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 137 | private PendingIntent getContentIntent(Context context, String packageName) { |
Bernardo Rufino | 8fefb94 | 2019-11-25 19:51:09 +0000 | [diff] [blame] | 138 | final Intent intent = new Intent(ACTION_MANAGE_APP_OVERLAY_PERMISSION, |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 139 | Uri.fromParts("package", packageName, null)); |
| 140 | intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 141 | // Calls into activity manager... |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 142 | return PendingIntent.getActivity(context, mRequestCode, intent, FLAG_CANCEL_CURRENT); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 145 | private void createNotificationChannel(Context context, String appName) { |
Wale Ogunwale | 4cd3f5c | 2017-06-08 16:51:48 -0700 | [diff] [blame] | 146 | if (sChannelGroup == null) { |
| 147 | sChannelGroup = new NotificationChannelGroup(CHANNEL_PREFIX, |
| 148 | mService.mContext.getString( |
| 149 | R.string.alert_windows_notification_channel_group_name)); |
| 150 | mNotificationManager.createNotificationChannelGroup(sChannelGroup); |
| 151 | } |
| 152 | |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 153 | final String nameChannel = |
| 154 | context.getString(R.string.alert_windows_notification_channel_name, appName); |
Wale Ogunwale | 6c8f2e4 | 2018-02-01 09:07:34 -0800 | [diff] [blame] | 155 | |
| 156 | NotificationChannel channel = mNotificationManager.getNotificationChannel(mNotificationTag); |
| 157 | if (channel != null) { |
| 158 | return; |
| 159 | } |
| 160 | channel = new NotificationChannel(mNotificationTag, nameChannel, IMPORTANCE_MIN); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 161 | channel.enableLights(false); |
| 162 | channel.enableVibration(false); |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 163 | channel.setBlockableSystem(true); |
Wale Ogunwale | face3bb | 2017-06-01 10:22:36 -0700 | [diff] [blame] | 164 | channel.setGroup(sChannelGroup.getId()); |
Julia Reynolds | fd80cff | 2018-03-18 14:28:48 -0400 | [diff] [blame] | 165 | channel.setBypassDnd(true); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 166 | mNotificationManager.createNotificationChannel(channel); |
| 167 | } |
| 168 | |
| 169 | |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 170 | private ApplicationInfo getApplicationInfo(PackageManager pm, String packageName) { |
| 171 | try { |
| 172 | return pm.getApplicationInfo(packageName, 0); |
| 173 | } catch (PackageManager.NameNotFoundException e) { |
| 174 | return null; |
| 175 | } |
| 176 | } |
| 177 | } |