2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.wifi.slice;
19 import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
20 import static android.provider.SettingsSlicesContract.KEY_WIFI;
22 import static com.android.settings.slices.CustomSliceRegistry.WIFI_SLICE_URI;
24 import android.annotation.ColorInt;
25 import android.app.PendingIntent;
26 import android.app.settings.SettingsEnums;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.graphics.Color;
30 import android.graphics.PorterDuff;
31 import android.graphics.PorterDuffColorFilter;
32 import android.graphics.drawable.ColorDrawable;
33 import android.graphics.drawable.Drawable;
34 import android.net.NetworkInfo;
35 import android.net.NetworkInfo.State;
36 import android.net.Uri;
37 import android.net.wifi.WifiInfo;
38 import android.net.wifi.WifiManager;
39 import android.net.wifi.WifiSsid;
40 import android.os.Bundle;
41 import android.text.Spannable;
42 import android.text.SpannableString;
43 import android.text.TextUtils;
44 import android.text.style.ForegroundColorSpan;
46 import androidx.annotation.VisibleForTesting;
47 import androidx.core.graphics.drawable.IconCompat;
48 import androidx.slice.Slice;
49 import androidx.slice.builders.ListBuilder;
50 import androidx.slice.builders.SliceAction;
52 import com.android.settings.R;
53 import com.android.settings.SubSettings;
54 import com.android.settings.Utils;
55 import com.android.settings.core.SubSettingLauncher;
56 import com.android.settings.slices.CustomSliceable;
57 import com.android.settings.slices.SliceBackgroundWorker;
58 import com.android.settings.slices.SliceBuilderUtils;
59 import com.android.settings.wifi.WifiDialogActivity;
60 import com.android.settings.wifi.WifiSettings;
61 import com.android.settings.wifi.WifiUtils;
62 import com.android.settings.wifi.details.WifiNetworkDetailsFragment;
63 import com.android.settingslib.wifi.AccessPoint;
64 import com.android.settingslib.wifi.WifiTracker;
66 import java.util.ArrayList;
67 import java.util.List;
70 * {@link CustomSliceable} for Wi-Fi, used by generic clients.
72 public class WifiSlice implements CustomSliceable {
75 static final int DEFAULT_EXPANDED_ROW_COUNT = 3;
77 protected final Context mContext;
78 protected final WifiManager mWifiManager;
80 public WifiSlice(Context context) {
82 mWifiManager = mContext.getSystemService(WifiManager.class);
87 return WIFI_SLICE_URI;
91 public Slice getSlice() {
92 // Reload theme for switching dark mode on/off
93 mContext.getTheme().applyStyle(R.style.Theme_Settings_Home, true /* force */);
95 final boolean isWifiEnabled = isWifiEnabled();
97 final IconCompat icon = IconCompat.createWithResource(mContext,
98 R.drawable.ic_settings_wireless);
99 final String title = mContext.getString(R.string.wifi_settings);
100 final CharSequence summary = getSummary();
101 final PendingIntent toggleAction = getBroadcastIntent(mContext);
102 final PendingIntent primaryAction = getPrimaryAction();
103 final SliceAction primarySliceAction = SliceAction.createDeeplink(primaryAction, icon,
104 ListBuilder.ICON_IMAGE, title);
105 final SliceAction toggleSliceAction = SliceAction.createToggle(toggleAction,
106 null /* actionTitle */, isWifiEnabled);
108 final ListBuilder listBuilder = new ListBuilder(mContext, getUri(), ListBuilder.INFINITY)
109 .setAccentColor(COLOR_NOT_TINTED)
110 .addRow(new ListBuilder.RowBuilder()
112 .setSubtitle(summary)
113 .addEndItem(toggleSliceAction)
114 .setPrimaryAction(primarySliceAction));
116 if (!isWifiEnabled) {
117 return listBuilder.build();
120 final SliceBackgroundWorker worker = SliceBackgroundWorker.getInstance(getUri());
121 final List<AccessPoint> results = worker != null ? worker.getResults() : null;
122 final int apCount = results == null ? 0 : results.size();
124 // Need a loading text when results are not ready or out of date.
125 boolean needLoadingRow = true;
126 int index = apCount > 0 && results.get(0).isActive() ? 1 : 0;
127 // This loop checks the existence of reachable APs to determine the validity of the current
129 for (; index < apCount; index++) {
130 if (results.get(index).isReachable()) {
131 needLoadingRow = false;
137 final CharSequence placeholder = mContext.getText(R.string.summary_placeholder);
138 for (int i = 0; i < DEFAULT_EXPANDED_ROW_COUNT; i++) {
140 listBuilder.addRow(getAccessPointRow(results.get(i)));
141 } else if (needLoadingRow) {
142 listBuilder.addRow(getLoadingRow());
143 needLoadingRow = false;
145 listBuilder.addRow(new ListBuilder.RowBuilder()
146 .setTitle(placeholder));
149 return listBuilder.build();
152 private ListBuilder.RowBuilder getAccessPointRow(AccessPoint accessPoint) {
153 final CharSequence title = getAccessPointName(accessPoint);
154 final IconCompat levelIcon = getAccessPointLevelIcon(accessPoint);
155 final ListBuilder.RowBuilder rowBuilder = new ListBuilder.RowBuilder()
156 .setTitleItem(levelIcon, ListBuilder.ICON_IMAGE)
158 .setPrimaryAction(SliceAction.create(
159 getAccessPointAction(accessPoint), levelIcon, ListBuilder.ICON_IMAGE,
162 final IconCompat endIcon = getEndIcon(accessPoint);
163 if (endIcon != null) {
164 rowBuilder.addEndItem(endIcon, ListBuilder.ICON_IMAGE);
169 private CharSequence getAccessPointName(AccessPoint accessPoint) {
170 final CharSequence name = accessPoint.getConfigName();
171 final Spannable span = new SpannableString(name);
172 @ColorInt final int color = Utils.getColorAttrDefaultColor(mContext,
173 android.R.attr.textColorPrimary);
174 span.setSpan(new ForegroundColorSpan(color), 0, name.length(),
175 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
179 private IconCompat getAccessPointLevelIcon(AccessPoint accessPoint) {
180 final Drawable d = mContext.getDrawable(
181 com.android.settingslib.Utils.getWifiIconResource(accessPoint.getLevel()));
184 if (accessPoint.isActive()) {
185 final NetworkInfo.State state = accessPoint.getNetworkInfo().getState();
186 if (state == NetworkInfo.State.CONNECTED) {
187 color = Utils.getColorAccentDefaultColor(mContext);
188 } else { // connecting
189 color = Utils.getDisabled(mContext, Utils.getColorAttrDefaultColor(mContext,
190 android.R.attr.colorControlNormal));
193 color = Utils.getColorAttrDefaultColor(mContext, android.R.attr.colorControlNormal);
196 d.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
197 return Utils.createIconWithDrawable(d);
200 private IconCompat getEndIcon(AccessPoint accessPoint) {
201 if (accessPoint.isActive()) {
203 } else if (accessPoint.getSecurity() != AccessPoint.SECURITY_NONE) {
204 return IconCompat.createWithResource(mContext, R.drawable.ic_friction_lock_closed);
205 } else if (accessPoint.isMetered()) {
206 return IconCompat.createWithResource(mContext, R.drawable.ic_friction_money);
211 private PendingIntent getAccessPointAction(AccessPoint accessPoint) {
212 final Bundle extras = new Bundle();
213 accessPoint.saveWifiState(extras);
216 if (accessPoint.isActive()) {
217 intent = new SubSettingLauncher(mContext)
218 .setTitleRes(R.string.pref_title_network_details)
219 .setDestination(WifiNetworkDetailsFragment.class.getName())
220 .setArguments(extras)
221 .setSourceMetricsCategory(SettingsEnums.WIFI)
223 } else if (WifiUtils.getConnectingType(accessPoint) != WifiUtils.CONNECT_TYPE_OTHERS) {
224 intent = new Intent(mContext, ConnectToWifiHandler.class);
225 intent.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
227 intent = new Intent(mContext, WifiDialogActivity.class);
228 intent.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
230 return PendingIntent.getActivity(mContext, accessPoint.hashCode() /* requestCode */,
231 intent, 0 /* flags */);
234 private ListBuilder.RowBuilder getLoadingRow() {
235 final CharSequence title = mContext.getText(R.string.wifi_empty_list_wifi_on);
237 // for aligning to the Wi-Fi AP's name
238 final IconCompat emptyIcon = Utils.createIconWithDrawable(
239 new ColorDrawable(Color.TRANSPARENT));
241 return new ListBuilder.RowBuilder()
242 .setTitleItem(emptyIcon, ListBuilder.ICON_IMAGE)
247 * Update the current wifi status to the boolean value keyed by
248 * {@link android.app.slice.Slice#EXTRA_TOGGLE_STATE} on {@param intent}.
251 public void onNotifyChange(Intent intent) {
252 final boolean newState = intent.getBooleanExtra(EXTRA_TOGGLE_STATE,
253 mWifiManager.isWifiEnabled());
254 mWifiManager.setWifiEnabled(newState);
255 // Do not notifyChange on Uri. The service takes longer to update the current value than it
256 // does for the Slice to check the current value again. Let {@link WifiScanWorker}
261 public Intent getIntent() {
262 final String screenTitle = mContext.getText(R.string.wifi_settings).toString();
263 final Uri contentUri = new Uri.Builder().appendPath(KEY_WIFI).build();
264 final Intent intent = SliceBuilderUtils.buildSearchResultPageIntent(mContext,
265 WifiSettings.class.getName(), KEY_WIFI, screenTitle,
266 SettingsEnums.DIALOG_WIFI_AP_EDIT)
267 .setClassName(mContext.getPackageName(), SubSettings.class.getName())
268 .setData(contentUri);
273 protected String getActiveSSID() {
274 if (mWifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) {
275 return WifiSsid.NONE;
277 return WifiInfo.removeDoubleQuotes(mWifiManager.getConnectionInfo().getSSID());
280 private boolean isWifiEnabled() {
281 switch (mWifiManager.getWifiState()) {
282 case WifiManager.WIFI_STATE_ENABLED:
283 case WifiManager.WIFI_STATE_ENABLING:
290 private CharSequence getSummary() {
291 switch (mWifiManager.getWifiState()) {
292 case WifiManager.WIFI_STATE_ENABLED:
293 final String ssid = getActiveSSID();
294 if (TextUtils.equals(ssid, WifiSsid.NONE)) {
295 return mContext.getText(R.string.disconnected);
298 case WifiManager.WIFI_STATE_ENABLING:
299 return mContext.getText(R.string.disconnected);
300 case WifiManager.WIFI_STATE_DISABLED:
301 case WifiManager.WIFI_STATE_DISABLING:
302 return mContext.getText(R.string.switch_off_text);
303 case WifiManager.WIFI_STATE_UNKNOWN:
309 private PendingIntent getPrimaryAction() {
310 final Intent intent = getIntent();
311 return PendingIntent.getActivity(mContext, 0 /* requestCode */,
312 intent, 0 /* flags */);
316 public Class getBackgroundWorkerClass() {
317 return WifiScanWorker.class;
320 public static class WifiScanWorker extends SliceBackgroundWorker<AccessPoint>
321 implements WifiTracker.WifiListener {
323 private final Context mContext;
325 private WifiTracker mWifiTracker;
327 public WifiScanWorker(Context context, Uri uri) {
333 protected void onSlicePinned() {
334 if (mWifiTracker == null) {
335 mWifiTracker = new WifiTracker(mContext, this /* wifiListener */,
336 true /* includeSaved */, true /* includeScans */);
338 mWifiTracker.onStart();
339 onAccessPointsChanged();
343 protected void onSliceUnpinned() {
344 mWifiTracker.onStop();
348 public void close() {
349 mWifiTracker.onDestroy();
353 public void onWifiStateChanged(int state) {
358 public void onConnectedChanged() {
362 public void onAccessPointsChanged() {
363 // in case state has changed
364 if (!mWifiTracker.getManager().isWifiEnabled()) {
368 // AccessPoints are sorted by the WifiTracker
369 final List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints();
370 final List<AccessPoint> resultList = new ArrayList<>();
371 for (AccessPoint ap : accessPoints) {
372 if (ap.isReachable()) {
373 resultList.add(clone(ap));
374 if (resultList.size() >= DEFAULT_EXPANDED_ROW_COUNT) {
379 updateResults(resultList);
382 private AccessPoint clone(AccessPoint accessPoint) {
383 final Bundle savedState = new Bundle();
384 accessPoint.saveWifiState(savedState);
385 return new AccessPoint(mContext, savedState);
389 protected boolean areListsTheSame(List<AccessPoint> a, List<AccessPoint> b) {
394 // compare access point states one by one
395 final int listSize = a.size();
396 for (int i = 0; i < listSize; i++) {
397 if (getState(a.get(i)) != getState(b.get(i))) {
404 private State getState(AccessPoint accessPoint) {
405 final NetworkInfo networkInfo = accessPoint.getNetworkInfo();
406 if (networkInfo != null) {
407 return networkInfo.getState();