1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.wifi;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settingslib.R;
import com.android.settingslib.Utils;
import com.android.wifitrackerlib.WifiEntry;
/**
* Preference to display a WifiEntry in a wifi picker.
*/
public class WifiEntryPreference extends Preference implements WifiEntry.WifiEntryCallback,
View.OnClickListener {
private static final int[] STATE_SECURED = {
R.attr.state_encrypted
};
private static final int[] FRICTION_ATTRS = {
R.attr.wifi_friction
};
// These values must be kept within [WifiEntry.WIFI_LEVEL_MIN, WifiEntry.WIFI_LEVEL_MAX]
private static final int[] WIFI_CONNECTION_STRENGTH = {
R.string.accessibility_no_wifi,
R.string.accessibility_wifi_one_bar,
R.string.accessibility_wifi_two_bars,
R.string.accessibility_wifi_three_bars,
R.string.accessibility_wifi_signal_full
};
// StateListDrawable to display secured lock / metered "$" icon
@Nullable private final StateListDrawable mFrictionSld;
private final IconInjector mIconInjector;
private WifiEntry mWifiEntry;
private int mLevel = -1;
private int mWifiStandard;
private boolean mVhtMax8SpatialStreamsSupport;
private boolean mHe8ssCapableAp;
private boolean mShowX; // Shows the Wi-Fi signl icon of Pie+x when it's true.
private CharSequence mContentDescription;
private OnButtonClickListener mOnButtonClickListener;
public WifiEntryPreference(@NonNull Context context, @NonNull WifiEntry wifiEntry) {
this(context, wifiEntry, new IconInjector(context));
}
@VisibleForTesting
WifiEntryPreference(@NonNull Context context, @NonNull WifiEntry wifiEntry,
@NonNull IconInjector iconInjector) {
super(context);
setLayoutResource(R.layout.preference_access_point);
setWidgetLayoutResource(R.layout.access_point_friction_widget);
mFrictionSld = getFrictionStateListDrawable();
mWifiEntry = wifiEntry;
mWifiEntry.setListener(this);
mIconInjector = iconInjector;
refresh();
}
public WifiEntry getWifiEntry() {
return mWifiEntry;
}
@Override
public void onBindViewHolder(final PreferenceViewHolder view) {
super.onBindViewHolder(view);
final Drawable drawable = getIcon();
if (drawable != null) {
drawable.setLevel(mLevel);
}
view.itemView.setContentDescription(mContentDescription);
// Turn off divider
view.findViewById(R.id.two_target_divider).setVisibility(View.INVISIBLE);
// Enable the icon button when the help string in this WifiEntry is not null.
final ImageButton imageButton = (ImageButton) view.findViewById(R.id.icon_button);
final ImageView frictionImageView = (ImageView) view.findViewById(
R.id.friction_icon);
if (mWifiEntry.getHelpUriString() != null
&& mWifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_DISCONNECTED) {
final Drawable drawablehelp = getDrawable(R.drawable.ic_help);
drawablehelp.setTintList(
Utils.getColorAttr(getContext(), android.R.attr.colorControlNormal));
((ImageView) imageButton).setImageDrawable(drawablehelp);
imageButton.setVisibility(View.VISIBLE);
imageButton.setOnClickListener(this);
imageButton.setContentDescription(
getContext().getText(R.string.help_label));
if (frictionImageView != null) {
frictionImageView.setVisibility(View.GONE);
}
} else {
imageButton.setVisibility(View.GONE);
if (frictionImageView != null) {
frictionImageView.setVisibility(View.VISIBLE);
bindFrictionImage(frictionImageView);
}
}
}
/**
* Updates the title and summary; may indirectly call notifyChanged().
*/
public void refresh() {
setTitle(mWifiEntry.getTitle());
final int level = mWifiEntry.getLevel();
final int standard = mWifiEntry.getWifiStandard();
final boolean vhtMax8SpatialStreamsSupport = mWifiEntry.isVhtMax8SpatialStreamsSupported();
final boolean he8ssCapableAp = mWifiEntry.isHe8ssCapableAp();
final boolean showX = mWifiEntry.shouldShowXLevelIcon();
if (level != mLevel || showX != mShowX || standard != mWifiStandard ||
he8ssCapableAp != mHe8ssCapableAp ||
vhtMax8SpatialStreamsSupport != mVhtMax8SpatialStreamsSupport) {
mLevel = level;
mWifiStandard = standard;
mHe8ssCapableAp = he8ssCapableAp;
mVhtMax8SpatialStreamsSupport = vhtMax8SpatialStreamsSupport;
mShowX = showX;
updateIcon(mShowX, mLevel, mWifiStandard, mHe8ssCapableAp && mVhtMax8SpatialStreamsSupport);
notifyChanged();
}
String summary = mWifiEntry.getSummary(false /* concise */);
if (mWifiEntry.isPskSaeTransitionMode()) {
summary = "WPA3(SAE Transition Mode) " + summary;
} else if (mWifiEntry.isOweTransitionMode()) {
summary = "WPA3(OWE Transition Mode) " + summary;
} else if (mWifiEntry.getSecurity() == WifiEntry.SECURITY_SAE) {
summary = "WPA3(SAE) " + summary;
} else if (mWifiEntry.getSecurity() == WifiEntry.SECURITY_OWE) {
summary = "WPA3(OWE) " + summary;
}
setSummary(summary);
mContentDescription = buildContentDescription();
}
/**
* Indicates the state of the WifiEntry has changed and clients may retrieve updates through
* the WifiEntry getter methods.
*/
public void onUpdated() {
// TODO(b/70983952): Fill this method in
refresh();
}
/**
* Result of the connect request indicated by the WifiEntry.CONNECT_STATUS constants.
*/
public void onConnectResult(int status) {
// TODO(b/70983952): Fill this method in
}
/**
* Result of the disconnect request indicated by the WifiEntry.DISCONNECT_STATUS constants.
*/
public void onDisconnectResult(int status) {
// TODO(b/70983952): Fill this method in
}
/**
* Result of the forget request indicated by the WifiEntry.FORGET_STATUS constants.
*/
public void onForgetResult(int status) {
// TODO(b/70983952): Fill this method in
}
/**
* Result of the sign-in request indecated by the WifiEntry.SIGNIN_STATUS constants
*/
public void onSignInResult(int status) {
// TODO(b/70983952): Fill this method in
}
protected int getIconColorAttr() {
final boolean accent = (mWifiEntry.hasInternetAccess()
&& mWifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTED);
return accent ? android.R.attr.colorAccent : android.R.attr.colorControlNormal;
}
private void updateIcon(boolean showX, int level, int standard, boolean isReady) {
if (level == -1) {
setIcon(null);
return;
}
final Drawable drawable = mIconInjector.getIcon(showX, level, standard, isReady);
if (drawable != null) {
drawable.setTint(Utils.getColorAttrDefaultColor(getContext(), getIconColorAttr()));
setIcon(drawable);
} else {
setIcon(null);
}
}
@Nullable
private StateListDrawable getFrictionStateListDrawable() {
TypedArray frictionSld;
try {
frictionSld = getContext().getTheme().obtainStyledAttributes(FRICTION_ATTRS);
} catch (Resources.NotFoundException e) {
// Fallback for platforms that do not need friction icon resources.
frictionSld = null;
}
return frictionSld != null ? (StateListDrawable) frictionSld.getDrawable(0) : null;
}
/**
* Binds the friction icon drawable using a StateListDrawable.
*
* <p>Friction icons will be rebound when notifyChange() is called, and therefore
* do not need to be managed in refresh()</p>.
*/
private void bindFrictionImage(ImageView frictionImageView) {
if (frictionImageView == null || mFrictionSld == null) {
return;
}
if ((mWifiEntry.getSecurity() != WifiEntry.SECURITY_NONE)
&& (mWifiEntry.getSecurity() != WifiEntry.SECURITY_OWE)) {
mFrictionSld.setState(STATE_SECURED);
}
frictionImageView.setImageDrawable(mFrictionSld.getCurrent());
}
/**
* Helper method to generate content description string.
*/
@VisibleForTesting
CharSequence buildContentDescription() {
final Context context = getContext();
CharSequence contentDescription = getTitle();
final CharSequence summary = getSummary();
if (!TextUtils.isEmpty(summary)) {
contentDescription = TextUtils.concat(contentDescription, ",", summary);
}
int level = mWifiEntry.getLevel();
if (level >= 0 && level < WIFI_CONNECTION_STRENGTH.length) {
contentDescription = TextUtils.concat(contentDescription, ",",
context.getString(WIFI_CONNECTION_STRENGTH[level]));
}
return TextUtils.concat(contentDescription, ",",
mWifiEntry.getSecurity() == WifiEntry.SECURITY_NONE
? context.getString(R.string.accessibility_wifi_security_type_none)
: context.getString(R.string.accessibility_wifi_security_type_secured));
}
static class IconInjector {
private final Context mContext;
IconInjector(Context context) {
mContext = context;
}
public Drawable getIcon(boolean showX, int level, int standard, boolean isReady) {
return mContext.getDrawable(WifiUtils.getInternetIconResource(level, showX, standard, isReady));
}
}
/**
* Set listeners, who want to listen the button client event.
*/
public void setOnButtonClickListener(OnButtonClickListener listener) {
mOnButtonClickListener = listener;
notifyChanged();
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.icon_button) {
if (mOnButtonClickListener != null) {
mOnButtonClickListener.onButtonClick(this);
}
}
}
/**
* Callback to inform the caller that the icon button is clicked.
*/
public interface OnButtonClickListener {
/**
* Register to listen the button click event.
*/
void onButtonClick(WifiEntryPreference preference);
}
private Drawable getDrawable(@DrawableRes int iconResId) {
Drawable buttonIcon = null;
try {
buttonIcon = getContext().getDrawable(iconResId);
} catch (Resources.NotFoundException exception) {
// Do nothing
}
return buttonIcon;
}
}
|