Merge "Add config for Wi-Fi Hotspot Settings hidden" into tm-qpr-dev

This commit is contained in:
TreeHugger Robot
2022-10-24 03:04:59 +00:00
committed by Android (Google) Code Review
7 changed files with 204 additions and 24 deletions

View File

@@ -22,14 +22,20 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.NetworkCapabilities;
import android.net.TetheringManager;
import android.net.wifi.ScanResult;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.wifitrackerlib.WifiEntry;
@@ -38,12 +44,16 @@ import java.nio.charset.StandardCharsets;
/** A utility class for Wi-Fi functions. */
public class WifiUtils extends com.android.settingslib.wifi.WifiUtils {
static final String TAG = "WifiUtils";
private static final int SSID_ASCII_MIN_LENGTH = 1;
private static final int SSID_ASCII_MAX_LENGTH = 32;
private static final int PSK_PASSPHRASE_ASCII_MIN_LENGTH = 8;
private static final int PSK_PASSPHRASE_ASCII_MAX_LENGTH = 63;
private static Boolean sCanShowWifiHotspotCached;
public static boolean isSSIDTooLong(String ssid) {
if (TextUtils.isEmpty(ssid)) {
return false;
@@ -240,4 +250,62 @@ public class WifiUtils extends com.android.settingslib.wifi.WifiUtils {
return WifiEntry.SECURITY_NONE;
}
/**
* Check if Wi-Fi hotspot settings can be displayed.
*
* @param context Context of caller
* @return true if Wi-Fi hotspot settings can be displayed
*/
public static boolean checkShowWifiHotspot(Context context) {
if (context == null) return false;
boolean showWifiHotspotSettings =
context.getResources().getBoolean(R.bool.config_show_wifi_hotspot_settings);
if (!showWifiHotspotSettings) {
Log.w(TAG, "config_show_wifi_hotspot_settings:false");
return false;
}
WifiManager wifiManager = context.getSystemService(WifiManager.class);
if (wifiManager == null) {
Log.e(TAG, "WifiManager is null");
return false;
}
TetheringManager tetheringManager = context.getSystemService(TetheringManager.class);
if (tetheringManager == null) {
Log.e(TAG, "TetheringManager is null");
return false;
}
String[] wifiRegexs = tetheringManager.getTetherableWifiRegexs();
if (wifiRegexs == null || wifiRegexs.length == 0) {
Log.w(TAG, "TetherableWifiRegexs is empty");
return false;
}
return true;
}
/**
* Return the cached result to see if Wi-Fi hotspot settings can be displayed.
*
* @param context Context of caller
* @return true if Wi-Fi hotspot settings can be displayed
*/
public static boolean canShowWifiHotspot(Context context) {
if (sCanShowWifiHotspotCached == null) {
sCanShowWifiHotspotCached = checkShowWifiHotspot(context);
}
return sCanShowWifiHotspotCached;
}
/**
* Sets the sCanShowWifiHotspotCached for testing purposes.
*
* @param cached Cached value for #canShowWifiHotspot()
*/
@VisibleForTesting
public static void setCanShowWifiHotspotCached(Boolean cached) {
sCanShowWifiHotspotCached = cached;
}
}

View File

@@ -16,9 +16,10 @@
package com.android.settings.wifi.tether;
import static com.android.settings.wifi.WifiUtils.canShowWifiHotspot;
import android.annotation.NonNull;
import android.content.Context;
import android.net.TetheringManager;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiClient;
import android.net.wifi.WifiManager;
@@ -46,7 +47,6 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
private static final String WIFI_TETHER_SETTINGS = "wifi_tether";
private boolean mIsWifiTetherable;
private WifiManager mWifiManager;
private boolean mIsWifiTetheringAllow;
private int mSoftApState;
@@ -57,8 +57,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) {
this(context, lifecycle,
context.getSystemService(WifiManager.class),
context.getSystemService(TetheringManager.class),
context.getApplicationContext().getSystemService(WifiManager.class),
true /* initSoftApManager */,
WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context));
}
@@ -68,15 +67,9 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
Context context,
Lifecycle lifecycle,
WifiManager wifiManager,
TetheringManager tetheringManager,
boolean initSoftApManager,
boolean isWifiTetheringAllow) {
super(context);
final String[] wifiRegexs = tetheringManager.getTetherableWifiRegexs();
if (wifiRegexs != null && wifiRegexs.length != 0) {
mIsWifiTetherable = true;
}
mIsWifiTetheringAllow = isWifiTetheringAllow;
if (!isWifiTetheringAllow) return;
@@ -92,7 +85,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
@Override
public boolean isAvailable() {
return mIsWifiTetherable && !Utils.isMonkeyRunning();
return canShowWifiHotspot(mContext) && !Utils.isMonkeyRunning();
}
@Override

View File

@@ -18,6 +18,8 @@ package com.android.settings.wifi.tether;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_CHANGED_ACTION;
import static com.android.settings.wifi.WifiUtils.canShowWifiHotspot;
import android.app.settings.SettingsEnums;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -107,6 +109,13 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (!canShowWifiHotspot(getContext())) {
Log.e(TAG, "can not launch Wi-Fi hotspot settings"
+ " because the config is not set to show.");
finish();
return;
}
setIfOnlyAvailableForAdmins(true);
mUnavailable = isUiRestricted() || !mWifiRestriction.isHotspotAvailable(getContext());
}