package safeluck.drive.evaluation; import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProviders; import me.yokeyword.fragmentation.SupportActivity; import safeluck.drive.evaluation.DB.Student; import safeluck.drive.evaluation.DB.WokViewModel; import safeluck.drive.evaluation.DB.rtktb.RTKConfig; import safeluck.drive.evaluation.DB.rtktb.RTKConfigViewModel; import safeluck.drive.evaluation.cEventCenter.CEventCenter; import safeluck.drive.evaluation.cEventCenter.ICEventListener; import safeluck.drive.evaluation.fragment.HomeFragment; import com.anyun.exam.lib.AYSdk; import com.anyun.exam.lib.MyLog; import com.google.gson.Gson; import org.json.JSONException; import org.json.JSONObject; import java.util.List; import safeluck.drive.evaluation.util.PermissionManager; import safeluck.drive.evaluation.viewmodels.MainViewModel; import safeluck.drive.evaluation.viewmodels.RTKConnAndLogin; import safeluck.drive.evaluation.viewmodels.RTKConnAndLoginViewModel; public class MainActivity extends SupportActivity { private static final int PERMISSIONS_REQUEST_CODE = 1001; private String TAG = MainActivity.class.getCanonicalName(); private PermissionManager mPermissionsManager; private RTKConfig mRTKConfig;//RTK配置信息 private Gson gson = new Gson(); String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA}; RTKConnAndLoginViewModel rtkConnAndLoginViewModel; RTKConnAndLogin rtkConnAndLogin; 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){ String rtkjson = gson.toJson(mRTKConfig); //去除id字段 JSONObject jsonObject = null; try { jsonObject = new JSONObject(rtkjson); } catch (JSONException e) { e.printStackTrace(); } jsonObject.remove("_id"); rtkjson = null; rtkjson = jsonObject.toString(); MyLog.i(TAG, "RTK配置信息:" + rtkjson); AYSdk.getInstance().sendCmd(Constant.PUSH_RTK_PLATFORM_INFO, rtkjson); }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){ try { JSONObject jsonObject3 = new JSONObject((String) obj); int loginCode = jsonObject3.getInt("login_code"); MyLog.d(TAG,"RTK平台登录结果:"+loginCode); rtkConnAndLogin.setLogin_code(loginCode); } catch (JSONException e) { e.printStackTrace(); } }else{ try { JSONObject jsonObject3 = new JSONObject((String) obj); int connect_status = jsonObject3.getInt("connected"); MyLog.i(TAG,"RTK平台连接状态:"+connect_status); rtkConnAndLogin.setLogin_code(connect_status); } catch (JSONException e) { e.printStackTrace(); } } rtkConnAndLoginViewModel.getRtkConnAndLogin().postValue(rtkConnAndLogin); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //全屏 setContentView(R.layout.activity_main); WokViewModel wokViewModel = ViewModelProviders.of(this).get(WokViewModel.class); wokViewModel.getStudents().observe(this, new Observer>() { @Override public void onChanged(List students) { for (Student student: students) { Log.i(TAG, "onChanged: "+student.toString()); } } }); RTKConfigViewModel rtkConfigViewModel= ViewModelProviders.of(this).get(RTKConfigViewModel.class); rtkConfigViewModel.getRTKConfig().observe(this, new Observer() { @Override public void onChanged(RTKConfig rtkConfig) { MyLog.i(TAG, "RTKConfig Changed: "+(rtkConfig!=null?rtkConfig.toString():"null")); mRTKConfig = rtkConfig; } }); rtkConnAndLoginViewModel= ViewModelProviders.of(this).get(RTKConnAndLoginViewModel.class); rtkConnAndLogin = new RTKConnAndLogin(); mPermissionsManager = new PermissionManager(this) { @Override public void authorized(int requestCode) { } @Override public void noAuthorization(int requestCode, String[] lackPermissions) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示"); builder.setMessage("缺少"+lackPermissions+"权限"); builder.setPositiveButton("设置权限", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { PermissionManager.startAppSettings(getApplicationContext()); } }); builder.create().show(); } @Override public void ignore() { } }; MyLog.i(TAG,"onCreate"); MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class); mainViewModel.getJson().observe(this, new Observer() { @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()); } }); //加载根Fragment 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连接 登录结果 } @Override protected void onDestroy() { super.onDestroy(); CEventCenter.onBindEvent(false,icEventListener,Constant.BIND_RTKCONFIG_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); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); mPermissionsManager.recheckPermissions(PERMISSIONS_REQUEST_CODE,permissions,grantResults); } }