Blame view

src/com/ectrip/cyt/ui/NewHandleScanerActivity.java 7.05 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  package com.ectrip.cyt.ui;
  
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.UnsupportedEncodingException;
  import java.security.InvalidParameterException;
  import java.util.ArrayList;
  import java.util.concurrent.atomic.AtomicBoolean;
  
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnClickListener;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  import android_serialport_api.SerialPortNewHandset;
  
  import com.ectrip.cyt.bean.ConfigBean;
  import com.ectrip.cyt.config.DevicTool;
  import com.ectrip.cyt.config.MyApp;
  import com.ectrip.cyt.constant.constant;
  import com.ectrip.cyt.db.DbManager;
  import com.ectrip.cyt.shield_home.LockLayer.MToast;
  import com.ectrip.cyt.utils.AESEncryptor;
  import com.ectrip.cyt.utils.DesUtil;
  import com.ectrip.trips.check.R;
  
  /**
   * @author jigo 新的手持机二维码扫描
   */
  public class NewHandleScanerActivity extends BaseActivity {
  	private final String TAG = "qrcode";
  	protected MyApp mApplication;
  	protected SerialPortNewHandset mSerialPort;
  	protected OutputStream mOutputStream;
  	private InputStream mInputStream;
  	private ReadThread mReadThread;
  	private String titleName;
  
  	private Button scanerBtn;
  	private View topBack;
  	private TextView title;
  
  	private AtomicBoolean isStop = new AtomicBoolean(false);
  
  	@Override
  	protected void onCreate(Bundle savedInstanceState) {
  		super.onCreate(savedInstanceState);
  		setContentView(R.layout.activity_newhandle_scaner);
  
  		initView();
  		init();
  
  		setpower("/sys/class/gpio/gpio28/value", false); // 给二代证下电
  		try {
  			Thread.sleep(1000);
  		} catch (InterruptedException e) {
  		}
  		setpower("/sys/class/gpio/gpio114/value", true);
  
  		// setTitle("Loopback test");
  		scanerBtn = (Button) findViewById(R.id.scanerBtn);
  		scanerBtn.setOnClickListener(new View.OnClickListener() {
  			public void onClick(View paramAnonymousView) {
  				byte[] arrayOfByte = { 0x1b, 0x31 };
  				try {
  					NewHandleScanerActivity.this.mOutputStream
  							.write(arrayOfByte);
  					return;
  				} catch (IOException localIOException) {
  					localIOException.printStackTrace();
  				}
  			}
  		});
  	}
  
  	private void initView() {
  		title = (TextView) findViewById(R.id.title);
  		topBack = findViewById(R.id.topBack);
  		topBack.setOnClickListener(new View.OnClickListener() {
  			@Override
  			public void onClick(View v) {
  				close();
  				finish();
  			}
  		});
  	}
  
  	private void init() {
  		mApplication = (MyApp) getApplication();
  		titleName = getIntent().getStringExtra("titleName");
  		title.setVisibility(View.VISIBLE);
  		if (titleName != null) {
  			title.setText(titleName);
  		} else {
  			title.setText(R.string.qr_code);
  		}
  
  		try {
  			mSerialPort = DevicTool.getInstance().getSerialPortNewHandset();
  			mOutputStream = mSerialPort.getOutputStream();
  			mInputStream = mSerialPort.getInputStream();
  
  			/* Create a receiving thread */
  			mReadThread = new ReadThread();
  			mReadThread.start();
  		} catch (SecurityException e) {
  			DisplayError(R.string.error_security);
  		} catch (IOException e) {
  			DisplayError(R.string.error_unknown);
  		} catch (InvalidParameterException e) {
  			DisplayError(R.string.error_configuration);
  		}
  	}
  
  	public void delay(int i) {
  		for (int j = 0; j < 1000; j++)
  			for (; i < 0; i--)
  				;
  	}
  
  	protected void onDataReceived(final byte[] buffer, final int size) {
  		runOnUiThread(new Runnable() {
  			public void run() {
  
  				try {
  					String tempstr = new String(buffer, 0, size);
  					handleDecode(new String(tempstr.getBytes("GBK"), "GBK"));
  				} catch (UnsupportedEncodingException e) {
  					e.printStackTrace();
  				} catch (Exception e) {
  					e.printStackTrace();
  				}
  				// mReception.append(new String(buffer, 0, size));
  			}
  		});
  	}
  
  	/**
  	 * 处理扫描结果
  	 *
  	 * @param result
  	 * @param barcode
  	 */
  	public void handleDecode(String result) {
  		if (result.equals("")) {
  			MToast(NewHandleScanerActivity.this, getString(R.string.scan_fail),
  					MToast.LENGTH_SHORT);
  		} else {
  			try {
  				result = result.substring(result.indexOf("_") + 1,
  						result.indexOf(","));
  				ArrayList<ConfigBean> beans = DbManager.GetConfigs();
  				// 畅游通生成的订单ID:
  				String ec_name = beans.get(0).getEc_signkey();
  				ec_name = AESEncryptor.decrypt(constant.decrypt, ec_name);
  				result = DesUtil.decrypt(result, ec_name);
  			} catch (Exception e) {
  				e.printStackTrace();
  				MToast(NewHandleScanerActivity.this,
  						getString(R.string.order_id_incorrect),
  						MToast.LENGTH_SHORT);
  			}
  
  			Intent intent = new Intent(NewHandleScanerActivity.this,
  					QRCodeOrderListActivity.class);
  			intent.putExtra("mode", 1);
  			intent.putExtra("input_orid", result);
  			if (result != null) {
  				MToast(NewHandleScanerActivity.this, result, Toast.LENGTH_SHORT);
  			}
  			intent.putExtra("titleName",getString(R.string.show_result));
  			startActivity(intent);
  			finish();
  		}
  		finish();
  	}
  
  	private void setpower(String path, boolean on) {
  		File file;
  		FileWriter fr;
  		try {
  			file = new File(path);
  			fr = new FileWriter(file);
  			if (on)
  				fr.write("1");
  			else
  				fr.write("0");
  			fr.close();
  		} catch (IOException e) {
  			Log.e(TAG, e.toString());
  		}
  	}
  
  	@Override
  	public void onBackPressed() {
  		super.onBackPressed();
  		close();
  		this.finish();
  	}
  
  	@Override
  	protected void onResume() {
  		super.onResume();
  		setpower("/sys/class/gpio/gpio114/value", true);
  	}
  
  	private class ReadThread extends Thread {
  
  		@Override
  		public void run() {
  			super.run();
  			while (!isInterrupted() && !isStop.get()) {
  				int size;
  				try {
  					byte[] buffer = new byte[64];
  					if (mInputStream == null)
  						return;
  					size = mInputStream.read(buffer);
  					if (size > 0) {
  						onDataReceived(buffer, size);
  					}
  				} catch (IOException e) {
  					e.printStackTrace();
  					return;
  				}
  			}
  		}
  	}
  
  	@Override
  	public void onPause() {
  		// bContinue = false;
  		try {
  			Thread.sleep(600);
  		} catch (InterruptedException e) {
  		}
  		// closeDrive();
  		super.onPause();
  		setpower("/sys/class/gpio/gpio114/value", false);
  
  	}
  
  	private void close() {
  		isStop.set(true);
  		if (mOutputStream != null) {
  			try {
  				mOutputStream.close();
  			} catch (IOException e) {
  				// TODO Auto-generated catch block
  				e.printStackTrace();
  			}
  		}
  		if (mOutputStream != null) {
  			try {
  				mOutputStream.close();
  			} catch (IOException e) {
  				e.printStackTrace();
  			}
  		}
  		if (mSerialPort != null) {
  			mSerialPort.close();
  		}
  		if (mReadThread != null)
  			mReadThread.interrupt();
  		DevicTool.getInstance().closeSerialPortNewHandset();
  		mSerialPort = null;
  	}
  
  	@Override
  	protected void onDestroy() {
  		close();
  		super.onDestroy();
  	}
  
  	private void DisplayError(int resourceId) {
  		AlertDialog.Builder b = new AlertDialog.Builder(this);
  		b.setTitle("Error");
  		b.setMessage(resourceId);
  		b.setPositiveButton("OK", new OnClickListener() {
  			public void onClick(DialogInterface dialog, int which) {
  				finish();
  			}
  		});
  		b.show();
  	}
  }