endian11
2020-01-10 9386cf855cace41b24bce436bb5ed70037a7396e
app/src/main/java/safeluck/drive/evaluation/MainActivity.java
@@ -47,15 +47,16 @@
    private PermissionManager mPermissionsManager;
    private RTKConfig mRTKConfig;//RTK配置信息
    private Gson gson = new Gson();
    String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA};
    String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
    RTKConnAndLoginViewModel rtkConnAndLoginViewModel;
    RTKConnAndLogin rtkConnAndLogin;
    RTKConfigViewModel rtkConfigViewModel;
    private ICEventListener icEventListener = new ICEventListener() {
        @Override
        public void onCEvent(String topic, int msgCode, int resultCode, Object obj) {
            if (msgCode == Constant.FETCH_RTK_PLATFORM_INFO){
                if (mRTKConfig != null){
            if (msgCode == Constant.FETCH_RTK_PLATFORM_INFO) {
                if (mRTKConfig != null) {
                    String rtkjson = gson.toJson(mRTKConfig);
                    //去除id字段
@@ -70,27 +71,27 @@
                    rtkjson = jsonObject.toString();
                    MyLog.i(TAG, "RTK配置信息:" + rtkjson);
                    AYSdk.getInstance().sendCmd(Constant.PUSH_RTK_PLATFORM_INFO, rtkjson);
                }else{
                    MyLog.d(TAG,"RTKConfig未取到数据");
                } else {
                    MyLog.d(TAG, "RTKConfig未取到数据");
                }
            }
            if (msgCode == Constant.RTK_PLATFORM_CONNECT_STATUS||msgCode== Constant.RTK_PLATFORM_REGISTER_RESULT){
                if (msgCode == Constant.RTK_PLATFORM_REGISTER_RESULT){
            if (msgCode == Constant.RTK_PLATFORM_CONNECT_STATUS || msgCode == Constant.RTK_PLATFORM_REGISTER_RESULT) {
                if (msgCode == Constant.RTK_PLATFORM_REGISTER_RESULT) {
                    try {
                        JSONObject jsonObject3 = new JSONObject((String) obj);
                        int loginCode = jsonObject3.getInt("login_code");
                        MyLog.d(TAG,"RTK平台登录结果:"+loginCode);
                        MyLog.d(TAG, "RTK平台登录结果:" + loginCode);
                        rtkConnAndLogin.setLogin_code(loginCode);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else{
                } else {
                    try {
                        JSONObject jsonObject3 = new JSONObject((String) obj);
                        int connect_status = jsonObject3.getInt("connected");
                        MyLog.i(TAG,"RTK平台连接状态:"+connect_status);
                        MyLog.i(TAG, "RTK平台连接状态:" + connect_status);
                        rtkConnAndLogin.setLogin_code(connect_status);
                    } catch (JSONException e) {
@@ -99,6 +100,23 @@
                }
                rtkConnAndLoginViewModel.getRtkConnAndLogin().postValue(rtkConnAndLogin);
            }
            if (msgCode == Constant.RTK_PLATFORM_REGISTER_STATUS) {
                try {
                    JSONObject jsonObject = new JSONObject((String) obj);
                    String rtkLoginPwd = jsonObject.getString("password");
                    int reg_code = jsonObject.getInt("register_code");
                    if (mRTKConfig != null) {
                        mRTKConfig.setPassword(rtkLoginPwd);
                        mRTKConfig.setRegistered(reg_code);
                        rtkConfigViewModel.insertRTKConfig(mRTKConfig);
                    }else{
                        MyLog.i(TAG,"mRTKConfig == null");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };
@@ -114,24 +132,24 @@
        wokViewModel.getStudents().observe(this, new Observer<List<Student>>() {
            @Override
            public void onChanged(List<Student> students) {
                for (Student student:
                for (Student student :
                        students) {
                    Log.i(TAG, "onChanged: "+student.toString());
                    Log.i(TAG, "onChanged: " + student.toString());
                }
            }
        });
        RTKConfigViewModel rtkConfigViewModel= ViewModelProviders.of(this).get(RTKConfigViewModel.class);
        rtkConfigViewModel  = ViewModelProviders.of(this).get(RTKConfigViewModel.class);
        rtkConfigViewModel.getRTKConfig().observe(this, new Observer<RTKConfig>() {
            @Override
            public void onChanged(RTKConfig rtkConfig) {
                MyLog.i(TAG, "RTKConfig Changed: "+(rtkConfig!=null?rtkConfig.toString():"null"));
                MyLog.i(TAG, "RTKConfig Changed: " + (rtkConfig != null ? rtkConfig.toString() : "null"));
                mRTKConfig = rtkConfig;
            }
        });
        rtkConnAndLoginViewModel= ViewModelProviders.of(this).get(RTKConnAndLoginViewModel.class);
        rtkConnAndLoginViewModel = ViewModelProviders.of(this).get(RTKConnAndLoginViewModel.class);
        rtkConnAndLogin = new RTKConnAndLogin();
@@ -145,7 +163,7 @@
            public void noAuthorization(int requestCode, String[] lackPermissions) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("提示");
                builder.setMessage("缺少"+lackPermissions+"权限");
                builder.setMessage("缺少" + lackPermissions + "权限");
                builder.setPositiveButton("设置权限", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
@@ -162,47 +180,45 @@
        };
        MyLog.i(TAG,"onCreate");
        MyLog.i(TAG, "onCreate");
        MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        mainViewModel.getJson().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String json) {
                Toast.makeText(MainActivity.this, json, Toast.LENGTH_SHORT).show();
                MyLog.i(TAG,"json=========="+json+" ThreadName:"+Thread.currentThread().getName());
                MyLog.i(TAG, "json==========" + json + " ThreadName:" + Thread.currentThread().getName());
            }
        });
        //加载根Fragment
        if (findFragment(HomeFragment.class) == null){
            loadRootFragment(R.id.fl_container,HomeFragment.newInstance());
        if (findFragment(HomeFragment.class) == null) {
            loadRootFragment(R.id.fl_container, HomeFragment.newInstance());
        }
        CEventCenter.onBindEvent(true,icEventListener,Constant.BIND_RTKCONFIG_TOPIC);//发送rtk配置消息
        CEventCenter.onBindEvent(true,icEventListener, Constant.BIND_CONNECT_RTK_TOPIC);//收到rtk连接 登录结果
        CEventCenter.onBindEvent(true, icEventListener, Constant.BIND_RTKCONFIG_TOPIC);//发送rtk配置消息
        CEventCenter.onBindEvent(true, icEventListener, Constant.BIND_CONNECT_RTK_TOPIC);//收到rtk连接 登录结果
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        CEventCenter.onBindEvent(false,icEventListener,Constant.BIND_RTKCONFIG_TOPIC);
        CEventCenter.onBindEvent(false, icEventListener, Constant.BIND_RTKCONFIG_TOPIC);
        CEventCenter.onBindEvent(false,icEventListener, Constant.BIND_CONNECT_RTK_TOPIC);
        CEventCenter.onBindEvent(false, icEventListener, Constant.BIND_CONNECT_RTK_TOPIC);
        Log.i(TAG, "onDestroy: ");
    }
    @Override
    protected void onResume() {
        super.onResume();
        mPermissionsManager.checkPermissions(PERMISSIONS_REQUEST_CODE,PERMISSIONS);
        mPermissionsManager.checkPermissions(PERMISSIONS_REQUEST_CODE, PERMISSIONS);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mPermissionsManager.recheckPermissions(PERMISSIONS_REQUEST_CODE,permissions,grantResults);
        mPermissionsManager.recheckPermissions(PERMISSIONS_REQUEST_CODE, permissions, grantResults);
    }
}