1 /*
2  * Copyright (C) 2019 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.annotation.NonNull;
20 import android.app.AlertDialog;
21 import android.app.Notification;
22 import android.app.NotificationManager;
23 import android.app.PendingIntent;
24 import android.content.DialogInterface;
25 import android.content.Intent;
26 import android.graphics.drawable.Icon;
27 import android.net.wifi.WifiConfiguration;
28 import android.os.Handler;
29 import android.view.WindowManager;
30 
31 import com.android.wifi.resources.R;
32 
33 /**
34  * Helper class for ConnectionFailureNotifier.
35  */
36 public class ConnectionFailureNotificationBuilder {
37     private static final String TAG = "ConnectionFailureNotifier";
38 
39     public static final String ACTION_SHOW_SET_RANDOMIZATION_DETAILS =
40             "com.android.server.wifi.ACTION_SHOW_SET_RANDOMIZATION_DETAILS";
41     public static final String RANDOMIZATION_SETTINGS_NETWORK_ID =
42             "com.android.server.wifi.RANDOMIZATION_SETTINGS_NETWORK_ID";
43     public static final String RANDOMIZATION_SETTINGS_NETWORK_SSID =
44             "com.android.server.wifi.RANDOMIZATION_SETTINGS_NETWORK_SSID";
45 
46     private WifiContext mContext;
47     private String mPackageName;
48     private FrameworkFacade mFrameworkFacade;
49     private WifiConnectivityManager mWifiConnectivityManager;
50     private NotificationManager mNotificationManager;
51     private Handler mHandler;
52 
ConnectionFailureNotificationBuilder(WifiContext context, String packageName, FrameworkFacade framework)53     public ConnectionFailureNotificationBuilder(WifiContext context, String packageName,
54             FrameworkFacade framework) {
55         mContext = context;
56         mPackageName = packageName;
57         mFrameworkFacade = framework;
58     }
59 
60     /**
61      * Creates a notification that alerts the user that the connection may be failing due to
62      * MAC randomization.
63      * @param config
64      */
buildNoMacRandomizationSupportNotification( @onNull WifiConfiguration config)65     public Notification buildNoMacRandomizationSupportNotification(
66             @NonNull WifiConfiguration config) {
67         String ssid = config.SSID;
68         String ssidAndSecurityType = config.getSsidAndSecurityTypeString();
69         String title = mContext.getResources().getString(
70                 R.string.wifi_cannot_connect_with_randomized_mac_title, ssid);
71         String content = mContext.getResources().getString(
72                 R.string.wifi_cannot_connect_with_randomized_mac_message);
73 
74         Intent showDetailIntent = new Intent(ACTION_SHOW_SET_RANDOMIZATION_DETAILS)
75                 .setPackage(mPackageName);
76         showDetailIntent.putExtra(RANDOMIZATION_SETTINGS_NETWORK_ID, config.networkId);
77         showDetailIntent.putExtra(RANDOMIZATION_SETTINGS_NETWORK_SSID, ssidAndSecurityType);
78         PendingIntent pendingShowDetailIntent = mFrameworkFacade.getBroadcast(
79                 mContext, 0, showDetailIntent, PendingIntent.FLAG_UPDATE_CURRENT);
80 
81         return mFrameworkFacade.makeNotificationBuilder(
82                 mContext, WifiService.NOTIFICATION_NETWORK_ALERTS)
83                 .setSmallIcon(Icon.createWithResource(mContext.getWifiOverlayApkPkgName(),
84                         com.android.wifi.resources.R.drawable.stat_notify_wifi_in_range))
85                 .setTicker(title)
86                 .setContentTitle(title)
87                 .setContentText(content)
88                 .setContentIntent(pendingShowDetailIntent)
89                 .setShowWhen(false)
90                 .setLocalOnly(true)
91                 .setColor(mContext.getResources().getColor(
92                         android.R.color.system_notification_accent_color, mContext.getTheme()))
93                 .setAutoCancel(true)
94                 .build();
95     }
96 
97     /**
98      * Creates an AlertDialog that allows the user to disable MAC randomization for a network.
99      * @param ssid the displayed SSID in the dialog
100      * @param onUserConfirm
101      */
buildChangeMacRandomizationSettingDialog( String ssid, DialogInterface.OnClickListener onUserConfirm)102     public AlertDialog buildChangeMacRandomizationSettingDialog(
103             String ssid, DialogInterface.OnClickListener onUserConfirm) {
104         AlertDialog.Builder builder = mFrameworkFacade.makeAlertDialogBuilder(mContext)
105                 .setTitle(mContext.getResources().getString(
106                         R.string.wifi_disable_mac_randomization_dialog_title))
107                 .setMessage(mContext.getResources().getString(
108                         R.string.wifi_disable_mac_randomization_dialog_message, ssid))
109                 .setPositiveButton(
110                         mContext.getResources().getString(
111                                 R.string.wifi_disable_mac_randomization_dialog_confirm_text),
112                         onUserConfirm)
113                 // A null listener allows the dialog to be dismissed directly.
114                 .setNegativeButton(android.R.string.no, null);
115         AlertDialog dialog = builder.create();
116         dialog.setCanceledOnTouchOutside(false);
117         dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
118         return dialog;
119     }
120 }
121