blob: 1bef0c27901fbe319a1bb8ff9e1c92621b92fad2 [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 Monke06b0652016-03-02 16:35:27 -050031import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
Jason Monkda68f592015-01-07 10:55:58 -050032
Julia Reynolds20aef8a2016-05-04 16:44:08 -040033import com.android.systemui.R;
34
Jason Monkda68f592015-01-07 10:55:58 -050035import java.util.Objects;
36
37
38public 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 Monk4cf95ae2015-11-16 15:59:53 -050043 private final WifiStatusTracker mWifiTracker;
Jason Monkda68f592015-01-07 10:55:58 -050044
45 public WifiSignalController(Context context, boolean hasMobileData,
Jason Monk07b75fe2015-05-14 16:47:03 -040046 CallbackHandler callbackHandler, NetworkControllerImpl networkController) {
Jason Monkda68f592015-01-07 10:55:58 -050047 super("WifiSignalController", context, NetworkCapabilities.TRANSPORT_WIFI,
Jason Monk07b75fe2015-05-14 16:47:03 -040048 callbackHandler, networkController);
Jason Monkda68f592015-01-07 10:55:58 -050049 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Jason Monk4cf95ae2015-11-16 15:59:53 -050050 mWifiTracker = new WifiStatusTracker(mWifiManager);
Jason Monkda68f592015-01-07 10:55:58 -050051 mHasMobileData = hasMobileData;
52 Handler handler = new WifiHandler();
53 mWifiChannel = new AsyncChannel();
54 Messenger wifiMessenger = mWifiManager.getWifiServiceMessenger();
55 if (wifiMessenger != null) {
56 mWifiChannel.connect(context, handler, wifiMessenger);
57 }
58 // WiFi only has one state.
59 mCurrentState.iconGroup = mLastState.iconGroup = new IconGroup(
60 "Wi-Fi Icons",
61 WifiIcons.WIFI_SIGNAL_STRENGTH,
62 WifiIcons.QS_WIFI_SIGNAL_STRENGTH,
63 AccessibilityContentDescriptions.WIFI_CONNECTION_STRENGTH,
64 WifiIcons.WIFI_NO_NETWORK,
65 WifiIcons.QS_WIFI_NO_NETWORK,
66 WifiIcons.WIFI_NO_NETWORK,
67 WifiIcons.QS_WIFI_NO_NETWORK,
68 AccessibilityContentDescriptions.WIFI_NO_CONNECTION
69 );
70 }
71
72 @Override
73 protected WifiState cleanState() {
74 return new WifiState();
75 }
76
77 @Override
Jason Monke06b0652016-03-02 16:35:27 -050078 public void notifyListeners(SignalCallback callback) {
Jason Monkda68f592015-01-07 10:55:58 -050079 // only show wifi in the cluster if connected or if wifi-only
Minming Qie2d48ca2017-03-10 13:56:44 +080080 boolean visibleWhenEnabled = mContext.getResources().getBoolean(
81 R.bool.config_showWifiIndicatorWhenEnabled);
Jason Monkda68f592015-01-07 10:55:58 -050082 boolean wifiVisible = mCurrentState.enabled
Minming Qie2d48ca2017-03-10 13:56:44 +080083 && (mCurrentState.connected || !mHasMobileData || visibleWhenEnabled);
Jason Monkda68f592015-01-07 10:55:58 -050084 String wifiDesc = wifiVisible ? mCurrentState.ssid : null;
85 boolean ssidPresent = wifiVisible && mCurrentState.ssid != null;
86 String contentDescription = getStringIfExists(getContentDescription());
Julia Reynolds20aef8a2016-05-04 16:44:08 -040087 if (mCurrentState.inetCondition == 0) {
88 contentDescription +=
89 ("," + mContext.getString(R.string.accessibility_quick_settings_no_internet));
90 }
Jason Monkda68f592015-01-07 10:55:58 -050091
Jason Monk07b75fe2015-05-14 16:47:03 -040092 IconState statusIcon = new IconState(wifiVisible, getCurrentIconId(), contentDescription);
93 IconState qsIcon = new IconState(mCurrentState.connected, getQsCurrentIconId(),
94 contentDescription);
Jason Monke06b0652016-03-02 16:35:27 -050095 callback.setWifiIndicators(mCurrentState.enabled, statusIcon, qsIcon,
Jason Monk07b75fe2015-05-14 16:47:03 -040096 ssidPresent && mCurrentState.activityIn, ssidPresent && mCurrentState.activityOut,
97 wifiDesc);
Jason Monkda68f592015-01-07 10:55:58 -050098 }
99
100 /**
101 * Extract wifi state directly from broadcasts about changes in wifi state.
102 */
103 public void handleBroadcast(Intent intent) {
Jason Monk4cf95ae2015-11-16 15:59:53 -0500104 mWifiTracker.handleBroadcast(intent);
105 mCurrentState.enabled = mWifiTracker.enabled;
106 mCurrentState.connected = mWifiTracker.connected;
107 mCurrentState.ssid = mWifiTracker.ssid;
108 mCurrentState.rssi = mWifiTracker.rssi;
109 mCurrentState.level = mWifiTracker.level;
Jason Monkda68f592015-01-07 10:55:58 -0500110 notifyListenersIfNecessary();
111 }
112
Jason Monkda68f592015-01-07 10:55:58 -0500113 @VisibleForTesting
114 void setActivity(int wifiActivity) {
115 mCurrentState.activityIn = wifiActivity == WifiManager.DATA_ACTIVITY_INOUT
116 || wifiActivity == WifiManager.DATA_ACTIVITY_IN;
117 mCurrentState.activityOut = wifiActivity == WifiManager.DATA_ACTIVITY_INOUT
118 || wifiActivity == WifiManager.DATA_ACTIVITY_OUT;
119 notifyListenersIfNecessary();
120 }
121
122 /**
123 * Handler to receive the data activity on wifi.
124 */
125 private class WifiHandler extends Handler {
126 @Override
127 public void handleMessage(Message msg) {
128 switch (msg.what) {
129 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
130 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
131 mWifiChannel.sendMessage(Message.obtain(this,
132 AsyncChannel.CMD_CHANNEL_FULL_CONNECTION));
133 } else {
134 Log.e(mTag, "Failed to connect to wifi");
135 }
136 break;
137 case WifiManager.DATA_ACTIVITY_NOTIFICATION:
138 setActivity(msg.arg1);
139 break;
140 default:
141 // Ignore
142 break;
143 }
144 }
145 }
146
147 static class WifiState extends SignalController.State {
148 String ssid;
149
150 @Override
151 public void copyFrom(State s) {
152 super.copyFrom(s);
153 WifiState state = (WifiState) s;
154 ssid = state.ssid;
155 }
156
157 @Override
158 protected void toString(StringBuilder builder) {
159 super.toString(builder);
160 builder.append(',').append("ssid=").append(ssid);
161 }
162
163 @Override
164 public boolean equals(Object o) {
165 return super.equals(o)
166 && Objects.equals(((WifiState) o).ssid, ssid);
167 }
168 }
169}