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; |
| 20 | import android.net.NetworkCapabilities; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 21 | import android.net.wifi.WifiManager; |
| 22 | import android.os.Handler; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 23 | import android.os.Looper; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 24 | import android.os.Message; |
| 25 | import android.os.Messenger; |
| 26 | import android.util.Log; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 27 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 28 | import com.android.internal.annotations.VisibleForTesting; |
| 29 | import com.android.internal.util.AsyncChannel; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 30 | import com.android.settingslib.wifi.WifiStatusTracker; |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 31 | import com.android.systemui.R; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 32 | import com.android.systemui.statusbar.policy.NetworkController.IconState; |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 33 | import com.android.systemui.statusbar.policy.NetworkController.SignalCallback; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 34 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 35 | import java.util.Objects; |
| 36 | |
| 37 | |
| 38 | public class WifiSignalController extends |
| 39 | SignalController<WifiSignalController.WifiState, SignalController.IconGroup> { |
| 40 | private final WifiManager mWifiManager; |
| 41 | private final AsyncChannel mWifiChannel; |
| 42 | private final boolean mHasMobileData; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 43 | private final WifiStatusTracker mWifiTracker; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 44 | |
| 45 | public WifiSignalController(Context context, boolean hasMobileData, |
Amin Shaikh | c7593b1 | 2018-03-09 16:07:09 -0500 | [diff] [blame^] | 46 | CallbackHandler callbackHandler, NetworkControllerImpl networkController, |
| 47 | WifiManager wifiManager) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 48 | super("WifiSignalController", context, NetworkCapabilities.TRANSPORT_WIFI, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 49 | callbackHandler, networkController); |
Amin Shaikh | c7593b1 | 2018-03-09 16:07:09 -0500 | [diff] [blame^] | 50 | mWifiManager = wifiManager; |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 51 | mWifiTracker = new WifiStatusTracker(mWifiManager); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 52 | mHasMobileData = hasMobileData; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 53 | Handler handler = new WifiHandler(Looper.getMainLooper()); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 54 | mWifiChannel = new AsyncChannel(); |
| 55 | Messenger wifiMessenger = mWifiManager.getWifiServiceMessenger(); |
| 56 | if (wifiMessenger != null) { |
| 57 | mWifiChannel.connect(context, handler, wifiMessenger); |
| 58 | } |
| 59 | // WiFi only has one state. |
| 60 | mCurrentState.iconGroup = mLastState.iconGroup = new IconGroup( |
| 61 | "Wi-Fi Icons", |
| 62 | WifiIcons.WIFI_SIGNAL_STRENGTH, |
| 63 | WifiIcons.QS_WIFI_SIGNAL_STRENGTH, |
| 64 | AccessibilityContentDescriptions.WIFI_CONNECTION_STRENGTH, |
| 65 | WifiIcons.WIFI_NO_NETWORK, |
| 66 | WifiIcons.QS_WIFI_NO_NETWORK, |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 67 | WifiIcons.WIFI_NO_NETWORK, |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 68 | WifiIcons.QS_WIFI_NO_NETWORK, |
| 69 | AccessibilityContentDescriptions.WIFI_NO_CONNECTION |
| 70 | ); |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 71 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected WifiState cleanState() { |
| 76 | return new WifiState(); |
| 77 | } |
| 78 | |
| 79 | @Override |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 80 | public void notifyListeners(SignalCallback callback) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 81 | // only show wifi in the cluster if connected or if wifi-only |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 82 | boolean wifiVisible = mCurrentState.enabled |
| 83 | && (mCurrentState.connected || !mHasMobileData); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 84 | String wifiDesc = wifiVisible ? mCurrentState.ssid : null; |
| 85 | boolean ssidPresent = wifiVisible && mCurrentState.ssid != null; |
| 86 | String contentDescription = getStringIfExists(getContentDescription()); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 87 | if (mCurrentState.inetCondition == 0) { |
Amin Shaikh | 43ad7fe | 2018-03-05 14:34:44 -0500 | [diff] [blame] | 88 | contentDescription += ("," + mContext.getString(R.string.data_connection_no_internet)); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 89 | } |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 90 | |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 91 | IconState statusIcon = new IconState(wifiVisible, getCurrentIconId(), contentDescription); |
| 92 | IconState qsIcon = new IconState(mCurrentState.connected, getQsCurrentIconId(), |
| 93 | contentDescription); |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 94 | callback.setWifiIndicators(mCurrentState.enabled, statusIcon, qsIcon, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 95 | ssidPresent && mCurrentState.activityIn, ssidPresent && mCurrentState.activityOut, |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 96 | wifiDesc, mCurrentState.isTransient); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Extract wifi state directly from broadcasts about changes in wifi state. |
| 101 | */ |
| 102 | public void handleBroadcast(Intent intent) { |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 103 | mWifiTracker.handleBroadcast(intent); |
| 104 | mCurrentState.enabled = mWifiTracker.enabled; |
| 105 | mCurrentState.connected = mWifiTracker.connected; |
| 106 | mCurrentState.ssid = mWifiTracker.ssid; |
| 107 | mCurrentState.rssi = mWifiTracker.rssi; |
| 108 | mCurrentState.level = mWifiTracker.level; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 109 | notifyListenersIfNecessary(); |
| 110 | } |
| 111 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 112 | @VisibleForTesting |
| 113 | void setActivity(int wifiActivity) { |
| 114 | mCurrentState.activityIn = wifiActivity == WifiManager.DATA_ACTIVITY_INOUT |
| 115 | || wifiActivity == WifiManager.DATA_ACTIVITY_IN; |
| 116 | mCurrentState.activityOut = wifiActivity == WifiManager.DATA_ACTIVITY_INOUT |
| 117 | || wifiActivity == WifiManager.DATA_ACTIVITY_OUT; |
| 118 | notifyListenersIfNecessary(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Handler to receive the data activity on wifi. |
| 123 | */ |
| 124 | private class WifiHandler extends Handler { |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 125 | WifiHandler(Looper looper) { |
| 126 | super(looper); |
| 127 | } |
| 128 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 129 | @Override |
| 130 | public void handleMessage(Message msg) { |
| 131 | switch (msg.what) { |
| 132 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: |
| 133 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 134 | mWifiChannel.sendMessage(Message.obtain(this, |
| 135 | AsyncChannel.CMD_CHANNEL_FULL_CONNECTION)); |
| 136 | } else { |
| 137 | Log.e(mTag, "Failed to connect to wifi"); |
| 138 | } |
| 139 | break; |
| 140 | case WifiManager.DATA_ACTIVITY_NOTIFICATION: |
| 141 | setActivity(msg.arg1); |
| 142 | break; |
| 143 | default: |
| 144 | // Ignore |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static class WifiState extends SignalController.State { |
| 151 | String ssid; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 152 | boolean isTransient; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 153 | |
| 154 | @Override |
| 155 | public void copyFrom(State s) { |
| 156 | super.copyFrom(s); |
| 157 | WifiState state = (WifiState) s; |
| 158 | ssid = state.ssid; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 159 | isTransient = state.isTransient; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected void toString(StringBuilder builder) { |
| 164 | super.toString(builder); |
| 165 | builder.append(',').append("ssid=").append(ssid); |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 166 | builder.append(',').append("isTransient=").append(isTransient); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean equals(Object o) { |
| 171 | return super.equals(o) |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 172 | && Objects.equals(((WifiState) o).ssid, ssid) |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 173 | && (((WifiState) o).isTransient == isTransient); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |