package com.base.pdfreaderallpdfreader.ui.splash

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.base.pdfreaderallpdfreader.utils.LogEx
import java.util.concurrent.atomic.AtomicBoolean

/**
 * 适配官方启动页
 * https://juejin.cn/post/7056576958875992077
 */
class SystemStartActivity : AppCompatActivity() {

    private val TAG = "SystemStartActivity"

    // 数据
    private var mKeepOnAtomicBool = AtomicBoolean(true)

    override fun onCreate(savedInstanceState: Bundle?) {
        // 初始化操作(必须放在setContentView()之前)
        val splashScreen = installSplashScreen()
        super.onCreate(savedInstanceState)
        // setContentView（可以省略）
//        setContentView(R.layout.activity_system_start)

        // 每次UI绘制前，会判断 Splash 是否有必要继续展示在屏幕上；直到不再满足条件时，隐藏Splash。
        splashScreen.setKeepOnScreenCondition {
            mKeepOnAtomicBool.get()
        }
        LogEx.logDebug(TAG, "setKeepOnScreenCondition...")
        startActivity(Intent(this@SystemStartActivity, MyStartActivity::class.java))
        this@SystemStartActivity.finish()
        overridePendingTransition(0, 0)
        mKeepOnAtomicBool.compareAndSet(true, false)

        // Splash动画展示完毕的监听方法,无动画不用
//        splashScreen.setOnExitAnimationListener(object : SplashScreen.OnExitAnimationListener {
//            override fun onSplashScreenExit(splashScreenViewProvider: SplashScreenViewProvider) {
//                LogEx.logDebug(TAG, "onSplashScreenExit")
//                startActivity(Intent(this@SystemStartActivity, MyStartActivity::class.java))
//                this@SystemStartActivity.finish()
//                overridePendingTransition(0, 0)
//                mKeepOnAtomicBool.compareAndSet(true, false)
//            }
//        })
    }
}