SettingMainActivity.kt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. package com.develop.setting.ui
  2. import android.annotation.SuppressLint
  3. import android.content.Intent
  4. import android.graphics.Rect
  5. import android.net.NetworkInfo
  6. import android.os.Bundle
  7. import android.os.CountDownTimer
  8. import android.os.Handler
  9. import android.util.Log
  10. import android.view.LayoutInflater
  11. import android.view.View
  12. import android.widget.ImageView
  13. import androidx.appcompat.widget.AppCompatTextView
  14. import androidx.recyclerview.widget.RecyclerView
  15. import com.alibaba.android.arouter.facade.annotation.Route
  16. import com.blankj.utilcode.util.ToastUtils
  17. import com.develop.base.ext.getCobotmix
  18. import com.develop.base.ext.getGoneLogin
  19. import com.develop.base.ext.getGoneTuYa
  20. import com.develop.base.ext.getSN
  21. import com.develop.base.ext.navigateTo
  22. import com.develop.base.ext.resId2Dimension
  23. import com.develop.base.ext.setVisible
  24. import com.develop.base.ext.src
  25. import com.develop.base.ext.updateText
  26. import com.develop.base.manager.WifiHelp
  27. import com.develop.base.util.FileKit
  28. import com.develop.base.util.MMkvUtils
  29. import com.develop.base.util.TopResumedAtyHolder
  30. import com.develop.common.data_repo.db.DataFactory
  31. import com.develop.common.data_repo.db.SettingModel
  32. import com.develop.common.data_repo.db.SettingType
  33. import com.develop.common.data_repo.net.Api
  34. import com.develop.common.data_repo.net.model.request.DeviceInfoBody
  35. import com.develop.common.data_repo.net.model.response.DevInfoResult
  36. import com.develop.common.dialog.CancelConfirmImageDialog
  37. import com.develop.common.dialog.PotCoverUnlockedDialog
  38. import com.develop.common.dialog.TuyaLoadingDialog
  39. import com.develop.common.dialog.TuyaRequestDialog
  40. import com.develop.common.router.Screens
  41. import com.develop.common.tag.ISTUYA
  42. import com.develop.common.tag.LANGUAGE_FROM_SETTING
  43. import com.develop.common.tag.WIFI_FROM_MAIN_SETTING
  44. import com.develop.common.ui.CommonBindingActivity
  45. import com.develop.common.utils.ConfigUtils
  46. import com.develop.common.utils.TuyaUtils
  47. import com.develop.setting.R
  48. import com.develop.setting.databinding.ActivitySettingMainBinding
  49. import com.drake.brv.utils.bindingAdapter
  50. import com.drake.brv.utils.grid
  51. import com.drake.brv.utils.models
  52. import com.drake.brv.utils.setup
  53. import com.drake.net.Post
  54. import com.drake.net.utils.scopeNetLife
  55. import com.google.gson.Gson
  56. import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
  57. import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
  58. @Route(path = Screens.Setting.MAIN_SETTING)
  59. class SettingMainActivity : CommonBindingActivity<ActivitySettingMainBinding>() {
  60. private var dataList = mutableListOf<SettingModel>()
  61. private var isTuya = false
  62. var exitTime: Long = 0 // 退出时间
  63. private var onIsResume = true
  64. private var isNetwork = false
  65. private var sn = getSN()
  66. private var isNetWordLoading = false
  67. private val tuyaLoadingDialog = TuyaLoadingDialog()
  68. private var tuyaRequestDialog: TuyaRequestDialog? = null
  69. private var timer: CountDownTimer? = null
  70. override fun createViewBinding(inflater: LayoutInflater): ActivitySettingMainBinding {
  71. return ActivitySettingMainBinding.inflate(layoutInflater)
  72. }
  73. ///sn.startsWith("032D") || sn.startsWith("011A") || sn.startsWith("030A")|| sn.startsWith("010F")
  74. override fun onCreate(savedInstanceState: Bundle?) {
  75. super.onCreate(savedInstanceState)
  76. var ctx = this
  77. isTuya = MMkvUtils.getBool(ISTUYA, false)
  78. tuyaRequestDialog = TuyaRequestDialog()
  79. tuyaRequestDialog?.onDialogClickListener =
  80. object : TuyaRequestDialog.OnDialogClickListener {
  81. override fun onCancel() {
  82. tuyaRequestDialog?.stopImageLoading()
  83. tuyaRequestDialog?.removeSelf()
  84. }
  85. }
  86. // dataList = if (getSN().startsWith("010D")) {
  87. // DataFactory.genSetting010DModesList(resources)
  88. // } else if (getGoneLogin()) {
  89. // DataFactory.genSetting032DModesList(resources)
  90. // } else {
  91. // DataFactory.genSettingModesList(resources)
  92. // }
  93. /**
  94. * 需要隐藏登录 032D 032E 011A 030A 010F 033
  95. * 需要隐藏涂鸦 010D 010F 032D 033 011A 036I 058A 058B
  96. * 需要隐藏亮度 010D
  97. *
  98. * ps:修改了的话,记得同步下面tuya代码
  99. * 也要修改GlobaExt 中的 getGoneTuYa
  100. * */
  101. dataList.clear()
  102. dataList = if (sn.startsWith("010D")||sn.startsWith("010I")) {
  103. DataFactory.genSetting010DModesList(resources)
  104. } else if (sn.startsWith("032D") || sn.startsWith("010F") || sn.startsWith("033")) {
  105. DataFactory.genSetting032DModesList(resources)
  106. } else if (sn.startsWith("011")) {
  107. DataFactory.genSetting011AModesList(resources)
  108. } else if (sn.startsWith("030A")||sn.startsWith("032E")) {
  109. DataFactory.genSetting030AModesList(resources)
  110. } else if (sn.startsWith("036I")|| sn.startsWith("058A") || sn.startsWith("058B")) {
  111. DataFactory.genSetting036IModesList(resources)
  112. } else {
  113. DataFactory.genSettingModesList(resources)
  114. }
  115. if (!getNetwork()) {
  116. dataList.forEach { settingModel ->
  117. if (settingModel.type == SettingType.TUYA) {
  118. settingModel.resId = com.develop.common.R.drawable.ic_setting_tuya_no
  119. }
  120. }
  121. }
  122. binding.tvTitle.updateText(getString(com.develop.common.R.string.settings))
  123. binding.ivClose.setVisible()
  124. binding.ivClose.setImageResource(com.develop.common.R.drawable.ic_close)
  125. binding.ivClose.setOnClickListener {
  126. finish()
  127. }
  128. binding.mainSettingRecycler.apply {
  129. addItemDecoration(object : RecyclerView.ItemDecoration() {
  130. override fun getItemOffsets(
  131. outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
  132. ) {
  133. super.getItemOffsets(outRect, view, parent, state)
  134. outRect.top = dp77
  135. }
  136. })
  137. grid(4)
  138. setup {
  139. addType<SettingModel>(R.layout.item_setting_main_view)
  140. onBind {
  141. val model = getModel<SettingModel>()
  142. findView<AppCompatTextView>(R.id.tv_name).updateText(model.name)
  143. findView<ImageView>(R.id.iv_icon).src = model.resId
  144. }
  145. R.id.iv_icon.onClick {
  146. //由于涂鸦的更新,可能会导致 absoluteAdapterPosition是-1 估在这做-1判断
  147. var pos = absoluteAdapterPosition
  148. if (pos == -1) {
  149. return@onClick
  150. }
  151. when (dataList[absoluteAdapterPosition].type) {
  152. SettingType.BRIGHTNESS -> {
  153. navigateTo(Screens.Setting.BRIGHTNESS)
  154. }
  155. SettingType.SOUND -> {
  156. navigateTo(Screens.Setting.SOUND)
  157. }
  158. SettingType.WIFI -> {
  159. navigateTo(Screens.Setting.WIFI, 200) {
  160. val bundle = Bundle()
  161. bundle.putBoolean(WIFI_FROM_MAIN_SETTING, true)
  162. with(bundle)
  163. }
  164. }
  165. SettingType.USER_ACCOUNT -> {
  166. navigateTo(Screens.Auth.MEMBER) {
  167. withBoolean("enter_from_home", true)
  168. }
  169. }
  170. SettingType.LANGUAGE -> {
  171. navigateTo(Screens.Setting.LANGUAGE) {
  172. val bundle = Bundle()
  173. bundle.putBoolean(LANGUAGE_FROM_SETTING, true)
  174. with(bundle)
  175. }
  176. }
  177. SettingType.ABOUT -> {
  178. navigateTo(Screens.Setting.ABOUT)
  179. }
  180. SettingType.RESTORE_FACTORY_SETTINGS -> {
  181. // //isTuya&&TuyaUtils.qrcodeUrl
  182. // showTuyaRequestDialog()
  183. navigateTo(Screens.Setting.RESTORE)
  184. }
  185. SettingType.STORAGE -> {
  186. openFileManager()
  187. }
  188. SettingType.TUYA -> {
  189. Log.e(
  190. "TAG isTuya",
  191. " SettingType.TUYA SettingType.TUYA SettingType.TUYA"
  192. )
  193. if (CofarSDK.devInfo().status !== DevStatus.STOP.toInt()) {
  194. ToastUtils.showShort(getString(com.develop.common.R.string.change_lang_tips))
  195. } else {
  196. if (getNetwork()) {
  197. Log.e(
  198. "TAG isTuya",
  199. "isNetWordLoading :" + isNetWordLoading + " isTuya: " + isTuya + " TuyaUtils.qrcodeUrl:" + TuyaUtils.qrcodeUrl
  200. )
  201. if (isNetWordLoading) {
  202. Log.e("TAG isTuya", "isNetWordLoadingisNetWordLoading")
  203. showTuyaRequestDialog()
  204. } else if (isTuya && TuyaUtils.qrcodeUrl == "") {
  205. Log.e("TAG isTuya", "isTuyaisTuyaisTuyaisTuya")
  206. //由于涂鸦初始化后,有绑定的话重启设备qrcodeUrl是不返回的,估在这在判断TuyaUtils.init
  207. if (TuyaUtils.init) {
  208. startTuya()
  209. } else {
  210. showTuyaRequestDialog()
  211. }
  212. } else if (!isTuya && getCobotmix()) {
  213. showTuyaLoadingDialog()
  214. } else {
  215. Log.e("TAG TUYA", "TUYATUYATUYATUYATUYATUYATUYA")
  216. startTuya()
  217. }
  218. } else {
  219. navigateTo(Screens.Setting.WIFI, 200) {
  220. val bundle = Bundle()
  221. bundle.putBoolean(WIFI_FROM_MAIN_SETTING, true)
  222. with(bundle)
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }.models = dataList
  231. }
  232. private fun startTuya() {
  233. navigateTo(Screens.Setting.TUYA)
  234. }
  235. private fun showTuyaLoadingDialog() {
  236. tuyaRequestDialog?.removeSelf()
  237. tuyaLoadingDialog.showDialog(supportFragmentManager, "TuyaLoadingDialog")
  238. tuyaLoadingDialog.onDialogClickListener = object : TuyaLoadingDialog.OnDialogClickListener {
  239. override fun onCancel() {
  240. tuyaLoadingDialog.removeSelf();
  241. }
  242. }
  243. }
  244. private fun showTuyaRequestDialog() {
  245. tuyaRequestDialog?.showDialog(supportFragmentManager, "TuyaRequestDialog")
  246. tuyaRequestDialog?.startImageLoading()
  247. countDownTimer()
  248. }
  249. //倒计时的方式一
  250. fun countDownTimer() {
  251. var num = 20
  252. timer = object : CountDownTimer((num + 1) * 1000L, 1000L) {
  253. override fun onTick(millisUntilFinished: Long) {
  254. // YYLogUtils.w("当时计数:" + num)
  255. Log.e("TAG 当时计数", "当时计数 :" + num)
  256. if (num == 0) {
  257. // YYLogUtils.w("重新开始")
  258. // num = 30
  259. } else {
  260. if (!isNetWordLoading) {
  261. downTimeVoid()
  262. }
  263. num--
  264. }
  265. }
  266. override fun onFinish() {
  267. timer?.let { timers ->
  268. timers.cancel()
  269. }
  270. //超时处理,另外弹tuya第二个框
  271. //处理 loading 没有关闭
  272. showTuyaLoadingDialog()
  273. }
  274. }
  275. timer?.start()
  276. }
  277. private fun downTimeVoid() {
  278. if (isTuya) {
  279. if (TuyaUtils.qrcodeUrl != ""||TuyaUtils.init){
  280. tuyaRequestDialog?.removeSelf()
  281. timer?.let { timer ->
  282. timer.cancel()
  283. }
  284. //跳涂鸦页面
  285. startTuya()
  286. }
  287. } else {
  288. timer?.let { timer ->
  289. timer.cancel()
  290. }
  291. tuyaRequestDialog?.removeSelf()
  292. if (getCobotmix()) {
  293. showTuyaLoadingDialog()
  294. } else {
  295. startTuya()
  296. }
  297. }
  298. }
  299. // 打开文件管理器选择文件
  300. private fun openFileManager() {
  301. // 打开文件管理器选择文件
  302. val intent = Intent(Intent.ACTION_GET_CONTENT)
  303. intent.type = "application/zip"
  304. intent.addCategory(Intent.CATEGORY_OPENABLE)
  305. startActivityForResult(intent, 100)
  306. }
  307. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  308. super.onActivityResult(requestCode, resultCode, data)
  309. if (requestCode == 100) {
  310. data?.data?.apply {
  311. val path = FileKit.getPath(this@SettingMainActivity, this)
  312. }
  313. } else if (requestCode == 200) {
  314. onIsResume = false
  315. //处理WiFi页面回来
  316. getDevNetwork(0)
  317. }
  318. }
  319. private fun getNetwork(): Boolean {
  320. return NetworkInfo.DetailedState.CONNECTED == WifiHelp.Build(this@SettingMainActivity)
  321. .build().getDetailedState()
  322. }
  323. private fun getDevTuya() {
  324. if (getNetwork() && !isTuya) {
  325. tuyaList(0)
  326. }
  327. }
  328. //从wifi功能回来方法
  329. private fun getDevNetwork(num: Int) {
  330. //无需获取涂鸦功能,估不网络请求涂鸦
  331. if (getGoneTuYa()) {
  332. return
  333. }
  334. //处理wifi点击回来
  335. if (getNetwork()) {
  336. /**
  337. * 因为有涂鸦功能不需要重新请求,但是会导致涂鸦图标是灰色
  338. * 估在这做多判断处理
  339. * */
  340. if (isTuya) {
  341. dataList.forEach { settingModel ->
  342. if (settingModel.type == SettingType.TUYA) {
  343. settingModel.resId = com.develop.common.R.drawable.ic_setting_tuya
  344. }
  345. }
  346. binding.mainSettingRecycler.bindingAdapter.notifyDataSetChanged()
  347. }
  348. //连接上wifi就调用涂鸦接口
  349. getDevTuya()
  350. } else {
  351. //没有连接wifi处理轮询
  352. if (num < 10) {
  353. //判断是否有网的时候
  354. var nums = num + 1
  355. Handler().postDelayed({ getDevNetwork(nums) }, 500)
  356. return
  357. }
  358. }
  359. }
  360. //网络不行重连3次涂鸦
  361. fun tuyaList(num: Int) {
  362. //代码重连,3次以后,就获取本地
  363. if (num > 3) {
  364. getAppTuya(true)
  365. return
  366. }
  367. this.apply {
  368. isNetWordLoading = true
  369. scopeNetLife {
  370. val result = Post<DevInfoResult>(Api.DEV_INFO) {
  371. body = DeviceInfoBody.genDeviceInfoBody()
  372. }.await()
  373. getInitTuya(result)
  374. }.catch {
  375. var nums = num + 1
  376. Handler().postDelayed({ tuyaList(nums) }, 1000)
  377. }
  378. }
  379. }
  380. //devinfo 数据回调
  381. fun getInitTuya(result: DevInfoResult) {
  382. // var gson = Gson()
  383. // var json = gson.toJson(result)
  384. if (result.tuyaLicense != null) {
  385. result.tuyaLicense?.let {
  386. TuyaUtils.mAk = it.ak.toString().trim()
  387. TuyaUtils.mPid = it.pid.toString().trim()
  388. TuyaUtils.mUid = it.uuid.toString().trim()
  389. TuyaUtils.initSDK(this@SettingMainActivity, "1.0.0")
  390. isTuya = true
  391. isNetWordLoading = false
  392. //涂鸦存值判断有涂鸦功能
  393. MMkvUtils.save(ISTUYA, true)
  394. tuyaEvent()
  395. }
  396. } else {
  397. getAppTuya(true)
  398. }
  399. }
  400. //获取本地
  401. fun getAppTuya(isCatch: Boolean) {
  402. var tuyaConfig = ConfigUtils.loadTuyaConfig()
  403. if (tuyaConfig != null) {
  404. TuyaUtils.mAk = tuyaConfig.get("ak")
  405. TuyaUtils.mPid = tuyaConfig.get("pid")
  406. TuyaUtils.mUid = tuyaConfig.get("uid")
  407. TuyaUtils.initSDK(this, "1.0.0")
  408. isTuya = true
  409. //涂鸦存值判断有涂鸦功能
  410. MMkvUtils.save(ISTUYA, true)
  411. } else {
  412. isTuya = false
  413. //涂鸦存值判断没有涂鸦功能
  414. MMkvUtils.save(ISTUYA, false)
  415. }
  416. isNetWordLoading = false
  417. tuyaEvent()
  418. }
  419. @SuppressLint("NotifyDataSetChanged")
  420. fun tuyaEvent() {
  421. /**
  422. * 需要隐藏登录 032D 032E 011A 030A 010F 033
  423. * 需要隐藏涂鸦 010D 010F 032D 033 011A 036I 058A 058B
  424. * 需要隐藏亮度 010D
  425. *
  426. * ps:修改了的话,记得同步下面tuya代码
  427. * 也要修改GlobaExt 中的 getGoneTuYa
  428. * */
  429. dataList = if (sn.startsWith("010D")||sn.startsWith("010I")) {
  430. DataFactory.genSetting010DModesList(resources)
  431. } else if (sn.startsWith("032D") || sn.startsWith("010F") || sn.startsWith("033")) {
  432. DataFactory.genSetting032DModesList(resources)
  433. } else if (sn.startsWith("011")) {
  434. DataFactory.genSetting011AModesList(resources)
  435. } else if (sn.startsWith("030A")||sn.startsWith("032E")) {
  436. DataFactory.genSetting030AModesList(resources)
  437. } else if (sn.startsWith("036I")|| sn.startsWith("058A") || sn.startsWith("058B")) {
  438. DataFactory.genSetting036IModesList(resources)
  439. } else {
  440. DataFactory.genSettingModesList(resources)
  441. }
  442. if (!getNetwork()) {
  443. dataList.forEach { settingModel ->
  444. if (settingModel.type == SettingType.TUYA) {
  445. settingModel.resId = com.develop.common.R.drawable.ic_setting_tuya_no
  446. }
  447. }
  448. }
  449. binding.mainSettingRecycler.bindingAdapter._data = dataList
  450. binding.mainSettingRecycler.bindingAdapter.notifyDataSetChanged()
  451. }
  452. override fun onResume() {
  453. super.onResume()
  454. //避免涂鸦功能不出现估做这个判断
  455. /**
  456. * onIsResume 预防 wifi回来重复刷新
  457. * isTuya 没有tuya初始化的时候
  458. * getGoneTuYa 是否需要涂鸦功能
  459. * **/
  460. if (onIsResume && !isTuya && !getGoneTuYa()) {
  461. getDevTuya()
  462. }
  463. }
  464. override fun onPause() {
  465. super.onPause()
  466. //预防wifi点击回来处理
  467. onIsResume = true
  468. }
  469. companion object {
  470. var dp156 = (com.develop.common.R.dimen.convert_156px).resId2Dimension().toInt()
  471. var dp40 = (com.develop.common.R.dimen.convert_40px).resId2Dimension().toInt()
  472. var dp77 = (com.develop.common.R.dimen.convert_77px).resId2Dimension().toInt()
  473. }
  474. }