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