OSDN Git Service

Merge "[MAC Randomization] Extend char limitation for translation"
[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.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;
44
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;
50
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;
64
65 import java.util.ArrayList;
66 import java.util.List;
67
68 /**
69  * {@link CustomSliceable} for Wi-Fi, used by generic clients.
70  */
71 public class WifiSlice implements CustomSliceable {
72
73     @VisibleForTesting
74     static final int DEFAULT_EXPANDED_ROW_COUNT = 3;
75
76     protected final Context mContext;
77     protected final WifiManager mWifiManager;
78
79     public WifiSlice(Context context) {
80         mContext = context;
81         mWifiManager = mContext.getSystemService(WifiManager.class);
82     }
83
84     @Override
85     public Uri getUri() {
86         return WIFI_SLICE_URI;
87     }
88
89     @Override
90     public Slice getSlice() {
91         // Reload theme for switching dark mode on/off
92         mContext.getTheme().applyStyle(R.style.Theme_Settings_Home, true /* force */);
93
94         final boolean isWifiEnabled = isWifiEnabled();
95
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);
106
107         final ListBuilder listBuilder = new ListBuilder(mContext, getUri(), ListBuilder.INFINITY)
108                 .setAccentColor(COLOR_NOT_TINTED)
109                 .addRow(new ListBuilder.RowBuilder()
110                         .setTitle(title)
111                         .setSubtitle(summary)
112                         .addEndItem(toggleSliceAction)
113                         .setPrimaryAction(primarySliceAction));
114
115         if (!isWifiEnabled) {
116             return listBuilder.build();
117         }
118
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();
122
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
127         // AP list.
128         for (; index < apCount; index++) {
129             if (results.get(index).isReachable()) {
130                 needLoadingRow = false;
131                 break;
132             }
133         }
134
135         // Add AP rows
136         final CharSequence placeholder = mContext.getText(R.string.summary_placeholder);
137         for (int i = 0; i < DEFAULT_EXPANDED_ROW_COUNT; i++) {
138             if (i < apCount) {
139                 listBuilder.addRow(getAccessPointRow(results.get(i)));
140             } else if (needLoadingRow) {
141                 listBuilder.addRow(getLoadingRow());
142                 needLoadingRow = false;
143             } else {
144                 listBuilder.addRow(new ListBuilder.RowBuilder()
145                         .setTitle(placeholder));
146             }
147         }
148         return listBuilder.build();
149     }
150
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)
156                 .setSubtitle(title)
157                 .setPrimaryAction(SliceAction.create(
158                         getAccessPointAction(accessPoint), levelIcon, ListBuilder.ICON_IMAGE,
159                         title));
160
161         final IconCompat endIcon = getEndIcon(accessPoint);
162         if (endIcon != null) {
163             rowBuilder.addEndItem(endIcon, ListBuilder.ICON_IMAGE);
164         }
165         return rowBuilder;
166     }
167
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);
175         return span;
176     }
177
178     private IconCompat getAccessPointLevelIcon(AccessPoint accessPoint) {
179         final Drawable d = mContext.getDrawable(
180                 com.android.settingslib.Utils.getWifiIconResource(accessPoint.getLevel()));
181
182         @ColorInt int color;
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));
190             }
191         } else {
192             color = Utils.getColorAttrDefaultColor(mContext, android.R.attr.colorControlNormal);
193         }
194
195         d.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
196         return Utils.createIconWithDrawable(d);
197     }
198
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);
206         }
207         return null;
208     }
209
210     private PendingIntent getAccessPointAction(AccessPoint accessPoint) {
211         final Bundle extras = new Bundle();
212         accessPoint.saveWifiState(extras);
213
214         Intent intent;
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)
221                     .toIntent();
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);
225         } else {
226             intent = new Intent(mContext, WifiDialogActivity.class);
227             intent.putExtra(WifiDialogActivity.KEY_ACCESS_POINT_STATE, extras);
228         }
229         return PendingIntent.getActivity(mContext, accessPoint.hashCode() /* requestCode */,
230                 intent, 0 /* flags */);
231     }
232
233     private ListBuilder.RowBuilder getLoadingRow() {
234         final CharSequence title = mContext.getText(R.string.wifi_empty_list_wifi_on);
235
236         // for aligning to the Wi-Fi AP's name
237         final IconCompat emptyIcon = Utils.createIconWithDrawable(
238                 new ColorDrawable(Color.TRANSPARENT));
239
240         return new ListBuilder.RowBuilder()
241                 .setTitleItem(emptyIcon, ListBuilder.ICON_IMAGE)
242                 .setSubtitle(title);
243     }
244
245     /**
246      * Update the current wifi status to the boolean value keyed by
247      * {@link android.app.slice.Slice#EXTRA_TOGGLE_STATE} on {@param intent}.
248      */
249     @Override
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}
256         // handle it.
257     }
258
259     @Override
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);
268
269         return intent;
270     }
271
272     protected String getActiveSSID() {
273         if (mWifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) {
274             return WifiSsid.NONE;
275         }
276         return WifiInfo.removeDoubleQuotes(mWifiManager.getConnectionInfo().getSSID());
277     }
278
279     private boolean isWifiEnabled() {
280         switch (mWifiManager.getWifiState()) {
281             case WifiManager.WIFI_STATE_ENABLED:
282             case WifiManager.WIFI_STATE_ENABLING:
283                 return true;
284             default:
285                 return false;
286         }
287     }
288
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);
295                 }
296                 return ssid;
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:
303             default:
304                 return null;
305         }
306     }
307
308     private PendingIntent getPrimaryAction() {
309         final Intent intent = getIntent();
310         return PendingIntent.getActivity(mContext, 0 /* requestCode */,
311                 intent, 0 /* flags */);
312     }
313
314     @Override
315     public Class getBackgroundWorkerClass() {
316         return WifiScanWorker.class;
317     }
318
319     public static class WifiScanWorker extends SliceBackgroundWorker<AccessPoint>
320             implements WifiTracker.WifiListener {
321
322         private final Context mContext;
323
324         private WifiTracker mWifiTracker;
325
326         public WifiScanWorker(Context context, Uri uri) {
327             super(context, uri);
328             mContext = context;
329         }
330
331         @Override
332         protected void onSlicePinned() {
333             if (mWifiTracker == null) {
334                 mWifiTracker = new WifiTracker(mContext, this /* wifiListener */,
335                         true /* includeSaved */, true /* includeScans */);
336             }
337             mWifiTracker.onStart();
338             onAccessPointsChanged();
339         }
340
341         @Override
342         protected void onSliceUnpinned() {
343             mWifiTracker.onStop();
344         }
345
346         @Override
347         public void close() {
348             mWifiTracker.onDestroy();
349         }
350
351         @Override
352         public void onWifiStateChanged(int state) {
353             notifySliceChange();
354         }
355
356         @Override
357         public void onConnectedChanged() {
358             notifySliceChange();
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(ap);
374                 }
375             }
376             updateResults(resultList);
377         }
378     }
379 }