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