Blame view

src/com/ectrip/cyt/ui/ScanerHandsetActivity.java 6.68 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
  package com.ectrip.cyt.ui;
  
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
  import java.util.ArrayList;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Handler;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  
  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.cyt.utils.LogUtil;
  import com.ectrip.cyt.utils.SharedPreferences2Obj;
  import com.ectrip.trips.check.R;
  
  /**
   * @author jigo 手持机二维码扫描
   *
   */
  public class ScanerHandsetActivity extends SerialPortHandsetActivity {
  	private TextView topBack;
  	private Button topBtns;
  	private Button button1;
  	private String titleName;
  
  	@Override
  	protected void onCreate(Bundle savedInstanceState) {
  		super.onCreate(savedInstanceState);
  		setContentView(R.layout.app_scan_handset);
  		init();
  	}
  
  	private void init() {
  		SharedPreferences2Obj.getInstance(ScanerHandsetActivity.this)
  				.setName("SelectAction").setObject("isStatistic", "0"); // 非统计
  		// 当判断是否为手持机
  		if (DevicTool.getInstance().isSerialPortHandset()) {
  			initSerialPortHandset();
  		}
  		titleName = getIntent().getStringExtra("titleName");
  		if (titleName != null) {
  			((TextView) findViewById(R.id.title)).setText(titleName);
  		} else {
  			((TextView) findViewById(R.id.title))
  					.setText(getString(R.string.scan_qr_code));
  		}
  
  		((TextView) findViewById(R.id.title)).setVisibility(View.VISIBLE);
  		topBtns = (Button) findViewById(R.id.topBtns);
  		topBtns.setVisibility(View.GONE);
  		topBack = (TextView) findViewById(R.id.topBack);
  		topBack.setOnClickListener(new OnClickListener() {
  			@Override
  			public void onClick(View arg0) {
  				finish();
  			}
  		});
  
  		button1 = (Button) findViewById(R.id.button1);
  		button1.setOnClickListener(new OnClickListener() {
  			@Override
  			public void onClick(View arg0) {
  				ScanCode();
  				new Handler().postDelayed(new Runnable() {
  					public void run() {
  						ScanCode();
  					}
  				}, 600);
  			}
  		});
  
  	}
  
  	/*
  	 * 下面都是手持机二维码代码
  	 */
  
  	private void initSerialPortHandset() {
  		setpower("/sys/class/gpio/gpio28/value", true);
  		try {
  			Thread.sleep(500);
  		} catch (InterruptedException e) {
  		}
  		setpower("/sys/class/gpio/gpio28/value", false); // 给二代证下电
  		try {
  			Thread.sleep(500);
  		} catch (InterruptedException e) {
  		}
  		setpower("/sys/class/gpio/gpio114/value", true);
  		try {
  			Thread.sleep(1000);
  		} catch (InterruptedException e) {
  		}
  		// thread=new ScanerHandsetThread();
  		// thread.start();
  	}
  
  	/**
  	 * 扫描二维码
  	 */
  	public void ScanCode() {
  		byte[] arrayOfByte = { 0x1b, 0x31 };
  		try {
  			mOutputStream.write(arrayOfByte);
  		} catch (IOException localIOException) {
  			localIOException.printStackTrace();
  		}
  	}
  
  	private int second = 0;
  	StringBuffer StringBuffer = new StringBuffer();
  
  	private String regEx = "[\u4e00-\u9fa5]";
  	private Pattern pat = Pattern.compile(regEx);
  
  	public boolean isContainsChinese(String str) {
  		Matcher matcher = pat.matcher(str);
  		boolean flg = false;
  		if (matcher.find()) {
  			flg = true;
  		}
  		return flg;
  	}
  
  	@Override
  	protected void onDataReceived(final byte[] buffer, final int size) {
  		runOnUiThread(new Runnable() {
  			public void run() {
  				String tempstr = new String(buffer, 0, size);
  				try {
  					String resultString = new String(tempstr.getBytes("GBK"),
  							"GBK");
  					if (resultString.contains("CYT_")) {
  						if (second < 2) {
  							StringBuffer.append(resultString);
  						}
  						second += 1;
  					} else {
  						if (second < 2) {
  							StringBuffer.append(resultString);
  						}
  					}
  					System.out.println("resultString:" + resultString);
  					if (second == 2) {
  						synchronized (StringBuffer) {
  
  							resultString = StringBuffer.toString();
  							resultString = resultString.substring(
  									resultString.indexOf("_") + 1,
  									resultString.indexOf(","));
  							ArrayList<ConfigBean> beans = DbManager
  									.GetConfigs();
  							// 畅游通生成的订单ID:
  							String ec_name = beans.get(0).getEc_signkey();
  							ec_name = AESEncryptor.decrypt(constant.decrypt,
  									ec_name);
  							resultString = DesUtil.decrypt(resultString,
  									ec_name);
  
  							if (resultString != null
  									&& isContainsChinese(resultString)) {
  								try {
  									MToast(ScanerHandsetActivity.this,
  											resultString, MToast.LENGTH_LONG);
  								} catch (Exception e) {
  									e.printStackTrace();
  								}
  								return;
  							}
  
  							Intent intent = new Intent(
  									ScanerHandsetActivity.this,
  									QRCodeOrderListActivity.class);
  							intent.putExtra("mode", 1);
  							intent.putExtra("input_orid", resultString);
c0c53083   杜方   畅游通核销app: 1.增加本地写入日志
192
  							LogUtil.i(TAG,"scan_result:" + resultString);
3c2353cd   杜方   1、畅游通核销app源码提交;
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
  							intent.putExtra("titleName",
  									getString(R.string.show_result));
  							startActivity(intent);
  							finish();
  						}
  					}
  				} catch (UnsupportedEncodingException e) {
  					e.printStackTrace();
  					try {
  						e.printStackTrace();
  						MToast(ScanerHandsetActivity.this,
  								getString(R.string.setting_is_correct),
  								MToast.LENGTH_LONG);
  					} catch (Exception e1) {
  						e1.printStackTrace();
  					}
  				} catch (Exception e) {
  					e.printStackTrace();
  					try {
  						e.printStackTrace();
  						MToast(ScanerHandsetActivity.this,
  								getString(R.string.setting_is_correct),
  								MToast.LENGTH_LONG);
  					} catch (Exception e1) {
  						e1.printStackTrace();
  					}
  				}
  			}
  		});
  	}
  
  	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) {
  			LogUtil.e(TAG, e.toString());
  		}
  	}
  
  	@Override
  	public boolean onKeyDown(int keyCode, KeyEvent event) {
  		if (keyCode == KeyEvent.KEYCODE_BACK) {
  			setpower("/sys/class/gpio/gpio114/value", false);
  			finish();
  		}
  		return false;
  	}
  
  	/*
  	 * 按返回键
  	 */
  	@Override
  	public void onBackPressed() {
  		setpower("/sys/class/gpio/gpio114/value", false);
  		super.onBackPressed();
  	}
  
  	@Override
  	protected void onDestroy() {
  		super.onDestroy();
  		try {
  			Thread.sleep(600);
  		} catch (InterruptedException e) {
  		}
  		setpower("/sys/class/gpio/gpio114/value", false);
  		// if (mReadThread != null)
  		// mReadThread.interrupt();
  	}
  }