1 /* <lambda>null2 * Copyright (C) 2025 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.utils 18 19 import android.util.Log 20 import android.view.View 21 import com.android.settings.R 22 import com.android.wifitrackerlib.WifiEntry.SECURITY_NONE 23 import com.android.wifitrackerlib.WifiEntry.SECURITY_PSK 24 import com.android.wifitrackerlib.WifiEntry.SECURITY_SAE 25 import com.android.wifitrackerlib.WifiEntry.SECURITY_WEP 26 27 /** 28 * The Wi-Fi password {@code TextInputGroup} that supports input validation. 29 */ 30 class WifiPasswordInput( 31 view: View, 32 var security: Int = SECURITY_NONE, 33 ) : TextInputGroup(view, R.id.password_input_layout, R.id.password, R.string.wifi_field_required) { 34 35 var canBeEmpty: Boolean = false 36 37 override fun validate(): Boolean { 38 if (!editText.isShown) return true 39 if (canBeEmpty && text.isEmpty()) return true 40 41 return when (security) { 42 SECURITY_WEP -> super.validate() 43 SECURITY_PSK -> super.validate() && isValidPsk(text).also { valid -> 44 if (!valid) { 45 error = view.context.getString(R.string.wifi_password_invalid) 46 Log.w(TAG, "validate failed in ${layout.hint ?: "unknown"} for PSK") 47 } 48 } 49 50 SECURITY_SAE -> super.validate() && isValidSae(text).also { valid -> 51 if (!valid) { 52 error = view.context.getString(R.string.wifi_password_invalid) 53 Log.w(TAG, "validate failed in ${layout.hint ?: "unknown"} for SAE") 54 } 55 } 56 57 else -> true 58 } 59 } 60 61 companion object { 62 const val TAG = "WifiPasswordInput" 63 64 fun isValidPsk(password: String): Boolean { 65 return (password.length == 64 && password.matches("[0-9A-Fa-f]{64}".toRegex())) || 66 (password.length in 8..63) 67 } 68 69 fun isValidSae(password: String): Boolean { 70 return password.length in 1..63 71 } 72 } 73 }