14cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk/*
24cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk * Copyright (C) 2015 The Android Open Source Project
34cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk *
44cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk * 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
54cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk *
64cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
74cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk *
84cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk * 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.
94cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk */
104cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk
114cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monkpackage com.android.settingslib.wifi;
124cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk
134cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monkimport android.content.Intent;
144cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monkimport android.net.NetworkInfo;
154cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monkimport android.net.wifi.WifiConfiguration;
164cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monkimport android.net.wifi.WifiInfo;
174cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monkimport android.net.wifi.WifiManager;
184cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk
194cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monkimport java.util.List;
204cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk
214cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monkpublic class WifiStatusTracker {
224cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk
234cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    private final WifiManager mWifiManager;
244cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    public boolean enabled;
254cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    public boolean connected;
264cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    public String ssid;
274cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    public int rssi;
284cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    public int level;
294cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk
304cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    public WifiStatusTracker(WifiManager wifiManager) {
314cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        mWifiManager = wifiManager;
324cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    }
334cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk
344cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    public void handleBroadcast(Intent intent) {
354cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        String action = intent.getAction();
364cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
374cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            enabled = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
384cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                    WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_ENABLED;
394cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
404cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            final NetworkInfo networkInfo = (NetworkInfo)
414cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                    intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
424cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            connected = networkInfo != null && networkInfo.isConnected();
434cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            // If Connected grab the signal strength and ssid.
444cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            if (connected) {
454cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                // try getting it out of the intent first
464cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                WifiInfo info = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO) != null
474cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                        ? (WifiInfo) intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO)
484cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                        : mWifiManager.getConnectionInfo();
494cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                if (info != null) {
504cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                    ssid = getSsid(info);
514cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                } else {
524cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                    ssid = null;
534cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                }
544cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            } else if (!connected) {
554cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                ssid = null;
564cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            }
574cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
584cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            // Default to -200 as its below WifiManager.MIN_RSSI.
594cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            rssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200);
604cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            level = WifiManager.calculateSignalLevel(rssi, 5);
614cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        }
624cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    }
634cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk
644cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    private String getSsid(WifiInfo info) {
654cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        String ssid = info.getSSID();
664cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        if (ssid != null) {
674cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            return ssid;
684cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        }
694cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        // OK, it's not in the connectionInfo; we have to go hunting for it
704cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        List<WifiConfiguration> networks = mWifiManager.getConfiguredNetworks();
714cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        int length = networks.size();
724cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        for (int i = 0; i < length; i++) {
734cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            if (networks.get(i).networkId == info.getNetworkId()) {
744cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk                return networks.get(i).SSID;
754cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk            }
764cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        }
774cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk        return null;
784cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk    }
794cf95aef839701a43c9bde292f5e2d8ca3213509Jason Monk}
80