package com.atmob.elec_asst.sdk.umeng;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.ProcessLifecycleOwner;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.atmob.elec_asst.BuildConfig;
import com.atmob.channelreader.ChannelReader;

public class UmengHelper {

    private static final String UMENG_APP_KEY =  UmengId.UMENG_APP_KEY;

    private UmengHelper() {

    }

    public static void preInit(@NonNull Application application){
        if (BuildConfig.DEBUG) {
            return;
        }
        UMConfigure.preInit(application,UMENG_APP_KEY, ChannelReader.getChannel(application));
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);

        ProcessLifecycleOwner.get().getLifecycle().addObserver((LifecycleEventObserver) (source, event) -> {
            if (event == Lifecycle.Event.ON_DESTROY) {
                saveData(application);
            }
        });
    }

    /**
     * 当程序退出时，用于保存统计数据
     */
    public static void saveData(Context context) {
        MobclickAgent.onKillProcess(context.getApplicationContext());
    }

}
