ScanerIdCardActivity.java 11.1 KB
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
package com.ectrip.cyt.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.atomic.AtomicBoolean;

import android.annotation.SuppressLint;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ectrip.cyt.config.MyApp;
import com.ectrip.cyt.shield_home.LockLayer.MToast;
import com.ectrip.cyt.utils.LogUtil;
import com.ectrip.cyt.utils.SharedPreferences2Obj;
import com.ectrip.trips.check.R;
import com.fri.idcread.idcread;

/**
 * 手持机身份证扫描页面
 *
 * @author jigo
 */
public class ScanerIdCardActivity extends BaseActivity {

	private Button topBtns;
	private TextView topBack;
	private Dialog dialog = null;
	private String shuju;
	private Button readIDButton;
	private String titleName;

	private AtomicBoolean isStop = new AtomicBoolean(false);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.read_idcard);
		initView();
		idcread.init();
	}

	private void initView() {
		try {
			SharedPreferences2Obj.getInstance(ScanerIdCardActivity.this)
					.setName("SelectAction").setObject("isStatistic", "0"); // 非统计
		} catch (Exception e) {
			e.printStackTrace();
		}
		titleName = getIntent().getStringExtra("titleName");
		if (titleName != null) {
			((TextView) findViewById(R.id.title)).setText(titleName);
		} else {
			((TextView) findViewById(R.id.title)).setText(R.string.scan_id);
		}
		((TextView) findViewById(R.id.title)).setVisibility(View.VISIBLE);
		topBack = (TextView) findViewById(R.id.topBack);
		topBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				isStop.set(true);
				setpower("/sys/class/gpio/gpio28/value", false);
				idcread.CloseComm();
				finish();
			}
		});
		topBtns = (Button) findViewById(R.id.topBtns);
		topBtns.setVisibility(View.GONE);
		readIDButton = (Button) findViewById(R.id.read);
		readIDButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if (dialog == null) {
					Builder builder = new Builder(ScanerIdCardActivity.this);
					builder.setTitle(R.string.scan_id);
					builder.setMessage(R.string.loadiing);
					dialog = builder.create();
					if (MyApp.getInstance().isHandset) {
						dialog.getWindow().setType(
								WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
					}
					dialog.setCanceledOnTouchOutside(true);
					dialog.setCancelable(true);
					dialog.show();
					// readIdCard(0);
					new readTread(0).start();
				} else {
					dialog.show();
					// readIdCard(0);
					new readTread(0).start();
				}
			}
		});
	}

	/**
	 * 读取身份证线程
	 *
	 * @author jigo
	 */
	class readTread extends Thread {
		public int num;

		public readTread(int num) {
			this.num = num;
		}

		@Override
		public void run() {
			super.run();
			try {
				readIdCard(num);
			} catch (Exception e) {
				e.printStackTrace();
			} catch (Throwable e) {
				// TODO: handle exception
			}
		}
	}

	/**
	 * 读取身份证
	 */
	@SuppressLint("SdCardPath")
	public void readIdCard(int num) throws Throwable {
		if (idcread.Authenticate() == 1 && !isStop.get())/* 身份证认证 */
		{
			LogUtil.d(TAG,getString(R.string.id_auth_success));
			if (idcread.ReadContent("/mnt/sdcard/") == 1)/* 读取身份证数据 *///
			{
				LogUtil.d(TAG,getString(R.string.read_id_info_success));
				try {
					testRead();
				} catch (Exception e) {
					e.printStackTrace();
					if (dialog != null && dialog.isShowing()) {
						try {
							dialog.dismiss();
						} catch (Exception e1) {
							e1.printStackTrace();
						} catch (Throwable e1) {
							e1.printStackTrace();
						}
					}
				}
			} else {
				if (dialog != null && dialog.isShowing()) {
					try {
						dialog.dismiss();
					} catch (Exception e) {
						e.printStackTrace();
					} catch (Throwable e) {
						e.printStackTrace();
					}
				}
				LogUtil.d(TAG,getString(R.string.read_id_info_fail));
				MToast(ScanerIdCardActivity.this,
						getString(R.string.read_id_info_fail),
						MToast.LENGTH_SHORT);
			}
		} else {
			num++;
			if (num <= 3) {
				readIdCard(num);
			} else {
				if (dialog != null && dialog.isShowing()) {
					try {
						dialog.dismiss();
						dialog = null;
					} catch (Exception e) {
						e.printStackTrace();
					} catch (Throwable e) {
						e.printStackTrace();
					}
				}
				num = 0;
				LogUtil.d(TAG,getString(R.string.id_auth_fail));
				MToast(ScanerIdCardActivity.this,
						getString(R.string.id_stick_to_the_back),
						MToast.LENGTH_SHORT);
			}

		}
	}

	public final int ReadHandler = 10;
	@SuppressLint("HandlerLeak")
	public final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case ReadHandler:
					try {
						String id = msg.getData().getString("id");
						String pwd = msg.getData().getString("pwd");
						MToast(ScanerIdCardActivity.this,
								getString(R.string.id_number) + id,
								Toast.LENGTH_SHORT);
						trunto(id, pwd);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				default:
					break;
			}
		}
	};

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		isStop.set(true);
		setpower("/sys/class/gpio/gpio28/value", false);
		idcread.CloseComm();
		this.finish();
	}

	@Override
	protected void onResume() {
		super.onResume();
		copyDatabase();
	}

	@Override
	protected void onDestroy() {
		isStop.set(true);
		try {
			Thread.sleep(600);
		} catch (InterruptedException e) {
		}
		// closeDrive();
		setpower("/sys/class/gpio/gpio28/value", false);
		idcread.CloseComm();
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_HOME) {
			isStop.set(true);
			// 在这里的处理和back一样
			setpower("/sys/class/gpio/gpio28/value", false);
			idcread.CloseComm();
			this.finish();
			// return super.onKeyDown(keyCode, event);
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			isStop.set(true);
			setpower("/sys/class/gpio/gpio28/value", false);
			idcread.CloseComm();
			this.finish();
		}
		return false;
	}

	public 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());
		}
	}

	public void testRead() throws IOException {
		// file1.createNewFile();
		FileInputStream in = new FileInputStream(idcread.file1);
		FileChannel channel = in.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate((int) idcread.file1.length());
		channel.read(buffer);
		getRestore(buffer);
		if (in != null) {
			in.close();
		}
	}

	public void getRestore(ByteBuffer oldBuf) {
		// System.out.println("hehe");
		oldBuf.rewind();
		int size = oldBuf.capacity();
		ByteBuffer reBuffer = ByteBuffer.allocate(size + 2);
		byte[] heard = new byte[] { (byte) 0xFE, (byte) 0xEF };
		reBuffer.put(heard);
		byte[] oldbyte = new byte[2];
		while (oldBuf.position() < size) {
			oldBuf.get(oldbyte);
			reBuffer.put(getswap(oldbyte));
		}
		try {
			shuju = new String(reBuffer.array(), "utf-16");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
		}
		chuli();
	}

	private byte[] getswap(byte[] bb) {
		byte[] returnb = new byte[2];
		returnb[0] = bb[1];
		returnb[1] = bb[0];
		return returnb;
	}

	public void chuli() {
		if (shuju == null) {
			return;
		}

		int a = shuju.length();
		char[] temp = shuju.toCharArray();
		shuju = null;
		StringBuffer nian = new StringBuffer();
		StringBuffer yue = new StringBuffer();
		StringBuffer ri = new StringBuffer();
		StringBuffer id = new StringBuffer();
		for (int i = 0; i < a; i++) {
			if (i >= 19 && i <= 22) {
				nian.append(temp[i]);
			}
			if (i >= 23 && i <= 24) {
				yue.append(temp[i]);
			}
			if (i >= 25 && i <= 26) {
				ri.append(temp[i]);
			}
			if (i >= 62 && i <= 79) {
				id.append(temp[i]);
			}
		}
		String pwd = nian.toString() + yue.toString() + ri.toString();
		// trunto(id.toString(), pwd);
		Message message = new Message();
		message.what = ReadHandler;
		Bundle data = new Bundle();
		data.putString("id", id.toString());
		data.putString("pwd", pwd);
		message.setData(data);
		handler.sendMessage(message);
	}

	/**
	 * 跳转页面
	 *
	 * @param id
	 * @param pwd
	 */
	private void trunto(String id, String pwd) {
		if (id == null) {
			MToast(ScanerIdCardActivity.this, getString(R.string.get_id_fail),
					Toast.LENGTH_SHORT);
			return;
		}

		Intent intent = new Intent(ScanerIdCardActivity.this,
				IDOrderListActivity.class);
		intent.putExtra("mode", 0);
		intent.putExtra("idcardNumber", id);
		intent.putExtra("titleName",getString(R.string.order_list));
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}
		startActivity(intent);

		isStop.set(true);
		setpower("/sys/class/gpio/gpio28/value", false);
		idcread.CloseComm();
		finish();
	}

	@Override
	protected void onPause() {
		isStop.set(true);
		try {
			Thread.sleep(600);
		} catch (InterruptedException e) {
		}
		// closeDrive();
		setpower("/sys/class/gpio/gpio28/value", false);
		idcread.CloseComm();
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
		super.onPause();
	}

	// 把数据库复制到指定目录
	public void copyDatabase() {
		try {
			// 获得li.db 绝对路径
			String DATABASEFN = idcread.databasepath + idcread.databasefn;
			File dir = new File(idcread.databasepath);

			// 如果/sdcard/li目录中存在,创建这个目录
			if (!dir.exists()) {
				// System.out.print("1");
				dir.mkdir();
			}
			// dir.mkdir();

			// 如果在目录中不存在
			// 文件,则从res\raw目录中复制这个文件到
			if (!(new File(DATABASEFN).exists())) {
				// 获得封装 文件的InputStream对象
				InputStream is = getResources().openRawResource(R.raw.license);
				// System.out.println("2");
				FileOutputStream fos = new FileOutputStream(DATABASEFN);
				// System.out.println("2.1");
				// 下面这一段代码不是很理解,有待研究
				int length = is.available();
				byte[] buffer = new byte[length]; // 8192
				// System.out.println("3");
				int count = 0;
				// 开始复制db文件
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
					// System.out.println("4");
				}
				fos.close();
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}