Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at |
| 5 | * |
| 6 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | * |
| 8 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| 9 | */ |
| 10 | |
| 11 | package com.android.settingslib.wifi; |
| 12 | |
| 13 | import android.content.Intent; |
| 14 | import android.net.NetworkInfo; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 15 | import android.net.NetworkKey; |
| 16 | import android.net.WifiKey; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 17 | import android.net.wifi.WifiConfiguration; |
| 18 | import android.net.wifi.WifiInfo; |
| 19 | import android.net.wifi.WifiManager; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 20 | import android.util.Log; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 21 | |
| 22 | import java.util.List; |
| 23 | |
| 24 | public class WifiStatusTracker { |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 25 | private static final String TAG = "WifiStatusTracker"; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 26 | |
| 27 | private final WifiManager mWifiManager; |
| 28 | public boolean enabled; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 29 | public int state; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 30 | public boolean connected; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 31 | public boolean connecting; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 32 | public String ssid; |
| 33 | public int rssi; |
| 34 | public int level; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 35 | public NetworkKey networkKey; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 36 | |
| 37 | public WifiStatusTracker(WifiManager wifiManager) { |
| 38 | mWifiManager = wifiManager; |
| 39 | } |
| 40 | |
| 41 | public void handleBroadcast(Intent intent) { |
| 42 | String action = intent.getAction(); |
| 43 | if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 44 | state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, |
| 45 | WifiManager.WIFI_STATE_UNKNOWN); |
| 46 | enabled = state == WifiManager.WIFI_STATE_ENABLED; |
| 47 | |
| 48 | |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 49 | enabled = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, |
| 50 | WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_ENABLED; |
| 51 | } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { |
| 52 | final NetworkInfo networkInfo = (NetworkInfo) |
| 53 | intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 54 | connecting = networkInfo != null && !networkInfo.isConnected() |
| 55 | && networkInfo.isConnectedOrConnecting(); |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 56 | connected = networkInfo != null && networkInfo.isConnected(); |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 57 | WifiInfo info = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO) != null |
| 58 | ? (WifiInfo) intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO) |
| 59 | : mWifiManager.getConnectionInfo(); |
| 60 | |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 61 | // If Connected grab the signal strength and ssid. |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 62 | if (connected && info != null) { |
| 63 | ssid = getSsid(info); |
| 64 | String bssid = info.getBSSID(); |
| 65 | if ((ssid != null) && (bssid != null)) { |
| 66 | // Reuse existing network key object if possible. |
| 67 | if ((networkKey == null) |
| 68 | || !networkKey.wifiKey.ssid.equals(ssid) |
| 69 | || !networkKey.wifiKey.bssid.equals(bssid)) { |
| 70 | try { |
| 71 | networkKey = new NetworkKey( |
| 72 | new WifiKey(ssid, bssid)); |
| 73 | } catch (IllegalArgumentException e) { |
| 74 | Log.e(TAG, "Cannot create NetworkKey", e); |
| 75 | } |
| 76 | } |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 77 | } else { |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 78 | networkKey = null; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 79 | } |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 80 | } else { |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 81 | ssid = null; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 82 | networkKey = null; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 83 | } |
| 84 | } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) { |
| 85 | // Default to -200 as its below WifiManager.MIN_RSSI. |
| 86 | rssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200); |
| 87 | level = WifiManager.calculateSignalLevel(rssi, 5); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private String getSsid(WifiInfo info) { |
| 92 | String ssid = info.getSSID(); |
| 93 | if (ssid != null) { |
| 94 | return ssid; |
| 95 | } |
| 96 | // OK, it's not in the connectionInfo; we have to go hunting for it |
| 97 | List<WifiConfiguration> networks = mWifiManager.getConfiguredNetworks(); |
| 98 | int length = networks.size(); |
| 99 | for (int i = 0; i < length; i++) { |
| 100 | if (networks.get(i).networkId == info.getNetworkId()) { |
| 101 | return networks.get(i).SSID; |
| 102 | } |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | } |