1 /* 2 * Copyright (C) 2020 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 android.app.ActivityManager; 20 import android.app.Notification; 21 import android.app.NotificationManager; 22 import android.app.PendingIntent; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.graphics.drawable.Icon; 27 import android.net.wifi.WifiConfiguration; 28 import android.os.UserHandle; 29 import android.provider.Settings; 30 import android.util.Log; 31 32 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; 33 import com.android.wifi.resources.R; 34 35 import java.util.List; 36 37 /** 38 * Helper class to generate SIM required notification 39 * 40 */ 41 public class SimRequiredNotifier { 42 43 private static final String TAG = "SimRequiredNotifier"; 44 private final WifiContext mContext; 45 private final FrameworkFacade mFrameworkFacade; 46 private final NotificationManager mNotificationManager; 47 SimRequiredNotifier(WifiContext context, FrameworkFacade framework)48 public SimRequiredNotifier(WifiContext context, FrameworkFacade framework) { 49 mContext = context; 50 mFrameworkFacade = framework; 51 mNotificationManager = 52 mContext.getSystemService(NotificationManager.class); 53 } 54 55 /** 56 * Show notification 57 */ showSimRequiredNotification(WifiConfiguration config, String carrier)58 public void showSimRequiredNotification(WifiConfiguration config, String carrier) { 59 String name; 60 if (config.isPasspoint()) { 61 name = config.providerFriendlyName; 62 } else { 63 name = config.SSID; 64 } 65 showNotification(name, carrier); 66 } 67 68 /** 69 * Dismiss notification 70 */ dismissSimRequiredNotification()71 public void dismissSimRequiredNotification() { 72 mNotificationManager.cancel(null, SystemMessage.NOTE_ID_WIFI_SIM_REQUIRED); 73 } 74 getSettingsPackageName()75 private String getSettingsPackageName() { 76 Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); 77 List<ResolveInfo> resolveInfos = mContext.getPackageManager().queryIntentActivitiesAsUser( 78 intent, PackageManager.MATCH_SYSTEM_ONLY | PackageManager.MATCH_DEFAULT_ONLY, 79 UserHandle.of(ActivityManager.getCurrentUser())); 80 if (resolveInfos == null || resolveInfos.isEmpty()) { 81 Log.e(TAG, "Failed to resolve wifi settings activity"); 82 return null; 83 } 84 // Pick the first one if there are more than 1 since the list is ordered from best to worst. 85 return resolveInfos.get(0).activityInfo.packageName; 86 } 87 showNotification(String ssid, String carrier)88 private void showNotification(String ssid, String carrier) { 89 String settingsPackage = getSettingsPackageName(); 90 if (settingsPackage == null) return; 91 Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS) 92 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 93 .setPackage(settingsPackage); 94 95 String title = mContext.getResources().getString( 96 R.string.wifi_sim_required_title); 97 String message = mContext.getResources().getString( 98 R.string.wifi_sim_required_message, 99 (ssid == null ? "" : ssid), 100 (carrier == null ? "" : carrier)); 101 Notification.Builder builder = mFrameworkFacade.makeNotificationBuilder(mContext, 102 WifiService.NOTIFICATION_NETWORK_ALERTS) 103 .setAutoCancel(true) 104 .setShowWhen(false) 105 .setLocalOnly(true) 106 .setColor(mContext.getResources().getColor( 107 android.R.color.system_notification_accent_color, mContext.getTheme())) 108 .setContentTitle(title) 109 .setTicker(title) 110 .setContentText(message) 111 .setStyle(new Notification.BigTextStyle().bigText(message)) 112 .setSmallIcon(Icon.createWithResource(mContext.getWifiOverlayApkPkgName(), 113 R.drawable.stat_notify_wifi_in_range)) 114 .setContentIntent(mFrameworkFacade.getActivity( 115 mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); 116 mNotificationManager.notify(SystemMessage.NOTE_ID_WIFI_SIM_REQUIRED, 117 builder.build()); 118 } 119 } 120