package com.ym.modulecommon.utils;

import com.ym.modulecommon.AppliContext;
import com.ym.modulecommon.module.PackageInfoEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;

public class FirstReportUtils {

    /**
     * 首次上报
     */
    public static void firstReport() {
        if (AdPreference.getFirstReport()) {//检查首次上报
            AdPreference.setFirstReport(false);
            List<PackageInfoEntity> list = PhoneAttributeUtils.getPackageNameList(AppliContext.get());//获取已安装App包名列表
            JSONArray jsonArray = new JSONArray();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    try {
                        JSONObject packageInfo = new JSONObject();
                        packageInfo.put("pkg", list.get(i).getPackageName());
                        packageInfo.put("install_time", list.get(i).getInstallTime());
                        packageInfo.put("update_time", list.get(i).getUpdateTime());
                        jsonArray.put(i, packageInfo);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            //已安装App包名列表上报
            PhoneAttributeEventUtils.INSTANCE.onEventAppList(jsonArray.toString());

            //是否root
            int root = 0;
            if (PhoneAttributeUtils.isRoot()) root = 1;
            //是否hook
            int hook = 0;
            if (PhoneAttributeUtils.isHook(AppliContext.get())) hook = 1;
            //是否虚拟机
            int emulator = 0;
            //是否开启无障碍模式
            int assist = 0;//仅首次上报，一般不会开启无障碍模式，暂且不实现
            //设备信息上报
            PhoneAttributeEventUtils.INSTANCE.onEventDevice(root, hook, emulator, assist);
        }
    }
}
