1 /* 2 * Copyright (C) 2023 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.annotation.StyleRes 20 import android.content.Context 21 import android.content.DialogInterface 22 import android.os.Bundle 23 import android.view.View 24 import android.view.WindowManager 25 import android.widget.Button 26 import android.widget.ImageButton 27 import android.widget.TextView 28 import androidx.annotation.OpenForTesting 29 import androidx.appcompat.app.AlertDialog 30 import com.android.settings.R 31 import com.android.settings.wifi.utils.WifiDialogHelper 32 import com.android.settingslib.RestrictedLockUtils 33 import com.android.settingslib.RestrictedLockUtilsInternal 34 import com.android.wifitrackerlib.WifiEntry 35 36 /** 37 * Dialog for users to edit a Wi-Fi network. 38 */ 39 @OpenForTesting 40 open class WifiDialog2 @JvmOverloads constructor( 41 context: Context, 42 private val listener: WifiDialog2Listener, 43 val wifiEntry: WifiEntry?, 44 private val mode: Int, 45 @StyleRes style: Int = 0, 46 private val hideSubmitButton: Boolean = mode == WifiConfigUiBase2.MODE_VIEW, 47 private val hideMeteredAndPrivacy: Boolean = false, 48 private val isSysUiCaller: Boolean = false, 49 ) : AlertDialog(context, style), WifiConfigUiBase2, DialogInterface.OnClickListener { 50 /** 51 * Host UI component of WifiDialog2 can receive callbacks by this interface. 52 */ 53 interface WifiDialog2Listener { 54 /** 55 * To forget the Wi-Fi network. 56 */ onForgetnull57 fun onForget(dialog: WifiDialog2) {} 58 59 /** 60 * To save the Wi-Fi network. 61 */ onSubmitnull62 fun onSubmit(dialog: WifiDialog2) {} 63 64 /** 65 * To trigger Wi-Fi QR code scanner. 66 */ onScannull67 fun onScan(dialog: WifiDialog2, ssid: String) {} 68 } 69 70 private lateinit var view: View 71 private lateinit var controller: WifiConfigController2 72 private lateinit var dialogHelper: WifiDialogHelper 73 getControllernull74 override fun getController(): WifiConfigController2 = controller 75 76 override fun onCreate(savedInstanceState: Bundle?) { 77 if (isSysUiCaller) { 78 setWindowsOverlay() 79 } 80 view = layoutInflater.inflate(R.layout.wifi_dialog, null) 81 setView(view) 82 controller = WifiConfigController2(this, view, wifiEntry, mode, hideMeteredAndPrivacy) 83 super.onCreate(savedInstanceState) 84 if (hideSubmitButton) { 85 controller.hideSubmitButton() 86 } else { 87 // During creation, the submit button can be unavailable to determine visibility. 88 // Right after creation, update button visibility 89 controller.enableSubmitIfAppropriate() 90 } 91 if (wifiEntry == null) { 92 controller.hideForgetButton() 93 } 94 dialogHelper = WifiDialogHelper(this, this, controller.validator) 95 } 96 setWindowsOverlaynull97 private fun setWindowsOverlay() { 98 window?.apply { 99 val lp = attributes 100 setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) 101 attributes = lp 102 } 103 } 104 onStartnull105 override fun onStart() { 106 super.onStart() 107 val ssidScannerButton = requireViewById<ImageButton>(R.id.ssid_scanner_button) 108 if (hideSubmitButton) { 109 ssidScannerButton.visibility = View.GONE 110 } else { 111 ssidScannerButton.setOnClickListener { 112 val ssidEditText = requireViewById<TextView>(R.id.ssid) 113 val ssid = ssidEditText.text.toString() 114 listener.onScan(this, ssid) 115 } 116 } 117 } 118 onRestoreInstanceStatenull119 override fun onRestoreInstanceState(savedInstanceState: Bundle) { 120 super.onRestoreInstanceState(savedInstanceState) 121 controller.updatePassword() 122 } 123 dispatchSubmitnull124 override fun dispatchSubmit() { 125 listener.onSubmit(this) 126 dismiss() 127 } 128 onClicknull129 override fun onClick(dialogInterface: DialogInterface, id: Int) { 130 when (id) { 131 BUTTON_SUBMIT -> listener.onSubmit(this) 132 BUTTON_FORGET -> { 133 if (WifiUtils.isNetworkLockedDown(context, wifiEntry!!.wifiConfiguration)) { 134 RestrictedLockUtils.sendShowAdminSupportDetailsIntent( 135 context, 136 RestrictedLockUtilsInternal.getDeviceOwner(context) 137 ) 138 return 139 } 140 listener.onForget(this) 141 } 142 } 143 } 144 getModenull145 override fun getMode(): Int = mode 146 147 override fun getSubmitButton(): Button? = getButton(BUTTON_SUBMIT) 148 149 override fun getForgetButton(): Button? = getButton(BUTTON_FORGET) 150 151 override fun getCancelButton(): Button? = getButton(BUTTON_NEGATIVE) 152 153 override fun setSubmitButton(text: CharSequence) { 154 setButton(BUTTON_SUBMIT, text, this) 155 } 156 setForgetButtonnull157 override fun setForgetButton(text: CharSequence) { 158 setButton(BUTTON_FORGET, text, this) 159 } 160 setCancelButtonnull161 override fun setCancelButton(text: CharSequence) { 162 setButton(BUTTON_NEGATIVE, text, this) 163 } 164 165 companion object { 166 private const val BUTTON_SUBMIT = BUTTON_POSITIVE 167 private const val BUTTON_FORGET = BUTTON_NEUTRAL 168 } 169 } 170