package com.mnopensdk.demo;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Process;
import android.os.StrictMode;
import android.text.TextUtils;

import com.mnopensdk.demo.base.ActivityStack;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import MNSDK.MNOpenSDK;



public class MNApplication extends Application {
//    public static String APP_KEY = "31cc93923faa4bff";// 公共KEY
//    public static String APP_SECRET = "f6c9deec31644885a123c8ffb573a52e";
    public static String APP_KEY="8Wa227sQ00S33p4y";// manniu
    public static String APP_SECRET="RlA8aCPlsuATT227kKTg003ncP35HYRI";
    public static String HOST_DOMAIN = "https://restcn.bullyun.com";

    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化MNSDK
        MNOpenSDK.initWithKeyAndSecret(this, APP_KEY, APP_SECRET);
        // 设置域名
        MNOpenSDK.setMNKitDomain(this, HOST_DOMAIN);
        // 设置需要提前建立与设备的连接
        MNOpenSDK.setP2pPreLinkState(this, true);


        mnApplication = this;
        mContext = getApplicationContext();
        mActivityStack = new ActivityStack();
        initConfiguration();
    }

    protected static MNApplication mnApplication = null;
    /**
     * 上下文
     */
    protected Context mContext = null;
    /**
     * Activity 栈
     */
    public ActivityStack mActivityStack = null;

    public static MNApplication getInstance() {
        return mnApplication;
    }

    private void initConfiguration() {
        // android 7.0系统解决拍照的问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();
        }

        //初始化的时候对上报进程进行控制，只在主进程下上报数据：判断是否是主进程（通过进程名是否为包名来判断）
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));

        //TODO 发布版本最后参数设置为false
        CrashReport.initCrashReport(getApplicationContext(), "60c8a5bf7f", true);
    }

    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }
}
