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