Jason Monk | da68f59 | 2015-01-07 10:55:58 -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"); |
| 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 | package com.android.systemui.statusbar.policy; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.Intent; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 20 | import android.database.ContentObserver; |
Sundeep Ghuman | 699deaf | 2017-02-13 15:32:13 -0800 | [diff] [blame] | 21 | import android.net.NetworkBadging; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 22 | import android.net.NetworkCapabilities; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 23 | import android.net.NetworkKey; |
| 24 | import android.net.NetworkScoreManager; |
| 25 | import android.net.ScoredNetwork; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 26 | import android.net.wifi.WifiManager; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 27 | import android.net.wifi.WifiNetworkScoreCache; |
| 28 | import android.net.wifi.WifiNetworkScoreCache.CacheListener; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 29 | import android.os.Handler; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 30 | import android.os.Looper; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 31 | import android.os.Message; |
| 32 | import android.os.Messenger; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 33 | import android.provider.Settings; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 34 | import android.util.Log; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 35 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 36 | import com.android.internal.annotations.VisibleForTesting; |
| 37 | import com.android.internal.util.AsyncChannel; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 38 | import com.android.settingslib.Utils; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 39 | import com.android.settingslib.wifi.WifiStatusTracker; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 40 | import com.android.systemui.statusbar.policy.NetworkController.IconState; |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 41 | import com.android.systemui.statusbar.policy.NetworkController.SignalCallback; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 42 | |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 43 | import com.android.systemui.R; |
| 44 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 45 | import java.util.Objects; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 46 | import java.util.List; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 47 | |
| 48 | |
| 49 | public class WifiSignalController extends |
| 50 | SignalController<WifiSignalController.WifiState, SignalController.IconGroup> { |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 51 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 52 | private final WifiManager mWifiManager; |
| 53 | private final AsyncChannel mWifiChannel; |
| 54 | private final boolean mHasMobileData; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 55 | private final NetworkScoreManager mNetworkScoreManager; |
| 56 | private final WifiNetworkScoreCache mScoreCache; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 57 | private final WifiStatusTracker mWifiTracker; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 58 | |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 59 | private boolean mScoringUiEnabled = false; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 60 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 61 | public WifiSignalController(Context context, boolean hasMobileData, |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 62 | CallbackHandler callbackHandler, NetworkControllerImpl networkController, |
| 63 | NetworkScoreManager networkScoreManager) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 64 | super("WifiSignalController", context, NetworkCapabilities.TRANSPORT_WIFI, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 65 | callbackHandler, networkController); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 66 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 67 | mWifiTracker = new WifiStatusTracker(mWifiManager); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 68 | mHasMobileData = hasMobileData; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 69 | Handler handler = new WifiHandler(Looper.getMainLooper()); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 70 | mWifiChannel = new AsyncChannel(); |
| 71 | Messenger wifiMessenger = mWifiManager.getWifiServiceMessenger(); |
| 72 | if (wifiMessenger != null) { |
| 73 | mWifiChannel.connect(context, handler, wifiMessenger); |
| 74 | } |
| 75 | // WiFi only has one state. |
| 76 | mCurrentState.iconGroup = mLastState.iconGroup = new IconGroup( |
| 77 | "Wi-Fi Icons", |
| 78 | WifiIcons.WIFI_SIGNAL_STRENGTH, |
| 79 | WifiIcons.QS_WIFI_SIGNAL_STRENGTH, |
| 80 | AccessibilityContentDescriptions.WIFI_CONNECTION_STRENGTH, |
| 81 | WifiIcons.WIFI_NO_NETWORK, |
| 82 | WifiIcons.QS_WIFI_NO_NETWORK, |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 83 | WifiIcons.WIFI_NO_NETWORK, |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 84 | WifiIcons.QS_WIFI_NO_NETWORK, |
| 85 | AccessibilityContentDescriptions.WIFI_NO_CONNECTION |
| 86 | ); |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 87 | |
| 88 | mScoreCache = new WifiNetworkScoreCache(context, new CacheListener(handler) { |
| 89 | @Override |
| 90 | public void networkCacheUpdated(List<ScoredNetwork> networks) { |
| 91 | mCurrentState.badgeEnum = getWifiBadgeEnum(); |
| 92 | notifyListenersIfNecessary(); |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | // Setup scoring |
| 97 | mNetworkScoreManager = networkScoreManager; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 98 | configureScoringGating(); |
| 99 | registerScoreCache(); |
| 100 | } |
| 101 | |
| 102 | private void configureScoringGating() { |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 103 | ContentObserver observer = new ContentObserver(new Handler(Looper.getMainLooper())) { |
| 104 | @Override |
| 105 | public void onChange(boolean selfChange) { |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 106 | mScoringUiEnabled = |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 107 | Settings.Global.getInt( |
| 108 | mContext.getContentResolver(), |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 109 | Settings.Global.NETWORK_SCORING_UI_ENABLED, 0) == 1; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 110 | } |
| 111 | }; |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 112 | mContext.getContentResolver().registerContentObserver( |
| 113 | Settings.Global.getUriFor(Settings.Global.NETWORK_SCORING_UI_ENABLED), |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 114 | false /* notifyForDescendants */, |
| 115 | observer); |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 116 | |
| 117 | observer.onChange(false /* selfChange */); // Set the initial values |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private void registerScoreCache() { |
| 121 | Log.d(mTag, "Registered score cache"); |
| 122 | mNetworkScoreManager.registerNetworkScoreCache( |
| 123 | NetworkKey.TYPE_WIFI, |
| 124 | mScoreCache, |
| 125 | NetworkScoreManager.CACHE_FILTER_CURRENT_NETWORK); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | protected WifiState cleanState() { |
| 130 | return new WifiState(); |
| 131 | } |
| 132 | |
| 133 | @Override |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 134 | public void notifyListeners(SignalCallback callback) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 135 | // only show wifi in the cluster if connected or if wifi-only |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 136 | boolean wifiVisible = mCurrentState.enabled |
| 137 | && (mCurrentState.connected || !mHasMobileData); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 138 | String wifiDesc = wifiVisible ? mCurrentState.ssid : null; |
| 139 | boolean ssidPresent = wifiVisible && mCurrentState.ssid != null; |
| 140 | String contentDescription = getStringIfExists(getContentDescription()); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 141 | if (mCurrentState.inetCondition == 0) { |
| 142 | contentDescription += |
| 143 | ("," + mContext.getString(R.string.accessibility_quick_settings_no_internet)); |
| 144 | } |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 145 | |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 146 | IconState statusIcon = new IconState(wifiVisible, getCurrentIconId(), |
| 147 | Utils.getWifiBadgeResource(mCurrentState.badgeEnum), contentDescription); |
| 148 | IconState qsIcon = new IconState( |
| 149 | mCurrentState.connected, getQsCurrentIconId(), |
| 150 | Utils.getWifiBadgeResource(mCurrentState.badgeEnum), contentDescription); |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 151 | callback.setWifiIndicators(mCurrentState.enabled, statusIcon, qsIcon, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 152 | ssidPresent && mCurrentState.activityIn, ssidPresent && mCurrentState.activityOut, |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 153 | wifiDesc, mCurrentState.isTransient); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 156 | @Override |
| 157 | public int getCurrentIconId() { |
Sundeep Ghuman | 699deaf | 2017-02-13 15:32:13 -0800 | [diff] [blame] | 158 | if (mCurrentState.badgeEnum != NetworkBadging.BADGING_NONE) { |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 159 | return Utils.WIFI_PIE_FOR_BADGING[mCurrentState.level]; |
| 160 | } |
| 161 | return super.getCurrentIconId(); |
| 162 | } |
| 163 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 164 | /** |
| 165 | * Extract wifi state directly from broadcasts about changes in wifi state. |
| 166 | */ |
| 167 | public void handleBroadcast(Intent intent) { |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 168 | // Update the WifiStatusTracker with the new information and update the score cache. |
| 169 | NetworkKey previousNetworkKey = mWifiTracker.networkKey; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 170 | mWifiTracker.handleBroadcast(intent); |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 171 | updateScoreCacheIfNecessary(previousNetworkKey); |
| 172 | |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 173 | mCurrentState.isTransient = mWifiTracker.state == WifiManager.WIFI_STATE_ENABLING |
| 174 | || mWifiTracker.state == WifiManager.WIFI_AP_STATE_DISABLING |
| 175 | || mWifiTracker.connecting; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 176 | mCurrentState.enabled = mWifiTracker.enabled; |
| 177 | mCurrentState.connected = mWifiTracker.connected; |
| 178 | mCurrentState.ssid = mWifiTracker.ssid; |
| 179 | mCurrentState.rssi = mWifiTracker.rssi; |
| 180 | mCurrentState.level = mWifiTracker.level; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 181 | mCurrentState.badgeEnum = getWifiBadgeEnum(); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 182 | notifyListenersIfNecessary(); |
| 183 | } |
| 184 | |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 185 | /** |
| 186 | * Clears old scores out of the cache and requests new scores if the network key has changed. |
| 187 | * |
| 188 | * <p>New scores are requested asynchronously. |
| 189 | */ |
| 190 | private void updateScoreCacheIfNecessary(NetworkKey previousNetworkKey) { |
| 191 | if (mWifiTracker.networkKey == null) { |
| 192 | return; |
| 193 | } |
| 194 | if ((previousNetworkKey == null) || !mWifiTracker.networkKey.equals(previousNetworkKey)) { |
| 195 | mScoreCache.clearScores(); |
| 196 | mNetworkScoreManager.requestScores(new NetworkKey[]{mWifiTracker.networkKey}); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns the wifi badge enum for the current {@link #mWifiTracker} state. |
| 202 | * |
| 203 | * <p>{@link #updateScoreCacheIfNecessary} should be called prior to this method. |
| 204 | */ |
| 205 | private int getWifiBadgeEnum() { |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 206 | if (!mScoringUiEnabled || mWifiTracker.networkKey == null) { |
Sundeep Ghuman | 699deaf | 2017-02-13 15:32:13 -0800 | [diff] [blame] | 207 | return NetworkBadging.BADGING_NONE; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 208 | } |
| 209 | ScoredNetwork score = mScoreCache.getScoredNetwork(mWifiTracker.networkKey); |
| 210 | |
| 211 | if (score != null) { |
| 212 | return score.calculateBadge(mWifiTracker.rssi); |
| 213 | } |
Sundeep Ghuman | 699deaf | 2017-02-13 15:32:13 -0800 | [diff] [blame] | 214 | return NetworkBadging.BADGING_NONE; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 217 | @VisibleForTesting |
| 218 | void setActivity(int wifiActivity) { |
| 219 | mCurrentState.activityIn = wifiActivity == WifiManager.DATA_ACTIVITY_INOUT |
| 220 | || wifiActivity == WifiManager.DATA_ACTIVITY_IN; |
| 221 | mCurrentState.activityOut = wifiActivity == WifiManager.DATA_ACTIVITY_INOUT |
| 222 | || wifiActivity == WifiManager.DATA_ACTIVITY_OUT; |
| 223 | notifyListenersIfNecessary(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Handler to receive the data activity on wifi. |
| 228 | */ |
| 229 | private class WifiHandler extends Handler { |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 230 | WifiHandler(Looper looper) { |
| 231 | super(looper); |
| 232 | } |
| 233 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 234 | @Override |
| 235 | public void handleMessage(Message msg) { |
| 236 | switch (msg.what) { |
| 237 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: |
| 238 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 239 | mWifiChannel.sendMessage(Message.obtain(this, |
| 240 | AsyncChannel.CMD_CHANNEL_FULL_CONNECTION)); |
| 241 | } else { |
| 242 | Log.e(mTag, "Failed to connect to wifi"); |
| 243 | } |
| 244 | break; |
| 245 | case WifiManager.DATA_ACTIVITY_NOTIFICATION: |
| 246 | setActivity(msg.arg1); |
| 247 | break; |
| 248 | default: |
| 249 | // Ignore |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static class WifiState extends SignalController.State { |
| 256 | String ssid; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 257 | int badgeEnum; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 258 | boolean isTransient; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 259 | |
| 260 | @Override |
| 261 | public void copyFrom(State s) { |
| 262 | super.copyFrom(s); |
| 263 | WifiState state = (WifiState) s; |
| 264 | ssid = state.ssid; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 265 | badgeEnum = state.badgeEnum; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 266 | isTransient = state.isTransient; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | protected void toString(StringBuilder builder) { |
| 271 | super.toString(builder); |
| 272 | builder.append(',').append("ssid=").append(ssid); |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 273 | builder.append(',').append("badgeEnum=").append(badgeEnum); |
| 274 | builder.append(',').append("isTransient=").append(isTransient); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public boolean equals(Object o) { |
| 279 | return super.equals(o) |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 280 | && Objects.equals(((WifiState) o).ssid, ssid) |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 281 | && (((WifiState) o).badgeEnum == badgeEnum) |
| 282 | && (((WifiState) o).isTransient == isTransient); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |