package com.xm.test.myfilemaster

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.SharedPreferences
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError
import com.applovin.mediation.ads.MaxAppOpenAd
import com.applovin.sdk.AppLovinMediationProvider
import com.applovin.sdk.AppLovinSdk
import com.cloud.cleanjunksdk.task.CheckSdkCallback
import com.cloud.cleanjunksdk.task.Clean
import com.cloud.cleanjunksdk.task.CleanSDK
import com.cloud.cleanjunksdk.tools.Region
import com.trustlook.sdk.cloudscan.CloudScanClient
import com.xm.test.myfilemaster.ad.GravitySensorManager
import com.xm.test.myfilemaster.notification.ActionBroadcast
import com.xm.test.myfilemaster.util.UrlManager

class MyApplication : Application() {

    private lateinit var gravitySensorManager: GravitySensorManager

    companion object {
        var mCleanSdk: Clean? = null
        @SuppressLint("StaticFieldLeak")
        var mCloudScan: CloudScanClient? = null
        var mSp: SharedPreferences? = null

        @SuppressLint("StaticFieldLeak")
        @JvmStatic
        lateinit var fContext: Context
    }

    override fun onCreate() {
        super.onCreate()
        fContext=this
//        AppLovinSdk.getInstance( this ).initializeSdk({ configuration: AppLovinSdkConfiguration ->
//            {
//                appOpenManager = ExampleAppOpenManager(applicationContext)
//            }
//            )
//        }
        mSp = getSharedPreferences("fileManager", MODE_PRIVATE)
        CleanSDK.init(this, this, Region.INTL, object : CheckSdkCallback {
            override fun onSuccess(p0: Clean?) {
                mCleanSdk = p0!!
                Log.e("yanglin", "onSuccess: clean INIT")
            }

            override fun onError(error: Int) {
                Log.e("yanglin", "onError: INIT error=$error")
            }
        })
        mCloudScan = CloudScanClient.Builder(this)
            .setRegion(com.trustlook.sdk.data.Region.INTL)
            .setConnectionTimeout(30000)
            .setSocketTimeout(30000)
            .build()
        AppLovinSdk.getInstance(this).initializeSdk {

        }
        AppLovinSdk.getInstance(this).mediationProvider = AppLovinMediationProvider.MAX
        initBroadcast()
    }

    private fun initBroadcast(){
        gravitySensorManager = GravitySensorManager(this)
        gravitySensorManager.registerListener()
        val filter = IntentFilter()
        filter.addAction(Intent.ACTION_SCREEN_OFF)
        filter.addAction(Intent.ACTION_SCREEN_ON)
        filter.addAction(Intent.ACTION_PACKAGE_ADDED)
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED)
        filter.addAction(Intent.ACTION_USER_PRESENT)
        filter.addAction(Intent.ACTION_POWER_CONNECTED)
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED)
        filter.addAction(Intent.ACTION_BATTERY_CHANGED)
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED)
        filter.addAction(Intent.ACTION_MEDIA_REMOVED)
        filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED)
        filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL)
        filter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW)
        filter.addAction(Intent.ACTION_DEVICE_STORAGE_OK)
        filter.addDataScheme("package")
        registerReceiver(ActionBroadcast(), filter)
    }
}