Browse Source

011 wifi 处理

zhangshenjie 9 months ago
parent
commit
722d5e77ef

+ 3 - 3
BusinessAirFryer/src/main/assets/config_011.json

@@ -104,7 +104,7 @@
       "bg": "ic_a_dicing_bg_011",
       "icon": "ic_a_dicing_011",
       "devMode": "SOUP",
-      "listShow": true
+      "listShow": false
     },
     {
       "type": "CITRUS_JUICER",
@@ -112,7 +112,7 @@
       "bg": "ic_a_citrus_juicer_bg_011",
       "icon": "ic_a_citrus_juicer_011",
       "devMode": "SOUP",
-      "listShow": true
+      "listShow": false
     },
 
     {
@@ -121,7 +121,7 @@
       "bg": "ic_a_potato_peeler_bg_011",
       "icon": "ic_a_potato_peeler_011",
       "devMode": "SOUP",
-      "listShow": true
+      "listShow": false
     },
 
     {

+ 28 - 0
BusinessCommon/src/main/res/layout/dialog_wifi_loading.xml

@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    xmlns:tools="http://schemas.android.com/tools"
+    tools:background="#222222">
+
+    <ProgressBar
+        android:id="@+id/wifi_bar"
+        android:layout_width="@dimen/convert_110px"
+        android:layout_height="@dimen/convert_110px"
+        android:layout_gravity="center"
+        android:progress="80"
+        android:indeterminateTint="#ffffff"/>
+    <TextView
+        android:id="@+id/wifi_text"
+        android:text="@string/wifi_loading_text"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:indeterminateTint="#ffffff"
+        android:textSize="@dimen/convert_20px"
+        android:textColor="#ffffff"
+        android:layout_gravity="center"
+        android:layout_marginTop="@dimen/convert_100px"
+
+        />
+</FrameLayout>

+ 1 - 0
BusinessCommon/src/main/res/values-en/strings.xml

@@ -155,6 +155,7 @@ Nanfang plus client is an online information platform developed and operated by
     <string name="network_wifi_status_failed">Failed</string>
     <string name="network_wifi_status_blocked">Blocked</string>
     <string name="network_wifi_status_verifying_poor_link">Temporarily shut down (bad network condition)</string>
+    <string name="wifi_loading_text">Connecting…</string>
     <string name="start">START</string>
     <string name="pause">PAUSE</string>
     <string name="cancel">CANCEL</string>

+ 1 - 0
BusinessCommon/src/main/res/values/strings.xml

@@ -129,6 +129,7 @@ Nanfang plus client is an online information platform developed and operated by
     <string name="network_wifi_status_failed">Failed</string>
     <string name="network_wifi_status_blocked">Blocked</string>
     <string name="network_wifi_status_verifying_poor_link">Temporarily shut down (bad network condition)</string>
+    <string name="wifi_loading_text">Connecting…</string>
     <string name="start">START</string>
     <string name="pause">PAUSE</string>
     <string name="cancel">CANCEL</string>

+ 4 - 1
BusinessMain/src/main/res/layout/activity_home.xml

@@ -12,7 +12,10 @@
         <ImageView
             android:id="@+id/home_image"
             android:layout_width="match_parent"
-            android:layout_height="match_parent"/>
+            android:layout_height="match_parent"
+            android:scaleType="fitXY"
+
+            />
 
         <com.develop.common.widget.HomeFunctionTopBar
             android:id="@+id/function_top_bar"

+ 2 - 0
BusinessSetting/src/main/java/com/develop/setting/ui/AboutActivity.kt

@@ -241,6 +241,8 @@ class AboutActivity : CommonBVMActivity<ActivityAboutBinding, AboutViewModel>()
                 binding.ivLogo.setBackgroundResource(com.develop.common.R.drawable.ic_logo_036i)
             }else if (sn.startsWith("011")){
                 binding.ivLogo.setBackgroundResource(com.develop.common.R.drawable.ic_logo_011)
+//                ivLogoParam.width = dimenRes(com.develop.common.R.dimen.convert_180px)
+//                ivLogoParam.height = dimenRes(com.develop.common.R.dimen.convert_180px)
             }
             ivUpdateParam.apply {
                 width = dimenRes(com.develop.common.R.dimen.convert_66px)

+ 82 - 17
BusinessSetting/src/main/java/com/develop/setting/ui/WifiListActivity.kt

@@ -32,6 +32,7 @@ import com.develop.base.util.MMkvUtils
 import com.develop.common.data_repo.net.Api
 import com.develop.common.data_repo.net.model.request.DeviceInfoBody
 import com.develop.common.data_repo.net.model.response.DevInfoResult
+import com.develop.common.dialog.PlainDialogView
 import com.develop.common.event.FinishAtyEvent
 import com.develop.common.event.TuyaEventBus
 import com.develop.common.router.Screens
@@ -67,6 +68,15 @@ class WifiListActivity : CommonBindingActivity<ActivityWifiListBinding>(),
     private var timer: CountDownTimer? = null
     private var isSuccess = false
     private var sn = getSN()
+
+    private var wifiBean: WifiData? = null
+    private var isWifiConnected = false
+    private var isWifiDialog = false
+    private val loadingDialog by lazy {
+        PlainDialogView(this, com.develop.common.R.layout.dialog_wifi_loading)
+    }
+
+
     override fun createViewBinding(inflater: LayoutInflater): ActivityWifiListBinding {
         return ActivityWifiListBinding.inflate(inflater)
     }
@@ -156,25 +166,29 @@ class WifiListActivity : CommonBindingActivity<ActivityWifiListBinding>(),
                     wifiContent.setWifiModel(model)
                     wifiContent.onOkClickListener = object : WifiContentView.OnOkClickListener {
                         override fun onOkClick(wifiData: WifiData) {
+
+                            showloadingDialog()
+                            isWifiConnected = false
+                            isWifiDialog = false
+                            wifiBean = wifiData
+                            wifiHelp.setConnectWifiDate(wifiData)
+
 //                            wifiHelp.connect(wifiData, wifiData.password)
                             wifiHelp.connectNew(wifiData, wifiData.password)
                         }
 
                         override fun onContentClick(wifiData: WifiData) {
 
-                            if (getNetAuthConnect()){
-                                GlobalToast.showToast("wifi 正在连接中")
-                                return
-                            }
+
 
                             val clickModel = wifiData
                             if (clickModel.isConnected) {
                                 GlobalToast.showToast(context.getString(com.develop.common.R.string.connected_successfully))
                             } else if (clickModel.password.isNotEmpty()) {
-                                showPlainDialog()
                                 wifiHelp.connectNew(clickModel, clickModel.password)
                             } else if (clickModel.isSavePwd) {
-                                showPlainDialog()
+                                showloadingDialog()
+                                isWifiDialog = false
                                 wifiHelp.connect(clickModel, true)
                             } else {
                                 updateSelectionPos(absoluteAdapterPosition)
@@ -256,7 +270,29 @@ class WifiListActivity : CommonBindingActivity<ActivityWifiListBinding>(),
             dismissPlainDialog()
         }.setNetworkStateChangedListener {
             //AUTHENTICATING
+            if (it == NetworkInfo.DetailedState.CONNECTED) {
+                /**
+                 * 当前如果有save 保存的wifi ,在输入多个错误的wifi,会导致连接上的wifi FAILED 掉
+                 * 估在这做处理,判断点击ok连接的wifi 如果没有连接上,需要remove掉,其他的话就保存
+                 * 由于CONNECTED 回调会走几次,估多一个isWifiConnected开关来避免重复
+                 * */
+                if (wifiBean != null&&!isWifiConnected) {
+//                        Log.e("TAG WIFI", "forRemove------ :")
+                    isWifiConnected = true
+                    Handler().postDelayed({
+                        forRemoveList()
+                    }, 2000)
+                }
+
+            }
 
+            if ((it == NetworkInfo.DetailedState.CONNECTED||it == NetworkInfo.DetailedState.FAILED)&&!isWifiDialog){
+                isWifiDialog = true
+                var time : Long = if (isWifiConnected) 2200 else 1500
+                Handler().postDelayed({
+                    dismissloadingDialog()
+                }, time)
+            }
             Log.e("TAG WIFI","STATE :"+it)
             dismissPlainDialog()
         }.build()
@@ -265,6 +301,27 @@ class WifiListActivity : CommonBindingActivity<ActivityWifiListBinding>(),
     }
 
 
+    private fun forRemoveList() {
+        var isRemove = false
+        wifiDataList.forEach { it ->
+            if (it.ssid == wifiBean?.ssid) {
+                //save 状态 需要remove 保存状态得wifi
+//                Log.e("TAG WIFI", "wifiBean :"+it)
+                if (it.isSavePwd && it.ipAddress.isNullOrEmpty()) {
+//                    Log.e("TAG WIFI", "forRemoveListName :"+it.ssid)
+                    isRemove = true
+                    wifiHelp.removeSaveWifi(it)
+                }
+            }
+        }
+        if (isRemove){
+            //获取已经保存的连接
+            wifiHelp. initWork()
+        }
+
+    }
+
+
     private fun updateSelectionPos(pos: Int) {
         binding.wifiRecycler.bindingAdapter.apply {
             val tempPosition = selectedPosition
@@ -316,20 +373,28 @@ class WifiListActivity : CommonBindingActivity<ActivityWifiListBinding>(),
     }
 
 
-    private  fun  getNetAuthConnect(): Boolean {
-        var isContend = wifiHelp.isNetworkConnected(this)
-                Log.d("TAG wifi","isContend :"+isContend)
-
-//        var netState = wifiHelp.getDetailedState()
-//        Log.d("TAG wifi","state :"+netState)
-//        //判断是否网络正在连接中
-//        return NetworkInfo.DetailedState.AUTHENTICATING == netState
-//                ||  NetworkInfo.DetailedState.CONNECTING ==netState
-//                ||  NetworkInfo.DetailedState.OBTAINING_IPADDR ==netState
+    fun showloadingDialog(cancelable: Boolean = false) {
+        loadingDialog.showDialog(cancelable)
+    }
 
-        return  isContend
+    fun dismissloadingDialog() {
+        loadingDialog.hideDialog()
     }
 
+//    private  fun  getNetAuthConnect(): Boolean {
+//        var isContend = wifiHelp.isNetworkConnected(this)
+//        Log.d("TAG wifi","isContend :"+isContend)
+//
+////        var netState = wifiHelp.getDetailedState()
+////        Log.d("TAG wifi","state :"+netState)
+////        //判断是否网络正在连接中
+////        return NetworkInfo.DetailedState.AUTHENTICATING == netState
+////                ||  NetworkInfo.DetailedState.CONNECTING ==netState
+////                ||  NetworkInfo.DetailedState.OBTAINING_IPADDR ==netState
+//
+//        return  isContend
+//    }
+
     //获取是否有涂鸦功能
 //    fun getDevInfo() {
 //        //已经有涂鸦功能,避免再次请求

+ 50 - 22
libBase/src/main/java/com/develop/base/manager/WifiHelp.kt

@@ -9,6 +9,7 @@ import android.net.*
 import android.net.NetworkInfo.DetailedState
 import android.net.wifi.*
 import android.os.Parcelable
+import android.util.Log
 import com.develop.base.ext.appGlobalScope
 import com.develop.base.util.TimeDownUtil
 import kotlinx.coroutines.*
@@ -21,8 +22,10 @@ class WifiHelp {
     private var timeDownUtil: TimeDownUtil? = null
     private val mapWifi = mutableMapOf<String, Int>() //存wifi重连次数
     private var currWifiSSID = ""
+    private var wifiData :WifiData ?=null
     private fun init(build: Build) {
-        mWifiManager = build.mContext?.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager
+        mWifiManager =
+            build.mContext?.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager
         mBuild = build
         build.mContext?.let {
             registerReceiver(it)
@@ -71,7 +74,7 @@ class WifiHelp {
 
     fun connect(wifiData: WifiData, isSaveType: Boolean) {
         countTime()
-        connectNew(wifiData, "",isSaveType)
+        connectNew(wifiData, "", isSaveType)
     }
 
     @SuppressLint("MissingPermission")
@@ -98,6 +101,7 @@ class WifiHelp {
                 WiFiPwdType.ESS -> {
                     wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
                 }
+
                 WiFiPwdType.WAP -> {
                     wifiConfiguration.preSharedKey = addQuotationMarks(password)
                     wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN)
@@ -110,12 +114,14 @@ class WifiHelp {
                     wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA)
                     wifiConfiguration.status = WifiConfiguration.Status.ENABLED
                 }
+
                 WiFiPwdType.WEP -> {
                     wifiConfiguration.wepKeys[0] = addQuotationMarks(password)
                     wifiConfiguration.wepTxKeyIndex = 0;
                     wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                     wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                 }
+
                 WiFiPwdType.WPA2_EAP -> {
                     //不支持企业WiFi密码
                 }
@@ -186,7 +192,6 @@ class WifiHelp {
     }
 
 
-
     /**
      * 断开当前连接的网络
      */
@@ -240,11 +245,15 @@ class WifiHelp {
                 // mWifiManager.reconnect()
             }
         }
+        mapWifi.forEach { (s, i) ->
+            mapWifi[s]  = 0
+        }
+
+        mBuild?.mNetworkStateChangedListener?.invoke(NetworkInfo.DetailedState.FAILED)
     }
 
     fun removeSaveSSIDWifi(ssid: String) {
         val wifiConfigurationList = mWifiManager.configuredNetworks
-        mBuild?.mNetworkStateChangedListener?.invoke(NetworkInfo.DetailedState.FAILED)
         for (item in wifiConfigurationList) {
             if (item.SSID == addQuotationMarks(ssid)) {
                 mWifiManager.disconnect()
@@ -253,6 +262,11 @@ class WifiHelp {
                 // mWifiManager.reconnect()
             }
         }
+        mapWifi.forEach { (s, i) ->
+            mapWifi[s]  = 0
+        }
+        mBuild?.mNetworkStateChangedListener?.invoke(NetworkInfo.DetailedState.FAILED)
+
     }
 
     @SuppressLint("MissingPermission")
@@ -352,7 +366,7 @@ class WifiHelp {
             //在频繁的切换WiFi与重连WiFi底层会返回 unknown ssid, 这里将其排除掉
             return null
         }
-        if (getStringId(wifiInfo.ipAddress)!="0.0.0.0"){
+        if (getStringId(wifiInfo.ipAddress) != "0.0.0.0") {
             //处理重连问题有时候会重连 已成功num会往上涨 估要重新设置为0
             mapWifi[removeQuotationMarks(wifiInfo.ssid)] = 0
         }
@@ -582,26 +596,29 @@ class WifiHelp {
                     mBuild?.mErrorAuthenticating?.invoke("failed to connect")
 
 //                    println("failed to connect :"+)
-                    if (currWifiSSID==""){
-                        return
-                    }
-                    if (mapWifi[currWifiSSID] != null) {
-                        mapWifi[currWifiSSID] = mapWifi[currWifiSSID]!! +1
-                    }else{
-                        mapWifi[currWifiSSID] = 0
-                    }
-                    //判断重连次数删除wifi
-                    if (mapWifi[currWifiSSID]!!>8){
-                        mapWifi[currWifiSSID] = 0
-                        removeSaveSSIDWifi(currWifiSSID)
-                        return
+                    if (wifiData!=null&&wifiData?.ssid==currWifiSSID){
+                        if (currWifiSSID==""){
+                            return
+                        }
+                        if (mapWifi[currWifiSSID] != null) {
+                            Log.e("TAG WIFI","currWifiSSID :"+currWifiSSID+ "  次数:"+mapWifi[currWifiSSID])
+                            mapWifi[currWifiSSID] = mapWifi[currWifiSSID]!! +1
+                        }else{
+                            mapWifi[currWifiSSID] = 0
+                        }
+                        //判断重连次数删除wifi
+                        if (mapWifi[currWifiSSID]!!>6){
+                            mapWifi[currWifiSSID] = 0
+                            removeSaveSSIDWifi(currWifiSSID)
+                            return
+                        }
                     }
                     /**
                      * 之前由于save连接也会走这个回调,固然添加isSavePwd判断不走删除WiFi,
                      * 当第一次连接的时候,会导致密码正确,但isSavaPwd 返回false,固加多个password 不为空的情况下,也不清除
                      * */
                     currWifiData?.apply {
-                        if (this.isSavePwd||this.password.isNotEmpty()) {
+                        if (this.isSavePwd || this.password.isNotEmpty()) {
                         } else {
                             removeSaveWifi(this)
                         }
@@ -609,7 +626,7 @@ class WifiHelp {
                     }
                 }
 
-                if (networkInfo.detailedState == NetworkInfo.DetailedState.BLOCKED){
+                if (networkInfo.detailedState == NetworkInfo.DetailedState.BLOCKED) {
                 }
                 if (networkInfo.detailedState == NetworkInfo.DetailedState.FAILED) {
                     mBuild?.mErrorAuthenticating?.invoke("failed to connect")
@@ -663,10 +680,10 @@ class WifiHelp {
         } else null
     }
 
-    fun isNetworkConnected (context: Context) :Boolean{
+    fun isNetworkConnected(context: Context): Boolean {
         var connectivityManager =
             context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
-        var activeNetworkInfo  = connectivityManager.activeNetworkInfo;
+        var activeNetworkInfo = connectivityManager.activeNetworkInfo;
         return activeNetworkInfo != null && activeNetworkInfo.isConnected;
     }
 
@@ -725,9 +742,20 @@ class WifiHelp {
         }
         return null
     }
+
+    fun setConnectWifiDate(wifiData: WifiData) {
+        this.wifiData = wifiData
+    }
+
+    fun getConnectWifiDate(): WifiData? {
+        return this.wifiData
+    }
+
 }
 
 
+
+
 /**
  * wifi数据
  */