package com.atmob.keyboard

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import com.atmob.keyboard.config.AppConfig
import com.atmob.keyboard_android.util.jump.JumpHostAppPageUtil
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache

class MainActivity : FlutterActivity() {
    private val mMainHandler = Handler(Looper.getMainLooper())

    /**
     * 使用预缓存的引擎
     */
    override fun provideFlutterEngine(context: Context): FlutterEngine? {
        return FlutterEngineCache.getInstance().get(AppConfig.FLUTTER_ENGINE_ID)
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        // 执行路由跳转
        JumpHostAppPageUtil.jumpAppPage(intent, flutterEngine)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        // 执行路由跳转
        flutterEngine?.let {
            JumpHostAppPageUtil.jumpAppPage(intent, it)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mMainHandler.removeCallbacksAndMessages(null)
    }
}