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