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 | |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 18 | import static android.net.wifi.WifiManager.TrafficStateCallback.DATA_ACTIVITY_IN; |
| 19 | import static android.net.wifi.WifiManager.TrafficStateCallback.DATA_ACTIVITY_INOUT; |
| 20 | import static android.net.wifi.WifiManager.TrafficStateCallback.DATA_ACTIVITY_OUT; |
| 21 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 25 | import android.net.NetworkCapabilities; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 26 | import android.net.NetworkScoreManager; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 27 | import android.net.wifi.WifiManager; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 28 | import android.text.TextUtils; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 29 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 30 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 31 | import com.android.settingslib.wifi.WifiStatusTracker; |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 32 | import com.android.systemui.R; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 33 | import com.android.systemui.statusbar.policy.NetworkController.IconState; |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 34 | import com.android.systemui.statusbar.policy.NetworkController.SignalCallback; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 35 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 36 | import java.util.Objects; |
| 37 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 38 | public class WifiSignalController extends |
| 39 | SignalController<WifiSignalController.WifiState, SignalController.IconGroup> { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 40 | private final boolean mHasMobileData; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 41 | private final WifiStatusTracker mWifiTracker; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 42 | |
| 43 | public WifiSignalController(Context context, boolean hasMobileData, |
Amin Shaikh | c7593b1 | 2018-03-09 16:07:09 -0500 | [diff] [blame] | 44 | CallbackHandler callbackHandler, NetworkControllerImpl networkController, |
| 45 | WifiManager wifiManager) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 46 | super("WifiSignalController", context, NetworkCapabilities.TRANSPORT_WIFI, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 47 | callbackHandler, networkController); |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 48 | NetworkScoreManager networkScoreManager = |
| 49 | context.getSystemService(NetworkScoreManager.class); |
| 50 | ConnectivityManager connectivityManager = |
| 51 | context.getSystemService(ConnectivityManager.class); |
| 52 | mWifiTracker = new WifiStatusTracker(mContext, wifiManager, networkScoreManager, |
| 53 | connectivityManager, this::handleStatusUpdated); |
| 54 | mWifiTracker.setListening(true); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 55 | mHasMobileData = hasMobileData; |
Roshan Pius | ad32dbf | 2019-11-19 06:18:51 -0800 | [diff] [blame] | 56 | if (wifiManager != null) { |
David Su | 09daba1 | 2020-01-23 12:47:24 -0800 | [diff] [blame] | 57 | wifiManager.registerTrafficStateCallback(context.getMainExecutor(), |
| 58 | new WifiTrafficStateCallback()); |
Roshan Pius | ad32dbf | 2019-11-19 06:18:51 -0800 | [diff] [blame] | 59 | } |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 60 | // WiFi only has one state. |
| 61 | mCurrentState.iconGroup = mLastState.iconGroup = new IconGroup( |
| 62 | "Wi-Fi Icons", |
| 63 | WifiIcons.WIFI_SIGNAL_STRENGTH, |
| 64 | WifiIcons.QS_WIFI_SIGNAL_STRENGTH, |
| 65 | AccessibilityContentDescriptions.WIFI_CONNECTION_STRENGTH, |
| 66 | WifiIcons.WIFI_NO_NETWORK, |
| 67 | WifiIcons.QS_WIFI_NO_NETWORK, |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 68 | WifiIcons.WIFI_NO_NETWORK, |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 69 | WifiIcons.QS_WIFI_NO_NETWORK, |
| 70 | AccessibilityContentDescriptions.WIFI_NO_CONNECTION |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected WifiState cleanState() { |
| 76 | return new WifiState(); |
| 77 | } |
| 78 | |
Fabian Kozynski | d9cb384 | 2018-09-28 13:34:14 -0400 | [diff] [blame] | 79 | void refreshLocale() { |
| 80 | mWifiTracker.refreshLocale(); |
| 81 | } |
| 82 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 83 | @Override |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 84 | public void notifyListeners(SignalCallback callback) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 85 | // only show wifi in the cluster if connected or if wifi-only |
Minming Qi | e2d48ca | 2017-03-10 13:56:44 +0800 | [diff] [blame] | 86 | boolean visibleWhenEnabled = mContext.getResources().getBoolean( |
| 87 | R.bool.config_showWifiIndicatorWhenEnabled); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 88 | boolean wifiVisible = mCurrentState.enabled |
Evan Laird | b0b399a | 2019-12-16 15:22:52 -0500 | [diff] [blame] | 89 | && ((mCurrentState.connected && mCurrentState.inetCondition == 1) |
| 90 | || !mHasMobileData || visibleWhenEnabled); |
Etan Cohen | 15fcce4 | 2020-02-20 17:33:31 -0800 | [diff] [blame^] | 91 | String wifiDesc = mCurrentState.connected ? mCurrentState.ssid : null; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 92 | boolean ssidPresent = wifiVisible && mCurrentState.ssid != null; |
Evan Laird | 5784a6b | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 93 | String contentDescription = getTextIfExists(getContentDescription()).toString(); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 94 | if (mCurrentState.inetCondition == 0) { |
Amin Shaikh | 43ad7fe | 2018-03-05 14:34:44 -0500 | [diff] [blame] | 95 | contentDescription += ("," + mContext.getString(R.string.data_connection_no_internet)); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 96 | } |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 97 | IconState statusIcon = new IconState(wifiVisible, getCurrentIconId(), contentDescription); |
Etan Cohen | 15fcce4 | 2020-02-20 17:33:31 -0800 | [diff] [blame^] | 98 | IconState qsIcon = new IconState(mCurrentState.connected, |
| 99 | mWifiTracker.isCaptivePortal ? R.drawable.ic_qs_wifi_disconnected |
| 100 | : getQsCurrentIconId(), contentDescription); |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 101 | callback.setWifiIndicators(mCurrentState.enabled, statusIcon, qsIcon, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 102 | ssidPresent && mCurrentState.activityIn, ssidPresent && mCurrentState.activityOut, |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 103 | wifiDesc, mCurrentState.isTransient, mCurrentState.statusLabel); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Extract wifi state directly from broadcasts about changes in wifi state. |
| 108 | */ |
| 109 | public void handleBroadcast(Intent intent) { |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 110 | mWifiTracker.handleBroadcast(intent); |
| 111 | mCurrentState.enabled = mWifiTracker.enabled; |
| 112 | mCurrentState.connected = mWifiTracker.connected; |
| 113 | mCurrentState.ssid = mWifiTracker.ssid; |
| 114 | mCurrentState.rssi = mWifiTracker.rssi; |
| 115 | mCurrentState.level = mWifiTracker.level; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 116 | mCurrentState.statusLabel = mWifiTracker.statusLabel; |
| 117 | notifyListenersIfNecessary(); |
| 118 | } |
| 119 | |
| 120 | private void handleStatusUpdated() { |
| 121 | mCurrentState.statusLabel = mWifiTracker.statusLabel; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 122 | notifyListenersIfNecessary(); |
| 123 | } |
| 124 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 125 | @VisibleForTesting |
| 126 | void setActivity(int wifiActivity) { |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 127 | mCurrentState.activityIn = wifiActivity == DATA_ACTIVITY_INOUT |
| 128 | || wifiActivity == DATA_ACTIVITY_IN; |
| 129 | mCurrentState.activityOut = wifiActivity == DATA_ACTIVITY_INOUT |
| 130 | || wifiActivity == DATA_ACTIVITY_OUT; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 131 | notifyListenersIfNecessary(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Handler to receive the data activity on wifi. |
| 136 | */ |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 137 | private class WifiTrafficStateCallback implements WifiManager.TrafficStateCallback { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 138 | @Override |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 139 | public void onStateChanged(int state) { |
| 140 | setActivity(state); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | static class WifiState extends SignalController.State { |
| 145 | String ssid; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 146 | boolean isTransient; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 147 | String statusLabel; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public void copyFrom(State s) { |
| 151 | super.copyFrom(s); |
| 152 | WifiState state = (WifiState) s; |
| 153 | ssid = state.ssid; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 154 | isTransient = state.isTransient; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 155 | statusLabel = state.statusLabel; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | protected void toString(StringBuilder builder) { |
| 160 | super.toString(builder); |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 161 | builder.append(",ssid=").append(ssid) |
| 162 | .append(",isTransient=").append(isTransient) |
| 163 | .append(",statusLabel=").append(statusLabel); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public boolean equals(Object o) { |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 168 | if (!super.equals(o)) { |
| 169 | return false; |
| 170 | } |
| 171 | WifiState other = (WifiState) o; |
| 172 | return Objects.equals(other.ssid, ssid) |
| 173 | && other.isTransient == isTransient |
| 174 | && TextUtils.equals(other.statusLabel, statusLabel); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |