1 /* 2 * Copyright (C) 2016 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.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.net.ConnectivityManager; 23 import android.net.NetworkInfo; 24 import android.net.wifi.WifiInfo; 25 import android.net.wifi.WifiManager; 26 import android.os.PowerManager; 27 28 import java.util.ArrayList; 29 30 /** 31 * Utils for wifi tests. 32 */ 33 public class TestUtil { 34 /** 35 * Send {@link WifiManager#NETWORK_STATE_CHANGED_ACTION} broadcast. 36 */ sendNetworkStateChanged(BroadcastReceiver broadcastReceiver, Context context, NetworkInfo.DetailedState detailedState)37 public static void sendNetworkStateChanged(BroadcastReceiver broadcastReceiver, 38 Context context, NetworkInfo.DetailedState detailedState) { 39 Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION); 40 NetworkInfo networkInfo = new NetworkInfo(0, 0, "", ""); 41 networkInfo.setDetailedState(detailedState, "", ""); 42 intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo); 43 broadcastReceiver.onReceive(context, intent); 44 } 45 46 /** 47 * Send {@link WifiManager#NETWORK_STATE_CHANGED_ACTION} broadcast. 48 */ sendNetworkStateChanged(BroadcastReceiver broadcastReceiver, Context context, NetworkInfo nwInfo, WifiInfo wifiInfo)49 public static void sendNetworkStateChanged(BroadcastReceiver broadcastReceiver, 50 Context context, NetworkInfo nwInfo, WifiInfo wifiInfo) { 51 Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION); 52 intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, nwInfo); 53 intent.putExtra(WifiManager.EXTRA_WIFI_INFO, wifiInfo); 54 broadcastReceiver.onReceive(context, intent); 55 } 56 57 /** 58 * Send {@link WifiManager#SCAN_RESULTS_AVAILABLE_ACTION} broadcast. 59 */ sendScanResultsAvailable(BroadcastReceiver broadcastReceiver, Context context)60 public static void sendScanResultsAvailable(BroadcastReceiver broadcastReceiver, 61 Context context) { 62 Intent intent = new Intent(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 63 broadcastReceiver.onReceive(context, intent); 64 } 65 66 /** 67 * Send {@link WifiManager#WIFI_SCAN_AVAILABLE} broadcast. 68 */ sendWifiScanAvailable(BroadcastReceiver broadcastReceiver, Context context, int scanAvailable)69 public static void sendWifiScanAvailable(BroadcastReceiver broadcastReceiver, 70 Context context, int scanAvailable) { 71 Intent intent = new Intent(WifiManager.WIFI_SCAN_AVAILABLE); 72 intent.putExtra(WifiManager.EXTRA_SCAN_AVAILABLE, scanAvailable); 73 broadcastReceiver.onReceive(context, intent); 74 } 75 76 /** 77 * Send {@link WifiManager#WIFI_STATE_CHANGED} broadcast. 78 */ sendWifiStateChanged(BroadcastReceiver broadcastReceiver, Context context, int wifiState)79 public static void sendWifiStateChanged(BroadcastReceiver broadcastReceiver, 80 Context context, int wifiState) { 81 Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION); 82 intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState); 83 broadcastReceiver.onReceive(context, intent); 84 } 85 86 /** 87 * Send {@link WifiManager#WIFI_AP_STATE_CHANGED} broadcast. 88 */ sendWifiApStateChanged(BroadcastReceiver broadcastReceiver, Context context, int apState, int previousState, int error, String ifaceName, int mode)89 public static void sendWifiApStateChanged(BroadcastReceiver broadcastReceiver, 90 Context context, int apState, int previousState, int error, String ifaceName, 91 int mode) { 92 Intent intent = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); 93 intent.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, apState); 94 intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_AP_STATE, previousState); 95 if (apState == WifiManager.WIFI_AP_STATE_FAILED) { 96 // only set reason number when softAP start failed 97 intent.putExtra(WifiManager.EXTRA_WIFI_AP_FAILURE_REASON, error); 98 } 99 intent.putExtra(WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME, ifaceName); 100 intent.putExtra(WifiManager.EXTRA_WIFI_AP_MODE, mode); 101 broadcastReceiver.onReceive(context, intent); 102 } 103 104 /** 105 * Send {@link ConnectivityManager#ACTION_TETHER_STATE_CHANGED} broadcast. 106 */ sendTetherStateChanged(BroadcastReceiver broadcastReceiver, Context context, ArrayList<String> available, ArrayList<String> active)107 public static void sendTetherStateChanged(BroadcastReceiver broadcastReceiver, 108 Context context, ArrayList<String> available, ArrayList<String> active) { 109 Intent intent = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); 110 intent.putExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER, available); 111 intent.putExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, active); 112 broadcastReceiver.onReceive(context, intent); 113 } 114 sendIdleModeChanged(BroadcastReceiver broadcastReceiver, Context context)115 public static void sendIdleModeChanged(BroadcastReceiver broadcastReceiver, Context context) { 116 Intent intent = new Intent(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); 117 broadcastReceiver.onReceive(context, intent); 118 } 119 } 120