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 android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.net.ConnectivityManager; 22 import android.net.NetworkCapabilities; 23 import android.net.Uri; 24 import android.net.wifi.WifiInfo; 25 import android.net.wifi.WifiManager; 26 import android.text.TextUtils; 27 28 import androidx.annotation.VisibleForTesting; 29 import androidx.core.graphics.drawable.IconCompat; 30 import androidx.slice.Slice; 31 import androidx.slice.builders.ListBuilder; 32 33 import com.android.settings.R; 34 import com.android.settings.Utils; 35 import com.android.settings.overlay.FeatureFactory; 36 import com.android.settings.slices.CustomSliceRegistry; 37 import com.android.settings.slices.CustomSliceable; 38 import com.android.wifitrackerlib.WifiEntry; 39 40 /** 41 * {@link CustomSliceable} for Wi-Fi, used by contextual homepage. 42 */ 43 public class ContextualWifiSlice extends WifiSlice { 44 45 @VisibleForTesting 46 static final int COLLAPSED_ROW_COUNT = 0; 47 48 @VisibleForTesting 49 static long sActiveUiSession = -1000; 50 @VisibleForTesting 51 static boolean sApRowCollapsed; 52 53 private final ConnectivityManager mConnectivityManager; 54 ContextualWifiSlice(Context context)55 public ContextualWifiSlice(Context context) { 56 super(context); 57 58 mConnectivityManager = mContext.getSystemService(ConnectivityManager.class); 59 } 60 61 @Override getUri()62 public Uri getUri() { 63 return CustomSliceRegistry.CONTEXTUAL_WIFI_SLICE_URI; 64 } 65 66 @Override getSlice()67 public Slice getSlice() { 68 final long currentUiSession = FeatureFactory.getFactory(mContext) 69 .getSlicesFeatureProvider().getUiSessionToken(); 70 if (currentUiSession != sActiveUiSession) { 71 sActiveUiSession = currentUiSession; 72 sApRowCollapsed = hasWorkingNetwork(); 73 } else if (!mWifiManager.isWifiEnabled()) { 74 sApRowCollapsed = false; 75 } 76 return super.getSlice(); 77 } 78 getApRowCount()79 static int getApRowCount() { 80 return sApRowCollapsed ? COLLAPSED_ROW_COUNT : DEFAULT_EXPANDED_ROW_COUNT; 81 } 82 83 @Override isApRowCollapsed()84 protected boolean isApRowCollapsed() { 85 return sApRowCollapsed; 86 } 87 88 @Override getHeaderRow(boolean isWifiEnabled, WifiSliceItem wifiSliceItem)89 protected ListBuilder.RowBuilder getHeaderRow(boolean isWifiEnabled, 90 WifiSliceItem wifiSliceItem) { 91 final ListBuilder.RowBuilder builder = super.getHeaderRow(isWifiEnabled, wifiSliceItem); 92 builder.setTitleItem(getHeaderIcon(isWifiEnabled, wifiSliceItem), ListBuilder.ICON_IMAGE); 93 if (sApRowCollapsed) { 94 builder.setSubtitle(getHeaderSubtitle(wifiSliceItem)); 95 } 96 return builder; 97 } 98 getHeaderIcon(boolean isWifiEnabled, WifiSliceItem wifiSliceItem)99 private IconCompat getHeaderIcon(boolean isWifiEnabled, WifiSliceItem wifiSliceItem) { 100 final Drawable drawable; 101 final int tint; 102 if (!isWifiEnabled) { 103 drawable = mContext.getDrawable(R.drawable.ic_wifi_off); 104 tint = Utils.getDisabled(mContext, Utils.getColorAttrDefaultColor(mContext, 105 android.R.attr.colorControlNormal)); 106 } else { 107 // get icon of medium signal strength 108 drawable = mContext.getDrawable(com.android.settingslib.Utils.getWifiIconResource(2)); 109 if (wifiSliceItem != null 110 && wifiSliceItem.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTED) { 111 tint = Utils.getColorAccentDefaultColor(mContext); 112 } else { 113 tint = Utils.getColorAttrDefaultColor(mContext, android.R.attr.colorControlNormal); 114 } 115 } 116 drawable.setTint(tint); 117 return Utils.createIconWithDrawable(drawable); 118 } 119 getHeaderSubtitle(WifiSliceItem wifiSliceItem)120 private CharSequence getHeaderSubtitle(WifiSliceItem wifiSliceItem) { 121 if (wifiSliceItem == null 122 || wifiSliceItem.getConnectedState() == WifiEntry.CONNECTED_STATE_DISCONNECTED) { 123 return mContext.getText(R.string.disconnected); 124 } 125 if (wifiSliceItem.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTING) { 126 return mContext.getString(R.string.wifi_connecting_to_message, 127 wifiSliceItem.getTitle()); 128 } 129 return mContext.getString(R.string.wifi_connected_to_message, wifiSliceItem.getTitle()); 130 } 131 hasWorkingNetwork()132 private boolean hasWorkingNetwork() { 133 return !TextUtils.equals(getActiveSSID(), WifiManager.UNKNOWN_SSID) && hasInternetAccess(); 134 } 135 getActiveSSID()136 private String getActiveSSID() { 137 if (mWifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) { 138 return WifiManager.UNKNOWN_SSID; 139 } 140 return WifiInfo.sanitizeSsid(mWifiManager.getConnectionInfo().getSSID()); 141 } 142 hasInternetAccess()143 private boolean hasInternetAccess() { 144 final NetworkCapabilities nc = mConnectivityManager.getNetworkCapabilities( 145 mWifiManager.getCurrentNetwork()); 146 return nc != null 147 && !nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL) 148 && !nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY) 149 && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); 150 } 151 152 @Override getBackgroundWorkerClass()153 public Class getBackgroundWorkerClass() { 154 return ContextualWifiScanWorker.class; 155 } 156 } 157