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.Notification;
20 import android.app.PendingIntent;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.graphics.drawable.Icon;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 import android.provider.Settings;
27 import android.util.Log;
28 
29 import com.android.internal.annotations.VisibleForTesting;
30 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
31 import com.android.modules.utils.build.SdkLevel;
32 import com.android.server.wifi.util.NativeUtil;
33 
34 /**
35  * Responsible for notifying user for wrong password errors.
36  */
37 public class WrongPasswordNotifier {
38     private static final String TAG = "WrongPasswordNotifier";
39     // Number of milliseconds to wait before automatically dismiss the notification.
40     private static final long CANCEL_TIMEOUT_MILLISECONDS = 5 * 60 * 1000;
41 
42     // Unique ID associated with the notification.
43     @VisibleForTesting
44     public static final int NOTIFICATION_ID = SystemMessage.NOTE_WIFI_WRONG_PASSWORD;
45 
46     // Flag indicating if a wrong password error is detected for the current connection.
47     private boolean mWrongPasswordDetected;
48 
49     private final WifiContext mContext;
50     private final WifiNotificationManager mNotificationManager;
51     private final FrameworkFacade mFrameworkFacade;
52 
WrongPasswordNotifier(WifiContext context, FrameworkFacade frameworkFacade, WifiNotificationManager wifiNotificationManager)53     public WrongPasswordNotifier(WifiContext context, FrameworkFacade frameworkFacade,
54             WifiNotificationManager wifiNotificationManager) {
55         mContext = context;
56         mFrameworkFacade = frameworkFacade;
57         mNotificationManager = wifiNotificationManager;
58     }
59 
60     /**
61      * Invoked when a wrong password error for a Wi-Fi network is detected.
62      *
63      * @param ssid The SSID of the Wi-Fi network
64      */
onWrongPasswordError(String ssid)65     public void onWrongPasswordError(String ssid) {
66         showNotification(ssid);
67         mWrongPasswordDetected = true;
68     }
69 
70     /**
71      * Invoked when attempting a new Wi-Fi network connection.
72      */
onNewConnectionAttempt()73     public void onNewConnectionAttempt() {
74         if (mWrongPasswordDetected) {
75             dismissNotification();
76             mWrongPasswordDetected = false;
77         }
78     }
79 
80     /**
81      * Display wrong password notification for a given Wi-Fi network (specified by its SSID).
82      *
83      * @param ssid SSID of the Wi-FI network
84      */
showNotification(String ssid)85     private void showNotification(String ssid) {
86         String settingsPackage = mFrameworkFacade.getSettingsPackageName(mContext);
87         if (settingsPackage == null) return;
88         Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS)
89                 .setPackage(settingsPackage)
90                 .putExtra("wifi_start_connect_ssid", NativeUtil.removeEnclosingQuotes(ssid));
91         CharSequence title = mContext.getString(
92                 com.android.wifi.resources.R.string.wifi_available_title_failed_to_connect);
93 
94         Context userContext = mContext;
95         if (SdkLevel.isAtLeastS() && UserManager.isHeadlessSystemUserMode()) {
96             // Need to pass the context of the current user to the activity that's launched when
97             // the notification is tapped
98             userContext = mContext.createContextAsUser(UserHandle.CURRENT, /* flags= */ 0);
99         }
100 
101         Log.i(TAG, "Showing '" + title + "' notification for user " + userContext.getUser()
102                 + " and package " + settingsPackage);
103         Notification.Builder builder = mFrameworkFacade.makeNotificationBuilder(mContext,
104                 WifiService.NOTIFICATION_NETWORK_ALERTS)
105                 .setAutoCancel(true)
106                 .setTimeoutAfter(CANCEL_TIMEOUT_MILLISECONDS)
107                 // TODO(zqiu): consider creating a new icon.
108                 .setSmallIcon(Icon.createWithResource(mContext.getWifiOverlayApkPkgName(),
109                         com.android.wifi.resources.R.drawable.stat_notify_wifi_in_range))
110                 .setContentTitle(title)
111                 .setContentText(ssid)
112                 .setContentIntent(mFrameworkFacade.getActivity(
113                         userContext, 0, intent,
114                         PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE))
115                 .setColor(mContext.getResources().getColor(
116                         android.R.color.system_notification_accent_color));
117         mNotificationManager.notify(NOTIFICATION_ID, builder.build());
118     }
119 
120     /**
121      * Dismiss the notification that was generated by {@link #showNotification}. The notification
122      * might have already been dismissed, either by user or timeout. We'll attempt to dismiss it
123      * regardless if it is been dismissed or not, to reduce code complexity.
124      */
dismissNotification()125     private void dismissNotification() {
126         // Notification might have already been dismissed, either by user or timeout. It is
127         // still okay to cancel it if already dismissed.
128         mNotificationManager.cancel(NOTIFICATION_ID);
129     }
130 }
131