1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.bluetooth;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Binder;
import android.os.UserHandle;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import java.util.ArrayList;
import java.util.List;
/**
* Notification manager for Bluetooth. All notification will be sent to the current user.
*/
public class BluetoothNotificationManager {
private static final String TAG = "BluetoothNotificationManager";
private static final String NOTIFICATION_TAG = "com.android.bluetooth";
public static final String APM_NOTIFICATION_CHANNEL = "apm_notification_channel";
private static final String APM_NOTIFICATION_GROUP = "apm_notification_group";
private static final String HELP_PAGE_URL =
"https://support.google.com/pixelphone/answer/12639358";
private final Context mContext;
private NotificationManager mNotificationManager;
private boolean mInitialized = false;
/**
* Constructor
*
* @param ctx The context to use to obtain access to the Notification Service
*/
BluetoothNotificationManager(Context ctx) {
mContext = ctx;
}
private NotificationManager getNotificationManagerForCurrentUser() {
final long callingIdentity = Binder.clearCallingIdentity();
try {
return mContext.createPackageContextAsUser(mContext.getPackageName(), 0,
UserHandle.CURRENT).getSystemService(NotificationManager.class);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Failed to get NotificationManager for current user: " + e.getMessage());
} finally {
Binder.restoreCallingIdentity(callingIdentity);
}
return null;
}
/**
* Update to the notification manager fot current user and create notification channels.
*/
public void createNotificationChannels() {
if (mNotificationManager != null) {
// Cancel all active notification from Bluetooth Stack.
cleanAllBtNotification();
}
mNotificationManager = getNotificationManagerForCurrentUser();
if (mNotificationManager == null) {
return;
}
List<NotificationChannel> channelsList = new ArrayList<>();
final NotificationChannel apmChannel = new NotificationChannel(
APM_NOTIFICATION_CHANNEL,
APM_NOTIFICATION_GROUP,
NotificationManager.IMPORTANCE_HIGH);
channelsList.add(apmChannel);
final long callingIdentity = Binder.clearCallingIdentity();
try {
mNotificationManager.createNotificationChannels(channelsList);
} catch (Exception e) {
Log.e(TAG, "Error Message: " + e.getMessage());
e.printStackTrace();
} finally {
Binder.restoreCallingIdentity(callingIdentity);
}
}
private void cleanAllBtNotification() {
for (StatusBarNotification notification : getActiveNotifications()) {
if (NOTIFICATION_TAG.equals(notification.getTag())) {
cancel(notification.getId());
}
}
}
/**
* Send notification to the current user.
*/
public void notify(int id, Notification notification) {
if (!mInitialized) {
createNotificationChannels();
mInitialized = true;
}
if (mNotificationManager == null) {
return;
}
mNotificationManager.notify(NOTIFICATION_TAG, id, notification);
}
/**
* Build and send the APM notification.
*/
public void sendApmNotification(String title, String message) {
if (!mInitialized) {
createNotificationChannels();
mInitialized = true;
}
Intent openLinkIntent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse(HELP_PAGE_URL))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent tapPendingIntent = PendingIntent.getActivity(
mContext.createContextAsUser(UserHandle.CURRENT, 0),
PendingIntent.FLAG_UPDATE_CURRENT, openLinkIntent, PendingIntent.FLAG_IMMUTABLE);
Notification notification = new Notification.Builder(mContext, APM_NOTIFICATION_CHANNEL)
.setAutoCancel(true)
.setLocalOnly(true)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(tapPendingIntent)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setStyle(new Notification.BigTextStyle().bigText(message))
.setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
.build();
notify(SystemMessage.NOTE_BT_APM_NOTIFICATION, notification);
}
/**
* Cancel the notification fot current user.
*/
public void cancel(int id) {
if (mNotificationManager == null) {
return;
}
mNotificationManager.cancel(NOTIFICATION_TAG, id);
}
/**
* Get active notifications for current user.
*/
public StatusBarNotification[] getActiveNotifications() {
if (mNotificationManager == null) {
return new StatusBarNotification[0];
}
return mNotificationManager.getActiveNotifications();
}
}
|