blob: 0de94d936f24addd1b7aef52081912cb4c558a07 [file] [log] [blame]
Wale Ogunwale387e4c62017-02-13 09:50:02 -08001/*
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
17package com.android.server.wm;
18
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +000019import static android.app.NotificationManager.IMPORTANCE_MIN;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080020import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
21import static android.content.Context.NOTIFICATION_SERVICE;
Wale Ogunwaled76881e2017-03-10 13:17:56 -080022import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
23import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Bernardo Rufino8fefb942019-11-25 19:51:09 +000024import static android.provider.Settings.ACTION_MANAGE_APP_OVERLAY_PERMISSION;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080025
26import android.app.Notification;
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +000027import android.app.NotificationChannel;
Wale Ogunwaleface3bb2017-06-01 10:22:36 -070028import android.app.NotificationChannelGroup;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080029import android.app.NotificationManager;
30import android.app.PendingIntent;
31import android.content.Context;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080032import android.content.Intent;
33import android.content.pm.ApplicationInfo;
34import android.content.pm.PackageManager;
35import android.graphics.Bitmap;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080036import android.graphics.drawable.Drawable;
Wale Ogunwaled76881e2017-03-10 13:17:56 -080037import android.net.Uri;
Julia Reynolds3c7de112018-03-28 09:33:13 -040038import android.os.Bundle;
39
Wale Ogunwale387e4c62017-02-13 09:50:02 -080040import com.android.internal.R;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080041import com.android.server.policy.IconUtilities;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080042
43/** Displays an ongoing notification for a process displaying an alert window */
44class AlertWindowNotification {
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +000045 private static final String CHANNEL_PREFIX = "com.android.server.wm.AlertWindowNotification - ";
Wale Ogunwale387e4c62017-02-13 09:50:02 -080046 private static final int NOTIFICATION_ID = 0;
47
48 private static int sNextRequestCode = 0;
Wale Ogunwaleface3bb2017-06-01 10:22:36 -070049 private static NotificationChannelGroup sChannelGroup;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080050 private final int mRequestCode;
51 private final WindowManagerService mService;
52 private String mNotificationTag;
53 private final NotificationManager mNotificationManager;
54 private final String mPackageName;
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070055 private boolean mPosted;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080056 private IconUtilities mIconUtilities;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080057
Wale Ogunwaled76881e2017-03-10 13:17:56 -080058 AlertWindowNotification(WindowManagerService service, String packageName) {
Wale Ogunwale387e4c62017-02-13 09:50:02 -080059 mService = service;
60 mPackageName = packageName;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080061 mNotificationManager =
62 (NotificationManager) mService.mContext.getSystemService(NOTIFICATION_SERVICE);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +000063 mNotificationTag = CHANNEL_PREFIX + mPackageName;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080064 mRequestCode = sNextRequestCode++;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080065 mIconUtilities = new IconUtilities(mService.mContext);
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070066 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -080067
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070068 void post() {
Wale Ogunwale387e4c62017-02-13 09:50:02 -080069 // 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 Ogunwale5aa86832017-02-28 10:40:27 -080071 mService.mH.post(this::onPostNotification);
Wale Ogunwale387e4c62017-02-13 09:50:02 -080072 }
73
74 /** Cancels the notification */
Wale Ogunwale6c8f2e42018-02-01 09:07:34 -080075 void cancel(boolean deleteChannel) {
Wale Ogunwale5aa86832017-02-28 10:40:27 -080076 // 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 Ogunwale6c8f2e42018-02-01 09:07:34 -080078 mService.mH.post(() -> onCancelNotification(deleteChannel));
Wale Ogunwale5aa86832017-02-28 10:40:27 -080079 }
80
81 /** Don't call with the window manager lock held! */
Wale Ogunwale6c8f2e42018-02-01 09:07:34 -080082 private void onCancelNotification(boolean deleteChannel) {
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070083 if (!mPosted) {
84 // Notification isn't currently posted...
85 return;
86 }
87 mPosted = false;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080088 mNotificationManager.cancel(mNotificationTag, NOTIFICATION_ID);
Wale Ogunwale6c8f2e42018-02-01 09:07:34 -080089 if (deleteChannel) {
90 mNotificationManager.deleteNotificationChannel(mNotificationTag);
91 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -080092 }
93
94 /** Don't call with the window manager lock held! */
Wale Ogunwale5aa86832017-02-28 10:40:27 -080095 private void onPostNotification() {
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070096 if (mPosted) {
97 // Notification already posted...
Wale Ogunwale5aa86832017-02-28 10:40:27 -080098 return;
99 }
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -0700100 mPosted = true;
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800101
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800102 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 Reynoldsf7321592017-05-24 16:09:19 -0400108 createNotificationChannel(context, appName);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000109
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800110 final String message = context.getString(R.string.alert_windows_notification_message,
111 appName);
Julia Reynolds3c7de112018-03-28 09:33:13 -0400112 Bundle extras = new Bundle();
113 extras.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[] {mPackageName});
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000114 final Notification.Builder builder = new Notification.Builder(context, mNotificationTag)
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800115 .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 Reynolds3c7de112018-03-28 09:33:13 -0400123 .addExtras(extras)
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800124 .setContentIntent(getContentIntent(context, mPackageName));
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800125
126 if (aInfo != null) {
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800127 final Drawable drawable = pm.getApplicationIcon(aInfo);
128 if (drawable != null) {
129 final Bitmap bitmap = mIconUtilities.createIconBitmap(drawable);
130 builder.setLargeIcon(bitmap);
131 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800132 }
133
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800134 mNotificationManager.notify(mNotificationTag, NOTIFICATION_ID, builder.build());
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800135 }
136
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800137 private PendingIntent getContentIntent(Context context, String packageName) {
Bernardo Rufino8fefb942019-11-25 19:51:09 +0000138 final Intent intent = new Intent(ACTION_MANAGE_APP_OVERLAY_PERMISSION,
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800139 Uri.fromParts("package", packageName, null));
140 intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800141 // Calls into activity manager...
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800142 return PendingIntent.getActivity(context, mRequestCode, intent, FLAG_CANCEL_CURRENT);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800143 }
144
Julia Reynoldsf7321592017-05-24 16:09:19 -0400145 private void createNotificationChannel(Context context, String appName) {
Wale Ogunwale4cd3f5c2017-06-08 16:51:48 -0700146 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 Ogunwaleaf38fd72017-05-23 01:43:59 +0000153 final String nameChannel =
154 context.getString(R.string.alert_windows_notification_channel_name, appName);
Wale Ogunwale6c8f2e42018-02-01 09:07:34 -0800155
156 NotificationChannel channel = mNotificationManager.getNotificationChannel(mNotificationTag);
157 if (channel != null) {
158 return;
159 }
160 channel = new NotificationChannel(mNotificationTag, nameChannel, IMPORTANCE_MIN);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000161 channel.enableLights(false);
162 channel.enableVibration(false);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400163 channel.setBlockableSystem(true);
Wale Ogunwaleface3bb2017-06-01 10:22:36 -0700164 channel.setGroup(sChannelGroup.getId());
Julia Reynoldsfd80cff2018-03-18 14:28:48 -0400165 channel.setBypassDnd(true);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000166 mNotificationManager.createNotificationChannel(channel);
167 }
168
169
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800170 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}