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