1 /* 2 * Copyright (C) 2017 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; 18 19 import android.app.admin.DevicePolicyManager; 20 import android.content.ComponentName; 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.content.pm.PackageManager; 24 import android.net.NetworkCapabilities; 25 import android.net.wifi.WifiConfiguration; 26 import android.provider.Settings; 27 import android.text.TextUtils; 28 29 import com.android.settingslib.wrapper.PackageManagerWrapper; 30 31 public class WifiUtils { 32 33 private static final int SSID_ASCII_MIN_LENGTH = 1; 34 private static final int SSID_ASCII_MAX_LENGTH = 32; 35 private static final int PASSWORD_MIN_LENGTH = 8; 36 private static final int PASSWORD_MAX_LENGTH = 63; 37 38 isSSIDTooLong(String ssid)39 public static boolean isSSIDTooLong(String ssid) { 40 if (TextUtils.isEmpty(ssid)) { 41 return false; 42 } 43 return ssid.length() > SSID_ASCII_MAX_LENGTH; 44 } 45 isSSIDTooShort(String ssid)46 public static boolean isSSIDTooShort(String ssid) { 47 if (TextUtils.isEmpty(ssid)) { 48 return true; 49 } 50 return ssid.length() < SSID_ASCII_MIN_LENGTH; 51 } 52 isHotspotPasswordValid(String password)53 public static boolean isHotspotPasswordValid(String password) { 54 if (TextUtils.isEmpty(password)) { 55 return false; 56 } 57 58 final int length = password.length(); 59 return length >= PASSWORD_MIN_LENGTH && length <= PASSWORD_MAX_LENGTH; 60 } 61 62 /** 63 * This method is a stripped and negated version of WifiConfigStore.canModifyNetwork. 64 * @param context Context of caller 65 * @param config The WiFi config. 66 * @return true if Settings cannot modify the config due to lockDown. 67 */ isNetworkLockedDown(Context context, WifiConfiguration config)68 public static boolean isNetworkLockedDown(Context context, WifiConfiguration config) { 69 if (config == null) { 70 return false; 71 } 72 73 final DevicePolicyManager dpm = 74 (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); 75 final PackageManagerWrapper pm = new PackageManagerWrapper(context.getPackageManager()); 76 77 // Check if device has DPM capability. If it has and dpm is still null, then we 78 // treat this case with suspicion and bail out. 79 if (pm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN) && dpm == null) { 80 return true; 81 } 82 83 boolean isConfigEligibleForLockdown = false; 84 if (dpm != null) { 85 final ComponentName deviceOwner = dpm.getDeviceOwnerComponentOnAnyUser(); 86 if (deviceOwner != null) { 87 final int deviceOwnerUserId = dpm.getDeviceOwnerUserId(); 88 try { 89 final int deviceOwnerUid = pm.getPackageUidAsUser(deviceOwner.getPackageName(), 90 deviceOwnerUserId); 91 isConfigEligibleForLockdown = deviceOwnerUid == config.creatorUid; 92 } catch (PackageManager.NameNotFoundException e) { 93 // don't care 94 } 95 } 96 } 97 if (!isConfigEligibleForLockdown) { 98 return false; 99 } 100 101 final ContentResolver resolver = context.getContentResolver(); 102 final boolean isLockdownFeatureEnabled = Settings.Global.getInt(resolver, 103 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0) != 0; 104 return isLockdownFeatureEnabled; 105 } 106 107 /** Returns true if the provided NetworkCapabilities indicate a captive portal network. */ canSignIntoNetwork(NetworkCapabilities capabilities)108 public static boolean canSignIntoNetwork(NetworkCapabilities capabilities) { 109 return (capabilities != null 110 && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL)); 111 } 112 } 113