package com.arpa.hndahesudintocctmsdriver.ui import android.Manifest import android.annotation.SuppressLint import android.content.Intent import android.os.Build import android.os.Bundle import android.os.Message import android.view.KeyEvent import android.widget.* import androidx.fragment.app.Fragment import com.arpa.hndahesudintocctmsdriver.R import com.arpa.hndahesudintocctmsdriver.event.WalletEvent import com.arpa.hndahesudintocctmsdriver.ui.home.HomeFragment import com.arpa.hndahesudintocctmsdriver.ui.login.LoginActivity import com.arpa.hndahesudintocctmsdriver.ui.my.MyFragment import com.arpa.hndahesudintocctmsdriver.ui.news.NewsFragment import com.arpa.hndahesudintocctmsdriver.ui.wallet.WalletFragment import com.arpa.hndahesudintocctmsdriver.util.http.RequsetCodeConstants import com.arpa.hndahesudintocctmsdriver.util.statusbar.StateStyleUtil import com.arpa.hndahesudintocctmsdriver.util.view.BaseAppCompatActivity import com.arpa.hndahesudintocctmsdriver.util.view.BottomTabUtil import com.arpa.hndahesudintocctmsdriver.util.view.PanDuanUtil import com.google.gson.Gson import com.gyf.cactus.ext.cactusUpdateNotification import com.permissionx.guolindev.PermissionX import org.greenrobot.eventbus.EventBus @Suppress("DIVISION_BY_ZERO") @SuppressLint("SetTextI18n") class MainActivity : BaseAppCompatActivity() { private var ibs = arrayOf() private var title_view = arrayOf() private var imgs0 = intArrayOf(R.mipmap.tab1, R.mipmap.tab2, R.mipmap.xiaoxi0, R.mipmap.tab3) private var imgs1 = intArrayOf(R.mipmap.tab1_s, R.mipmap.tab2_s, R.mipmap.xiaoxi1, R.mipmap.tab3_s) private val gson = Gson() override fun msgMethod(m: Message?) { super.msgMethod(m) when (m!!.what) { 1 -> { isExit = false; } RequsetCodeConstants.SUCCESS -> { } } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) StateStyleUtil.stateTextColor(this) setContentView(R.layout.activity_main) cactusUpdateNotification { setTitle("好运司机") setContent("您的接单小助手") } con = this act = this //隐藏标题栏 val actionBar = supportActionBar actionBar?.hide() var r1 = findViewById(R.id.r1) var r2 = findViewById(R.id.r2) var r3 = findViewById(R.id.r3) var r4 = findViewById(R.id.r4) var fs = arrayOfNulls(4) fs[0] = HomeFragment() fs[1] = WalletFragment() fs[2] = NewsFragment() fs[3] = MyFragment() ibs = arrayOf( findViewById(R.id.huoyuan), findViewById(R.id.qianbao), findViewById(R.id.xiaoxi), findViewById(R.id.wode)) title_view = arrayOf(findViewById(R.id.title1), findViewById(R.id.title2), findViewById(R.id.title3), findViewById(R.id.title4)) var bt = BottomTabUtil(this, R.id.body, fs) bt.selectItem(fs[0]) r1.setOnClickListener { v -> selectImage(bt.index, 0) bt.setDefaultFragment(fs[0]) bt.index = 0 } r2.setOnClickListener { v -> if (!PanDuanUtil.isLogin(con)) { jumpLogin() } else { selectImage(bt.index, 1) if (!fs[1]!!.isAdded) { bt.hide(fs[bt.index]) bt.selectItem(fs[1]) } else { EventBus.getDefault().post(WalletEvent("refresh")) bt.setDefaultFragment(fs[1]) } bt.index = 1 } } r3.setOnClickListener { v -> if (!PanDuanUtil.isLogin(con)) { jumpLogin() } else { selectImage(bt.index, 2) if (!fs[2]!!.isAdded) { bt.hide(fs[bt.index]) bt.selectItem(fs[2]) } else { bt.setDefaultFragment(fs[2]) } bt.index = 2 } } r4.setOnClickListener { v -> if (!PanDuanUtil.isLogin(con)) { jumpLogin() } else { selectImage(bt.index, 3) if (!fs[3]!!.isAdded) { bt.hide(fs[bt.index]) bt.selectItem(fs[3]) } else { bt.setDefaultFragment(fs[3]) } bt.index = 3 } } // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // getPression() // } } fun getPression() { PermissionX.init(this) .permissions(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA) .request { allGranted, grantedList, deniedList -> if (allGranted) { } else { Toast.makeText(con, "开启权限失败,请在应用设置-权限-定位-始终允许", Toast.LENGTH_SHORT).show() } } } fun selectImage(old: Int, new: Int) { ibs[old].setImageResource(imgs0[old]) ibs[new].setImageResource(imgs1[new]) title_view[old].setTextColor(resources.getColor(R.color.tab_select_no, null)) title_view[new].setTextColor(resources.getColor(R.color.tab_select, null)) } fun jumpLogin() { var inl = Intent(con, LoginActivity::class.java) startActivity(inl) } override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { return if (keyCode == KeyEvent.KEYCODE_BACK) { exit() false } else { super.onKeyDown(keyCode, event) } } private var isExit = false fun exit() { if (!isExit) { isExit = true Toast.makeText(applicationContext, "再按一次退出程序", Toast.LENGTH_SHORT).show() hd.sendEmptyMessageDelayed(0, 2000) } else { val intent = Intent(Intent.ACTION_MAIN) intent.addCategory(Intent.CATEGORY_HOME) startActivity(intent) System.exit(1) } } }