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 | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame^] | 56 | wifiManager.registerTrafficStateCallback(new WifiTrafficStateCallback(), null); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 57 | // WiFi only has one state. |
| 58 | mCurrentState.iconGroup = mLastState.iconGroup = new IconGroup( |
| 59 | "Wi-Fi Icons", |
| 60 | WifiIcons.WIFI_SIGNAL_STRENGTH, |
| 61 | WifiIcons.QS_WIFI_SIGNAL_STRENGTH, |
| 62 | AccessibilityContentDescriptions.WIFI_CONNECTION_STRENGTH, |
| 63 | WifiIcons.WIFI_NO_NETWORK, |
| 64 | WifiIcons.QS_WIFI_NO_NETWORK, |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 65 | WifiIcons.WIFI_NO_NETWORK, |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 66 | WifiIcons.QS_WIFI_NO_NETWORK, |
| 67 | AccessibilityContentDescriptions.WIFI_NO_CONNECTION |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected WifiState cleanState() { |
| 73 | return new WifiState(); |
| 74 | } |
| 75 | |
| 76 | @Override |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 77 | public void notifyListeners(SignalCallback callback) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 78 | // only show wifi in the cluster if connected or if wifi-only |
Minming Qi | e2d48ca | 2017-03-10 13:56:44 +0800 | [diff] [blame] | 79 | boolean visibleWhenEnabled = mContext.getResources().getBoolean( |
| 80 | R.bool.config_showWifiIndicatorWhenEnabled); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 81 | boolean wifiVisible = mCurrentState.enabled |
Minming Qi | e2d48ca | 2017-03-10 13:56:44 +0800 | [diff] [blame] | 82 | && (mCurrentState.connected || !mHasMobileData || visibleWhenEnabled); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 83 | String wifiDesc = wifiVisible ? mCurrentState.ssid : null; |
| 84 | boolean ssidPresent = wifiVisible && mCurrentState.ssid != null; |
| 85 | String contentDescription = getStringIfExists(getContentDescription()); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 86 | if (mCurrentState.inetCondition == 0) { |
Amin Shaikh | 43ad7fe | 2018-03-05 14:34:44 -0500 | [diff] [blame] | 87 | contentDescription += ("," + mContext.getString(R.string.data_connection_no_internet)); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 88 | } |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 89 | IconState statusIcon = new IconState(wifiVisible, getCurrentIconId(), contentDescription); |
| 90 | IconState qsIcon = new IconState(mCurrentState.connected, getQsCurrentIconId(), |
| 91 | contentDescription); |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 92 | callback.setWifiIndicators(mCurrentState.enabled, statusIcon, qsIcon, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 93 | ssidPresent && mCurrentState.activityIn, ssidPresent && mCurrentState.activityOut, |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 94 | wifiDesc, mCurrentState.isTransient, mCurrentState.statusLabel); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Extract wifi state directly from broadcasts about changes in wifi state. |
| 99 | */ |
| 100 | public void handleBroadcast(Intent intent) { |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 101 | mWifiTracker.handleBroadcast(intent); |
| 102 | mCurrentState.enabled = mWifiTracker.enabled; |
| 103 | mCurrentState.connected = mWifiTracker.connected; |
| 104 | mCurrentState.ssid = mWifiTracker.ssid; |
| 105 | mCurrentState.rssi = mWifiTracker.rssi; |
| 106 | mCurrentState.level = mWifiTracker.level; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 107 | mCurrentState.statusLabel = mWifiTracker.statusLabel; |
| 108 | notifyListenersIfNecessary(); |
| 109 | } |
| 110 | |
| 111 | private void handleStatusUpdated() { |
| 112 | mCurrentState.statusLabel = mWifiTracker.statusLabel; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 113 | notifyListenersIfNecessary(); |
| 114 | } |
| 115 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 116 | @VisibleForTesting |
| 117 | void setActivity(int wifiActivity) { |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame^] | 118 | mCurrentState.activityIn = wifiActivity == DATA_ACTIVITY_INOUT |
| 119 | || wifiActivity == DATA_ACTIVITY_IN; |
| 120 | mCurrentState.activityOut = wifiActivity == DATA_ACTIVITY_INOUT |
| 121 | || wifiActivity == DATA_ACTIVITY_OUT; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 122 | notifyListenersIfNecessary(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Handler to receive the data activity on wifi. |
| 127 | */ |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame^] | 128 | private class WifiTrafficStateCallback implements WifiManager.TrafficStateCallback { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 129 | @Override |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame^] | 130 | public void onStateChanged(int state) { |
| 131 | setActivity(state); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | static class WifiState extends SignalController.State { |
| 136 | String ssid; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 137 | boolean isTransient; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 138 | String statusLabel; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 139 | |
| 140 | @Override |
| 141 | public void copyFrom(State s) { |
| 142 | super.copyFrom(s); |
| 143 | WifiState state = (WifiState) s; |
| 144 | ssid = state.ssid; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 145 | isTransient = state.isTransient; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 146 | statusLabel = state.statusLabel; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | protected void toString(StringBuilder builder) { |
| 151 | super.toString(builder); |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 152 | builder.append(",ssid=").append(ssid) |
| 153 | .append(",isTransient=").append(isTransient) |
| 154 | .append(",statusLabel=").append(statusLabel); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public boolean equals(Object o) { |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 159 | if (!super.equals(o)) { |
| 160 | return false; |
| 161 | } |
| 162 | WifiState other = (WifiState) o; |
| 163 | return Objects.equals(other.ssid, ssid) |
| 164 | && other.isTransient == isTransient |
| 165 | && TextUtils.equals(other.statusLabel, statusLabel); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |