1212
1313import com .csl .cslibrary4a .RfidReaderChipData ;
1414
15+ import java .io .IOException ;
16+ import java .net .DatagramPacket ;
17+ import java .net .DatagramSocket ;
18+ import java .net .InetAddress ;
19+ import java .net .SocketException ;
1520import java .util .ArrayList ;
1621
1722import static android .content .ContentValues .TAG ;
1823
1924public class CustomIME extends InputMethodService { //implements KeyboardView.OnKeyboardActionListener {
2025 Handler mHandler = new Handler ();
26+ InputConnection ic ;
27+ DatagramSocket datagramSocket ;
28+ InetAddress hostAddress = null ;
2129
2230 @ Override
2331 public void onCreate () {
2432 super .onCreate ();
33+ try {
34+ hostAddress = hostAddress = InetAddress .getByName ("127.0.0.1" );
35+ Log .i ("Hello" , "udpSocket hostAddress is valid" );
36+ datagramSocket = new DatagramSocket (9394 , hostAddress );
37+ if (datagramSocket == null ) Log .i ("Hello" , "udpSocket is null" );
38+ else Log .i ("Hello" , "udpSocket is valid" );
39+ //byte[] buffer = new byte[16];
40+ //DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("localhost"), 14552);
41+ //udpsocket.send(packet);
42+ } catch (SocketException e ) {
43+ Log .e ("UDP: " , "udpSocket Socket Error: " , e );
44+ } catch (IOException e ) {
45+ Log .e ("UDP Send: " , "udpSocket IO Error" , e );
46+ } catch (Exception ex ) {
47+ Log .i ("Hello" , "udpSocket Exception Error: " + ex .getMessage ());
48+ }
2549 appendToLog ("CustomIME.onCreate()" );
2650 }
2751 @ Override
@@ -35,81 +59,139 @@ public View onCreateInputView() {
3559 keyboardView .setKeyboard (keyboard );
3660 //keyboardView.setOnKeyboardActionListener(this);
3761 }
62+ ic = getCurrentInputConnection ();
63+ Log .i ("Hello" , "udpSocket inputConnection is " + (ic == null ? "null" : "valid" ));
3864 return keyboardView ;
3965 }
4066 @ Override
4167 public void onDestroy () {
4268 appendToLog ("CustomIME.onDestroy()" );
4369 mHandler .removeCallbacks (serviceRunnable );
70+ datagramSocket .close ();
4471 super .onDestroy ();
4572 }
4673
74+ Runnable yourRunnable = new Runnable () {
75+ @ Override
76+ public void run () {
77+ byte [] buffer = new byte [250 ];
78+ Log .i ("Hello" , "udpSocket yourRunnable starts" );
79+ DatagramPacket datagramPacket = new DatagramPacket (buffer , buffer .length );
80+ ic = getCurrentInputConnection ();
81+ try {
82+ Log .i ("Hello" , "udpSocket try starts" );
83+ // String outString = "Client say: bye bye";
84+ // buffer = outString.getBytes();
85+ // DatagramPacket outDatagramPackat = new DatagramPacket(buffer, buffer.length, hostAddress, 9394);
86+ // datagramSocket.send(outDatagramPackat);
87+ // Log.i("Hello", "udpSocket sent data");
88+
89+ String strDataReceived ;
90+ do {
91+ strDataReceived = null ;
92+ datagramSocket .receive (datagramPacket );
93+ strDataReceived = new String (datagramPacket .getData (), 0 , datagramPacket .getLength ());
94+ Log .i ("Hello" , "udpSocket received data: " + strDataReceived );
95+ ic .commitText (strDataReceived , 1 );
96+ } while (strDataReceived != null && strDataReceived .length () != 0 );
97+ } catch (IOException ex ) {
98+ Log .i ("Hello" , "udpSocket receive IOException Error: " + ex .toString ());
99+ }
100+ }
101+ };
102+
47103 ArrayList <String > epcArrayList = new ArrayList <String >();
48104 InventoryRfidTask inventoryRfidTask ;
105+ InventoryBarcodeTask inventoryBarcodeTask ;
49106 boolean inventoring = false ;
50107 private Runnable serviceRunnable = new Runnable () {
51108 @ Override
52109 public void run () {
53110 String strCurrentIME = Settings .Secure .getString (getContentResolver (), Settings .Secure .DEFAULT_INPUT_METHOD );
54111 String strCompare = getPackageName ();
55112 appendToLog ("CustomIME Debug 0 with strCurrentIME = " + strCurrentIME + ", strCompare = " + strCompare );
56- if (strCurrentIME .contains (strCompare ) == false ) return ;
57-
58- mHandler .postDelayed (serviceRunnable , 1000 );
59- if (MainActivity .sharedObjects == null ) return ;
60- if (MainActivity .csLibrary4A == null ) return ;
61-
62- if (inventoring == false ) { MainActivity .sharedObjects .serviceArrayList .clear (); epcArrayList .clear (); }
63- if (MainActivity .mContext == null ) return ;
64- appendToLog ("CustomIME Debug 1 with activityActive = " + MainActivity .activityActive + ", wedged = " + MainActivity .wedged + ", isBleConnected = " + MainActivity .csLibrary4A .isBleConnected ());
65- if (MainActivity .activityActive == false /*&& MainActivity.wedged*/ && MainActivity .csLibrary4A .isBleConnected ()) {
66- if (MainActivity .csLibrary4A .getTriggerButtonStatus () == false ) {
67- appendToLog ("CustomIME Debug 2" );
68- startStopHandler ();
69- inventoring = false ;
70- } else if (inventoring == false ) {
71- appendToLog ("CustomIME Debug 3" );
72- if (MainActivity .sharedObjects .runningInventoryRfidTask == false && MainActivity .sharedObjects .runningInventoryBarcodeTask == false && MainActivity .csLibrary4A .mrfidToWriteSize () == 0 ) {
113+ if (strCurrentIME .contains (strCompare ) == false ) { }
114+ else if (MainActivity .sharedObjects == null || MainActivity .csLibrary4A == null ) {
115+ if (false ) { new Thread (yourRunnable ).start (); }
116+ }
117+ else if (MainActivity .mContext == null ) return ;
118+ else {
119+ if (inventoring == false ) {
120+ MainActivity .sharedObjects .serviceArrayList .clear ();
121+ epcArrayList .clear ();
122+ }
123+ appendToLog ("CustomIME Debug 1 with activityActive = " + MainActivity .activityActive + ", wedged = " + MainActivity .wedged + ", isBleConnected = " + MainActivity .csLibrary4A .isBleConnected ());
124+ if (MainActivity .activityActive == false /*&& MainActivity.wedged*/ && MainActivity .csLibrary4A .isBleConnected ()) {
125+ if (MainActivity .csLibrary4A .getTriggerButtonStatus () == false ) {
126+ appendToLog ("CustomIME Debug 2 with runningInventoryRfidTask = " + MainActivity .sharedObjects .runningInventoryRfidTask );
127+ appendToLog ("CustomIME Debug 2 with runningInventoryBarcodeTask = " + MainActivity .sharedObjects .runningInventoryBarcodeTask );
73128 startStopHandler ();
74- inventoring = true ;
75- }
76- } else {
77- appendToLog ("CustomIME Debug 4" );
78- while (MainActivity .sharedObjects .serviceArrayList .size () != 0 ) {
79- String strEpc = MainActivity .sharedObjects .serviceArrayList .get (0 ); MainActivity .sharedObjects .serviceArrayList .remove (0 );
80- boolean matched = false ;
81- for (int i = 0 ; i < epcArrayList .size (); i ++) {
82- if (epcArrayList .get (i ).matches (strEpc )) {
83- matched = true ;
84- break ;
85- }
129+ inventoring = false ;
130+ } else if (inventoring == false ) {
131+ appendToLog ("CustomIME Debug 3 with runningInventoryRfidTask = " + MainActivity .sharedObjects .runningInventoryRfidTask + ", and mrfidToWriteSize = " + MainActivity .csLibrary4A .mrfidToWriteSize ());
132+ appendToLog ("CustomIME Debug 3 with runningInventoryBarcodeTask = " + MainActivity .sharedObjects .runningInventoryBarcodeTask );
133+ if (MainActivity .sharedObjects .runningInventoryRfidTask == false && MainActivity .sharedObjects .runningInventoryBarcodeTask == false && MainActivity .csLibrary4A .mrfidToWriteSize () == 0 ) {
134+ startStopHandler ();
135+ inventoring = true ;
86136 }
87- if (matched == false && strEpc != null ) {
88- epcArrayList .add (strEpc );
89- InputConnection ic = getCurrentInputConnection ();
90- if (MainActivity .wedgePrefix != null ) strEpc = MainActivity .wedgePrefix + strEpc ;
91- if (MainActivity .wedgeSuffix != null ) strEpc += MainActivity .wedgeSuffix ;
92- switch (MainActivity .wedgeDelimiter ) {
93- default :
94- strEpc += "\n " ;
95- break ;
96- case 0x09 :
97- strEpc += "\t " ;
98- break ;
99- case 0x2C :
100- strEpc += "," ;
101- break ;
102- case 0x20 :
103- strEpc += " " ;
104- break ;
105- case -1 :
106- break ;
137+ } else {
138+ appendToLog ("CustomIME Debug 4" );
139+ while (MainActivity .sharedObjects .serviceArrayList .size () != 0 ) {
140+ String strEpc = MainActivity .sharedObjects .serviceArrayList .get (0 );
141+ MainActivity .sharedObjects .serviceArrayList .remove (0 );
142+ appendToLog ("CustomIME Debug 4A with strEpc = " + strEpc );
143+ String strSgtin = null ;
144+ if (MainActivity .csLibrary4A .getWedgeOutput () == 1 ) {
145+ strSgtin = MainActivity .csLibrary4A .getUpcSerial (strEpc );
146+ MainActivity .csLibrary4A .appendToLog ("strSgtin = " + (strSgtin == null ? "null" : strSgtin ));
147+ if (strSgtin == null ) strEpc = null ;
148+ }
149+ boolean matched = false ;
150+ if (epcArrayList != null && strEpc != null ) {
151+ for (int i = 0 ; i < epcArrayList .size (); i ++) {
152+ if (epcArrayList .get (i ).matches (strEpc )) {
153+ matched = true ;
154+ break ;
155+ }
156+ }
157+ }
158+ if (matched == false && strEpc != null ) {
159+ epcArrayList .add (strEpc );
160+ InputConnection ic = getCurrentInputConnection ();
161+ String strValue = strEpc ;
162+ if (strSgtin != null ) strValue = strSgtin ;
163+ if (MainActivity .csLibrary4A .getWedgePrefix () != null )
164+ strValue = MainActivity .csLibrary4A .getWedgePrefix () + strValue ;
165+ if (MainActivity .csLibrary4A .getWedgeSuffix () != null )
166+ strValue += MainActivity .csLibrary4A .getWedgeSuffix ();
167+ MainActivity .csLibrary4A .appendToLog ("CustomIME, serviceRunnable: wedgeDelimiter = " + MainActivity .csLibrary4A .getWedgeDelimiter ());
168+ switch (MainActivity .csLibrary4A .getWedgeDelimiter ()) {
169+ default :
170+ strValue += "\n " ;
171+ break ;
172+ case 0x09 :
173+ strValue += "\t " ;
174+ break ;
175+ case 0x2C :
176+ strValue += "," ;
177+ break ;
178+ case 0x20 :
179+ strValue += " " ;
180+ break ;
181+ case -1 :
182+ break ;
183+ }
184+ MainActivity .csLibrary4A .appendToLog ("CustomIME BtData to Keyboard: " + strValue );
185+ ic .commitText (strValue , 1 );
107186 }
108- ic .commitText (strEpc , 1 );
109187 }
110188 }
111189 }
112190 }
191+ int iDelayms = 500 ;
192+ if (inventoring ) iDelayms = 100 ;
193+ MainActivity .csLibrary4A .appendToLog ("CustomIME BtData set next time as " + iDelayms );
194+ mHandler .postDelayed (serviceRunnable , iDelayms );
113195 }
114196 };
115197
@@ -121,17 +203,26 @@ void startStopHandler() {
121203 if (inventoryRfidTask != null ) {
122204 if (inventoryRfidTask .getStatus () == AsyncTask .Status .RUNNING ) started = true ;
123205 }
206+ if (inventoryBarcodeTask != null ) {
207+ if (inventoryBarcodeTask .getStatus () == AsyncTask .Status .RUNNING ) started = true ;
208+ }
124209 appendToLog ("CustomIME Debug 10" );
125210 if ((started && MainActivity .csLibrary4A .getTriggerButtonStatus ()) || (started == false && MainActivity .csLibrary4A .getTriggerButtonStatus () == false )) return ;
126211 if (started == false ) {
212+ appendToLog ("CustomIME Debug 11 with BtData wedgeOutput = " + MainActivity .csLibrary4A .getWedgeOutput ());
213+ if (MainActivity .csLibrary4A .getWedgeOutput () == 2 ) {
214+ inventoryBarcodeTask = new InventoryBarcodeTask ();
215+ inventoryBarcodeTask .execute ();
216+ } else {
217+ MainActivity .csLibrary4A .setPowerLevel (MainActivity .csLibrary4A .getWedgePower ());
218+ MainActivity .csLibrary4A .startOperation (RfidReaderChipData .OperationTypes .TAG_INVENTORY_COMPACT );
219+ inventoryRfidTask = new InventoryRfidTask ();
220+ inventoryRfidTask .execute ();
221+ }
222+ } else {
127223 appendToLog ("CustomIME Debug 11" );
128- MainActivity .csLibrary4A .startOperation (RfidReaderChipData .OperationTypes .TAG_INVENTORY );
129- inventoryRfidTask = new InventoryRfidTask ();
130- inventoryRfidTask .execute ();
131- }
132- else {
133- appendToLog ("CustomIME Debug 11" );
134- inventoryRfidTask .taskCancelReason = InventoryRfidTask .TaskCancelRReason .BUTTON_RELEASE ;
224+ if (inventoryRfidTask != null ) inventoryRfidTask .taskCancelReason = InventoryRfidTask .TaskCancelRReason .BUTTON_RELEASE ;
225+ if (inventoryBarcodeTask != null ) inventoryBarcodeTask .taskCancelReason = InventoryBarcodeTask .TaskCancelRReason .BUTTON_RELEASE ;
135226 }
136227 }
137228}
0 commit comments