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