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 | face3bb | 2017-06-01 10:22:36 -0700 | [diff] [blame^] | 66 | if (sChannelGroup == null) { |
| 67 | sChannelGroup = new NotificationChannelGroup(CHANNEL_PREFIX, |
| 68 | mService.mContext.getString( |
| 69 | R.string.alert_windows_notification_channel_group_name)); |
| 70 | mNotificationManager.createNotificationChannelGroup(sChannelGroup); |
| 71 | } |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 72 | } |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 73 | |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 74 | void post() { |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 75 | // We can't create/post the notification while the window manager lock is held since it will |
| 76 | // 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] | 77 | mService.mH.post(this::onPostNotification); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** Cancels the notification */ |
| 81 | void cancel() { |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 82 | // We can't call into NotificationManager with WM lock held since it might call into AM. |
| 83 | // So, we post a message to do it later. |
| 84 | mService.mH.post(this::onCancelNotification); |
| 85 | } |
| 86 | |
| 87 | /** Don't call with the window manager lock held! */ |
| 88 | private void onCancelNotification() { |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 89 | if (!mPosted) { |
| 90 | // Notification isn't currently posted... |
| 91 | return; |
| 92 | } |
| 93 | mPosted = false; |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 94 | mNotificationManager.cancel(mNotificationTag, NOTIFICATION_ID); |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** Don't call with the window manager lock held! */ |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 98 | private void onPostNotification() { |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 99 | if (mPosted) { |
| 100 | // Notification already posted... |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 101 | return; |
| 102 | } |
Wale Ogunwale | a10fc7e | 2017-04-06 07:09:51 -0700 | [diff] [blame] | 103 | mPosted = true; |
Wale Ogunwale | 5aa8683 | 2017-02-28 10:40:27 -0800 | [diff] [blame] | 104 | |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 105 | final Context context = mService.mContext; |
| 106 | final PackageManager pm = context.getPackageManager(); |
| 107 | final ApplicationInfo aInfo = getApplicationInfo(pm, mPackageName); |
| 108 | final String appName = (aInfo != null) |
| 109 | ? pm.getApplicationLabel(aInfo).toString() : mPackageName; |
| 110 | |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 111 | createNotificationChannel(context, appName); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 112 | |
Wale Ogunwale | d76881e | 2017-03-10 13:17:56 -0800 | [diff] [blame] | 113 | final String message = context.getString(R.string.alert_windows_notification_message, |
| 114 | appName); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 115 | final Notification.Builder builder = new Notification.Builder(context, mNotificationTag) |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 116 | .setOngoing(true) |
| 117 | .setContentTitle( |
| 118 | context.getString(R.string.alert_windows_notification_title, appName)) |
| 119 | .setContentText(message) |
| 120 | .setSmallIcon(R.drawable.alert_window_layer) |
| 121 | .setColor(context.getColor(R.color.system_notification_accent_color)) |
| 122 | .setStyle(new Notification.BigTextStyle().bigText(message)) |
| 123 | .setLocalOnly(true) |
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) { |
| 138 | final Intent intent = new Intent(ACTION_MANAGE_OVERLAY_PERMISSION, |
| 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 | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 146 | final String nameChannel = |
| 147 | context.getString(R.string.alert_windows_notification_channel_name, appName); |
| 148 | final NotificationChannel channel = |
| 149 | new NotificationChannel(mNotificationTag, nameChannel, IMPORTANCE_MIN); |
| 150 | channel.enableLights(false); |
| 151 | channel.enableVibration(false); |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 152 | channel.setBlockableSystem(true); |
Wale Ogunwale | face3bb | 2017-06-01 10:22:36 -0700 | [diff] [blame^] | 153 | channel.setGroup(sChannelGroup.getId()); |
Wale Ogunwale | af38fd7 | 2017-05-23 01:43:59 +0000 | [diff] [blame] | 154 | mNotificationManager.createNotificationChannel(channel); |
| 155 | } |
| 156 | |
| 157 | |
Wale Ogunwale | 387e4c6 | 2017-02-13 09:50:02 -0800 | [diff] [blame] | 158 | private ApplicationInfo getApplicationInfo(PackageManager pm, String packageName) { |
| 159 | try { |
| 160 | return pm.getApplicationInfo(packageName, 0); |
| 161 | } catch (PackageManager.NameNotFoundException e) { |
| 162 | return null; |
| 163 | } |
| 164 | } |
| 165 | } |