package com.ray650128.gstreamer_demo_app

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.util.Log
import kotlin.system.exitProcess

class MyApplication : Application(), Application.ActivityLifecycleCallbacks {

    override fun onCreate() {
        super.onCreate()
        instance = this
        registerActivityLifecycleCallbacks(this)
    }

    private var currentActivity: Activity? = null

    override fun onActivityCreated(p0: Activity, p1: Bundle?) {}

    override fun onActivityStarted(p0: Activity) {}

    override fun onActivityResumed(p0: Activity) {
        currentActivity = p0
    }

    override fun onActivityPaused(p0: Activity) {}

    override fun onActivityStopped(p0: Activity) {}

    override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {}

    override fun onActivityDestroyed(p0: Activity) {
        if (currentActivity?.equals(p0) == true) {
            Log.e("MyApplication", "EXIT...")
            onTerminate()
            exitProcess(0)
        }
    }

    companion object {
        var instance: Application? = null

        fun getAppContext(): Context {
            return instance!!.applicationContext
        }
    }
}