blob: 3f320793826f4d86ea793df79bfe9c9b7cfe6fc6 [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;
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 */
75 void cancel() {
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.
78 mService.mH.post(this::onCancelNotification);
79 }
80
81 /** Don't call with the window manager lock held! */
82 private void onCancelNotification() {
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 Ogunwale387e4c62017-02-13 09:50:02 -080089 }
90
91 /** Don't call with the window manager lock held! */
Wale Ogunwale5aa86832017-02-28 10:40:27 -080092 private void onPostNotification() {
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070093 if (mPosted) {
94 // Notification already posted...
Wale Ogunwale5aa86832017-02-28 10:40:27 -080095 return;
96 }
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070097 mPosted = true;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080098
Wale Ogunwale387e4c62017-02-13 09:50:02 -080099 final Context context = mService.mContext;
100 final PackageManager pm = context.getPackageManager();
101 final ApplicationInfo aInfo = getApplicationInfo(pm, mPackageName);
102 final String appName = (aInfo != null)
103 ? pm.getApplicationLabel(aInfo).toString() : mPackageName;
104
Julia Reynoldsf7321592017-05-24 16:09:19 -0400105 createNotificationChannel(context, appName);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000106
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800107 final String message = context.getString(R.string.alert_windows_notification_message,
108 appName);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000109 final Notification.Builder builder = new Notification.Builder(context, mNotificationTag)
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800110 .setOngoing(true)
111 .setContentTitle(
112 context.getString(R.string.alert_windows_notification_title, appName))
113 .setContentText(message)
114 .setSmallIcon(R.drawable.alert_window_layer)
115 .setColor(context.getColor(R.color.system_notification_accent_color))
116 .setStyle(new Notification.BigTextStyle().bigText(message))
117 .setLocalOnly(true)
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800118 .setContentIntent(getContentIntent(context, mPackageName));
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800119
120 if (aInfo != null) {
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800121 final Drawable drawable = pm.getApplicationIcon(aInfo);
122 if (drawable != null) {
123 final Bitmap bitmap = mIconUtilities.createIconBitmap(drawable);
124 builder.setLargeIcon(bitmap);
125 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800126 }
127
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800128 mNotificationManager.notify(mNotificationTag, NOTIFICATION_ID, builder.build());
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800129 }
130
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800131 private PendingIntent getContentIntent(Context context, String packageName) {
132 final Intent intent = new Intent(ACTION_MANAGE_OVERLAY_PERMISSION,
133 Uri.fromParts("package", packageName, null));
134 intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800135 // Calls into activity manager...
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800136 return PendingIntent.getActivity(context, mRequestCode, intent, FLAG_CANCEL_CURRENT);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800137 }
138
Julia Reynoldsf7321592017-05-24 16:09:19 -0400139 private void createNotificationChannel(Context context, String appName) {
Wale Ogunwale4cd3f5c2017-06-08 16:51:48 -0700140 if (sChannelGroup == null) {
141 sChannelGroup = new NotificationChannelGroup(CHANNEL_PREFIX,
142 mService.mContext.getString(
143 R.string.alert_windows_notification_channel_group_name));
144 mNotificationManager.createNotificationChannelGroup(sChannelGroup);
145 }
146
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000147 final String nameChannel =
148 context.getString(R.string.alert_windows_notification_channel_name, appName);
149 final NotificationChannel channel =
150 new NotificationChannel(mNotificationTag, nameChannel, IMPORTANCE_MIN);
151 channel.enableLights(false);
152 channel.enableVibration(false);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400153 channel.setBlockableSystem(true);
Wale Ogunwaleface3bb2017-06-01 10:22:36 -0700154 channel.setGroup(sChannelGroup.getId());
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000155 mNotificationManager.createNotificationChannel(channel);
156 }
157
158
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800159 private ApplicationInfo getApplicationInfo(PackageManager pm, String packageName) {
160 try {
161 return pm.getApplicationInfo(packageName, 0);
162 } catch (PackageManager.NameNotFoundException e) {
163 return null;
164 }
165 }
166}