package com.develop.setting.ui import android.annotation.SuppressLint import android.app.backup.BackupManager import android.content.Context import android.content.res.Configuration import android.os.Bundle import android.os.PowerManager import android.view.LayoutInflater import android.view.View import android.widget.ImageView import android.widget.RelativeLayout import androidx.appcompat.widget.AppCompatTextView import com.develop.base.ext.load import com.alibaba.android.arouter.facade.annotation.Route import com.blankj.utilcode.util.LanguageUtils import com.develop.base.ext.getSN import com.develop.base.ext.isBrand011A import com.develop.base.ext.isBrand036I import com.develop.base.ext.isBrand054A import com.develop.base.ext.isBrand062 import com.develop.base.ext.isNightTheme import com.develop.base.ext.navigateTo import com.develop.base.ext.resId2Dimension import com.develop.base.ext.setGone import com.develop.base.ext.setVisible import com.develop.base.ext.updateText import com.develop.base.mvvm.BaseBindingActivity import com.develop.base.util.MMkvUtils import com.develop.base.util.SerialportUtils import com.develop.common.data_repo.db.DataFactory import com.develop.common.data_repo.db.LanguageModel import com.develop.common.event.FinishAtyEvent import com.develop.common.router.Screens import com.develop.common.tag.* import com.develop.common.dialog.CancelConfirmDialog import com.develop.common.utils.ConfigUtils import com.develop.setting.R import com.develop.setting.databinding.ActivityLanguageBinding import com.drake.brv.utils.bindingAdapter import com.drake.brv.utils.linear import com.drake.brv.utils.models import com.drake.brv.utils.setup import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK import org.greenrobot.eventbus.Subscribe import java.lang.reflect.Method import java.util.* @Route(path = Screens.Setting.LANGUAGE) class LanguageActivity : BaseBindingActivity() { private val cancelConfirmDialog by lazy { CancelConfirmDialog() } private var selectedPosition = -1 private var isLanguageTip = false private var isFromSetting = false private var language :String? = "" private var is062 = false private var is011 = false override fun createViewBinding(inflater: LayoutInflater): ActivityLanguageBinding { return ActivityLanguageBinding.inflate(inflater) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) language = MMkvUtils.getString(CURRENT_LANGUAGE) is062 = isBrand062() is011 = isBrand011A() if (isBrand036I()) { binding.ivLogo.setVisible() binding.ivBanner.setBackgroundResource(com.develop.common.R.drawable.guide_bg_036i) }else if (isBrand054A()) { binding.ivLogo.setGone() binding.ivBanner.setBackgroundResource(com.develop.common.R.drawable.account_background_054a) }else if (is062) { binding.ivLogo.setGone() binding.ivBanner.setBackgroundResource(com.develop.common.R.drawable.account_background_062a) }else if (getSN().startsWith("045")){ binding.ivLogo.setGone() binding.ivBanner.setBackgroundResource(com.develop.common.R.drawable.account_background_045a) } binding.ivLanguageClose.setVisible() binding.ivLanguageClose.setImageResource( com.develop.common.R.drawable.ic_close) isFromSetting = intent.extras?.getBoolean(LANGUAGE_FROM_SETTING) ?: false if (isFromSetting) { binding.apply { ivBanner.visibility = View.GONE tvLanguage.visibility = View.GONE binding.ivLogo.setGone() // ivLogo.visibility = View.GONE ivLanguageClose.visibility = View.VISIBLE tvTitle.visibility = View.VISIBLE ivLanguageClose.setOnClickListener { finish() } tvTitle.updateText(getString(com.develop.common.R.string.language)) // val params = languageRecycle.layoutParams as RelativeLayout.LayoutParams // params.topMargin = dp81 // params.marginStart = dp310 // params.marginEnd = dp310 // languageRecycle.layoutParams = params // val nextParam = tvNext.layoutParams as RelativeLayout.LayoutParams // nextParam.marginStart = dp310 // nextParam.marginEnd = dp310 // tvNext.layoutParams = nextParam tvNext.updateText(getString(com.develop.common.R.string.confirm)) } } var list = DataFactory.createLanguageDataV2(resources) binding.languageRecycle.apply { linear() setup { addType(R.layout.item_language_view) onBind { val ivIcon = findView(R.id.ic_icon) val tvTitle = findView(R.id.tv_title) val cardView = findView(R.id.content_layout) if (selectedPosition == absoluteAdapterPosition) { if (is062){ tvTitle.setTextColor(context.resources.getColor(com.develop.common.R.color.color_5887DA)) cardView.setBackgroundResource(com.develop.common.R.drawable.shape_44px5887da_stoke) }else if (is011){ tvTitle.setTextColor(context.resources.getColor(com.develop.common.R.color.color_C10A27)) cardView.setBackgroundResource(com.develop.common.R.drawable.shape_44pxc10a27_stoke) }else{ tvTitle.setTextColor(context.resources.getColor(com.develop.common.R.color.color_EE8F08)) cardView.setBackgroundResource(com.develop.common.R.drawable.shape_44pxfff6e9_stoke) } } else { tvTitle.setTextColor(context.resources.getColor(com.develop.common.R.color.color_1A1A1A)) cardView.setBackgroundResource(com.develop.common.R.drawable.shape_44pxffffff_stoke) } val model = getModel() ivIcon.load(context.resources.getIdentifier(model.icon,"drawable","com.develop.foodcooking")) model.showName?.let { it1 -> tvTitle.updateText(it1) } } R.id.content_layout.onClick { updateSelectionPos(absoluteAdapterPosition) } } }.models =list selectedPosition = getSelectedPos() cancelConfirmDialog.onDialogClickListener = object : CancelConfirmDialog.OnDialogClickListener { override fun onConfirm() { if (isLanguageTip) { updateLanguage() // navigateTo(Screens.Main.ENTRANCE_CHOSEN) // finish() if (!isNightTheme() && !isBrand036I()) { val pm = getSystemService(Context.POWER_SERVICE) as PowerManager pm.reboot("") } else if (isFromSetting) { finish() } } } override fun onCancel() { } } binding.tvNext.setOnClickListener { if (CofarSDK.devInfo().status == DevStatus.RUNNING.toInt()) { isLanguageTip = false cancelConfirmDialog.title = getString(com.develop.common.R.string.change_lang_tips) cancelConfirmDialog.apply { showCancel = false showDialog(supportFragmentManager, "confirm") } return@setOnClickListener } if (isFromSetting) { //相同语言的时候不可点击 if (language ==list[selectedPosition].value){ finish() return@setOnClickListener } if (isNightTheme() || isBrand036I()) { if (binding.languageRecycle.models?.size!! > 1) { isLanguageTip = true cancelConfirmDialog.title = getString(com.develop.common.R.string.change_lang_tips2) cancelConfirmDialog.showDialog(supportFragmentManager, "tips2") } else { finish() } } else { if (binding.languageRecycle.models?.size!! > 1) { isLanguageTip = true cancelConfirmDialog.title = getString(com.develop.common.R.string.change_lang_tips2) cancelConfirmDialog.showDialog(supportFragmentManager, "tips2") } else { finish() } } } else { updateLanguage() navigateTo(Screens.Setting.POLICY) } } } private fun updateSelectionPos(pos: Int) { val tempPosition = selectedPosition selectedPosition = pos binding.languageRecycle.bindingAdapter.apply { notifyItemChanged(tempPosition) notifyItemChanged(selectedPosition) } } private fun updateLanguage() { var index = 0 index = selectedPosition if(selectedPosition == -1){ index = 0 } binding.languageRecycle.models?.get(index)?.apply { var l = this as ConfigUtils.Lang; MMkvUtils.save(CURRENT_LANGUAGE, l.value) if (isFromSetting){ //系统变化语言 updateLanguage(Locale(l.value.lowercase(),l.value.uppercase())) } LanguageUtils.applyLanguage(Locale(l.value.lowercase(),l.value.uppercase())) } } private fun getSelectedPos(): Int { for (i in binding.languageRecycle.models?.indices!!) { var l = binding.languageRecycle.models!![i] as ConfigUtils.Lang; if (language == l.value) { selectedPosition = i } } if(selectedPosition == -1){ selectedPosition = 0 } return selectedPosition } @Subscribe fun onFinishAtyEvent(event: FinishAtyEvent) { finish() } override fun onDestroy() { super.onDestroy() cancelConfirmDialog.removeSelf() } companion object { var dp81 = (com.develop.common.R.dimen.convert_81px).resId2Dimension().toInt() var dp310 = (com.develop.common.R.dimen.convert_310px).resId2Dimension().toInt() var dp6 = (com.develop.common.R.dimen.convert_6px).resId2Dimension() } @SuppressLint("PrivateApi", "DiscouragedPrivateApi") fun updateLanguage(locale: Locale) { try { val objIActMag: Any val clzIActMag = Class.forName("android.app.IActivityManager") val clzActMagNative = Class .forName("android.app.ActivityManagerNative") //amn = ActivityManagerNative.getDefault(); val `mtdActMagNative$getDefault`: Method = clzActMagNative .getDeclaredMethod("getDefault") objIActMag = `mtdActMagNative$getDefault`.invoke(clzActMagNative)!! // objIActMag = amn.getConfiguration(); val `mtdIActMag$getConfiguration`: Method = clzIActMag .getDeclaredMethod("getConfiguration") val config: Configuration = `mtdIActMag$getConfiguration` .invoke(objIActMag) as Configuration // set the locale to the new value config.locale = locale //持久化 config.userSetLocale = true; val clzConfig = Class .forName("android.content.res.Configuration") val userSetLocale = clzConfig .getField("userSetLocale") userSetLocale[config] = true // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION // 会重新调用 onCreate(); val clzParams = arrayOf>( Configuration::class.java ) // objIActMag.updateConfiguration(config); val `mtdIActMag$updateConfiguration`: Method = clzIActMag .getDeclaredMethod("updateConfiguration", *clzParams) `mtdIActMag$updateConfiguration`.invoke(objIActMag, config) BackupManager.dataChanged("com.android.providers.settings") } catch (e: Exception) { e.printStackTrace() } } }