123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795 |
- package xn.hxp.ui
- import android.app.AlertDialog
- import android.content.Intent
- import android.content.IntentFilter
- import android.graphics.Color
- import android.graphics.drawable.GradientDrawable
- import android.hardware.usb.UsbManager
- import android.os.Bundle
- import android.os.Handler
- import android.os.Looper
- import android.os.Message
- import android.util.Log
- import android.view.KeyEvent
- import android.view.LayoutInflater
- import android.view.View
- import android.widget.EditText
- import androidx.recyclerview.widget.LinearLayoutManager
- import androidx.recyclerview.widget.RecyclerView
- import com.blankj.utilcode.util.ActivityUtils
- import com.blankj.utilcode.util.AppUtils
- import com.bumptech.glide.Glide
- import com.bumptech.glide.load.engine.DiskCacheStrategy
- import com.bumptech.glide.request.RequestOptions
- import xn.hxp.app.ChemicalApp
- import xn.hxp.R
- import xn.hxp.databinding.ActivityMainBinding
- import xn.hxp.receiver.UsbReceiver
- import xn.hxp.ui.adapter.CabinetAdapter
- import xn.hxp.ui.plan.PlanAddActivity
- import xn.hxp.ui.discard.LedgerActivity
- import xn.hxp.ui.discard.WasteChemicalsActivity
- import xn.hxp.ui.inquiry.InquiryActivity
- import xn.hxp.ui.login.FacialCardActivity
- import xn.hxp.ui.login.FacialLoginActivity
- import xn.hxp.ui.login.ScanLoginActivity
- import xn.hxp.ui.login.SwipeActivity
- import xn.hxp.ui.still.ChemicalsAlsoActivity
- import xn.hxp.ui.still.MsdsActivity
- import xn.hxp.ui.uses.UseActivity
- import xn.hxp.ui.uses.WarningEventsActivity
- import xn.hxp.ui.warehousing.ChemicalLabelingActivity
- import xn.hxp.utils.RelativeLayoutDebouncer
- import xn.hxp.utils.SharedPreferencesHelper
- import xn.hxp.utils.TimeUpdater
- import xn.hxp.utils.UiManager
- import xn.hxp.weidith.CustomDialog
- import com.lztek.toolkit.Lztek
- import com.blankj.utilcode.util.LogUtils
- import com.rc.core.ui.activity.RcBaseActivity
- import com.rc.core.util.DeviceUtils
- import com.rc.httpcore.HttpClient
- import com.rc.httpcore.HttpConfig
- import com.rc.httpcore.client.ApiRepository
- import com.rc.httpcore.exception.NetException
- import retrofit2.HttpException
- import xn.hxp.receiver.TimeTickReceiver
- import xn.hxp.utils.PrintTool
- import java.net.ConnectException
- import java.net.SocketTimeoutException
- //首页 - 登录or 未登录
- class MainActivity : RcBaseActivity<ActivityMainBinding>() {
- private lateinit var timeUpdater: TimeUpdater
- private val mAdapter by lazy { CabinetAdapter() }
- private var currentPosition = 0
- private lateinit var recyclerView: RecyclerView
- private var mUsbReceiver: UsbReceiver? = null // 刷卡广播注册
- private var mHandleScanEvent = false //当前是否已经获取过 usb返回的参数
- private var mLztek: Lztek? = null
- private var mDeviceNum: String? = null //设备唯一标识
- private var handler: Handler = Handler(Looper.getMainLooper())
- private lateinit var runnable: Runnable
- private lateinit var faceList: List<String> //人员区间值
- private var mLoginType: String? = null //登录方式
- //退出账号
- private var mCounter = 0
- private var MAX_TIME = 60 //返回时间 秒
- private val WHAT_COUNT_DOWN = 1
- private lateinit var layoutManager: LinearLayoutManager
- override fun createViewBinding() = ActivityMainBinding.inflate(LayoutInflater.from(this))
- override fun onResume() {
- super.onResume()
- if (ChemicalApp.userData != null) {
- //已登录
- viewBinding.inc.userLogin.visibility = View.GONE
- viewBinding.inc.loggedIn.visibility = View.VISIBLE
- viewBinding.inc.tvName.text = ChemicalApp.userData!!.userName
- val imageView = viewBinding.inc.imageName
- // 使用 Glide 加载网络图片
- Glide.with(this)
- .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
- .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
- .into(imageView)
- viewBinding.inc.imgE.setBackgroundResource(R.mipmap.cshrk_dl_tc)
- MAX_TIME = ChemicalApp.confs!!.signOutTime
- mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
- mCountDownHandler.sendEmptyMessage(WHAT_COUNT_DOWN)
- } else {
- viewBinding.inc.loggedIn.visibility = View.GONE
- viewBinding.inc.userLogin.visibility = View.VISIBLE
- viewBinding.inc.imgE.setBackgroundResource(R.mipmap.icon_login_img)
- }
- if (mAdapter.data != null && mAdapter.data.size > 1) {
- // startAutoScroll()
- startUpdating()
- }
- timeUpdater.startUpdating()
- getCabinetIn()
- }
- var timeTickReceiver: TimeTickReceiver = TimeTickReceiver()
- //必须在创建时注册信息
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- PrintTool.INSTANCE
- // 监听分钟广播
- registerReceiver(timeTickReceiver, IntentFilter(Intent.ACTION_TIME_TICK))
- //注册广播
- // EventBus.getDefault().register(this)
- viewBinding.versionName.text = "版本号:${AppUtils.getAppVersionName()}"
- // 创建定时任务
- runnable = object : Runnable {
- override fun run() {
- // 每隔一个小时执行一次操作
- // 这里可以添加你需要执行的代码
- subAddData()
- // 重新启动定时任务
- handler.postDelayed(this, 3600000) // 3600000 毫秒为一小时
- }
- }
- // 第一次启动定时任务
- handler.post(runnable)
- viewBinding.test.setOnClickListener {
- showTextInputDialog("请输入内容") { input ->
- if (input != null) {
- // 处理用户输入的逻辑
- if (input.isNotEmpty()) {
- var pwd = "123" //默认密码123
- try {
- val urlBase = SharedPreferencesHelper.getUrlBase(this)
- if (urlBase != null && urlBase.pwd != null) {
- LogUtils.i("=====本地密码:${urlBase!!.pwd}")
- pwd = urlBase.pwd!!
- }
- } catch (e: Exception) {
- }
- if (input == pwd) {
- UiManager.switcher(this, SettingActivity::class.java)
- } else {
- showToast("密码错误")
- }
- }
- } else {
- // 用户点击了取消按钮
- showToast("用户取消了输入")
- }
- }
- }
- }
- override fun initData() {
- super.initData()
- //获取基础配置
- basicConf()
- }
- //获取刷卡信息
- override fun dispatchKeyEvent(event: KeyEvent): Boolean {
- // mPortScanHelper.dispatchKeyEvent(event)
- return super.dispatchKeyEvent(event)
- }
- override fun onStop() {
- super.onStop()
- LogUtils.i("============onStop")
- }
- //停止 销毁广播传递
- override fun onPause() {
- // mPortScanHelper.onPause()
- LogUtils.i("============onPause")
- try {
- stopUpdating()
- timeUpdater.stopUpdating()
- LogUtils.i("========UI在刷新停止")
- mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
- mCountDownHandler.removeCallbacksAndMessages(null)
- } catch (e: Exception) {
- }
- super.onPause()
- }
- // //必须写这个方法 防止注册失败
- // @Subscribe(threadMode = ThreadMode.MAIN)
- // fun onUpdateEventEvent(event: KeyEvent) {
- //
- // }
- override fun onNewIntent(intent: Intent) {
- super.onNewIntent(intent)
- // registerUsbBroadcast() //注册广播
- }
- override fun initViews(savedInstanceState: Bundle?) {
- super.initViews(savedInstanceState)
- getUserIds()
- val handler = Handler(Looper.getMainLooper())
- timeUpdater = TimeUpdater(handler) { currentTime ->
- viewBinding.inc.nowTime.text = "$currentTime"
- }
- // 启动定时更新
- timeUpdater.startUpdating()
- initAdapter()
- viewBinding.imgLeft.setOnClickListener {
- if (currentPosition > 0) {
- currentPosition--
- recyclerView.smoothScrollToPosition(currentPosition)
- }
- }
- viewBinding.imgRight.setOnClickListener {
- if (currentPosition < mAdapter.data.size - 1) {
- currentPosition++
- recyclerView.smoothScrollToPosition(currentPosition)
- }
- }
- //登录
- RelativeLayoutDebouncer.setDebouncedOnClickListener(viewBinding.inc.userLogin, 1000L) {
- basicConfLog()
- }
- // viewBinding.inc.userLogin.setOnClickListener {
- // basicConfLog()
- // }
- viewBinding.addChemicals.setOnClickListener {
- // UiManager.switcher(this, PlanAddActivity::class.java)
- //新增
- if (ChemicalApp.userData != null) {
- if (ChemicalApp.administrators || ChemicalApp.responsibles) {
- UiManager.switcher(this, PlanAddActivity::class.java)
- } else {
- customDialogView(2, "当前人员无权限")
- }
- } else {
- basicConfLog()
- }
- }
- //待入库 TODO 暂时隐藏
- // viewBinding.reSto.setOnClickListener {
- // if (ChemicalApp.userData != null) {
- // UiManager.switcher(this, WarehousingActivity::class.java)
- // } else {
- // basicConfLog()
- // }
- // }
- //废弃
- viewBinding.reDis.setOnClickListener {
- if (ChemicalApp.userData != null) {
- UiManager.switcher(this, WasteChemicalsActivity::class.java)
- } else {
- basicConfLog()
- }
- }
- //查询
- viewBinding.reInq.setOnClickListener {
- val map = mutableMapOf<String, Any>()
- if (ChemicalApp.userData != null) {
- map["logIn"] = 1
- } else {
- map["logIn"] = 0
- }
- map["cabinetId"] = "a"
- map["doorId"] = "a"
- UiManager.switcher(this, map, InquiryActivity::class.java)
- }
- //领用
- viewBinding.reCla.setOnClickListener {
- if (ChemicalApp.userData != null) {
- UiManager.switcher(this, UseActivity::class.java)
- } else {
- basicConfLog()
- }
- }
- //归还
- viewBinding.reRet.setOnClickListener {
- if (ChemicalApp.userData != null) {
- UiManager.switcher(this, ChemicalsAlsoActivity::class.java)
- } else {
- basicConfLog()
- }
- }
- //电子台账
- viewBinding.rlLedger.setOnClickListener {
- if (ChemicalApp.userData != null) {
- UiManager.switcher(this, LedgerActivity::class.java)
- } else {
- basicConfLog()
- }
- }
- //标签管理
- viewBinding.reLedger.setOnClickListener {
- if (ChemicalApp.userData != null) {
- // UiManager.switcher(this, ElectronicLedgerActivity::class.java)
- //化学品标签
- UiManager.switcher(this, ChemicalLabelingActivity::class.java)
- } else {
- basicConfLog()
- }
- }
- //预警事件
- viewBinding.rlWarning.setOnClickListener {
- if (ChemicalApp.userData != null) {
- UiManager.switcher(this, WarningEventsActivity::class.java)
- } else {
- basicConfLog()
- }
- }
- //演示流程 -MSDS
- viewBinding.rlDem.setOnClickListener {
- //暂时不用演示流程 跳转MSDS
- // UiManager.switcher(this, DemonstrateActivity::class.java)
- UiManager.switcher(this, MsdsActivity::class.java)
- }
- viewBinding.inc.tvOutLogin.setOnClickListener {
- ChemicalApp.userData = null
- // ChemicalApp.subjectId = null
- HttpClient.token = null
- ActivityUtils.startActivity(SplashActivity::class.java)
- LogUtils.d(ActivityUtils.getActivityList())
- }
- }
- /**
- * 查询柜子
- */
- private fun getCabinetIn() {
- showLoading("查询中...")
- val disposable = ApiRepository.getCabinetBySubId(ChemicalApp.subjectId!!)
- .subscribe({ data ->
- dismissLoading()
- mAdapter.setNewInstance(data.toMutableList())
- if (mAdapter.data != null && mAdapter.data.size > 1) {
- // 初始化 Handler 和 Runnable
- handlerBanner = Handler(Looper.getMainLooper())
- updateRunnable = object : Runnable {
- override fun run() {
- // 更新 UI 操作
- val currentPosition = layoutManager.findFirstVisibleItemPosition()
- if (currentPosition != RecyclerView.NO_POSITION) {
- val nextPosition = (currentPosition + 1) % mAdapter.itemCount
- recyclerView.smoothScrollToPosition(nextPosition)
- }
- if (isUpdating) {
- handlerBanner.postDelayed(this, 5000) // 5 秒后再次执行
- }
- }
- }
- startUpdating()
- }
- }, { throwable ->
- dismissLoading()
- // throwableView(throwable)
- throwable.printStackTrace()
- })
- addDisposable(disposable)
- }
- private fun getUserIds() {
- if (ChemicalApp.subjectId != null) {
- showLoading("查询中...")
- val disposable = ApiRepository.getUserIds(ChemicalApp.subjectId!!)
- .subscribe({ data ->
- dismissLoading()
- faceList = data
- }, { throwable ->
- dismissLoading()
- // throwableView(throwable)
- })
- addDisposable(disposable)
- }
- }
- private fun initAdapter() {
- recyclerView = viewBinding.relView
- layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
- recyclerView.layoutManager = layoutManager
- recyclerView.adapter = mAdapter
- recyclerView.setOnTouchListener { _, _ ->
- true
- }
- mAdapter.setOnItemChildClickListener { adapter, view, position ->
- // 在这里处理子View的点击事件
- if (ChemicalApp.userData != null) {
- val map = mutableMapOf<String, Any>()
- map["logIn"] = 1
- when (view.id) {
- R.id.lingOne -> {
- map["cabinetId"] = mAdapter.data[position].cabinetId
- map["doorId"] = mAdapter.data[position].cabinetDoorVoList[0].doorUniqueId
- UiManager.switcher(this, map, InquiryActivity::class.java)
- }
- R.id.lintTwo -> {
- map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[1].cabinetId
- map["doorId"] = mAdapter.data[position].cabinetDoorVoList[1].doorUniqueId
- UiManager.switcher(this, map, InquiryActivity::class.java)
- }
- R.id.lintThree -> {
- map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[2].cabinetId
- map["doorId"] = mAdapter.data[position].cabinetDoorVoList[2].doorUniqueId
- UiManager.switcher(this, map, InquiryActivity::class.java)
- }
- R.id.lintFour -> {
- map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[3].cabinetId
- map["doorId"] = mAdapter.data[position].cabinetDoorVoList[3].doorUniqueId
- UiManager.switcher(this, map, InquiryActivity::class.java)
- }
- R.id.imgViews -> {
- map["cabinetId"] = "a"
- map["doorId"] = "a"
- UiManager.switcher(this, map, InquiryActivity::class.java)
- }
- }
- } else {
- //进行登录
- basicConfLog()
- }
- }
- }
- private lateinit var handlerBanner: Handler
- private lateinit var updateRunnable: Runnable
- private var isUpdating = false
- private fun startUpdating() {
- if (!isUpdating) {
- isUpdating = true
- handlerBanner.post(updateRunnable)
- }
- }
- private fun stopUpdating() {
- if (isUpdating) {
- isUpdating = false
- handlerBanner.removeCallbacks(updateRunnable)
- }
- }
- // private val mPortScanHelper by lazy {
- // PortScanHelper(this, object : OnSerialScanListener {
- // override fun dispatchScanEvent(type: OnSerialScanListener.ScanType, content: String) {
- // if (!mHandleScanEvent) {
- // if (content.isNotBlank()) {
- // LogUtils.i("==========当前usb返回参数$content")
- // mHandleScanEvent = true
- // handleScanEvent(content)
- // }
- //
- // }
- // }
- //
- // })
- // }
- //调用刷卡信息
- private fun handleScanEvent(cont: String) {
- // showLoading("查询中...")
- // val disposable = ApiRepository.querySubInfo()
- // .subscribe({ data ->
- // dismissLoading()
- //
- // }, { throwable ->
- // dismissLoading()
- // showNetError(throwable)
- // throwable.printStackTrace()
- // })
- // addDisposable(disposable)
- }
- //刷卡usb链接
- private fun registerUsbBroadcast() {
- if (null == mUsbReceiver) {
- val filter = IntentFilter().apply {
- addAction(UsbReceiver.ACTION_USB_PERMISSION)
- addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
- addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
- addAction(UsbReceiver.ACTION_USB_STATE) // usb连接状态广播
- }
- mUsbReceiver = UsbReceiver()
- registerReceiver(mUsbReceiver, filter)
- }
- }
- override fun onDestroy() {
- super.onDestroy()
- unregisterReceiver(timeTickReceiver)
- try {
- // 停止定时更新
- timeUpdater.stopUpdating()
- // unregisterReceiver(mUsbReceiver)
- // EventBus.getDefault().unregister(this) //关闭广播
- // 移除未执行的定时任务
- handler.removeCallbacks(runnable)
- handler.removeCallbacksAndMessages(null)
- stopUpdating()
- mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
- mCountDownHandler.removeCallbacksAndMessages(null)
- } catch (e: Exception) {
- }
- }
- private fun subAddData() {
- //数据上报
- mLztek = Lztek.create(this)
- val toUpperCase = mLztek!!.ethMac.toUpperCase()
- mDeviceNum = toUpperCase.replace(":", "")
- val disposable = ApiRepository.monitor(mDeviceNum!!)
- .subscribe({ data ->
- }, { throwable ->
- showNetError(throwable)
- })
- addDisposable(disposable)
- }
- //loginType 登陆方式,1人脸 2刷卡 3扫码 4人脸+刷卡
- //verifyType 双人验证方式 1人脸 2刷卡 3扫码
- private fun basicConf() {
- val disposable = ApiRepository.basicConfig(ChemicalApp.subjectId!!)
- .subscribe({ data ->
- ChemicalApp.confs = data
- mLoginType = data.loginType //登陆方式
- if (data.levelName != null) {
- val result = data.levelName!!.toCharArray().joinToString("\n")
- viewBinding.tvNoName.text = result
- // 创建一个GradientDrawable对象
- val shapes = GradientDrawable()
- // 设置形状为矩形
- shapes.shape = GradientDrawable.RECTANGLE
- // 设置背景色为红色(这里使用颜色资源)
- shapes.setColor(Color.parseColor(data.levelColor))
- shapes.cornerRadius = 5f
- // 将GradientDrawable对象设置为View的背景
- viewBinding.tvNoName.background = shapes
- } else {
- viewBinding.tvNoName.visibility = View.GONE
- }
- viewBinding.subName.text = data.subName
- viewBinding.inc.deptName.text = "${data.deptName}"
- Glide.with(this)
- .load("${HttpConfig.API_BASE_IMG_URL}${data.circularLogo}")
- .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
- .into(viewBinding.inc.circularLogo)
- }, { throwable ->
- showNetError(throwable)
- })
- addDisposable(disposable)
- }
- //loginType 登陆方式,1人脸 2刷卡 3扫码 4人脸+刷卡
- //verifyType 双人验证方式 1人脸 2刷卡 3扫码
- private fun basicConfLog() {
- val disposable = ApiRepository.basicConfig(ChemicalApp.subjectId!!)
- .subscribe({ data ->
- LogUtils.json(data)
- ChemicalApp.confs = data
- mLoginType = data.loginType //登陆方式
- if (mLoginType == null) {
- customDialogView(2, "登录方式配置有误")
- } else {
- if (mLoginType!!.contains("4")) {
- //需要刷卡+人脸
- UiManager.switcher(this, FacialCardActivity::class.java)
- } else {
- val map = mutableMapOf<String, String>()
- //1人脸 2刷卡 3扫码 4人脸+刷卡
- if (mLoginType!!.length == 1) {
- when (mLoginType) {
- "1" -> {
- //人脸
- map["faceList"] = faceList.toString()
- map["mtypes"] = "1"
- UiManager.switcher(this, map, FacialLoginActivity::class.java)
- }
- "2" -> {
- //刷卡
- map["mtypes"] = "1"
- UiManager.switcher(this, map, SwipeActivity::class.java)
- }
- else -> {
- //扫码
- map["mtypes"] = "1"
- UiManager.switcher(this, map, ScanLoginActivity::class.java)
- }
- }
- } else {
- val array = mLoginType!!.split(",").toTypedArray()
- when (array.size) {
- 2 -> {
- //1-2(人脸+刷卡) 1-3(人脸+扫码) 2-3(刷卡+扫码)
- if (mLoginType == "1,2" || mLoginType == "2,1") {
- //人脸+刷卡
- map["faceList"] = faceList.toString()
- map["mtypes"] = "4" //隐藏扫码
- UiManager.switcher(
- this,
- map,
- FacialLoginActivity::class.java
- )
- } else if (mLoginType == "1,3" || mLoginType == "3,1") {
- //人脸+扫码
- map["faceList"] = faceList.toString()
- map["mtypes"] = "5" //隐藏刷卡
- UiManager.switcher(
- this,
- map,
- FacialLoginActivity::class.java
- )
- } else if (mLoginType == "2,3" || mLoginType == "3,2") {
- //刷卡+扫码
- map["mtypes"] = "6" //隐藏人脸
- UiManager.switcher(this, map, SwipeActivity::class.java)
- } else {
- showToast("登录方式$mLoginType")
- }
- }
- else -> {
- //3条
- try {
- map["mtypes"] = "0"
- map["faceList"] = faceList.toString()
- UiManager.switcher(
- this,
- map,
- FacialLoginActivity::class.java
- )
- } catch (e: Exception) {
- map["mtypes"] = "0"
- UiManager.switcher(this, map, SwipeActivity::class.java)
- }
- }
- }
- }
- }
- }
- }, { throwable ->
- showNetError(throwable)
- })
- addDisposable(disposable)
- }
- /**
- * 0 没有图标 1 绿色(成功) 2红色(失败)
- * 失败或者成功的弹框
- */
- private fun customDialogView(types: Int, msg: String) {
- val customDialog = CustomDialog(this, types, msg)
- if (!this.isFinishing && !this.isDestroyed) {
- customDialog.show()
- }
- }
- /**
- * 异常处理
- */
- private fun throwableView(throwable: Throwable) {
- when (throwable) {
- is NetException -> {
- if (throwable.message.isNullOrEmpty()) {
- "接口请求失败(${throwable.code})"
- } else {
- throwable.message!!
- }
- }
- is SocketTimeoutException -> "请求超时,请稍后重试"
- is ConnectException -> "无法连接服务器,请检查网络"
- is HttpException -> "服务器繁忙,请稍后重试"
- else -> null
- }?.let { customDialogView(2, "$it") }
- }
- //只管注销
- private val mCountDownHandler = object : Handler(Looper.getMainLooper()) {
- override fun handleMessage(msg: Message) {
- if (WHAT_COUNT_DOWN == msg.what) {
- val countDown = MAX_TIME - mCounter
- if (isDestroyed) return
- if (countDown <= 0) {
- ChemicalApp.confs = null
- ChemicalApp.subjectId = null
- ChemicalApp.userData = null
- callLogoutApis {
- ActivityUtils.startActivity(SplashActivity::class.java)
- }
- } else {
- sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 1000)
- }
- mCounter++
- }
- }
- }
- override fun onUserInteraction() {
- mCounter = 0
- }
- private fun callLogoutApis(callback: ((success: Boolean) -> Unit)? = null) {
- showLoading("退出中...")
- val disposable = ApiRepository.loginOut()
- .subscribe({
- dismissLoading()
- callback?.invoke(true)
- }, {
- dismissLoading()
- callback?.invoke(false)
- })
- addDisposable(disposable)
- }
- private fun showTextInputDialog(title: String, callback: (String?) -> Unit) {
- val editText = EditText(this)
- val dialog = AlertDialog.Builder(this)
- .setTitle(title)
- .setView(editText)
- .setPositiveButton("确定") { _, _ ->
- val inputText = editText.text.toString()
- callback(inputText)
- }
- .setNegativeButton("取消") { _, _ ->
- callback(null)
- }
- .create()
- dialog.show()
- }
- }
|