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.net.NetworkCapabilities; 21 import android.net.Uri; 22 import android.net.wifi.WifiInfo; 23 import android.net.wifi.WifiManager; 24 import android.net.wifi.WifiSsid; 25 import android.text.TextUtils; 26 import android.util.Log; 27 28 import androidx.annotation.VisibleForTesting; 29 import androidx.slice.Slice; 30 31 import com.android.settings.overlay.FeatureFactory; 32 import com.android.settings.slices.CustomSliceRegistry; 33 import com.android.settings.slices.CustomSliceable; 34 35 /** 36 * {@link CustomSliceable} for Wi-Fi, used by contextual homepage. 37 */ 38 public class ContextualWifiSlice extends WifiSlice { 39 40 private static final String TAG = "ContextualWifiSlice"; 41 @VisibleForTesting 42 static long sActiveUiSession = -1000; 43 @VisibleForTesting 44 static boolean sPreviouslyDisplayed; 45 ContextualWifiSlice(Context context)46 public ContextualWifiSlice(Context context) { 47 super(context); 48 } 49 50 @Override getUri()51 public Uri getUri() { 52 return CustomSliceRegistry.CONTEXTUAL_WIFI_SLICE_URI; 53 } 54 55 @Override getSlice()56 public Slice getSlice() { 57 final long currentUiSession = FeatureFactory.getFactory(mContext) 58 .getSlicesFeatureProvider().getUiSessionToken(); 59 if (currentUiSession != sActiveUiSession) { 60 sActiveUiSession = currentUiSession; 61 sPreviouslyDisplayed = false; 62 } 63 if (!sPreviouslyDisplayed && hasWorkingNetwork()) { 64 Log.d(TAG, "Wifi is connected, no point showing any suggestion."); 65 return null; 66 } 67 // Set sPreviouslyDisplayed to true - we will show *something* on the screen. So we should 68 // keep showing this card to keep UI stable, even if wifi connects to a network later. 69 sPreviouslyDisplayed = true; 70 71 return super.getSlice(); 72 } 73 hasWorkingNetwork()74 private boolean hasWorkingNetwork() { 75 return !TextUtils.equals(getActiveSSID(), WifiSsid.NONE) && hasInternetAccess(); 76 } 77 getActiveSSID()78 private String getActiveSSID() { 79 if (mWifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) { 80 return WifiSsid.NONE; 81 } 82 return WifiInfo.removeDoubleQuotes(mWifiManager.getConnectionInfo().getSSID()); 83 } 84 hasInternetAccess()85 private boolean hasInternetAccess() { 86 final NetworkCapabilities nc = mConnectivityManager.getNetworkCapabilities( 87 mWifiManager.getCurrentNetwork()); 88 return nc != null 89 && !nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL) 90 && !nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY) 91 && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); 92 } 93 94 @Override getBackgroundWorkerClass()95 public Class getBackgroundWorkerClass() { 96 return ContextualWifiScanWorker.class; 97 } 98 } 99