1 /* 2 * Copyright (C) 2017 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.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.graphics.drawable.Icon; 28 import android.os.UserHandle; 29 import android.provider.Settings; 30 import android.util.Log; 31 32 import com.android.internal.annotations.VisibleForTesting; 33 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; 34 import com.android.server.wifi.util.NativeUtil; 35 36 import java.util.List; 37 38 /** 39 * Responsible for notifying user for wrong password errors. 40 */ 41 public class WrongPasswordNotifier { 42 private static final String TAG = "WrongPasswordNotifier"; 43 // Number of milliseconds to wait before automatically dismiss the notification. 44 private static final long CANCEL_TIMEOUT_MILLISECONDS = 5 * 60 * 1000; 45 46 // Unique ID associated with the notification. 47 @VisibleForTesting 48 public static final int NOTIFICATION_ID = SystemMessage.NOTE_WIFI_WRONG_PASSWORD; 49 50 // Flag indicating if a wrong password error is detected for the current connection. 51 private boolean mWrongPasswordDetected; 52 53 private final WifiContext mContext; 54 private final NotificationManager mNotificationManager; 55 private final FrameworkFacade mFrameworkFacade; 56 WrongPasswordNotifier(WifiContext context, FrameworkFacade frameworkFacade)57 public WrongPasswordNotifier(WifiContext context, FrameworkFacade frameworkFacade) { 58 mContext = context; 59 mFrameworkFacade = frameworkFacade; 60 mNotificationManager = 61 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 62 } 63 64 /** 65 * Invoked when a wrong password error for a Wi-Fi network is detected. 66 * 67 * @param ssid The SSID of the Wi-Fi network 68 */ onWrongPasswordError(String ssid)69 public void onWrongPasswordError(String ssid) { 70 showNotification(ssid); 71 mWrongPasswordDetected = true; 72 } 73 74 /** 75 * Invoked when attempting a new Wi-Fi network connection. 76 */ onNewConnectionAttempt()77 public void onNewConnectionAttempt() { 78 if (mWrongPasswordDetected) { 79 dismissNotification(); 80 mWrongPasswordDetected = false; 81 } 82 } 83 getSettingsPackageName()84 private String getSettingsPackageName() { 85 Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); 86 List<ResolveInfo> resolveInfos = mContext.getPackageManager().queryIntentActivitiesAsUser( 87 intent, PackageManager.MATCH_SYSTEM_ONLY | PackageManager.MATCH_DEFAULT_ONLY, 88 UserHandle.of(ActivityManager.getCurrentUser())); 89 if (resolveInfos == null || resolveInfos.isEmpty()) { 90 Log.e(TAG, "Failed to resolve wifi settings activity"); 91 return null; 92 } 93 // Pick the first one if there are more than 1 since the list is ordered from best to worst. 94 return resolveInfos.get(0).activityInfo.packageName; 95 } 96 97 /** 98 * Display wrong password notification for a given Wi-Fi network (specified by its SSID). 99 * 100 * @param ssid SSID of the Wi-FI network 101 */ showNotification(String ssid)102 private void showNotification(String ssid) { 103 String settingsPackage = getSettingsPackageName(); 104 if (settingsPackage == null) return; 105 Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS) 106 .setPackage(settingsPackage) 107 .putExtra("wifi_start_connect_ssid", NativeUtil.removeEnclosingQuotes(ssid)); 108 Notification.Builder builder = mFrameworkFacade.makeNotificationBuilder(mContext, 109 WifiService.NOTIFICATION_NETWORK_ALERTS) 110 .setAutoCancel(true) 111 .setTimeoutAfter(CANCEL_TIMEOUT_MILLISECONDS) 112 // TODO(zqiu): consider creating a new icon. 113 .setSmallIcon(Icon.createWithResource(mContext.getWifiOverlayApkPkgName(), 114 com.android.wifi.resources.R.drawable.stat_notify_wifi_in_range)) 115 .setContentTitle(mContext.getString( 116 com.android.wifi.resources.R.string.wifi_available_title_failed_to_connect)) 117 .setContentText(ssid) 118 .setContentIntent(mFrameworkFacade.getActivity( 119 mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)) 120 .setColor(mContext.getResources().getColor( 121 android.R.color.system_notification_accent_color)); 122 mNotificationManager.notify(NOTIFICATION_ID, builder.build()); 123 } 124 125 /** 126 * Dismiss the notification that was generated by {@link #showNotification}. The notification 127 * might have already been dismissed, either by user or timeout. We'll attempt to dismiss it 128 * regardless if it is been dismissed or not, to reduce code complexity. 129 */ dismissNotification()130 private void dismissNotification() { 131 // Notification might have already been dismissed, either by user or timeout. It is 132 // still okay to cancel it if already dismissed. 133 mNotificationManager.cancel(null, NOTIFICATION_ID); 134 } 135 } 136