blob: 9177d253e4642ad20e1491e91b8c467ad1885526 [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;
24import static android.provider.Settings.ACTION_MANAGE_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;
32
33import android.content.Intent;
34import android.content.pm.ApplicationInfo;
35import android.content.pm.PackageManager;
36import android.graphics.Bitmap;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080037import android.graphics.drawable.Drawable;
38
Wale Ogunwaled76881e2017-03-10 13:17:56 -080039import android.net.Uri;
Julia Reynolds3c7de112018-03-28 09:33:13 -040040import android.os.Bundle;
41
Wale Ogunwale387e4c62017-02-13 09:50:02 -080042import com.android.internal.R;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080043import com.android.server.policy.IconUtilities;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080044
45/** Displays an ongoing notification for a process displaying an alert window */
46class AlertWindowNotification {
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +000047 private static final String CHANNEL_PREFIX = "com.android.server.wm.AlertWindowNotification - ";
Wale Ogunwale387e4c62017-02-13 09:50:02 -080048 private static final int NOTIFICATION_ID = 0;
49
50 private static int sNextRequestCode = 0;
Wale Ogunwaleface3bb2017-06-01 10:22:36 -070051 private static NotificationChannelGroup sChannelGroup;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080052 private final int mRequestCode;
53 private final WindowManagerService mService;
54 private String mNotificationTag;
55 private final NotificationManager mNotificationManager;
56 private final String mPackageName;
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070057 private boolean mPosted;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080058 private IconUtilities mIconUtilities;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080059
Wale Ogunwaled76881e2017-03-10 13:17:56 -080060 AlertWindowNotification(WindowManagerService service, String packageName) {
Wale Ogunwale387e4c62017-02-13 09:50:02 -080061 mService = service;
62 mPackageName = packageName;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080063 mNotificationManager =
64 (NotificationManager) mService.mContext.getSystemService(NOTIFICATION_SERVICE);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +000065 mNotificationTag = CHANNEL_PREFIX + mPackageName;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080066 mRequestCode = sNextRequestCode++;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080067 mIconUtilities = new IconUtilities(mService.mContext);
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070068 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -080069
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070070 void post() {
Wale Ogunwale387e4c62017-02-13 09:50:02 -080071 // 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 Ogunwale5aa86832017-02-28 10:40:27 -080073 mService.mH.post(this::onPostNotification);
Wale Ogunwale387e4c62017-02-13 09:50:02 -080074 }
75
76 /** Cancels the notification */
Wale Ogunwale6c8f2e42018-02-01 09:07:34 -080077 void cancel(boolean deleteChannel) {
Wale Ogunwale5aa86832017-02-28 10:40:27 -080078 // 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 Ogunwale6c8f2e42018-02-01 09:07:34 -080080 mService.mH.post(() -> onCancelNotification(deleteChannel));
Wale Ogunwale5aa86832017-02-28 10:40:27 -080081 }
82
83 /** Don't call with the window manager lock held! */
Wale Ogunwale6c8f2e42018-02-01 09:07:34 -080084 private void onCancelNotification(boolean deleteChannel) {
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070085 if (!mPosted) {
86 // Notification isn't currently posted...
87 return;
88 }
89 mPosted = false;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080090 mNotificationManager.cancel(mNotificationTag, NOTIFICATION_ID);
Wale Ogunwale6c8f2e42018-02-01 09:07:34 -080091 if (deleteChannel) {
92 mNotificationManager.deleteNotificationChannel(mNotificationTag);
93 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -080094 }
95
96 /** Don't call with the window manager lock held! */
Wale Ogunwale5aa86832017-02-28 10:40:27 -080097 private void onPostNotification() {
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070098 if (mPosted) {
99 // Notification already posted...
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800100 return;
101 }
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -0700102 mPosted = true;
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800103
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800104 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 Reynoldsf7321592017-05-24 16:09:19 -0400110 createNotificationChannel(context, appName);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000111
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800112 final String message = context.getString(R.string.alert_windows_notification_message,
113 appName);
Julia Reynolds3c7de112018-03-28 09:33:13 -0400114 Bundle extras = new Bundle();
115 extras.putStringArray(Notification.EXTRA_FOREGROUND_APPS, new String[] {mPackageName});
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000116 final Notification.Builder builder = new Notification.Builder(context, mNotificationTag)
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800117 .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 Reynolds3c7de112018-03-28 09:33:13 -0400125 .addExtras(extras)
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800126 .setContentIntent(getContentIntent(context, mPackageName));
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800127
128 if (aInfo != null) {
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800129 final Drawable drawable = pm.getApplicationIcon(aInfo);
130 if (drawable != null) {
131 final Bitmap bitmap = mIconUtilities.createIconBitmap(drawable);
132 builder.setLargeIcon(bitmap);
133 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800134 }
135
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800136 mNotificationManager.notify(mNotificationTag, NOTIFICATION_ID, builder.build());
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800137 }
138
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800139 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 Ogunwale387e4c62017-02-13 09:50:02 -0800143 // Calls into activity manager...
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800144 return PendingIntent.getActivity(context, mRequestCode, intent, FLAG_CANCEL_CURRENT);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800145 }
146
Julia Reynoldsf7321592017-05-24 16:09:19 -0400147 private void createNotificationChannel(Context context, String appName) {
Wale Ogunwale4cd3f5c2017-06-08 16:51:48 -0700148 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 Ogunwaleaf38fd72017-05-23 01:43:59 +0000155 final String nameChannel =
156 context.getString(R.string.alert_windows_notification_channel_name, appName);
Wale Ogunwale6c8f2e42018-02-01 09:07:34 -0800157
158 NotificationChannel channel = mNotificationManager.getNotificationChannel(mNotificationTag);
159 if (channel != null) {
160 return;
161 }
162 channel = new NotificationChannel(mNotificationTag, nameChannel, IMPORTANCE_MIN);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000163 channel.enableLights(false);
164 channel.enableVibration(false);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400165 channel.setBlockableSystem(true);
Wale Ogunwaleface3bb2017-06-01 10:22:36 -0700166 channel.setGroup(sChannelGroup.getId());
Julia Reynoldsfd80cff2018-03-18 14:28:48 -0400167 channel.setBypassDnd(true);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000168 mNotificationManager.createNotificationChannel(channel);
169 }
170
171
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800172 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}