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