1 /* 2 * Copyright (C) 2021 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 17 package com.android.server.wifi; 18 19 import static com.android.server.wifi.WifiService.NOTIFICATION_APM_ALERTS; 20 import static com.android.server.wifi.WifiService.NOTIFICATION_NETWORK_ALERTS; 21 import static com.android.server.wifi.WifiService.NOTIFICATION_NETWORK_AVAILABLE; 22 import static com.android.server.wifi.WifiService.NOTIFICATION_NETWORK_STATUS; 23 24 import android.annotation.SuppressLint; 25 import android.app.Notification; 26 import android.app.NotificationChannel; 27 import android.app.NotificationManager; 28 import android.content.Context; 29 import android.content.pm.PackageManager; 30 import android.os.UserHandle; 31 import android.service.notification.StatusBarNotification; 32 import android.util.Log; 33 34 import com.android.wifi.resources.R; 35 36 import java.util.ArrayList; 37 import java.util.List; 38 39 /** 40 * Notification manager for Wifi. All notification will be sent to the current user. 41 */ 42 public class WifiNotificationManager { 43 private static final String TAG = "WifiNotificationManager"; 44 private static final String NOTIFICATION_TAG = "com.android.wifi"; 45 46 private final Context mContext; 47 private NotificationManager mNotificationManager; 48 WifiNotificationManager(Context context)49 public WifiNotificationManager(Context context) { 50 mContext = context; 51 } 52 getNotificationManagerForCurrentUser()53 private NotificationManager getNotificationManagerForCurrentUser() { 54 try { 55 return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, 56 UserHandle.CURRENT).getSystemService(NotificationManager.class); 57 } catch (PackageManager.NameNotFoundException e) { 58 Log.e(TAG, "Failed to get NotificationManager for current user: " + e.getMessage()); 59 } 60 return null; 61 } 62 63 /** 64 * Update to the notification manager fot current user and create notification channels. 65 */ 66 // TODO(b/193460475): Remove when tooling supports APIS moving from SystemApi to public 67 @SuppressLint("NewApi") createNotificationChannels()68 public void createNotificationChannels() { 69 if (mNotificationManager != null) { 70 // Cancel all active notification from Wi-Fi Stack. 71 cleanAllWifiNotification(); 72 } 73 mNotificationManager = getNotificationManagerForCurrentUser(); 74 if (mNotificationManager == null) { 75 return; 76 } 77 List<NotificationChannel> channelsList = new ArrayList<>(); 78 final NotificationChannel networkStatusChannel = new NotificationChannel( 79 NOTIFICATION_NETWORK_STATUS, 80 mContext.getResources().getString( 81 R.string.notification_channel_network_status), 82 NotificationManager.IMPORTANCE_LOW); 83 channelsList.add(networkStatusChannel); 84 85 final NotificationChannel networkAlertsChannel = new NotificationChannel( 86 NOTIFICATION_NETWORK_ALERTS, 87 mContext.getResources().getString( 88 R.string.notification_channel_network_alerts), 89 NotificationManager.IMPORTANCE_HIGH); 90 networkAlertsChannel.setBlockable(true); 91 channelsList.add(networkAlertsChannel); 92 93 final NotificationChannel networkAvailable = new NotificationChannel( 94 NOTIFICATION_NETWORK_AVAILABLE, 95 mContext.getResources().getString( 96 R.string.notification_channel_network_available), 97 NotificationManager.IMPORTANCE_LOW); 98 networkAvailable.setBlockable(true); 99 channelsList.add(networkAvailable); 100 101 final NotificationChannel apmAlertsChannel = new NotificationChannel( 102 NOTIFICATION_APM_ALERTS, 103 mContext.getResources().getString( 104 R.string.notification_channel_apm_alerts), 105 NotificationManager.IMPORTANCE_HIGH); 106 apmAlertsChannel.setBlockable(true); 107 channelsList.add(apmAlertsChannel); 108 109 mNotificationManager.createNotificationChannels(channelsList); 110 } 111 cleanAllWifiNotification()112 private void cleanAllWifiNotification() { 113 for (StatusBarNotification notification : getActiveNotifications()) { 114 if (NOTIFICATION_TAG.equals(notification.getTag())) { 115 cancel(notification.getId()); 116 } 117 } 118 } 119 120 /** 121 * Send notification to the current user. 122 */ notify(int id, Notification notification)123 public void notify(int id, Notification notification) { 124 if (mNotificationManager == null) { 125 return; 126 } 127 mNotificationManager.notify(NOTIFICATION_TAG, id, notification); 128 } 129 130 /** 131 * Cancel the notification fot current user. 132 */ cancel(int id)133 public void cancel(int id) { 134 if (mNotificationManager == null) { 135 return; 136 } 137 mNotificationManager.cancel(NOTIFICATION_TAG, id); 138 } 139 140 /** 141 * Get active notifications for current user. 142 */ getActiveNotifications()143 public StatusBarNotification[] getActiveNotifications() { 144 if (mNotificationManager == null) { 145 return new StatusBarNotification[0]; 146 } 147 return mNotificationManager.getActiveNotifications(); 148 } 149 } 150