11package com .csl .cs108ademoapp ;
22
33import android .os .AsyncTask ;
4+ import android .os .Handler ;
45import android .widget .Button ;
56import android .widget .TextView ;
67import android .widget .Toast ;
@@ -13,10 +14,12 @@ public class AccessTask extends AsyncTask<Void, String, String> {
1314 final boolean DEBUG = true ;
1415 final boolean skipSelect = false ;
1516 public enum TaskCancelRReason {
16- NULL , INVALD_REQUEST , DESTORY , STOP , BUTTON_RELEASE , TIMEOUT
17+ NULL , INVALD_REQUEST , DESTORY , STOP , BUTTON_RELEASE , ERROR , TIMEOUT
1718 }
1819 public TaskCancelRReason taskCancelReason ;
1920 public String accessResult ;
21+ Handler mHandler = new Handler ();
22+ Runnable updateRunnable = null ;
2023
2124 Button button ; String buttonText ;
2225 TextView registerRunTime , registerTagGot , registerVoltageLevel ;
@@ -46,7 +49,8 @@ public enum TaskCancelRReason {
4649
4750 public AccessTask (Button button , boolean invalidRequest ,
4851 String selectMask , int selectBank , int selectOffset ,
49- String strPassword , int powerLevel , Cs108Connector .HostCommands hostCommand , boolean bEnableErrorPopWindow ) {
52+ String strPassword , int powerLevel , Cs108Connector .HostCommands hostCommand ,
53+ boolean bEnableErrorPopWindow , Runnable updateRunnable ) {
5054 this .button = button ;
5155 this .registerTagGot = registerTagGot ;
5256 this .registerVoltageLevel = registerVoltageLevel ;
@@ -59,6 +63,7 @@ public AccessTask(Button button, boolean invalidRequest,
5963 this .powerLevel = powerLevel ;
6064 this .hostCommand = hostCommand ;
6165 this .bEnableErrorPopWindow = bEnableErrorPopWindow ;
66+ this .updateRunnable = updateRunnable ;
6267 if (true ) {
6368 total = 0 ;
6469 tagList .clear ();
@@ -95,6 +100,9 @@ public AccessTask(Button button, TextView textViewWriteCount, boolean invalidReq
95100 }
96101 preExecute ();
97102 }
103+ public void setRunnable (Runnable updateRunnable ) {
104+ this .updateRunnable = updateRunnable ;
105+ }
98106
99107 void preExecute () {
100108 accessResult = null ;
@@ -106,13 +114,14 @@ void preExecute() {
106114 String buttonText1 = null ; String strLastChar = buttonText .substring (buttonText .length ()-1 );
107115 if (strLastChar .toUpperCase ().matches ("E" )) {
108116 buttonText1 = buttonText .substring (0 , buttonText .length ()-1 );
109- if (strLastChar . matches ("E " )) buttonText1 += "ING" ;
110- else buttonText1 += "ing" ;
111- }
117+ } else if (buttonText . toUpperCase (). matches ("STOP " )) {
118+ buttonText1 = buttonText ; buttonText1 += buttonText1 . substring ( buttonText . length ()- 1 ) ;
119+ } else buttonText1 = buttonText ;
112120 if (repeat || buttonText .length () == 0 ) button .setText ("Stop" );
113- else if (buttonText1 != null ) button .setText (buttonText1 );
114- else if (Character .isUpperCase (strLastChar .charAt (0 ))) button .setText (buttonText + "ING" );
115- else button .setText (buttonText + "ing" );
121+ else {
122+ if (Character .isUpperCase (strLastChar .charAt (0 ))) button .setText (buttonText1 + "ING" );
123+ else button .setText (buttonText1 + "ing" );
124+ }
116125 if (registerYield != null && tagList .size ()==0 ) registerYield .setText ("" );
117126 if (registerTotal != null && total == 0 ) registerTotal .setText ("" );
118127
@@ -163,6 +172,7 @@ else if (MainActivity.mCs108Library4a.setSelectedTag(selectMask, selectBank, sel
163172 @ Override
164173 protected String doInBackground (Void ... a ) {
165174 boolean ending = false ;
175+ int iTimeOut = 5000 ;
166176
167177 while (MainActivity .mCs108Library4a .isBleConnected () && isCancelled () == false && ending == false ) {
168178 int batteryCount = MainActivity .mCs108Library4a .getBatteryCount ();
@@ -174,6 +184,7 @@ protected String doInBackground(Void... a) {
174184 runTimeMillis = System .currentTimeMillis ();
175185 publishProgress ("WW" );
176186 }
187+ byte [] notificationData = MainActivity .mCs108Library4a .onNotificationEvent ();
177188 Cs108Connector .Rx000pkgData rx000pkgData = MainActivity .mCs108Library4a .onRFIDEvent ();
178189 if (MainActivity .mCs108Library4a .mrfidToWriteSize () != 0 ) timeMillis = System .currentTimeMillis ();
179190 else if (rx000pkgData != null ) {
@@ -182,11 +193,19 @@ else if (rx000pkgData != null) {
182193 } else if (rx000pkgData .responseType == Cs108Connector .HostCmdResponseTypes .TYPE_18K6C_TAG_ACCESS ) {
183194 if (true ) {
184195 if (rx000pkgData .decodedError == null ) {
185- if (done == false ) { accessResult = rx000pkgData .decodedResult ; MainActivity .mCs108Library4a .appendToLog ("HelloA, accResult=" + accessResult );}
196+ if (done == false ) {
197+ accessResult = rx000pkgData .decodedResult ;
198+ MainActivity .mCs108Library4a .appendToLog ("StreamOut, accResult=" + accessResult );
199+ if (updateRunnable != null ) {
200+ MainActivity .mCs108Library4a .appendToLog ("StreamOut: start updateRunnable" );
201+ mHandler .post (updateRunnable );
202+ }
203+ }
186204 done = true ;
187205 publishProgress (null , rx000pkgData .decodedResult );
188206 }
189207 else publishProgress (rx000pkgData .decodedError );
208+ iTimeOut = 500 ;
190209 }
191210 } else if (rx000pkgData .responseType == Cs108Connector .HostCmdResponseTypes .TYPE_COMMAND_END ) {
192211 if (rx000pkgData .decodedError != null ) { endingMessaage = rx000pkgData .decodedError ; ending = true ; }
@@ -204,11 +223,20 @@ else if (repeat && (nextNew == false || resultError.length() != 0)) {
204223 }
205224 timeMillis = System .currentTimeMillis ();
206225 }
207- if (System .currentTimeMillis () - timeMillis > 5000 ) {
208- cancel (true );
226+ else if (notificationData != null ) {
227+ MainActivity .mCs108Library4a .appendToLog ("resultError=" + MainActivity .mCs108Library4a .byteArrayToString (notificationData ));
228+ publishProgress ("Received notification uplink event 0xA101 with error code=" + MainActivity .mCs108Library4a .byteArrayToString (notificationData ));
229+ timeMillis = System .currentTimeMillis ();
230+ iTimeOut = 500 ;
231+ }
232+ if (System .currentTimeMillis () - timeMillis > iTimeOut ) {
233+ MainActivity .mCs108Library4a .appendToLog ("endingMessage: iTimeout = " + iTimeOut );
209234 taskCancelReason = TaskCancelRReason .TIMEOUT ;
210235 }
211- if (taskCancelReason != TaskCancelRReason .NULL ) cancel (true );
236+ if (taskCancelReason != TaskCancelRReason .NULL ) {
237+ MainActivity .mCs108Library4a .appendToLog ("taskCancelReason=" + TaskCancelRReason .values ());
238+ cancel (true );
239+ }
212240 }
213241 return "End of Asynctask():" + ending ;
214242 }
@@ -244,6 +272,7 @@ else if (output[0] != null) {
244272 resultError += output [0 ];
245273 if (true )
246274 MainActivity .mCs108Library4a .appendToLog ("output[0]: " + output [0 ] + ", resultError = " + resultError );
275+ taskCancelReason = TaskCancelRReason .ERROR ;
247276 }
248277 } else {
249278 if (registerYield != null ) {
@@ -260,8 +289,8 @@ else if (output[0] != null) {
260289 @ Override
261290 protected void onCancelled () {
262291 super .onCancelled ();
263- if (DEBUG ) MainActivity .mCs108Library4a .appendToLog ("AccessSecurityLockFragment.InventoryRfidTask.onCancelled()" );
264-
292+ if (DEBUG ) MainActivity .mCs108Library4a .appendToLog ("endingMesssage: taskCancelReason = " + taskCancelReason );
293+ //if (taskCancelReason != TaskCancelRReason.NULL) MainActivity.mCs108Library4a.abortOperation1(); else
265294 MainActivity .mCs108Library4a .abortOperation ();
266295 if (taskCancelReason == TaskCancelRReason .NULL ) taskCancelReason = TaskCancelRReason .DESTORY ;
267296 DeviceConnectTask4RegisterEnding ();
@@ -339,6 +368,9 @@ void DeviceConnectTask4RegisterEnding() {
339368 case BUTTON_RELEASE :
340369 strErrorMessage += "Finish as BUTTON is released. " ;
341370 break ;
371+ case ERROR :
372+ strErrorMessage += "Finish due to error received." ;
373+ break ;
342374 case TIMEOUT :
343375 strErrorMessage += "TIMEOUT without COMMAND_END. " ;
344376 break ;
@@ -354,7 +386,7 @@ void DeviceConnectTask4RegisterEnding() {
354386 if (strErrorMessage .length () != 0 ) endingMessaage = strErrorMessage ;
355387 button .setText (buttonText );
356388 if (endingMessaage != null ) {
357- if (endingMessaage .length () != 0 || true ) {
389+ if (endingMessaage .length () != 0 ) {
358390 MainActivity .mCs108Library4a .appendToLog ("endingMessage=" + endingMessaage );
359391 if (bEnableErrorPopWindow ) {
360392 CustomPopupWindow customPopupWindow = new CustomPopupWindow (MainActivity .mContext );
0 commit comments