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.Base64; 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 */ public void handleDecode(String result) { if (result.equals("")) { MToast(NewHandleScanerActivity.this, getString(R.string.scan_fail), MToast.LENGTH_SHORT); } else { try { if (result.startsWith("CYT_")) { result = result.substring(result.indexOf("_") + 1, result.indexOf(",")); ArrayList 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); } else if (result.startsWith("TDOS_")) { result = result.substring( result.indexOf("_") + 1, result.indexOf(",")); result = new String(Base64.decode(result), "utf-8"); } else if ((result.startsWith("TY_") || (result.startsWith("PMS_")))) { result = result; } else { result = result; } } 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(); } }