1 /* 2 * Copyright (C) 2021 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.car.settings.wifi; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.net.TetheringManager; 22 import android.net.wifi.SoftApConfiguration; 23 import android.text.TextUtils; 24 25 import com.android.car.settings.R; 26 import com.android.internal.util.ConcurrentUtils; 27 import com.android.settingslib.wifi.WifiUtils; 28 29 /** 30 * Collection of helper methods for Wi-Fi tethering. 31 */ 32 public class WifiTetherUtil { WifiTetherUtil()33 private WifiTetherUtil() { 34 } 35 36 /** 37 * Helper method to enable tethering with {@link TetheringManager.StartTetheringCallback} 38 * on success or failure. 39 */ startTethering(TetheringManager tetheringManager, TetheringManager.StartTetheringCallback callback)40 public static void startTethering(TetheringManager tetheringManager, 41 TetheringManager.StartTetheringCallback callback) { 42 tetheringManager.startTethering(ConnectivityManager.TETHERING_WIFI, 43 ConcurrentUtils.DIRECT_EXECUTOR, callback); 44 } 45 46 /** 47 * Helper method to disable tethering. 48 */ stopTethering(TetheringManager tetheringManager)49 public static void stopTethering(TetheringManager tetheringManager) { 50 tetheringManager.stopTethering(ConnectivityManager.TETHERING_WIFI); 51 } 52 53 /** Returns the subtitle to be shown for hotspot action preferences. 54 * There are three different states that can be shown: 55 * - If tethering is disabled, return the off string. 56 * - If tethering is enabled but no devices are connected, return the ssid + password string. 57 * - If tethering is enabled and devices are connected, return the devices connected string. 58 */ getHotspotSubtitle(Context context, SoftApConfiguration softApConfig, boolean hotspotEnabled, int connectedDevices)59 public static String getHotspotSubtitle(Context context, SoftApConfiguration softApConfig, 60 boolean hotspotEnabled, int connectedDevices) { 61 if (!hotspotEnabled) { 62 return context.getString(R.string.wifi_hotspot_state_off); 63 } 64 if (connectedDevices > 0) { 65 return WifiUtils.getWifiTetherSummaryForConnectedDevices(context, connectedDevices); 66 } 67 String subtitle = softApConfig.getSsid(); 68 if (TextUtils.isEmpty(subtitle)) { 69 // If there currently is no SSID to show, use a default "On" string 70 return context.getString(R.string.car_ui_preference_switch_on); 71 } 72 String password = getHotspotPassword(softApConfig); 73 if (!TextUtils.isEmpty(password)) { 74 subtitle += " / " + password; 75 } 76 return subtitle; 77 } 78 getHotspotPassword(SoftApConfiguration softApConfig)79 private static String getHotspotPassword(SoftApConfiguration softApConfig) { 80 if (softApConfig.getSecurityType() == SoftApConfiguration.SECURITY_TYPE_OPEN) { 81 return null; 82 } 83 return softApConfig.getPassphrase(); 84 } 85 } 86