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.Uri;
36 import android.net.wifi.WifiInfo;
37 import android.net.wifi.WifiManager;
38 import android.net.wifi.WifiSsid;
39 import android.os.Bundle;
40 import android.text.Spannable;
41 import android.text.SpannableString;
42 import android.text.TextUtils;
43 import android.text.style.ForegroundColorSpan;
45 import androidx.annotation.VisibleForTesting;
46 import androidx.core.graphics.drawable.IconCompat;
47 import androidx.slice.Slice;
48 import androidx.slice.builders.ListBuilder;
49 import androidx.slice.builders.SliceAction;
51 import com.android.settings.R;
52 import com.android.settings.SubSettings;
53 import com.android.settings.Utils;
54 import com.android.settings.core.SubSettingLauncher;
55 import com.android.settings.slices.CustomSliceable;
56 import com.android.settings.slices.SliceBackgroundWorker;
57 import com.android.settings.slices.SliceBuilderUtils;
58 import com.android.settings.wifi.WifiDialogActivity;
59 import com.android.settings.wifi.WifiSettings;
60 import com.android.settings.wifi.WifiUtils;
61 import com.android.settings.wifi.details.WifiNetworkDetailsFragment;
62 import com.android.settingslib.wifi.AccessPoint;
63 import com.android.settingslib.wifi.WifiTracker;
65 import java.util.ArrayList;
66 import java.util.List;
69 * {@link CustomSliceable} for Wi-Fi, used by generic clients.
71 public class WifiSlice implements CustomSliceable {
74 static final int DEFAULT_EXPANDED_ROW_COUNT = 3;
76 protected final Context mContext;
77 protected final WifiManager mWifiManager;
79 public WifiSlice(Context context) {
81 mWifiManager = mContext.getSystemService(WifiManager.class);
86 return WIFI_SLICE_URI;
90 public Slice getSlice() {
91 // Reload theme for switching dark mode on/off
92 mContext.getTheme().applyStyle(R.style.Theme_Settings_Home, true /* force */);
94 final boolean isWifiEnabled = isWifiEnabled();
96 final IconCompat icon = IconCompat.createWithResource(mContext,
97 R.drawable.ic_settings_wireless);
98 final String title = mContext.getString(R.string.wifi_settings);
99 final CharSequence summary = getSummary();
100 final PendingIntent toggleAction = getBroadcastIntent(mContext);
101 final PendingIntent primaryAction = getPrimaryAction();
102 final SliceAction primarySliceAction = SliceAction.createDeeplink(primaryAction, icon,
103 ListBuilder.ICON_IMAGE, title);
104 final SliceAction toggleSliceAction = SliceAction.createToggle(toggleAction,
105 null /* actionTitle */, isWifiEnabled);
107 final ListBuilder listBuilder = new ListBuilder(mContext, getUri(), ListBuilder.INFINITY)
108 .setAccentColor(COLOR_NOT_TINTED)
109 .addRow(new ListBuilder.RowBuilder()
111 .setSubtitle(summary)
112 .addEndItem(toggleSliceAction)
113 .setPrimaryAction(primarySliceAction));
115 if (!isWifiEnabled) {
116 return listBuilder.build();
119 final SliceBackgroundWorker worker = SliceBackgroundWorker.getInstance(getUri());
120 final List<AccessPoint> results = worker != null ? worker.getResults() : null;
121 final int apCount = results == null ? 0 : results.size();
123 // Need a loading text when results are not ready or out of date.
124 boolean needLoadingRow = true;
125 int index = apCount > 0 && results.get(0).isActive() ? 1 : 0;
126 // This loop checks the existence of reachable APs to determine the validity of the current
128 for (; index < apCount; index++) {
129 if (results.get(index).isReachable()) {
130 needLoadingRow = false;
136 final CharSequence placeholder = mContext.getText(R.string.summary_placeholder);
137 for (int i = 0; i < DEFAULT_EXPANDED_ROW_COUNT; i++) {
139 listBuilder.addRow(getAccessPointRow(results.get(i)));
140 } else if (needLoadingRow) {
141 listBuilder.addRow(getLoadingRow());
142 needLoadingRow = false;
144 listBuilder.addRow(new ListBuilder.RowBuilder()
145 .setTitle(placeholder));
148 return listBuilder.build();
151 private ListBuilder.RowBuilder getAccessPointRow(AccessPoint accessPoint) {
152 final CharSequence title = getAccessPointName(accessPoint);
153 final IconCompat levelIcon = getAccessPointLevelIcon(accessPoint);
154 final ListBuilder.RowBuilder rowBuilder = new ListBuilder.RowBuilder()
155 .setTitleItem(levelIcon, ListBuilder.ICON_IMAGE)
157 .setPrimaryAction(SliceAction.create(
158 getAccessPointAction(accessPoint), levelIcon, ListBuilder.ICON_IMAGE,
161 final IconCompat endIcon = getEndIcon(accessPoint);
162 if (endIcon != null) {
163 rowBuilder.addEndItem(endIcon, ListBuilder.ICON_IMAGE);
168 private CharSequence getAccessPointName(AccessPoint accessPoint) {
169 final CharSequence name = accessPoint.getConfigName();
170 final Spannable span = new SpannableString(name);
171 @ColorInt final int color = Utils.getColorAttrDefaultColor(mContext,
172 android.R.attr.textColorPrimary);
173 span.setSpan(new ForegroundColorSpan(color), 0, name.length(),
174 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
178 private IconCompat getAccessPointLevelIcon(AccessPoint accessPoint) {
179 final Drawable d = mContext.getDrawable(
180 com.android.settingslib.Utils.getWifiIconResource(accessPoint.getLevel()));
183 if (accessPoint.isActive()) {
184 final NetworkInfo.State state = accessPoint.getNetworkInfo().getState();
185 if (state == NetworkInfo.State.CONNECTED) {
186 color = Utils.getColorAccentDefaultColor(mContext);
187 } else { // connecting
188 color = Utils.getDisabled(mContext, Utils.getColorAttrDefaultColor(mContext,
189 android.R.attr.colorControlNormal));
192 color = Utils.getColorAttrDefaultColor(mContext, android.R.attr.colorControlNormal);
195 d.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
196 return Utils.createIconWithDrawable(d);
199 private IconCompat getEndIcon(AccessPoint accessPoint) {
200 if (accessPoint.isActive()) {
201 return IconCompat.createWithResource(mContext, R.drawable.ic_settings_accent);
202 } else if (accessPoint.getSecurity() != AccessPoint.SECURITY_NONE) {
203 return IconCompat.createWithResource(mContext, R.drawable.ic_friction_lock_closed);
204 } else if (accessPoint.isMetered()) {
205 return IconCompat.createWithResource(mContext, R.drawable.ic_friction_money);
210 private PendingIntent getAccessPointAction(AccessPoint accessPoint) {
211 final Bundle extras = new Bundle();
212 accessPoint.saveWifiState(extras);
215 if (accessPoint.isActive()) {
216 intent = new SubSettingLauncher(mContext)
217 .setTitleRes(R.string.pref_title_network_details)
218 .setDestination(WifiNetworkDetailsFragment.class.getName())
219 .setArguments(extras)
220 .setSourceMetricsCategory(SettingsEnums.WIFI)
222 } else if (WifiUtils.getConnectingType(accessPoint) != WifiUtils.CONNECT_TYPE_OTHERS) {
223 intent = new Intent(mContext, ConnectToWifiHandler.class);
224 intent.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
226 intent = new Intent(mContext, WifiDialogActivity.class);
227 intent.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
229 return PendingIntent.getActivity(mContext, accessPoint.hashCode() /* requestCode */,
230 intent, 0 /* flags */);
233 private ListBuilder.RowBuilder getLoadingRow() {
234 final CharSequence title = mContext.getText(R.string.wifi_empty_list_wifi_on);
236 // for aligning to the Wi-Fi AP's name
237 final IconCompat emptyIcon = Utils.createIconWithDrawable(
238 new ColorDrawable(Color.TRANSPARENT));
240 return new ListBuilder.RowBuilder()
241 .setTitleItem(emptyIcon, ListBuilder.ICON_IMAGE)
246 * Update the current wifi status to the boolean value keyed by
247 * {@link android.app.slice.Slice#EXTRA_TOGGLE_STATE} on {@param intent}.
250 public void onNotifyChange(Intent intent) {
251 final boolean newState = intent.getBooleanExtra(EXTRA_TOGGLE_STATE,
252 mWifiManager.isWifiEnabled());
253 mWifiManager.setWifiEnabled(newState);
254 // Do not notifyChange on Uri. The service takes longer to update the current value than it
255 // does for the Slice to check the current value again. Let {@link WifiScanWorker}
260 public Intent getIntent() {
261 final String screenTitle = mContext.getText(R.string.wifi_settings).toString();
262 final Uri contentUri = new Uri.Builder().appendPath(KEY_WIFI).build();
263 final Intent intent = SliceBuilderUtils.buildSearchResultPageIntent(mContext,
264 WifiSettings.class.getName(), KEY_WIFI, screenTitle,
265 SettingsEnums.DIALOG_WIFI_AP_EDIT)
266 .setClassName(mContext.getPackageName(), SubSettings.class.getName())
267 .setData(contentUri);
272 protected String getActiveSSID() {
273 if (mWifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) {
274 return WifiSsid.NONE;
276 return WifiInfo.removeDoubleQuotes(mWifiManager.getConnectionInfo().getSSID());
279 private boolean isWifiEnabled() {
280 switch (mWifiManager.getWifiState()) {
281 case WifiManager.WIFI_STATE_ENABLED:
282 case WifiManager.WIFI_STATE_ENABLING:
289 private CharSequence getSummary() {
290 switch (mWifiManager.getWifiState()) {
291 case WifiManager.WIFI_STATE_ENABLED:
292 final String ssid = getActiveSSID();
293 if (TextUtils.equals(ssid, WifiSsid.NONE)) {
294 return mContext.getText(R.string.disconnected);
297 case WifiManager.WIFI_STATE_ENABLING:
298 return mContext.getText(R.string.disconnected);
299 case WifiManager.WIFI_STATE_DISABLED:
300 case WifiManager.WIFI_STATE_DISABLING:
301 return mContext.getText(R.string.switch_off_text);
302 case WifiManager.WIFI_STATE_UNKNOWN:
308 private PendingIntent getPrimaryAction() {
309 final Intent intent = getIntent();
310 return PendingIntent.getActivity(mContext, 0 /* requestCode */,
311 intent, 0 /* flags */);
315 public Class getBackgroundWorkerClass() {
316 return WifiScanWorker.class;
319 public static class WifiScanWorker extends SliceBackgroundWorker<AccessPoint>
320 implements WifiTracker.WifiListener {
322 private final Context mContext;
324 private WifiTracker mWifiTracker;
326 public WifiScanWorker(Context context, Uri uri) {
332 protected void onSlicePinned() {
333 if (mWifiTracker == null) {
334 mWifiTracker = new WifiTracker(mContext, this /* wifiListener */,
335 true /* includeSaved */, true /* includeScans */);
337 mWifiTracker.onStart();
338 onAccessPointsChanged();
342 protected void onSliceUnpinned() {
343 mWifiTracker.onStop();
347 public void close() {
348 mWifiTracker.onDestroy();
352 public void onWifiStateChanged(int state) {
357 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()) {
376 updateResults(resultList);