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; |
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); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 112 | final Notification.Builder builder = new Notification.Builder(context, mNotificationTag) |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 113 | .setOngoing(true) |
| 114 | .setContentTitle( |
| 115 | context.getString(R.string.alert_windows_notification_title, appName)) |
| 116 | .setContentText(message) |
| 117 | .setSmallIcon(R.drawable.alert_window_layer) |
| 118 | .setColor(context.getColor(R.color.system_notification_accent_color)) |
| 119 | .setStyle(new Notification.BigTextStyle().bigText(message)) |
| 120 | .setLocalOnly(true) |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 121 | .setContentIntent(getContentIntent(context, mPackageName)); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 122 | |
| 123 | if (aInfo != null) { |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 124 | final Drawable drawable = pm.getApplicationIcon(aInfo); |
| 125 | if (drawable != null) { |
| 126 | final Bitmap bitmap = mIconUtilities.createIconBitmap(drawable); |
| 127 | builder.setLargeIcon(bitmap); |
| 128 | } |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 131 | mNotificationManager.notify(mNotificationTag, NOTIFICATION_ID, builder.build()); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 134 | private PendingIntent getContentIntent(Context context, String packageName) { |
| 135 | final Intent intent = new Intent(ACTION_MANAGE_OVERLAY_PERMISSION, |
| 136 | Uri.fromParts("package", packageName, null)); |
| 137 | intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 138 | // Calls into activity manager... |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 139 | return PendingIntent.getActivity(context, mRequestCode, intent, FLAG_CANCEL_CURRENT); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 142 | private void createNotificationChannel(Context context, String appName) { |
Wale Ogunwale | 4cd3f5c | 2017-06-08 16:51:48 -0700 | [diff] [blame] | 143 | if (sChannelGroup == null) { |
| 144 | sChannelGroup = new NotificationChannelGroup(CHANNEL_PREFIX, |
| 145 | mService.mContext.getString( |
| 146 | R.string.alert_windows_notification_channel_group_name)); |
| 147 | mNotificationManager.createNotificationChannelGroup(sChannelGroup); |
| 148 | } |
| 149 | |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 150 | final String nameChannel = |
| 151 | context.getString(R.string.alert_windows_notification_channel_name, appName); |
Wale Ogunwale | 6c8f2e4 | 2018-02-01 09:07:34 -0800 | [diff] [blame^] | 152 | |
| 153 | NotificationChannel channel = mNotificationManager.getNotificationChannel(mNotificationTag); |
| 154 | if (channel != null) { |
| 155 | return; |
| 156 | } |
| 157 | channel = new NotificationChannel(mNotificationTag, nameChannel, IMPORTANCE_MIN); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 158 | channel.enableLights(false); |
| 159 | channel.enableVibration(false); |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 160 | channel.setBlockableSystem(true); |
Wale Ogunwale | face3bb | 2017-06-01 10:22:36 -0700 | [diff] [blame] | 161 | channel.setGroup(sChannelGroup.getId()); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 162 | mNotificationManager.createNotificationChannel(channel); |
| 163 | } |
| 164 | |
| 165 | |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 166 | private ApplicationInfo getApplicationInfo(PackageManager pm, String packageName) { |
| 167 | try { |
| 168 | return pm.getApplicationInfo(packageName, 0); |
| 169 | } catch (PackageManager.NameNotFoundException e) { |
| 170 | return null; |
| 171 | } |
| 172 | } |
| 173 | } |