package com.daya.live_teaching;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Process;

import com.cooleshow.base.common.BaseApplication;
import com.daya.live_teaching.im.IMManager;
import com.daya.live_teaching.utils.log.SLog;

import java.util.Iterator;
import java.util.List;


public class LiveTeachingApp extends BaseApplication {
    private static LiveTeachingApp instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        // 初始化日志打印
        SLog.init(this);
        if (!getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {
            return;
        }
        IMManager.init(this,"c9kqb3rdc451j");
    }


    /**
     * 获取当前进程的名称
     * @param context
     * @return
     */
    public String getCurProcessName(Context context) {
        int pid = Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfoList = mActivityManager.getRunningAppProcesses();
        if (runningAppProcessInfoList == null) {
            return null;
        } else {
            Iterator processInfoIterator = runningAppProcessInfoList.iterator();

            ActivityManager.RunningAppProcessInfo appProcess;
            do {
                if (!processInfoIterator.hasNext()) {
                    return null;
                }

                appProcess = (ActivityManager.RunningAppProcessInfo) processInfoIterator.next();
            } while (appProcess.pid != pid);

            return appProcess.processName;
        }
    }

    /**
     * 获取当前 Application 实例
     * @return
     */
    public static LiveTeachingApp getApplication() {
        return instance;
    }
}
