12
12
13
13
import com .csl .cslibrary4a .RfidReaderChipData ;
14
14
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 ;
15
20
import java .util .ArrayList ;
16
21
17
22
import static android .content .ContentValues .TAG ;
18
23
19
24
public class CustomIME extends InputMethodService { //implements KeyboardView.OnKeyboardActionListener {
20
25
Handler mHandler = new Handler ();
26
+ InputConnection ic ;
27
+ DatagramSocket datagramSocket ;
28
+ InetAddress hostAddress = null ;
21
29
22
30
@ Override
23
31
public void onCreate () {
24
32
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
+ }
25
49
appendToLog ("CustomIME.onCreate()" );
26
50
}
27
51
@ Override
@@ -35,81 +59,139 @@ public View onCreateInputView() {
35
59
keyboardView .setKeyboard (keyboard );
36
60
//keyboardView.setOnKeyboardActionListener(this);
37
61
}
62
+ ic = getCurrentInputConnection ();
63
+ Log .i ("Hello" , "udpSocket inputConnection is " + (ic == null ? "null" : "valid" ));
38
64
return keyboardView ;
39
65
}
40
66
@ Override
41
67
public void onDestroy () {
42
68
appendToLog ("CustomIME.onDestroy()" );
43
69
mHandler .removeCallbacks (serviceRunnable );
70
+ datagramSocket .close ();
44
71
super .onDestroy ();
45
72
}
46
73
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
+
47
103
ArrayList <String > epcArrayList = new ArrayList <String >();
48
104
InventoryRfidTask inventoryRfidTask ;
105
+ InventoryBarcodeTask inventoryBarcodeTask ;
49
106
boolean inventoring = false ;
50
107
private Runnable serviceRunnable = new Runnable () {
51
108
@ Override
52
109
public void run () {
53
110
String strCurrentIME = Settings .Secure .getString (getContentResolver (), Settings .Secure .DEFAULT_INPUT_METHOD );
54
111
String strCompare = getPackageName ();
55
112
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 );
73
128
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 ;
86
136
}
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 );
107
186
}
108
- ic .commitText (strEpc , 1 );
109
187
}
110
188
}
111
189
}
112
190
}
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 );
113
195
}
114
196
};
115
197
@@ -121,17 +203,26 @@ void startStopHandler() {
121
203
if (inventoryRfidTask != null ) {
122
204
if (inventoryRfidTask .getStatus () == AsyncTask .Status .RUNNING ) started = true ;
123
205
}
206
+ if (inventoryBarcodeTask != null ) {
207
+ if (inventoryBarcodeTask .getStatus () == AsyncTask .Status .RUNNING ) started = true ;
208
+ }
124
209
appendToLog ("CustomIME Debug 10" );
125
210
if ((started && MainActivity .csLibrary4A .getTriggerButtonStatus ()) || (started == false && MainActivity .csLibrary4A .getTriggerButtonStatus () == false )) return ;
126
211
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 {
127
223
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 ;
135
226
}
136
227
}
137
228
}
0 commit comments