blob: efc92cf02dde9e89062e6740f7977e2a5104aa74 [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 Ogunwale387e4c62017-02-13 09:50:02 -080019import static android.app.NotificationManager.IMPORTANCE_MIN;
20import 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;
27import android.app.NotificationChannel;
28import 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 {
44 private static final String CHANNEL_PREFIX = "com.android.server.wm.AlertWindowNotification - ";
45 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 Ogunwale387e4c62017-02-13 09:50:02 -080053 private boolean mCancelled;
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);
61 mNotificationTag = CHANNEL_PREFIX + mPackageName;
62 mRequestCode = sNextRequestCode++;
Wale Ogunwale5aa86832017-02-28 10:40:27 -080063 mIconUtilities = new IconUtilities(mService.mContext);
Wale Ogunwale387e4c62017-02-13 09:50:02 -080064
65 // We can't create/post the notification while the window manager lock is held since it will
66 // end up calling into activity manager. So, we post a message to do it later.
Wale Ogunwale5aa86832017-02-28 10:40:27 -080067 mService.mH.post(this::onPostNotification);
Wale Ogunwale387e4c62017-02-13 09:50:02 -080068 }
69
70 /** Cancels the notification */
71 void cancel() {
Wale Ogunwale5aa86832017-02-28 10:40:27 -080072 // We can't call into NotificationManager with WM lock held since it might call into AM.
73 // So, we post a message to do it later.
74 mService.mH.post(this::onCancelNotification);
75 }
76
77 /** Don't call with the window manager lock held! */
78 private void onCancelNotification() {
Wale Ogunwale387e4c62017-02-13 09:50:02 -080079 mNotificationManager.cancel(mNotificationTag, NOTIFICATION_ID);
80 mCancelled = true;
81 }
82
83 /** Don't call with the window manager lock held! */
Wale Ogunwale5aa86832017-02-28 10:40:27 -080084 private void onPostNotification() {
85 if (mCancelled) {
86 // Notification was cancelled, so nothing more to do...
87 return;
88 }
89
Wale Ogunwale387e4c62017-02-13 09:50:02 -080090 final Context context = mService.mContext;
91 final PackageManager pm = context.getPackageManager();
92 final ApplicationInfo aInfo = getApplicationInfo(pm, mPackageName);
93 final String appName = (aInfo != null)
94 ? pm.getApplicationLabel(aInfo).toString() : mPackageName;
95
96 createNotificationChannelIfNeeded(context, appName);
97
Wale Ogunwaled76881e2017-03-10 13:17:56 -080098 final String message = context.getString(R.string.alert_windows_notification_message,
99 appName);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800100 final Notification.Builder builder = new Notification.Builder(context, mNotificationTag)
101 .setOngoing(true)
102 .setContentTitle(
103 context.getString(R.string.alert_windows_notification_title, appName))
104 .setContentText(message)
105 .setSmallIcon(R.drawable.alert_window_layer)
106 .setColor(context.getColor(R.color.system_notification_accent_color))
107 .setStyle(new Notification.BigTextStyle().bigText(message))
108 .setLocalOnly(true)
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800109 .setContentIntent(getContentIntent(context, mPackageName));
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800110
111 if (aInfo != null) {
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800112 final Drawable drawable = pm.getApplicationIcon(aInfo);
113 if (drawable != null) {
114 final Bitmap bitmap = mIconUtilities.createIconBitmap(drawable);
115 builder.setLargeIcon(bitmap);
116 }
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800117 }
118
Wale Ogunwale5aa86832017-02-28 10:40:27 -0800119 mNotificationManager.notify(mNotificationTag, NOTIFICATION_ID, builder.build());
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800120 }
121
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800122 private PendingIntent getContentIntent(Context context, String packageName) {
123 final Intent intent = new Intent(ACTION_MANAGE_OVERLAY_PERMISSION,
124 Uri.fromParts("package", packageName, null));
125 intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800126 // Calls into activity manager...
Wale Ogunwaled76881e2017-03-10 13:17:56 -0800127 return PendingIntent.getActivity(context, mRequestCode, intent, FLAG_CANCEL_CURRENT);
Wale Ogunwale387e4c62017-02-13 09:50:02 -0800128 }
129
130 private void createNotificationChannelIfNeeded(Context context, String appName) {
131 if (mNotificationManager.getNotificationChannel(mNotificationTag) != null) {
132 return;
133 }
134 final String nameChannel =
135 context.getString(R.string.alert_windows_notification_channel_name, appName);
136 final NotificationChannel channel =
137 new NotificationChannel(mNotificationTag, nameChannel, IMPORTANCE_MIN);
138 channel.enableLights(false);
139 channel.enableVibration(false);
140 mNotificationManager.createNotificationChannel(channel);
141 }
142
143
144 private ApplicationInfo getApplicationInfo(PackageManager pm, String packageName) {
145 try {
146 return pm.getApplicationInfo(packageName, 0);
147 } catch (PackageManager.NameNotFoundException e) {
148 return null;
149 }
150 }
151}