blob: 972623c9478dc68048719fc4e8250be3eb5891a3 [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 Ogunwale387e4c62017-02-13 09:50:02 -080028import android.app.NotificationManager;
29import android.app.PendingIntent;
30import android.content.Context;
31
32import 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;
37
Wale Ogunwaled76881e2017-03-10 13:17:56 -080038import android.net.Uri;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080039import com.android.internal.R;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080040import com.android.server.policy.IconUtilities;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080041
42/** Displays an ongoing notification for a process displaying an alert window */
43class AlertWindowNotification {
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +000044 private static final String CHANNEL_PREFIX = "com.android.server.wm.AlertWindowNotification - ";
Wale Ogunwale387e4c62017-02-13 09:50:02 -080045 private static final int NOTIFICATION_ID = 0;
46
47 private static int sNextRequestCode = 0;
48 private final int mRequestCode;
49 private final WindowManagerService mService;
50 private String mNotificationTag;
51 private final NotificationManager mNotificationManager;
52 private final String mPackageName;
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070053 private boolean mPosted;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080054 private IconUtilities mIconUtilities;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080055
Wale Ogunwaled76881e2017-03-10 13:17:56 -080056 AlertWindowNotification(WindowManagerService service, String packageName) {
Wale Ogunwale387e4c62017-02-13 09:50:02 -080057 mService = service;
58 mPackageName = packageName;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080059 mNotificationManager =
60 (NotificationManager) mService.mContext.getSystemService(NOTIFICATION_SERVICE);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +000061 mNotificationTag = CHANNEL_PREFIX + mPackageName;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080062 mRequestCode = sNextRequestCode++;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080063 mIconUtilities = new IconUtilities(mService.mContext);
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070064 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -080065
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070066 void post() {
Wale Ogunwale387e4c62017-02-13 09:50:02 -080067 // We can't create/post the notification while the window manager lock is held since it will
68 // end up calling into activity manager. So, we post a message to do it later.
Wale Ogunwale5aa86832017-02-28 10:40:27 -080069 mService.mH.post(this::onPostNotification);
Wale Ogunwale387e4c62017-02-13 09:50:02 -080070 }
71
72 /** Cancels the notification */
73 void cancel() {
Wale Ogunwale5aa86832017-02-28 10:40:27 -080074 // We can't call into NotificationManager with WM lock held since it might call into AM.
75 // So, we post a message to do it later.
76 mService.mH.post(this::onCancelNotification);
77 }
78
79 /** Don't call with the window manager lock held! */
80 private void onCancelNotification() {
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070081 if (!mPosted) {
82 // Notification isn't currently posted...
83 return;
84 }
85 mPosted = false;
Wale Ogunwale387e4c62017-02-13 09:50:02 -080086 mNotificationManager.cancel(mNotificationTag, NOTIFICATION_ID);
Wale Ogunwale387e4c62017-02-13 09:50:02 -080087 }
88
89 /** Don't call with the window manager lock held! */
Wale Ogunwale5aa86832017-02-28 10:40:27 -080090 private void onPostNotification() {
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070091 if (mPosted) {
92 // Notification already posted...
Wale Ogunwale5aa86832017-02-28 10:40:27 -080093 return;
94 }
Wale Ogunwalea10fc7e2017-04-06 07:09:51 -070095 mPosted = true;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080096
Wale Ogunwale387e4c62017-02-13 09:50:02 -080097 final Context context = mService.mContext;
98 final PackageManager pm = context.getPackageManager();
99 final ApplicationInfo aInfo = getApplicationInfo(pm, mPackageName);
100 final String appName = (aInfo != null)
101 ? pm.getApplicationLabel(aInfo).toString() : mPackageName;
102
Julia Reynoldsf7321592017-05-24 16:09:19 -0400103 createNotificationChannel(context, appName);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000104
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800105 final String message = context.getString(R.string.alert_windows_notification_message,
106 appName);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000107 final Notification.Builder builder = new Notification.Builder(context, mNotificationTag)
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800108 .setOngoing(true)
109 .setContentTitle(
110 context.getString(R.string.alert_windows_notification_title, appName))
111 .setContentText(message)
112 .setSmallIcon(R.drawable.alert_window_layer)
113 .setColor(context.getColor(R.color.system_notification_accent_color))
114 .setStyle(new Notification.BigTextStyle().bigText(message))
115 .setLocalOnly(true)
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800116 .setContentIntent(getContentIntent(context, mPackageName));
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800117
118 if (aInfo != null) {
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800119 final Drawable drawable = pm.getApplicationIcon(aInfo);
120 if (drawable != null) {
121 final Bitmap bitmap = mIconUtilities.createIconBitmap(drawable);
122 builder.setLargeIcon(bitmap);
123 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800124 }
125
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800126 mNotificationManager.notify(mNotificationTag, NOTIFICATION_ID, builder.build());
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800127 }
128
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800129 private PendingIntent getContentIntent(Context context, String packageName) {
130 final Intent intent = new Intent(ACTION_MANAGE_OVERLAY_PERMISSION,
131 Uri.fromParts("package", packageName, null));
132 intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800133 // Calls into activity manager...
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800134 return PendingIntent.getActivity(context, mRequestCode, intent, FLAG_CANCEL_CURRENT);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800135 }
136
Julia Reynoldsf7321592017-05-24 16:09:19 -0400137 private void createNotificationChannel(Context context, String appName) {
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000138 final String nameChannel =
139 context.getString(R.string.alert_windows_notification_channel_name, appName);
140 final NotificationChannel channel =
141 new NotificationChannel(mNotificationTag, nameChannel, IMPORTANCE_MIN);
142 channel.enableLights(false);
143 channel.enableVibration(false);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400144 channel.setBlockableSystem(true);
Wale Ogunwaleaf38fd72017-05-23 01:43:59 +0000145 mNotificationManager.createNotificationChannel(channel);
146 }
147
148
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800149 private ApplicationInfo getApplicationInfo(PackageManager pm, String packageName) {
150 try {
151 return pm.getApplicationInfo(packageName, 0);
152 } catch (PackageManager.NameNotFoundException e) {
153 return null;
154 }
155 }
156}