endian11
2020-01-10 9386cf855cace41b24bce436bb5ed70037a7396e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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;
    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) {
                    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);
            }
            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();
                }
            }
        }
    };
 
    @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<List<Student>>() {
            @Override
            public void onChanged(List<Student> students) {
                for (Student student :
                        students) {
                    Log.i(TAG, "onChanged: " + student.toString());
                }
            }
        });
 
        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"));
                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<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());
 
            }
        });
 
        //加载根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);
    }
}