|
28
|
+ private SyncTimeListener listener;
|
|
|
29
|
+ private boolean isCancelled;
|
|
|
30
|
+
|
|
|
31
|
+ public SyncTimeInteractor(String lensmanId, SyncTimeListener listener){
|
|
|
32
|
+ this.listener = listener;
|
|
|
33
|
+ this.lensmanId = lensmanId;
|
|
|
34
|
+ }
|
|
|
35
|
+
|
|
|
36
|
+ @Override
|
|
|
37
|
+ public void startJob() {
|
|
|
38
|
+ cancelJob();
|
|
|
39
|
+ isCancelled = false;
|
|
|
40
|
+ HashMap<String,String> params = new HashMap<>();
|
|
|
41
|
+ params.put("user_id",lensmanId);
|
|
|
42
|
+ if(BuildConfig.isTestMode){
|
|
|
43
|
+ listener.onTimeSync(true,0);
|
|
|
44
|
+ }
|
|
|
45
|
+ timeSyncTask = new HttpPostTask(params){
|
|
|
46
|
+
|
|
|
47
|
+ int msgId = R.string.sync_time_box_error;
|
|
|
48
|
+
|
|
|
49
|
+ @Override
|
|
|
50
|
+ protected boolean parseResponse(String response) {
|
|
|
51
|
+ try{
|
|
|
52
|
+ JSONObject json = new JSONObject(response);
|
|
|
53
|
+ int status = json.getInt("status");
|
|
|
54
|
+ if(status == 200){
|
|
|
55
|
+ JSONObject data = json.getJSONObject("data");
|
|
|
56
|
+ String time = data.getString("time");
|
|
|
57
|
+ HashMap<String,String> boxParams = new HashMap<>();
|
|
|
58
|
+ boxParams.put("time",time);
|
|
|
59
|
+ if(isCancelled){
|
|
|
60
|
+ return false;
|
|
|
61
|
+ }
|
|
|
62
|
+ HttpUtils.doHttpPost(BoxUrlContainer.SET_TIME_URL,boxParams);
|
|
|
63
|
+ if(isCancelled){
|
|
|
64
|
+ return false;
|
|
|
65
|
+ }
|
|
|
66
|
+ SystemClock.sleep(1000);
|
|
|
67
|
+ if(isCancelled){
|
|
|
68
|
+ return false;
|
|
|
69
|
+ }
|
|
|
70
|
+ String boxResp = HttpUtils.doHttpPost(BoxUrlContainer.BOX_TIME_URL,null);
|
|
|
71
|
+ if(isCancelled){
|
|
|
72
|
+ return false;
|
|
|
73
|
+ }
|
|
|
74
|
+ JSONObject boxJson = new JSONObject(boxResp);
|
|
|
75
|
+ int boxStatus = boxJson.getInt("status");
|
|
|
76
|
+ if(boxStatus == 200){
|
|
|
77
|
+ String boxTime = boxJson.getJSONObject("data").getString("time");
|
|
|
78
|
+ if(time.substring(0,13).equals(boxTime.substring(0,13))){
|
|
|
79
|
+ return true;
|
|
|
80
|
+ }
|
|
|
81
|
+ }
|
|
|
82
|
+ }else{
|
|
|
83
|
+ msgId = R.string.sync_time_server_error;
|
|
|
84
|
+ }
|
|
|
85
|
+ }catch (Exception e){
|
|
|
86
|
+ e.printStackTrace();
|
|
|
87
|
+ }
|
|
|
88
|
+ return false;
|
|
|
89
|
+ }
|
|
|
90
|
+
|
|
|
91
|
+ @Override
|
|
|
92
|
+ protected void onPostFail() {
|
|
|
93
|
+ super.onPostFail();
|
|
|
94
|
+ listener.onTimeSync(false,msgId);
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+ @Override
|
|
|
98
|
+ protected void onPostSuccess() {
|
|
|
99
|
+ super.onPostSuccess();
|
|
|
100
|
+ listener.onTimeSync(true,0);
|
|
|
101
|
+ }
|
|
|
102
|
+ };
|
|
|
103
|
+ timeSyncTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.SERVER_TIME_URL);
|
|
|
104
|
+ }
|
|
|
105
|
+
|
|
|
106
|
+ @Override
|
|
|
107
|
+ public void cancelJob() {
|
|
|
108
|
+ if(timeSyncTask==null){
|
|
|
109
|
+ return;
|
|
|
110
|
+ }
|
|
|
111
|
+ if(timeSyncTask.getStatus()== AsyncTask.Status.RUNNING){
|
|
|
112
|
+ timeSyncTask.cancel(true);
|
|
|
113
|
+ }
|
|
|
114
|
+ isCancelled = true;
|
|
|
115
|
+ timeSyncTask = null;
|
|
|
116
|
+ }
|
|
|
117
|
+
|
|
|
118
|
+ public interface SyncTimeListener{
|
|
|
119
|
+
|
|
|
120
|
+ void onTimeSync(boolean result,@StringRes int strId);
|
|
|
121
|
+ }
|
|
|
122
|
+}
|
|
|
|
@@ -173,4 +173,8 @@
|
|
173
|
173
|
<string name="upload_fast">短</string>
|
|
174
|
174
|
|
|
175
|
175
|
<string name="upload_settings">上传管理</string>
|
|
|
176
|
+
|
|
|
177
|
+ <string name="sync_time_server_error">时间同步出错,请检查网络连接后退出重试</string>
|
|
|
178
|
+ <string name="sync_time_box_error">时间同步出错,请检查盒子是否打开后退出重试</string>
|
|
|
179
|
+
|
|
176
|
180
|
</resources>
|