123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554 |
- package com.develop.setting.ui
- import android.annotation.SuppressLint
- import android.content.Intent
- import android.graphics.Rect
- import android.net.NetworkInfo
- import android.os.Bundle
- import android.os.CountDownTimer
- import android.os.Handler
- import android.util.Log
- import android.view.LayoutInflater
- import android.view.View
- import android.widget.ImageView
- import androidx.appcompat.widget.AppCompatTextView
- import androidx.recyclerview.widget.RecyclerView
- import com.alibaba.android.arouter.facade.annotation.Route
- import com.blankj.utilcode.util.ToastUtils
- import com.develop.base.ext.getCobotmix
- import com.develop.base.ext.getGoneLogin
- import com.develop.base.ext.getGoneTuYa
- import com.develop.base.ext.getSN
- import com.develop.base.ext.navigateTo
- import com.develop.base.ext.resId2Dimension
- import com.develop.base.ext.setVisible
- import com.develop.base.ext.src
- import com.develop.base.ext.updateText
- import com.develop.base.manager.WifiHelp
- import com.develop.base.util.FileKit
- import com.develop.base.util.MMkvUtils
- import com.develop.base.util.TopResumedAtyHolder
- import com.develop.common.data_repo.db.DataFactory
- import com.develop.common.data_repo.db.SettingModel
- import com.develop.common.data_repo.db.SettingType
- 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.CancelConfirmImageDialog
- import com.develop.common.dialog.PotCoverUnlockedDialog
- import com.develop.common.dialog.TuyaLoadingDialog
- import com.develop.common.dialog.TuyaRequestDialog
- import com.develop.common.router.Screens
- import com.develop.common.tag.ISTUYA
- import com.develop.common.tag.LANGUAGE_FROM_SETTING
- import com.develop.common.tag.WIFI_FROM_MAIN_SETTING
- import com.develop.common.ui.CommonBindingActivity
- import com.develop.common.utils.ConfigUtils
- import com.develop.common.utils.TuyaUtils
- import com.develop.setting.R
- import com.develop.setting.databinding.ActivitySettingMainBinding
- import com.drake.brv.utils.bindingAdapter
- import com.drake.brv.utils.grid
- import com.drake.brv.utils.models
- import com.drake.brv.utils.setup
- import com.drake.net.Post
- import com.drake.net.utils.scopeNetLife
- import com.google.gson.Gson
- import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
- import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
- @Route(path = Screens.Setting.MAIN_SETTING)
- class SettingMainActivity : CommonBindingActivity<ActivitySettingMainBinding>() {
- private var dataList = mutableListOf<SettingModel>()
- private var isTuya = false
- var exitTime: Long = 0 // 退出时间
- private var onIsResume = true
- private var isNetwork = false
- private var sn = getSN()
- private var isNetWordLoading = false
- private val tuyaLoadingDialog = TuyaLoadingDialog()
- private var tuyaRequestDialog: TuyaRequestDialog? = null
- private var timer: CountDownTimer? = null
- override fun createViewBinding(inflater: LayoutInflater): ActivitySettingMainBinding {
- return ActivitySettingMainBinding.inflate(layoutInflater)
- }
- ///sn.startsWith("032D") || sn.startsWith("011A") || sn.startsWith("030A")|| sn.startsWith("010F")
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- var ctx = this
- isTuya = MMkvUtils.getBool(ISTUYA, false)
- tuyaRequestDialog = TuyaRequestDialog()
- tuyaRequestDialog?.onDialogClickListener =
- object : TuyaRequestDialog.OnDialogClickListener {
- override fun onCancel() {
- tuyaRequestDialog?.stopImageLoading()
- tuyaRequestDialog?.removeSelf()
- }
- }
- // dataList = if (getSN().startsWith("010D")) {
- // DataFactory.genSetting010DModesList(resources)
- // } else if (getGoneLogin()) {
- // DataFactory.genSetting032DModesList(resources)
- // } else {
- // DataFactory.genSettingModesList(resources)
- // }
- /**
- * 需要隐藏登录 032D 032E 011A 030A 010F 033
- * 需要隐藏涂鸦 010D 010F 032D 033 011A 036I 058A 058B
- * 需要隐藏亮度 010D
- *
- * ps:修改了的话,记得同步下面tuya代码
- * 也要修改GlobaExt 中的 getGoneTuYa
- * */
- dataList.clear()
- dataList = if (sn.startsWith("010D")||sn.startsWith("010I")) {
- DataFactory.genSetting010DModesList(resources)
- } else if (sn.startsWith("032D") || sn.startsWith("010F") || sn.startsWith("033")) {
- DataFactory.genSetting032DModesList(resources)
- } else if (sn.startsWith("011")) {
- DataFactory.genSetting011AModesList(resources)
- } else if (sn.startsWith("030A")||sn.startsWith("032E")) {
- DataFactory.genSetting030AModesList(resources)
- } else if (sn.startsWith("036I")|| sn.startsWith("058A") || sn.startsWith("058B")) {
- DataFactory.genSetting036IModesList(resources)
- } else {
- DataFactory.genSettingModesList(resources)
- }
- if (!getNetwork()) {
- dataList.forEach { settingModel ->
- if (settingModel.type == SettingType.TUYA) {
- settingModel.resId = com.develop.common.R.drawable.ic_setting_tuya_no
- }
- }
- }
- binding.tvTitle.updateText(getString(com.develop.common.R.string.settings))
- binding.ivClose.setVisible()
- binding.ivClose.setImageResource(com.develop.common.R.drawable.ic_close)
- binding.ivClose.setOnClickListener {
- finish()
- }
- binding.mainSettingRecycler.apply {
- addItemDecoration(object : RecyclerView.ItemDecoration() {
- override fun getItemOffsets(
- outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
- ) {
- super.getItemOffsets(outRect, view, parent, state)
- outRect.top = dp77
- }
- })
- grid(4)
- setup {
- addType<SettingModel>(R.layout.item_setting_main_view)
- onBind {
- val model = getModel<SettingModel>()
- findView<AppCompatTextView>(R.id.tv_name).updateText(model.name)
- findView<ImageView>(R.id.iv_icon).src = model.resId
- }
- R.id.iv_icon.onClick {
- //由于涂鸦的更新,可能会导致 absoluteAdapterPosition是-1 估在这做-1判断
- var pos = absoluteAdapterPosition
- if (pos == -1) {
- return@onClick
- }
- when (dataList[absoluteAdapterPosition].type) {
- SettingType.BRIGHTNESS -> {
- navigateTo(Screens.Setting.BRIGHTNESS)
- }
- SettingType.SOUND -> {
- navigateTo(Screens.Setting.SOUND)
- }
- SettingType.WIFI -> {
- navigateTo(Screens.Setting.WIFI, 200) {
- val bundle = Bundle()
- bundle.putBoolean(WIFI_FROM_MAIN_SETTING, true)
- with(bundle)
- }
- }
- SettingType.USER_ACCOUNT -> {
- navigateTo(Screens.Auth.MEMBER) {
- withBoolean("enter_from_home", true)
- }
- }
- SettingType.LANGUAGE -> {
- navigateTo(Screens.Setting.LANGUAGE) {
- val bundle = Bundle()
- bundle.putBoolean(LANGUAGE_FROM_SETTING, true)
- with(bundle)
- }
- }
- SettingType.ABOUT -> {
- navigateTo(Screens.Setting.ABOUT)
- }
- SettingType.RESTORE_FACTORY_SETTINGS -> {
- // //isTuya&&TuyaUtils.qrcodeUrl
- // showTuyaRequestDialog()
- navigateTo(Screens.Setting.RESTORE)
- }
- SettingType.STORAGE -> {
- openFileManager()
- }
- SettingType.TUYA -> {
- Log.e(
- "TAG isTuya",
- " SettingType.TUYA SettingType.TUYA SettingType.TUYA"
- )
- if (CofarSDK.devInfo().status !== DevStatus.STOP.toInt()) {
- ToastUtils.showShort(getString(com.develop.common.R.string.change_lang_tips))
- } else {
- if (getNetwork()) {
- Log.e(
- "TAG isTuya",
- "isNetWordLoading :" + isNetWordLoading + " isTuya: " + isTuya + " TuyaUtils.qrcodeUrl:" + TuyaUtils.qrcodeUrl
- )
- if (isNetWordLoading) {
- Log.e("TAG isTuya", "isNetWordLoadingisNetWordLoading")
- showTuyaRequestDialog()
- } else if (isTuya && TuyaUtils.qrcodeUrl == "") {
- Log.e("TAG isTuya", "isTuyaisTuyaisTuyaisTuya")
- //由于涂鸦初始化后,有绑定的话重启设备qrcodeUrl是不返回的,估在这在判断TuyaUtils.init
- if (TuyaUtils.init) {
- startTuya()
- } else {
- showTuyaRequestDialog()
- }
- } else if (!isTuya && getCobotmix()) {
- showTuyaLoadingDialog()
- } else {
- Log.e("TAG TUYA", "TUYATUYATUYATUYATUYATUYATUYA")
- startTuya()
- }
- } else {
- navigateTo(Screens.Setting.WIFI, 200) {
- val bundle = Bundle()
- bundle.putBoolean(WIFI_FROM_MAIN_SETTING, true)
- with(bundle)
- }
- }
- }
- }
- }
- }
- }
- }.models = dataList
- }
- private fun startTuya() {
- navigateTo(Screens.Setting.TUYA)
- }
- private fun showTuyaLoadingDialog() {
- tuyaRequestDialog?.removeSelf()
- tuyaLoadingDialog.showDialog(supportFragmentManager, "TuyaLoadingDialog")
- tuyaLoadingDialog.onDialogClickListener = object : TuyaLoadingDialog.OnDialogClickListener {
- override fun onCancel() {
- tuyaLoadingDialog.removeSelf();
- }
- }
- }
- private fun showTuyaRequestDialog() {
- tuyaRequestDialog?.showDialog(supportFragmentManager, "TuyaRequestDialog")
- tuyaRequestDialog?.startImageLoading()
- countDownTimer()
- }
- //倒计时的方式一
- fun countDownTimer() {
- var num = 20
- timer = object : CountDownTimer((num + 1) * 1000L, 1000L) {
- override fun onTick(millisUntilFinished: Long) {
- // YYLogUtils.w("当时计数:" + num)
- Log.e("TAG 当时计数", "当时计数 :" + num)
- if (num == 0) {
- // YYLogUtils.w("重新开始")
- // num = 30
- } else {
- if (!isNetWordLoading) {
- downTimeVoid()
- }
- num--
- }
- }
- override fun onFinish() {
- timer?.let { timers ->
- timers.cancel()
- }
- //超时处理,另外弹tuya第二个框
- //处理 loading 没有关闭
- showTuyaLoadingDialog()
- }
- }
- timer?.start()
- }
- private fun downTimeVoid() {
- if (isTuya) {
- if (TuyaUtils.qrcodeUrl != ""||TuyaUtils.init){
- tuyaRequestDialog?.removeSelf()
- timer?.let { timer ->
- timer.cancel()
- }
- //跳涂鸦页面
- startTuya()
- }
- } else {
- timer?.let { timer ->
- timer.cancel()
- }
- tuyaRequestDialog?.removeSelf()
- if (getCobotmix()) {
- showTuyaLoadingDialog()
- } else {
- startTuya()
- }
- }
- }
- // 打开文件管理器选择文件
- private fun openFileManager() {
- // 打开文件管理器选择文件
- val intent = Intent(Intent.ACTION_GET_CONTENT)
- intent.type = "application/zip"
- intent.addCategory(Intent.CATEGORY_OPENABLE)
- startActivityForResult(intent, 100)
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- if (requestCode == 100) {
- data?.data?.apply {
- val path = FileKit.getPath(this@SettingMainActivity, this)
- }
- } else if (requestCode == 200) {
- onIsResume = false
- //处理WiFi页面回来
- getDevNetwork(0)
- }
- }
- private fun getNetwork(): Boolean {
- return NetworkInfo.DetailedState.CONNECTED == WifiHelp.Build(this@SettingMainActivity)
- .build().getDetailedState()
- }
- private fun getDevTuya() {
- if (getNetwork() && !isTuya) {
- tuyaList(0)
- }
- }
- //从wifi功能回来方法
- private fun getDevNetwork(num: Int) {
- //无需获取涂鸦功能,估不网络请求涂鸦
- if (getGoneTuYa()) {
- return
- }
- //处理wifi点击回来
- if (getNetwork()) {
- /**
- * 因为有涂鸦功能不需要重新请求,但是会导致涂鸦图标是灰色
- * 估在这做多判断处理
- * */
- if (isTuya) {
- dataList.forEach { settingModel ->
- if (settingModel.type == SettingType.TUYA) {
- settingModel.resId = com.develop.common.R.drawable.ic_setting_tuya
- }
- }
- binding.mainSettingRecycler.bindingAdapter.notifyDataSetChanged()
- }
- //连接上wifi就调用涂鸦接口
- getDevTuya()
- } else {
- //没有连接wifi处理轮询
- if (num < 10) {
- //判断是否有网的时候
- var nums = num + 1
- Handler().postDelayed({ getDevNetwork(nums) }, 500)
- return
- }
- }
- }
- //网络不行重连3次涂鸦
- fun tuyaList(num: Int) {
- //代码重连,3次以后,就获取本地
- if (num > 3) {
- getAppTuya(true)
- return
- }
- this.apply {
- isNetWordLoading = true
- scopeNetLife {
- val result = Post<DevInfoResult>(Api.DEV_INFO) {
- body = DeviceInfoBody.genDeviceInfoBody()
- }.await()
- getInitTuya(result)
- }.catch {
- var nums = num + 1
- Handler().postDelayed({ tuyaList(nums) }, 1000)
- }
- }
- }
- //devinfo 数据回调
- fun getInitTuya(result: DevInfoResult) {
- // var gson = Gson()
- // var json = gson.toJson(result)
- if (result.tuyaLicense != null) {
- result.tuyaLicense?.let {
- TuyaUtils.mAk = it.ak.toString().trim()
- TuyaUtils.mPid = it.pid.toString().trim()
- TuyaUtils.mUid = it.uuid.toString().trim()
- TuyaUtils.initSDK(this@SettingMainActivity, "1.0.0")
- isTuya = true
- isNetWordLoading = false
- //涂鸦存值判断有涂鸦功能
- MMkvUtils.save(ISTUYA, true)
- tuyaEvent()
- }
- } else {
- getAppTuya(true)
- }
- }
- //获取本地
- fun getAppTuya(isCatch: Boolean) {
- var tuyaConfig = ConfigUtils.loadTuyaConfig()
- if (tuyaConfig != null) {
- TuyaUtils.mAk = tuyaConfig.get("ak")
- TuyaUtils.mPid = tuyaConfig.get("pid")
- TuyaUtils.mUid = tuyaConfig.get("uid")
- TuyaUtils.initSDK(this, "1.0.0")
- isTuya = true
- //涂鸦存值判断有涂鸦功能
- MMkvUtils.save(ISTUYA, true)
- } else {
- isTuya = false
- //涂鸦存值判断没有涂鸦功能
- MMkvUtils.save(ISTUYA, false)
- }
- isNetWordLoading = false
- tuyaEvent()
- }
- @SuppressLint("NotifyDataSetChanged")
- fun tuyaEvent() {
- /**
- * 需要隐藏登录 032D 032E 011A 030A 010F 033
- * 需要隐藏涂鸦 010D 010F 032D 033 011A 036I 058A 058B
- * 需要隐藏亮度 010D
- *
- * ps:修改了的话,记得同步下面tuya代码
- * 也要修改GlobaExt 中的 getGoneTuYa
- * */
- dataList = if (sn.startsWith("010D")||sn.startsWith("010I")) {
- DataFactory.genSetting010DModesList(resources)
- } else if (sn.startsWith("032D") || sn.startsWith("010F") || sn.startsWith("033")) {
- DataFactory.genSetting032DModesList(resources)
- } else if (sn.startsWith("011")) {
- DataFactory.genSetting011AModesList(resources)
- } else if (sn.startsWith("030A")||sn.startsWith("032E")) {
- DataFactory.genSetting030AModesList(resources)
- } else if (sn.startsWith("036I")|| sn.startsWith("058A") || sn.startsWith("058B")) {
- DataFactory.genSetting036IModesList(resources)
- } else {
- DataFactory.genSettingModesList(resources)
- }
- if (!getNetwork()) {
- dataList.forEach { settingModel ->
- if (settingModel.type == SettingType.TUYA) {
- settingModel.resId = com.develop.common.R.drawable.ic_setting_tuya_no
- }
- }
- }
- binding.mainSettingRecycler.bindingAdapter._data = dataList
- binding.mainSettingRecycler.bindingAdapter.notifyDataSetChanged()
- }
- override fun onResume() {
- super.onResume()
- //避免涂鸦功能不出现估做这个判断
- /**
- * onIsResume 预防 wifi回来重复刷新
- * isTuya 没有tuya初始化的时候
- * getGoneTuYa 是否需要涂鸦功能
- * **/
- if (onIsResume && !isTuya && !getGoneTuYa()) {
- getDevTuya()
- }
- }
- override fun onPause() {
- super.onPause()
- //预防wifi点击回来处理
- onIsResume = true
- }
- companion object {
- var dp156 = (com.develop.common.R.dimen.convert_156px).resId2Dimension().toInt()
- var dp40 = (com.develop.common.R.dimen.convert_40px).resId2Dimension().toInt()
- var dp77 = (com.develop.common.R.dimen.convert_77px).resId2Dimension().toInt()
- }
- }
|