package safeluck.drive.evaluation; import android.app.Application; import android.os.Environment; import android.text.TextUtils; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.work.Data; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkManager; import com.anyun.exam.lib.AYSdk; import com.anyun.exam.lib.IAYExamListener; import me.yokeyword.fragmentation.Fragmentation; import me.yokeyword.fragmentation.helper.ExceptionHandler; import com.anyun.exam.lib.MyLog; import com.anyun.exam.lib.crash.CrashHandler; import com.facebook.stetho.Stetho; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.json.JSONException; import org.json.JSONObject; import java.util.Random; import safeluck.drive.evaluation.DB.failitems.FailedProj; import safeluck.drive.evaluation.DB.failitems.FailedProjRepository; import safeluck.drive.evaluation.DB.gps.GpsInfoWorker; import safeluck.drive.evaluation.DB.rtktb.RTKConfig; import safeluck.drive.evaluation.DB.rtktb.RTKWorkRepository; import safeluck.drive.evaluation.cEventCenter.CEvent; import safeluck.drive.evaluation.cEventCenter.CEventCenter; import safeluck.drive.evaluation.util.FileUtil; import safeluck.drive.evaluation.util.SystemUtil; /** * MyApplication2 * Created by lzw on 2019/3/15. 10:53:52 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class app extends Application implements IAYExamListener { private static final String TAG = "app"; private Gson gson; RTKConfig rtkConfig; FailedProjRepository failedProjRepository;//失败项目表数据库 RTKWorkRepository rtkWorkRepository;//保存RTK配置表 Random random = new Random(); @Override public void onCreate() { super.onCreate(); //初始化Fragment 建议在Application onCreate里面初始化 if (SystemUtil.compareProcessName(this)) { Fragmentation.builder().stackViewMode(Fragmentation.BUBBLE).debug(false) .handleException(new ExceptionHandler() { @Override public void onException(@NonNull Exception e) { MyLog.i(TAG, "onException: " + e.getMessage()); } }) .install(); CrashHandler crashHandler = CrashHandler.getInstance(); crashHandler.init(getApplicationContext()); AYSdk.getInstance().init(getApplicationContext()); AYSdk.getInstance().registListener(this); MyLog.createIfNotExist(); Stetho.initializeWithDefaults(this); //数据库操作 MyLog.i(TAG, "onCreate111"); FileUtil.createdirs(getApplicationContext()); failedProjRepository = new FailedProjRepository(this); rtkWorkRepository = new RTKWorkRepository(this); } } @Override public void onTerminate() { super.onTerminate(); MyLog.e(TAG, "OnTerminate()"); AYSdk.getInstance().uninit(); } @Override public void onLowMemory() { super.onLowMemory(); MyLog.e(TAG, "OnTerminate()"); } @Override public void callBackMsg(final int cmd, String json) { MyLog.d(TAG, String.format("收到命令[%d],Json内容为%s", cmd, json)); switch (cmd) { case Constant.NDK_START: break; case Constant.RTK_PLATFORM_REGISTER_STATUS: //RTK平台注册状态,需要保存数据库 CEventCenter.dispatchEvent(Constant.BIND_CONNECT_RTK_TOPIC,cmd,0,json); break; case Constant.RTK_PLATFORM_REGISTER_RESULT: //RTK平台登录结果 CEventCenter.dispatchEvent(Constant.BIND_CONNECT_RTK_TOPIC,cmd,0,json); break; case Constant.FETCH_RTK_PLATFORM_INFO: CEventCenter.dispatchEvent(Constant.BIND_RTKCONFIG_TOPIC,cmd,0,""); break; case Constant.JUDGE_INFO: if(!TextUtils.isEmpty(json)){ JsonArray jsonArray = JsonParser.parseString(json).getAsJsonArray(); for (int i = 0; i < jsonArray.size(); i++) { JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); int emp_id = jsonObject.get("wrong_id").getAsInt(); failedProjRepository.insert(new FailedProj(Constant.SUBJECT_I, emp_id, Constant.TEST_STU_ID)); } } break; case Constant.EXAM_STATUS_REPLY: MyLog.d(TAG, "考试开始,需要删除上一个考试学员的数据库失败项目表"); failedProjRepository.deleteAll(); break; case Constant.RTK_PLATFORM_CONNECT_STATUS: CEventCenter.dispatchEvent(Constant.BIND_CONNECT_RTK_TOPIC,cmd,0,json); break; case Constant.FETCH_MAP_INFO: StringBuffer buffer =FileUtil.readTxtFileFromSD(this,Constant.MAP); if (buffer != null){ AYSdk.getInstance().sendCmd(Constant.PUSH_MAP_INFO,buffer.toString()); }else{ MyLog.d(TAG,String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+getApplicationContext().getPackageName())); } break; case Constant.REQ_VECHILE_PROFILE: StringBuffer vebuffer =FileUtil.readTxtFileFromSD(this,Constant.VEHICLE); if (vebuffer != null){ AYSdk.getInstance().sendCmd(Constant.PUSH_VECHILE_PROFILE,vebuffer.toString()); }else{ MyLog.d(TAG,String.format("车辆模型模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+getApplicationContext().getPackageName())); } break; case Constant.GPS_INFO: Data gpsData = new Data.Builder().putString(Constant.GPS_INFO_DATA,json).build(); OneTimeWorkRequest gpsinfoWorkRequest = new OneTimeWorkRequest.Builder(GpsInfoWorker.class).setInputData(gpsData).build(); WorkManager.getInstance(getApplicationContext()).enqueue(gpsinfoWorkRequest); break; } } }