OSDN Git Service

1c44204daf10e1285d06ec0d0f6c0fece4d70bc3
[android-x86/packages-apps-Settings.git] / src / com / android / settings / wifi / slice / WifiSlice.java
1 /*
2  * Copyright (C) 2018 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  */
16
17 package com.android.settings.wifi.slice;
18
19 import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
20 import static android.provider.SettingsSlicesContract.KEY_WIFI;
21
22 import static com.android.settings.slices.CustomSliceRegistry.WIFI_SLICE_URI;
23
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;
45
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;
51
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;
65
66 import java.util.ArrayList;
67 import java.util.List;
68
69 /**
70  * {@link CustomSliceable} for Wi-Fi, used by generic clients.
71  */
72 public class WifiSlice implements CustomSliceable {
73
74     @VisibleForTesting
75     static final int DEFAULT_EXPANDED_ROW_COUNT = 3;
76
77     protected final Context mContext;
78     protected final WifiManager mWifiManager;
79
80     public WifiSlice(Context context) {
81         mContext = context;
82         mWifiManager = mContext.getSystemService(WifiManager.class);
83     }
84
85     @Override
86     public Uri getUri() {
87         return WIFI_SLICE_URI;
88     }
89
90     @Override
91     public Slice getSlice() {
92         // Reload theme for switching dark mode on/off
93         mContext.getTheme().applyStyle(R.style.Theme_Settings_Home, true /* force */);
94
95         final boolean isWifiEnabled = isWifiEnabled();
96
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);
107
108         final ListBuilder listBuilder = new ListBuilder(mContext, getUri(), ListBuilder.INFINITY)
109                 .setAccentColor(COLOR_NOT_TINTED)
110                 .addRow(new ListBuilder.RowBuilder()
111                         .setTitle(title)
112                         .setSubtitle(summary)
113                         .addEndItem(toggleSliceAction)
114                         .setPrimaryAction(primarySliceAction));
115
116         if (!isWifiEnabled) {
117             return listBuilder.build();
118         }
119
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();
123
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
128         // AP list.
129         for (; index < apCount; index++) {
130             if (results.get(index).isReachable()) {
131                 needLoadingRow = false;
132                 break;
133             }
134         }
135
136         // Add AP rows
137         final CharSequence placeholder = mContext.getText(R.string.summary_placeholder);
138         for (int i = 0; i < DEFAULT_EXPANDED_ROW_COUNT; i++) {
139             if (i < apCount) {
140                 listBuilder.addRow(getAccessPointRow(results.get(i)));
141             } else if (needLoadingRow) {
142                 listBuilder.addRow(getLoadingRow());
143                 needLoadingRow = false;
144             } else {
145                 listBuilder.addRow(new ListBuilder.RowBuilder()
146                         .setTitle(placeholder));
147             }
148         }
149         return listBuilder.build();
150     }
151
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)
157                 .setSubtitle(title)
158                 .setPrimaryAction(SliceAction.create(
159                         getAccessPointAction(accessPoint), levelIcon, ListBuilder.ICON_IMAGE,
160                         title));
161
162         final IconCompat endIcon = getEndIcon(accessPoint);
163         if (endIcon != null) {
164             rowBuilder.addEndItem(endIcon, ListBuilder.ICON_IMAGE);
165         }
166         return rowBuilder;
167     }
168
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);
176         return span;
177     }
178
179     private IconCompat getAccessPointLevelIcon(AccessPoint accessPoint) {
180         final Drawable d = mContext.getDrawable(
181                 com.android.settingslib.Utils.getWifiIconResource(accessPoint.getLevel()));
182
183         @ColorInt int color;
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));
191             }
192         } else {
193             color = Utils.getColorAttrDefaultColor(mContext, android.R.attr.colorControlNormal);
194         }
195
196         d.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
197         return Utils.createIconWithDrawable(d);
198     }
199
200     private IconCompat getEndIcon(AccessPoint accessPoint) {
201         if (accessPoint.isActive()) {
202             return null;
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);
207         }
208         return null;
209     }
210
211     private PendingIntent getAccessPointAction(AccessPoint accessPoint) {
212         final Bundle extras = new Bundle();
213         accessPoint.saveWifiState(extras);
214
215         Intent intent;
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)
222                     .toIntent();
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);
226         } else {
227             intent = new Intent(mContext, WifiDialogActivity.class);
228             intent.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
229         }
230         return PendingIntent.getActivity(mContext, accessPoint.hashCode() /* requestCode */,
231                 intent, 0 /* flags */);
232     }
233
234     private ListBuilder.RowBuilder getLoadingRow() {
235         final CharSequence title = mContext.getText(R.string.wifi_empty_list_wifi_on);
236
237         // for aligning to the Wi-Fi AP's name
238         final IconCompat emptyIcon = Utils.createIconWithDrawable(
239                 new ColorDrawable(Color.TRANSPARENT));
240
241         return new ListBuilder.RowBuilder()
242                 .setTitleItem(emptyIcon, ListBuilder.ICON_IMAGE)
243                 .setSubtitle(title);
244     }
245
246     /**
247      * Update the current wifi status to the boolean value keyed by
248      * {@link android.app.slice.Slice#EXTRA_TOGGLE_STATE} on {@param intent}.
249      */
250     @Override
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}
257         // handle it.
258     }
259
260     @Override
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);
269
270         return intent;
271     }
272
273     protected String getActiveSSID() {
274         if (mWifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) {
275             return WifiSsid.NONE;
276         }
277         return WifiInfo.removeDoubleQuotes(mWifiManager.getConnectionInfo().getSSID());
278     }
279
280     private boolean isWifiEnabled() {
281         switch (mWifiManager.getWifiState()) {
282             case WifiManager.WIFI_STATE_ENABLED:
283             case WifiManager.WIFI_STATE_ENABLING:
284                 return true;
285             default:
286                 return false;
287         }
288     }
289
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);
296                 }
297                 return ssid;
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:
304             default:
305                 return null;
306         }
307     }
308
309     private PendingIntent getPrimaryAction() {
310         final Intent intent = getIntent();
311         return PendingIntent.getActivity(mContext, 0 /* requestCode */,
312                 intent, 0 /* flags */);
313     }
314
315     @Override
316     public Class getBackgroundWorkerClass() {
317         return WifiScanWorker.class;
318     }
319
320     public static class WifiScanWorker extends SliceBackgroundWorker<AccessPoint>
321             implements WifiTracker.WifiListener {
322
323         private final Context mContext;
324
325         private WifiTracker mWifiTracker;
326
327         public WifiScanWorker(Context context, Uri uri) {
328             super(context, uri);
329             mContext = context;
330         }
331
332         @Override
333         protected void onSlicePinned() {
334             if (mWifiTracker == null) {
335                 mWifiTracker = new WifiTracker(mContext, this /* wifiListener */,
336                         true /* includeSaved */, true /* includeScans */);
337             }
338             mWifiTracker.onStart();
339             onAccessPointsChanged();
340         }
341
342         @Override
343         protected void onSliceUnpinned() {
344             mWifiTracker.onStop();
345         }
346
347         @Override
348         public void close() {
349             mWifiTracker.onDestroy();
350         }
351
352         @Override
353         public void onWifiStateChanged(int state) {
354             notifySliceChange();
355         }
356
357         @Override
358         public void onConnectedChanged() {
359         }
360
361         @Override
362         public void onAccessPointsChanged() {
363             // in case state has changed
364             if (!mWifiTracker.getManager().isWifiEnabled()) {
365                 updateResults(null);
366                 return;
367             }
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) {
375                         break;
376                     }
377                 }
378             }
379             updateResults(resultList);
380         }
381
382         private AccessPoint clone(AccessPoint accessPoint) {
383             final Bundle savedState = new Bundle();
384             accessPoint.saveWifiState(savedState);
385             return new AccessPoint(mContext, savedState);
386         }
387
388         @Override
389         protected boolean areListsTheSame(List<AccessPoint> a, List<AccessPoint> b) {
390             if (!a.equals(b)) {
391                 return false;
392             }
393
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))) {
398                     return false;
399                 }
400             }
401             return true;
402         }
403
404         private State getState(AccessPoint accessPoint) {
405             final NetworkInfo networkInfo = accessPoint.getNetworkInfo();
406             if (networkInfo != null) {
407                 return networkInfo.getState();
408             }
409             return null;
410         }
411     }
412 }