blob: eab6e137bd4c6a8d70413f0060c4e61ecf7ec613 [file] [log] [blame]
Jason Monkda68f592015-01-07 10:55:58 -05001/*
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 */
16package com.android.systemui.statusbar.policy;
17
18import android.content.Context;
19import android.content.Intent;
20import android.net.NetworkCapabilities;
Jason Monkda68f592015-01-07 10:55:58 -050021import android.net.wifi.WifiManager;
22import android.os.Handler;
23import android.os.Message;
24import android.os.Messenger;
25import android.util.Log;
Jason Monkda68f592015-01-07 10:55:58 -050026import com.android.internal.annotations.VisibleForTesting;
27import com.android.internal.util.AsyncChannel;
Jason Monk4cf95ae2015-11-16 15:59:53 -050028import com.android.settingslib.wifi.WifiStatusTracker;
Jason Monk07b75fe2015-05-14 16:47:03 -040029import com.android.systemui.statusbar.policy.NetworkController.IconState;
Jason Monkda68f592015-01-07 10:55:58 -050030
Jason Monkda68f592015-01-07 10:55:58 -050031import java.util.Objects;
32
33
34public 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 Monk4cf95ae2015-11-16 15:59:53 -050039 private final WifiStatusTracker mWifiTracker;
Jason Monkda68f592015-01-07 10:55:58 -050040
41 public WifiSignalController(Context context, boolean hasMobileData,
Jason Monk07b75fe2015-05-14 16:47:03 -040042 CallbackHandler callbackHandler, NetworkControllerImpl networkController) {
Jason Monkda68f592015-01-07 10:55:58 -050043 super("WifiSignalController", context, NetworkCapabilities.TRANSPORT_WIFI,
Jason Monk07b75fe2015-05-14 16:47:03 -040044 callbackHandler, networkController);
Jason Monkda68f592015-01-07 10:55:58 -050045 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Jason Monk4cf95ae2015-11-16 15:59:53 -050046 mWifiTracker = new WifiStatusTracker(mWifiManager);
Jason Monkda68f592015-01-07 10:55:58 -050047 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 Monkda68f592015-01-07 10:55:58 -050081
Jason Monk07b75fe2015-05-14 16:47:03 -040082 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 Monkda68f592015-01-07 10:55:58 -050088 }
89
90 /**
91 * Extract wifi state directly from broadcasts about changes in wifi state.
92 */
93 public void handleBroadcast(Intent intent) {
Jason Monk4cf95ae2015-11-16 15:59:53 -050094 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 Monkda68f592015-01-07 10:55:58 -0500100 notifyListenersIfNecessary();
101 }
102
Jason Monkda68f592015-01-07 10:55:58 -0500103 @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}