LanguageActivity.kt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package com.develop.setting.ui
  2. import android.annotation.SuppressLint
  3. import android.app.backup.BackupManager
  4. import android.content.Context
  5. import android.content.res.Configuration
  6. import android.os.Bundle
  7. import android.os.PowerManager
  8. import android.view.LayoutInflater
  9. import android.view.View
  10. import android.widget.ImageView
  11. import android.widget.RelativeLayout
  12. import androidx.appcompat.widget.AppCompatTextView
  13. import com.develop.base.ext.load
  14. import com.alibaba.android.arouter.facade.annotation.Route
  15. import com.blankj.utilcode.util.LanguageUtils
  16. import com.develop.base.ext.getSN
  17. import com.develop.base.ext.isBrand011A
  18. import com.develop.base.ext.isBrand036I
  19. import com.develop.base.ext.isBrand054A
  20. import com.develop.base.ext.isBrand062
  21. import com.develop.base.ext.isNightTheme
  22. import com.develop.base.ext.navigateTo
  23. import com.develop.base.ext.resId2Dimension
  24. import com.develop.base.ext.setGone
  25. import com.develop.base.ext.setVisible
  26. import com.develop.base.ext.updateText
  27. import com.develop.base.mvvm.BaseBindingActivity
  28. import com.develop.base.util.MMkvUtils
  29. import com.develop.base.util.SerialportUtils
  30. import com.develop.common.data_repo.db.DataFactory
  31. import com.develop.common.data_repo.db.LanguageModel
  32. import com.develop.common.event.FinishAtyEvent
  33. import com.develop.common.router.Screens
  34. import com.develop.common.tag.*
  35. import com.develop.common.dialog.CancelConfirmDialog
  36. import com.develop.common.utils.ConfigUtils
  37. import com.develop.setting.R
  38. import com.develop.setting.databinding.ActivityLanguageBinding
  39. import com.drake.brv.utils.bindingAdapter
  40. import com.drake.brv.utils.linear
  41. import com.drake.brv.utils.models
  42. import com.drake.brv.utils.setup
  43. import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
  44. import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
  45. import org.greenrobot.eventbus.Subscribe
  46. import java.lang.reflect.Method
  47. import java.util.*
  48. @Route(path = Screens.Setting.LANGUAGE)
  49. class LanguageActivity : BaseBindingActivity<ActivityLanguageBinding>() {
  50. private val cancelConfirmDialog by lazy {
  51. CancelConfirmDialog()
  52. }
  53. private var selectedPosition = -1
  54. private var isLanguageTip = false
  55. private var isFromSetting = false
  56. private var language :String? = ""
  57. private var is062 = false
  58. private var is011 = false
  59. override fun createViewBinding(inflater: LayoutInflater): ActivityLanguageBinding {
  60. return ActivityLanguageBinding.inflate(inflater)
  61. }
  62. override fun onCreate(savedInstanceState: Bundle?) {
  63. super.onCreate(savedInstanceState)
  64. language = MMkvUtils.getString(CURRENT_LANGUAGE)
  65. is062 = isBrand062()
  66. is011 = isBrand011A()
  67. if (isBrand036I()) {
  68. binding.ivLogo.setVisible()
  69. binding.ivBanner.setBackgroundResource(com.develop.common.R.drawable.guide_bg_036i)
  70. }else if (isBrand054A()) {
  71. binding.ivLogo.setGone()
  72. binding.ivBanner.setBackgroundResource(com.develop.common.R.drawable.account_background_054a)
  73. }else if (is062) {
  74. binding.ivLogo.setGone()
  75. binding.ivBanner.setBackgroundResource(com.develop.common.R.drawable.account_background_062a)
  76. }else if (getSN().startsWith("045")){
  77. binding.ivLogo.setGone()
  78. binding.ivBanner.setBackgroundResource(com.develop.common.R.drawable.account_background_045a)
  79. }
  80. binding.ivLanguageClose.setVisible()
  81. binding.ivLanguageClose.setImageResource( com.develop.common.R.drawable.ic_close)
  82. isFromSetting = intent.extras?.getBoolean(LANGUAGE_FROM_SETTING) ?: false
  83. if (isFromSetting) {
  84. binding.apply {
  85. ivBanner.visibility = View.GONE
  86. tvLanguage.visibility = View.GONE
  87. binding.ivLogo.setGone()
  88. // ivLogo.visibility = View.GONE
  89. ivLanguageClose.visibility = View.VISIBLE
  90. tvTitle.visibility = View.VISIBLE
  91. ivLanguageClose.setOnClickListener {
  92. finish()
  93. }
  94. tvTitle.updateText(getString(com.develop.common.R.string.language))
  95. // val params = languageRecycle.layoutParams as RelativeLayout.LayoutParams
  96. // params.topMargin = dp81
  97. // params.marginStart = dp310
  98. // params.marginEnd = dp310
  99. // languageRecycle.layoutParams = params
  100. // val nextParam = tvNext.layoutParams as RelativeLayout.LayoutParams
  101. // nextParam.marginStart = dp310
  102. // nextParam.marginEnd = dp310
  103. // tvNext.layoutParams = nextParam
  104. tvNext.updateText(getString(com.develop.common.R.string.confirm))
  105. }
  106. }
  107. var list = DataFactory.createLanguageDataV2(resources)
  108. binding.languageRecycle.apply {
  109. linear()
  110. setup {
  111. addType<ConfigUtils.Lang>(R.layout.item_language_view)
  112. onBind {
  113. val ivIcon = findView<ImageView>(R.id.ic_icon)
  114. val tvTitle = findView<AppCompatTextView>(R.id.tv_title)
  115. val cardView = findView<RelativeLayout>(R.id.content_layout)
  116. if (selectedPosition == absoluteAdapterPosition) {
  117. if (is062){
  118. tvTitle.setTextColor(context.resources.getColor(com.develop.common.R.color.color_5887DA))
  119. cardView.setBackgroundResource(com.develop.common.R.drawable.shape_44px5887da_stoke)
  120. }else if (is011){
  121. tvTitle.setTextColor(context.resources.getColor(com.develop.common.R.color.color_C10A27))
  122. cardView.setBackgroundResource(com.develop.common.R.drawable.shape_44pxc10a27_stoke)
  123. }else{
  124. tvTitle.setTextColor(context.resources.getColor(com.develop.common.R.color.color_EE8F08))
  125. cardView.setBackgroundResource(com.develop.common.R.drawable.shape_44pxfff6e9_stoke)
  126. }
  127. } else {
  128. tvTitle.setTextColor(context.resources.getColor(com.develop.common.R.color.color_1A1A1A))
  129. cardView.setBackgroundResource(com.develop.common.R.drawable.shape_44pxffffff_stoke)
  130. }
  131. val model = getModel<ConfigUtils.Lang>()
  132. ivIcon.load(context.resources.getIdentifier(model.icon,"drawable","com.develop.foodcooking"))
  133. model.showName?.let { it1 -> tvTitle.updateText(it1) }
  134. }
  135. R.id.content_layout.onClick {
  136. updateSelectionPos(absoluteAdapterPosition)
  137. }
  138. }
  139. }.models =list
  140. selectedPosition = getSelectedPos()
  141. cancelConfirmDialog.onDialogClickListener =
  142. object : CancelConfirmDialog.OnDialogClickListener {
  143. override fun onConfirm() {
  144. if (isLanguageTip) {
  145. updateLanguage()
  146. // navigateTo(Screens.Main.ENTRANCE_CHOSEN)
  147. // finish()
  148. if (!isNightTheme() && !isBrand036I()) {
  149. val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
  150. pm.reboot("")
  151. } else if (isFromSetting) {
  152. finish()
  153. }
  154. }
  155. }
  156. override fun onCancel() {
  157. }
  158. }
  159. binding.tvNext.setOnClickListener {
  160. if (CofarSDK.devInfo().status == DevStatus.RUNNING.toInt()) {
  161. isLanguageTip = false
  162. cancelConfirmDialog.title = getString(com.develop.common.R.string.change_lang_tips)
  163. cancelConfirmDialog.apply {
  164. showCancel = false
  165. showDialog(supportFragmentManager, "confirm")
  166. }
  167. return@setOnClickListener
  168. }
  169. if (isFromSetting) {
  170. //相同语言的时候不可点击
  171. if (language ==list[selectedPosition].value){
  172. finish()
  173. return@setOnClickListener
  174. }
  175. if (isNightTheme() || isBrand036I()) {
  176. if (binding.languageRecycle.models?.size!! > 1) {
  177. isLanguageTip = true
  178. cancelConfirmDialog.title =
  179. getString(com.develop.common.R.string.change_lang_tips2)
  180. cancelConfirmDialog.showDialog(supportFragmentManager, "tips2")
  181. } else {
  182. finish()
  183. }
  184. } else {
  185. if (binding.languageRecycle.models?.size!! > 1) {
  186. isLanguageTip = true
  187. cancelConfirmDialog.title =
  188. getString(com.develop.common.R.string.change_lang_tips2)
  189. cancelConfirmDialog.showDialog(supportFragmentManager, "tips2")
  190. } else {
  191. finish()
  192. }
  193. }
  194. } else {
  195. updateLanguage()
  196. navigateTo(Screens.Setting.POLICY)
  197. }
  198. }
  199. }
  200. private fun updateSelectionPos(pos: Int) {
  201. val tempPosition = selectedPosition
  202. selectedPosition = pos
  203. binding.languageRecycle.bindingAdapter.apply {
  204. notifyItemChanged(tempPosition)
  205. notifyItemChanged(selectedPosition)
  206. }
  207. }
  208. private fun updateLanguage() {
  209. var index = 0
  210. index = selectedPosition
  211. if(selectedPosition == -1){
  212. index = 0
  213. }
  214. binding.languageRecycle.models?.get(index)?.apply {
  215. var l = this as ConfigUtils.Lang;
  216. MMkvUtils.save(CURRENT_LANGUAGE, l.value)
  217. if (isFromSetting){
  218. //系统变化语言
  219. updateLanguage(Locale(l.value.lowercase(),l.value.uppercase()))
  220. }
  221. LanguageUtils.applyLanguage(Locale(l.value.lowercase(),l.value.uppercase()))
  222. }
  223. }
  224. private fun getSelectedPos(): Int {
  225. for (i in binding.languageRecycle.models?.indices!!) {
  226. var l = binding.languageRecycle.models!![i] as ConfigUtils.Lang;
  227. if (language == l.value) {
  228. selectedPosition = i
  229. }
  230. }
  231. if(selectedPosition == -1){
  232. selectedPosition = 0
  233. }
  234. return selectedPosition
  235. }
  236. @Subscribe
  237. fun onFinishAtyEvent(event: FinishAtyEvent) {
  238. finish()
  239. }
  240. override fun onDestroy() {
  241. super.onDestroy()
  242. cancelConfirmDialog.removeSelf()
  243. }
  244. companion object {
  245. var dp81 = (com.develop.common.R.dimen.convert_81px).resId2Dimension().toInt()
  246. var dp310 = (com.develop.common.R.dimen.convert_310px).resId2Dimension().toInt()
  247. var dp6 = (com.develop.common.R.dimen.convert_6px).resId2Dimension()
  248. }
  249. @SuppressLint("PrivateApi", "DiscouragedPrivateApi")
  250. fun updateLanguage(locale: Locale) {
  251. try {
  252. val objIActMag: Any
  253. val clzIActMag = Class.forName("android.app.IActivityManager")
  254. val clzActMagNative = Class
  255. .forName("android.app.ActivityManagerNative")
  256. //amn = ActivityManagerNative.getDefault();
  257. val `mtdActMagNative$getDefault`: Method = clzActMagNative
  258. .getDeclaredMethod("getDefault")
  259. objIActMag = `mtdActMagNative$getDefault`.invoke(clzActMagNative)!!
  260. // objIActMag = amn.getConfiguration();
  261. val `mtdIActMag$getConfiguration`: Method = clzIActMag
  262. .getDeclaredMethod("getConfiguration")
  263. val config: Configuration = `mtdIActMag$getConfiguration`
  264. .invoke(objIActMag) as Configuration
  265. // set the locale to the new value
  266. config.locale = locale
  267. //持久化 config.userSetLocale = true;
  268. val clzConfig = Class
  269. .forName("android.content.res.Configuration")
  270. val userSetLocale = clzConfig
  271. .getField("userSetLocale")
  272. userSetLocale[config] = true
  273. // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION
  274. // 会重新调用 onCreate();
  275. val clzParams = arrayOf<Class<*>>(
  276. Configuration::class.java
  277. )
  278. // objIActMag.updateConfiguration(config);
  279. val `mtdIActMag$updateConfiguration`: Method = clzIActMag
  280. .getDeclaredMethod("updateConfiguration", *clzParams)
  281. `mtdIActMag$updateConfiguration`.invoke(objIActMag, config)
  282. BackupManager.dataChanged("com.android.providers.settings")
  283. } catch (e: Exception) {
  284. e.printStackTrace()
  285. }
  286. }
  287. }