blob: 7eebe39f4ee46e36866523d7a311ae4eb8bd5f2e [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 Ogunwaleface3bb2017-06-01 10:22:36 -070066 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 Ogunwalea10fc7e2017-04-06 07:09:51 -070072 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -080073
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070074 void post() {
Wale Ogunwale387e4c62017-02-13 09:50:02 -080075 // 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 Ogunwale5aa86832017-02-28 10:40:27 -080077 mService.mH.post(this::onPostNotification);
Wale Ogunwale387e4c62017-02-13 09:50:02 -080078 }
79
80 /** Cancels the notification */
81 void cancel() {
Wale Ogunwale5aa86832017-02-28 10:40:27 -080082 // 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 Ogunwalea10fc7e2017-04-06 07:09:51 -070089 if (!mPosted) {
90 // Notification isn't currently posted...
91 return;
92 }
93 mPosted = false;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080094 mNotificationManager.cancel(mNotificationTag, NOTIFICATION_ID);
Wale Ogunwale387e4c62017-02-13 09:50:02 -080095 }
96
97 /** Don't call with the window manager lock held! */
Wale Ogunwale5aa86832017-02-28 10:40:27 -080098 private void onPostNotification() {
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070099 if (mPosted) {
100 // Notification already posted...
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800101 return;
102 }
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -0700103 mPosted = true;
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800104
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800105 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 Reynoldsf7321592017-05-24 16:09:19 -0400111 createNotificationChannel(context, appName);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000112
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800113 final String message = context.getString(R.string.alert_windows_notification_message,
114 appName);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000115 final Notification.Builder builder = new Notification.Builder(context, mNotificationTag)
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800116 .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 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) {
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 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 Ogunwaleaf38fd72017-05-23 01:43:59 +0000146 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 Reynoldsf7321592017-05-24 16:09:19 -0400152 channel.setBlockableSystem(true);
Wale Ogunwaleface3bb2017-06-01 10:22:36 -0700153 channel.setGroup(sChannelGroup.getId());
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000154 mNotificationManager.createNotificationChannel(channel);
155 }
156
157
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800158 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}