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.qc; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.net.TetheringManager; 22 import android.net.Uri; 23 import android.net.wifi.WifiManager; 24 25 import com.android.internal.util.ConcurrentUtils; 26 27 /** 28 * Helper methods for hotspot related quick controls. 29 */ 30 public final class HotspotQCUtils { HotspotQCUtils()31 private HotspotQCUtils() { 32 } 33 34 /** 35 * Returns whether or not hotspot is currently enabled. 36 */ isHotspotEnabled(WifiManager wifiManager)37 public static boolean isHotspotEnabled(WifiManager wifiManager) { 38 int state = wifiManager.getWifiApState(); 39 return state == WifiManager.WIFI_AP_STATE_ENABLED 40 || state == WifiManager.WIFI_AP_STATE_ENABLING; 41 } 42 43 /** 44 * Returns whether or not hotspot is currently busy. 45 */ isHotspotBusy(WifiManager wifiManager)46 public static boolean isHotspotBusy(WifiManager wifiManager) { 47 int state = wifiManager.getWifiApState(); 48 return state == WifiManager.WIFI_AP_STATE_ENABLING 49 || state == WifiManager.WIFI_AP_STATE_DISABLING; 50 } 51 52 /** 53 * Helper method to enable tethering with {@link TetheringManager.StartTetheringCallback} 54 * on success or failure. 55 */ enableHotspot(TetheringManager tetheringManager, TetheringManager.StartTetheringCallback callback)56 public static void enableHotspot(TetheringManager tetheringManager, 57 TetheringManager.StartTetheringCallback callback) { 58 tetheringManager.startTethering(ConnectivityManager.TETHERING_WIFI, 59 ConcurrentUtils.DIRECT_EXECUTOR, callback); 60 } 61 62 /** 63 * Helper method to disable tethering. 64 */ disableHotspot(TetheringManager tetheringManager)65 public static void disableHotspot(TetheringManager tetheringManager) { 66 tetheringManager.stopTethering(ConnectivityManager.TETHERING_WIFI); 67 } 68 69 /** 70 * Helper method to get the default {@link TetheringManager.StartTetheringCallback} used by 71 * hotspot quick controls. 72 */ getDefaultStartTetheringCallback( Context context, Uri uri)73 public static TetheringManager.StartTetheringCallback getDefaultStartTetheringCallback( 74 Context context, Uri uri) { 75 return new TetheringManager.StartTetheringCallback() { 76 @Override 77 public void onTetheringFailed(final int result) { 78 context.getContentResolver().notifyChange(uri, /* observer= */null); 79 } 80 }; 81 } 82 } 83