1 /* 2 * Copyright (C) 2024 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 @file:Suppress("DEPRECATION", "MissingPermission") 18 19 package com.android.settings.wifi.utils 20 21 import android.content.Context 22 import android.net.ConnectivityManager 23 import android.net.NetworkCapabilities 24 import android.net.TetheringManager 25 import android.net.wifi.WifiManager 26 27 /** 28 * Gets the {@link android.net.wifi.WifiManager} system service. 29 * 30 * Use application context to get system services to avoid memory leaks. 31 */ 32 val Context.wifiManager: WifiManager? 33 get() = applicationContext.getSystemService(WifiManager::class.java) 34 35 /** Return the UTF-8 String set to be the SSID for the Soft AP. */ 36 val Context.wifiSoftApSsid 37 get() = wifiManager?.softApConfiguration?.ssid 38 39 /** Gets the tethered Wi-Fi hotspot enabled state. */ 40 val Context.wifiApState 41 get() = wifiManager?.wifiApState 42 43 /** Gets/Sets the Wi-Fi enabled state. */ 44 var Context.isWifiEnabled: Boolean 45 get() = wifiManager?.isWifiEnabled == true 46 set(value) { 47 wifiManager?.isWifiEnabled = value 48 } 49 50 /** 51 * Gets the {@link android.net.TetheringManager} system service. 52 * 53 * Use application context to get system services to avoid memory leaks. 54 */ 55 val Context.tetheringManager: TetheringManager? 56 get() = applicationContext.getSystemService(TetheringManager::class.java) 57 58 /** 59 * Gets the {@link android.net.ConnectivityManager} system service. 60 * 61 * Use application context to get system services to avoid memory leaks. 62 */ 63 val Context.connectivityManager: ConnectivityManager? 64 get() = applicationContext.getSystemService(ConnectivityManager::class.java) 65 66 /** Return true if the default network is a Wi-Fi network */ 67 val Context.isDefaultNetworkWifi: Boolean 68 get() = 69 connectivityManager 70 ?.getNetworkCapabilities(connectivityManager?.activeNetwork) 71 ?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true 72