package com.andorid.MuliModeComdemo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.Toast;

import com.andorid.MFR0311HID.R;
import com.gary.msr.hid.USBOp;
import com.gary.rfid.proto.RFIDPnPacket;

public class RFID_DEV extends Activity implements OnClickListener {
	Spinner serialPort_spinner;
	Button	fm4_Open;
	Button	fm4_Refresh;
	Button	fm4_Init;
	Button	fm4_Sleep;
	Button	fm4_Close;
	/////////////////////////////////////////////////////////////////////////////////
	Spinner fm4_spinner;
	Button fm4_search;
	Button fm4_read;
	Button fm4_write;
	Button fm4_Initialize;
	Button fm4_Increase;
	Button fm4_Decrease;
	Button fm4_Balance;
	Button fm4_cancel;
	Button fm4_CPUSearch;
	Button fm4_CPUAPDU;
	Button fm4_CPUCancel;
	/////////////////////
	Button fm4_ISO14443BSearch;
	Button fm4_ISO14443BAPDU;
	Button fm4_ISO14443BCancel;
	Button fm4_FelicaSearch;
	Button fm4_FelicaReadBlock;
	Button fm4_FelicaCancel;
	Button fm4_ISO15693Search;
	Button fm4_ISO15693ReadBlock;
	Button fm4_ISO15693Cancel;
	
	/////////////////////
	EditText fm4_accno;
	EditText fm4_keytext;
	EditText fm4_keytext1;
	EditText fm4_blocktext1;
	EditText fm4_blocktext2;
	EditText fm4_blocktext3;
	EditText fm4_blocktext4;
	EditText fm4_value1;
	EditText fm4_value2;
	EditText fm4_cpuapdutext;
	EditText fm4_cpuarectext;
	/////////////////////////////
	EditText fm4_ISO14443Bapdutext;
	EditText fm4_ISO14443Barectext;
	EditText fm4_FelicaDatatext;
	EditText fm4_ISO15693Datatext;
	/////////////////////////////
	Spinner fm4_sector1;
	Spinner fm4_block1;
	Spinner fm4_sector2;
	Spinner fm4_block2;
	/////////////////////////////
	Spinner fm4_block3;
	Spinner fm4_block4;
	/////////////////////////////
	RadioGroup fm4_rdg1;
	RadioGroup fm4_rdg2;
	RadioButton fm4_Akey1, fm4_Bkey1, fm4_Akey2, fm4_Bkey2;
	public int fm4_index1=0;// /用于存储radiobutton选择的设备号 0 = A密钥 1= B密钥 读写数据
	public int fm4_index2=0;// /用于存储radiobutton选择的设备号 0 = A密钥 1= B密钥 钱包操作
	private String character[] = { "MifareOne", };
	private String character_sector[] = { "0", "1", "2", "3", "4", "5", "6",
			"7", "8", "9", "10", "11", "12", "13", "14", "15", };
	private String character_block[] = { "0", "1", "2", "3", };
	private String character_Felicablock[] = { "0", "1", "2", "3", "4"};
	private String character_ISO15693block[] = { "0", "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",};
	private List<String> list = new ArrayList<String>();
	private List<String> list_sector = new ArrayList<String>();
	private List<String> list_block = new ArrayList<String>();
	private List<String> list_Felicablock = new ArrayList<String>();
	private List<String> list_ISO15693block = new ArrayList<String>();
	/////////////////////////////////////////////////////////////////////////////////
	
	
	myTask  mTask;
	int		myTaskResult;
	int		fm4_OperationIndex;//想要做什么操作的的索引号,给AsyncTask使用
	int 	[]myCardNoData=new int[32];
	int     []myCardNoDataLen=new int[1];
	int 	[]myCardData=new int[128];
	int 	[]myCardDataLen=new int[1];
	int		[]myKey = new int[12];
	int 	myBlock; //当前操作绝对块号 扇区*4+块号
	long 	myAmount;//存储当前金额 设置和读取金额同一参数
	int		[]myAmountBuf=new int[4];;//存储金额整数串
	int 	[]myApduData=new int[1024];//存储APDU指令
	int 	[]myArecData=new int[1024];//存储返回的数据
	int		myApduDataLen;
	int		myArecDataLen;
	int 	irr;
	public static RFIDPnPacket mRFIDPacket;
	private static final String ACTION_USB_PERMISSION="com.jack.rfiddemo.USB_PERMISSION";
	
	private class myTask extends AsyncTask<String, String, String>
	{

		protected void onPreExecute() 
		{
			myTaskResult=0;
		}
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			switch(fm4_OperationIndex){
				case 0:				//初始化RFID设备
					myTaskResult=mRFIDPacket.PN_RF_Set(0);
					break;
				case 1:				//关闭天线
					myTaskResult=mRFIDPacket.PN_RF_Set(1);
					break;
				case 2:
					irr=0;
					irr=mRFIDPacket.PN_RF_Set(0);//打开天线
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					Arrays.fill(myCardNoData,0);
					irr=mRFIDPacket.PN_RF_M1_Search(myCardNoData,myCardNoDataLen,5);
					myTaskResult=irr;
					//myTaskResult=4;//霸蛮
					break;
				case 3:
					irr=0;
					Arrays.fill(myCardData, 0);
					//验证秘钥
					irr=mRFIDPacket.PN_RF_M1_Authentication(myBlock, myCardNoData, 4, myKey, 6, 1);
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_M1_Read(myBlock, myCardData, 1);
					myTaskResult=irr;
					break;
				case 4://写卡
					irr=0;
					//Arrays.fill(myCardData, 0);
					//验证秘钥
					irr=mRFIDPacket.PN_RF_M1_Authentication(myBlock, myCardNoData, 4, myKey, 6, 1);
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_M1_Write(myBlock, myCardData, 16, 1);
					myTaskResult=irr;
					break;
				case 5://初始化钱包
					irr=0;
					//验证秘钥
					irr=mRFIDPacket.PN_RF_M1_Authentication(myBlock, myCardNoData, 4, myKey, 6, 1);
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_M1_PurseInit(myBlock, myAmount, 1);
					myTaskResult=irr;
					break;
				case 6://钱包充值
					//验证秘钥
					irr=mRFIDPacket.PN_RF_M1_Authentication(myBlock, myCardNoData, 4, myKey, 6, 1);
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_M1_PurseIncrease(myBlock, myAmount, 1);
					myTaskResult=irr;
					break;
				case 7://钱包扣款
					//验证秘钥
					irr=mRFIDPacket.PN_RF_M1_Authentication(myBlock, myCardNoData, 4, myKey, 6, 1);
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_M1_PurseDecrease(myBlock, myAmount, 1);
					myTaskResult=irr;
					break;
				case 8://读余额
					//验证秘钥
					irr=mRFIDPacket.PN_RF_M1_Authentication(myBlock, myCardNoData, 4, myKey, 6, 1);
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					myAmount=mRFIDPacket.PN_RF_M1_PurseBalance(myBlock, 1);
					if(myAmount<0)//如果读到的余额小于0 则是操作失败
					{
						irr=(int)myAmount;
						myAmount=0;
					}
					else
					{
						irr=0;
					}
					myTaskResult=irr;
					break;
				case 9://CPU卡寻卡
					irr=0;
					irr=mRFIDPacket.PN_RF_Set(0);//打开天线
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_CPU_Search(5);
					myTaskResult=irr;
					break;
				case 10://apdu指令
					irr=0;
					irr=mRFIDPacket.PN_RF_CPU_APDU(myApduData, myApduDataLen, myArecData, 1);
					if(irr<0)
					{
						myTaskResult=irr;
						break;	
					}
					else
					{
						myTaskResult=irr;
						irr=0;
					}
					break;
				case 11://ISO14443B卡寻卡
					irr=0;
					irr=mRFIDPacket.PN_RF_Set(0);//打开天线
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_14443B_Search(5);
					myTaskResult=irr;
					break;
				case 12://apdu指令
					irr=0;
					irr=mRFIDPacket.PN_RF_14443B_APDU(myApduData, myApduDataLen, myArecData, 1);
					if(irr<0)
					{
						myTaskResult=irr;
						break;	
					}
					else
					{
						myTaskResult=irr;
						irr=0;
					}
					break;
				case 13://FELICA卡寻卡
					irr=0;
					irr=mRFIDPacket.PN_RF_Set(0);//打开天线
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_F_Search(5);
					myTaskResult=irr;
					break;
				case 14://FELICA读卡
					irr=0;
					Arrays.fill(myCardData, 0);
					Arrays.fill(myCardDataLen, 0);
					irr=mRFIDPacket.PN_RF_F_Read_Block(myBlock, myCardData, myCardDataLen);
					myTaskResult=irr;
					break;
				case 15://FELICA卡寻卡
					irr=0;
					irr=mRFIDPacket.PN_RF_Set(0);//打开天线
					if(irr<0)
					{
						myTaskResult=irr;
						break;
					}
					irr=mRFIDPacket.PN_RF_V_Search(5);
					myTaskResult=irr;
					break;
				case 16://FELICA读卡
					irr=0;
					Arrays.fill(myCardData, 0);
					Arrays.fill(myCardDataLen, 0);
					irr=mRFIDPacket.PN_RF_V_Read_Block(myBlock, myCardData, myCardDataLen);
					myTaskResult=irr;
					break;
				
					
				default:
					break;
			
			}
			return null;
		}
		protected void onProgressUpdate(String... progresses) {
			
		}
		protected void onPostExecute(String result){
			
			switch(fm4_OperationIndex)
			{
				case 0:	
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Init successful", Toast.LENGTH_SHORT).show();
						onWakeupButtonSetup();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Init error", Toast.LENGTH_SHORT).show();
						onOpenButtonSetup();
					}
					break;
				case 1:
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Sleep successful", Toast.LENGTH_SHORT).show();
						onOpenButtonSetup();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Sleep error", Toast.LENGTH_SHORT).show();
						onWakeupButtonSetup();
					}
					break;
				case 2:
					if(myTaskResult>=0)
					{
						//myCardNoDataLen
						String tmpstr = new String();
						//tmpstr = USBOp.convert2String(myCardNoData, myTaskResult);
						tmpstr = USBOp.convert2String(myCardNoData, myCardNoDataLen[0]);
						fm4_accno.setText(tmpstr);
						Toast.makeText(RFID_DEV.this, "Search Card successful", Toast.LENGTH_SHORT).show();	
					}
					else
					{
						String tmpstr = new String();
						tmpstr="Search Card error";
						tmpstr+=myTaskResult;
						Toast.makeText(RFID_DEV.this, tmpstr, Toast.LENGTH_SHORT).show();	
					}
					break;
				case 3:
					//读卡
					if(myTaskResult>=0)
					{
						
						String tmpstr = new String();
						tmpstr = USBOp.convert2String(myCardData, myTaskResult);
						if(fm4_block1.getSelectedItemId()==1)
							fm4_blocktext2.setText(tmpstr);
						else if(fm4_block1.getSelectedItemId()==2)
							fm4_blocktext3.setText(tmpstr);
						else if(fm4_block1.getSelectedItemId()==3)
							fm4_blocktext4.setText(tmpstr);
						else
							fm4_blocktext1.setText(tmpstr);
						Toast.makeText(RFID_DEV.this, "Read Card successful", Toast.LENGTH_SHORT).show();
						
					}
					else
					{
						Toast.makeText(RFID_DEV.this, "Read Card error", Toast.LENGTH_SHORT).show();
						
					}
					break;
				case 4://写卡
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Write card successful", Toast.LENGTH_SHORT).show();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Write card error", Toast.LENGTH_SHORT).show();
					}
					break;
				case 5://初始化钱包
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Purse Initialize successful", Toast.LENGTH_SHORT).show();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Purse Initialize error", Toast.LENGTH_SHORT).show();
					}
					break;
				case 6://钱包充值
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Purse Increase successful", Toast.LENGTH_SHORT).show();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Purse Increase error", Toast.LENGTH_SHORT).show();
					}
					break;
				case 7://钱包扣款
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Purse Decrease successful", Toast.LENGTH_SHORT).show();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Purse Decrease error", Toast.LENGTH_SHORT).show();
					}
					break;
				case 8://读余额
					if(myTaskResult==0)
					{
						String tmpstr = new String();
						Arrays.fill(myAmountBuf, 0);
						myAmountBuf[0] = (int) myAmount % 256;
						myAmountBuf[1] = (int) (myAmount / 256) % 256;
						myAmountBuf[2] = (int) (myAmount / (256 * 256)) % 256;
						myAmountBuf[3] = (int) (myAmount / (256 * 256 * 256)) % 256;
						tmpstr = USBOp.convert2String(myAmountBuf, 4);
						fm4_value2.setText(tmpstr);
						tmpstr=String.valueOf(myAmount);
						fm4_value1.setText(tmpstr);
						Toast.makeText(RFID_DEV.this, "Purse Balance successful", Toast.LENGTH_SHORT).show();
						
					}
					else
					{
						Toast.makeText(RFID_DEV.this, "Purse Balance error", Toast.LENGTH_SHORT).show();
						
					}
					break;
				case 9://寻CPU卡
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Search CPU card successful", Toast.LENGTH_SHORT).show();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Search CPU card error", Toast.LENGTH_SHORT).show();
					}
					break;
				case 10://APDU指令
					
					//Toast.makeText(RFID_DEV.this, "到了这里", Toast.LENGTH_SHORT).show();
					
					if(myTaskResult>=0)
					{
						
						String tmpstr = new String();
						tmpstr = USBOp.convert2String(myArecData, myTaskResult);
						fm4_cpuarectext.setText(tmpstr);//fm4_cpuarectext
						//fm4_cpuarectext.setText(String.valueOf(myTaskResult));
						Toast.makeText(RFID_DEV.this, "Apdu operation successful", Toast.LENGTH_SHORT).show();
					}
					else
					{
						Toast.makeText(RFID_DEV.this, "Apdu operation error", Toast.LENGTH_SHORT).show();
						
					}
					break;
				case 11://寻14443BCPU卡
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Search 14443B card successful", Toast.LENGTH_SHORT).show();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Search 14443B card error", Toast.LENGTH_SHORT).show();
					}
					break;
				case 12://ISO14443B APDU指令
					
					//Toast.makeText(RFID_DEV.this, "到了这里", Toast.LENGTH_SHORT).show();
					//fm4_ISO14443Barectext.setText(String.valueOf(myTaskResult));
					if(myTaskResult>=0)
					{
						
						String tmpstr = new String();
						tmpstr = USBOp.convert2String(myArecData, myTaskResult);
						fm4_ISO14443Barectext.setText(tmpstr);//fm4_cpuarectext
						//fm4_cpuarectext.setText(String.valueOf(myTaskResult));
						Toast.makeText(RFID_DEV.this, "Apdu operation successful", Toast.LENGTH_SHORT).show();
					}
					else
					{
						Toast.makeText(RFID_DEV.this, "Apdu operation error", Toast.LENGTH_SHORT).show();
						
					}
					break;
				case 13://寻14443BCPU卡
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Search Felica card successful", Toast.LENGTH_SHORT).show();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Search Felica card error", Toast.LENGTH_SHORT).show();
					}
					break;
				case 14:
					//读卡
					if(myTaskResult>=0)
					{
						
						String tmpstr = new String();
						tmpstr = USBOp.convert2String(myCardData, myCardDataLen[0]);
						fm4_FelicaDatatext.setText(tmpstr);
						Toast.makeText(RFID_DEV.this, "Read Block successful", Toast.LENGTH_SHORT).show();
						
					}
					else
					{
						Toast.makeText(RFID_DEV.this, "Read Block error", Toast.LENGTH_SHORT).show();
						
					}
					break;
				case 15://寻14443BCPU卡
					if(myTaskResult>=0)
					{
						Toast.makeText(RFID_DEV.this, "Search ISO15693 card successful", Toast.LENGTH_SHORT).show();
					}
					else{
						Toast.makeText(RFID_DEV.this, "Search ISO15693 card error", Toast.LENGTH_SHORT).show();
					}
					break;
				case 16:
					//读卡
					if(myTaskResult>=0)
					{
						
						String tmpstr = new String();
						tmpstr = USBOp.convert2String(myCardData, myCardDataLen[0]);
						fm4_ISO15693Datatext.setText(tmpstr);
						Toast.makeText(RFID_DEV.this, "Read Block successful", Toast.LENGTH_SHORT).show();
						
					}
					else
					{
						Toast.makeText(RFID_DEV.this, "Read Block error", Toast.LENGTH_SHORT).show();
						
					}
					break;
				
				default:
					break;
			}
		}
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);//activity_rfid__dev
		setContentView(R.layout.activity_rfid__dev);
		
		TabHost tabhost=(TabHost)findViewById(android.R.id.tabhost);
		tabhost.setup();
		tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("Setting")
				.setContent(R.id.COMSetting));
		tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("M1-CARD")
				.setContent(R.id.M1layout));
		tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("ISO14443ACPUCARD")
				.setContent(R.id.CPUlayout));
		tabhost.addTab(tabhost.newTabSpec("tab4").setIndicator("ISO14443BCPUCARD")
				.setContent(R.id.ISO14443Blayout));
		tabhost.addTab(tabhost.newTabSpec("tab5").setIndicator("FelicaCARD")
				.setContent(R.id.Felicalayout));
		tabhost.addTab(tabhost.newTabSpec("tab6").setIndicator("ISO15693CARD")
				.setContent(R.id.ISO15693layout));
		tabhost.setCurrentTab(0);
		
		//serialPort_spinner=(Spinner)findViewById(R.id.serialport_Select);
		fm4_Open=(Button)findViewById(R.id.fmrfid_setopen);
		fm4_Refresh=(Button)findViewById(R.id.fmrfid_setRefresh);
		fm4_Init=(Button)findViewById(R.id.fmrfid_setinit);
		fm4_Sleep=(Button)findViewById(R.id.fmrfid_setsleep);
		fm4_Close=(Button)findViewById(R.id.fmrfid_setclose);
		//////////////////////////////////////////////////////////////////////////////////////////////
		fm4_sector1 = (Spinner) findViewById(R.id.card_sector1);
		fm4_sector2 = (Spinner) findViewById(R.id.card_sector2);
		fm4_block1 = (Spinner) findViewById(R.id.card_block1);
		fm4_block2 = (Spinner) findViewById(R.id.card_block2);

		fm4_search = (Button) findViewById(R.id.fmrfid_search);
		fm4_read = (Button) findViewById(R.id.fmrfid_readcard);
		fm4_write = (Button) findViewById(R.id.fmrfid_writecard);
		fm4_Initialize = (Button) findViewById(R.id.fmrfid_init);
		fm4_Increase =(Button)findViewById(R.id.fmrfid_increase);
		fm4_Decrease =(Button)findViewById(R.id.fmrfid_drease);
		fm4_Balance =(Button)findViewById(R.id.fmrfid_balance);
		fm4_cancel =(Button)findViewById(R.id.fmrfid_cancel);

		fm4_accno = (EditText) findViewById(R.id.fmrfid_idtext);
		fm4_keytext = (EditText) findViewById(R.id.fmrfid_keytext1);
		fm4_keytext1 = (EditText) findViewById(R.id.fmrfid_keytext2);
		fm4_blocktext1 = (EditText) findViewById(R.id.fmrfid_blocktext1);
		fm4_blocktext2 = (EditText) findViewById(R.id.fmrfid_blocktext2);
		fm4_blocktext3 = (EditText) findViewById(R.id.fmrfid_blocktext3);
		fm4_blocktext4 = (EditText) findViewById(R.id.fmrfid_blocktext4);
		fm4_value1 = (EditText) findViewById(R.id.fmrfid_valuetext1);
		fm4_value2 = (EditText) findViewById(R.id.fmrfid_valuetext2);

		fm4_Akey1 = (RadioButton) findViewById(R.id.rfid_akey1);
		fm4_Akey2 = (RadioButton) findViewById(R.id.rfid_akey2);
		fm4_Bkey1 = (RadioButton) findViewById(R.id.rfid_bkey1);
		fm4_Bkey2 = (RadioButton) findViewById(R.id.rfid_bkey2);
		fm4_rdg1 = (RadioGroup) findViewById(R.id.rfid_key1);
		fm4_rdg2 = (RadioGroup) findViewById(R.id.rfid_key2);
		/////CPU card
		fm4_CPUSearch=(Button)findViewById(R.id.fmrfid_cpusearch);
		fm4_CPUAPDU=(Button)findViewById(R.id.fmrfid_cpuapdu);
		fm4_CPUCancel=(Button)findViewById(R.id.fmrfid_cpucancel);
		fm4_cpuapdutext = (EditText) findViewById(R.id.fmrfid_cpuapdutext);
		fm4_cpuarectext = (EditText) findViewById(R.id.fmrfid_cpuarectext);
		
		/////ISO14443B CPU
		fm4_ISO14443BSearch=(Button)findViewById(R.id.fmrfid_ISO14443Bsearch);
		fm4_ISO14443BAPDU=(Button)findViewById(R.id.fmrfid_ISO14443Bapdu);
		fm4_ISO14443BCancel=(Button)findViewById(R.id.fmrfid_ISO14443Bcancel);
		fm4_ISO14443Bapdutext = (EditText) findViewById(R.id.fmrfid_ISO14443Bapdutext);
		fm4_ISO14443Barectext = (EditText) findViewById(R.id.fmrfid_ISO14443Barectext);
		
		/////Felica
		fm4_FelicaSearch=(Button)findViewById(R.id.fmrfid_Felicasearch);
		fm4_FelicaReadBlock=(Button)findViewById(R.id.fmrfid_FelicaReadBlock);
		fm4_FelicaCancel=(Button)findViewById(R.id.fmrfid_Felicacancel);
		fm4_FelicaDatatext = (EditText) findViewById(R.id.fmrfid_FelicaDatatext);
		fm4_block3 = (Spinner) findViewById(R.id.fmrfid_FelicaBlockSelect);
		
		/////ISO15693 CARD
		fm4_ISO15693Search=(Button)findViewById(R.id.fmrfid_ISO15693search);
		fm4_ISO15693ReadBlock=(Button)findViewById(R.id.fmrfid_ISO15693ReadBlock);
		fm4_ISO15693Cancel=(Button)findViewById(R.id.fmrfid_ISO15693cancel);
		fm4_ISO15693Datatext = (EditText) findViewById(R.id.fmrfid_ISO15693Datatext);
		fm4_block4 = (Spinner) findViewById(R.id.fmrfid_ISO15693BlockSelect);
		
		
		fm4_search.setOnClickListener(this);
		fm4_read.setOnClickListener(this);
		fm4_write.setOnClickListener(this);
		fm4_Initialize.setOnClickListener(this);
		fm4_Increase.setOnClickListener(this);
		fm4_Decrease.setOnClickListener(this);
		fm4_Balance.setOnClickListener(this);
		fm4_cancel.setOnClickListener(this);
		//////////////////////////////////////////////////////////////////////////////////////////////
		fm4_Open.setOnClickListener(this);
		fm4_Refresh.setOnClickListener(this);
		fm4_Init.setOnClickListener(this);
		fm4_Sleep.setOnClickListener(this);
		fm4_Close.setOnClickListener(this);
		/////cpu card/////////////////////////////////////////////////////////////////////////////////
		fm4_CPUSearch.setOnClickListener(this);
		fm4_CPUAPDU.setOnClickListener(this);
		fm4_CPUCancel.setOnClickListener(this);
		///////ISO14443B CPU//////////////////////////////////////////////////////////////////////////
		fm4_ISO14443BSearch.setOnClickListener(this);
		fm4_ISO14443BAPDU.setOnClickListener(this);
		fm4_ISO14443BCancel.setOnClickListener(this);
		///////Felica////////////////////////////////////////////////////////////////////////////////
		fm4_FelicaSearch.setOnClickListener(this);
		fm4_FelicaReadBlock.setOnClickListener(this);
		fm4_FelicaCancel.setOnClickListener(this);
		///////ISO15693//////////////////////////////////////////////////////////////////////////////
		fm4_ISO15693Search.setOnClickListener(this);
		fm4_ISO15693ReadBlock.setOnClickListener(this);
		fm4_ISO15693Cancel.setOnClickListener(this);
		
		for (int i = 0; i < character.length; i++) {
			list.add(character[i]);

		}
		for (int i = 0; i < character_sector.length; i++) {
			list_sector.add(character_sector[i]);

		}
		for (int i = 0; i < character_block.length; i++) {
			list_block.add(character_block[i]);

		}
		for (int i = 0; i < character_Felicablock.length; i++) {
			list_Felicablock.add(character_Felicablock[i]);

		}
		for (int i = 0; i < character_ISO15693block.length; i++) {
			list_ISO15693block.add(character_ISO15693block[i]);

		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, list);
		ArrayAdapter<String> adapter_sector = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, list_sector);
		ArrayAdapter<String> adapter_block = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, list_block);
		ArrayAdapter<String> adapter_Felicablock = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, list_Felicablock);
		ArrayAdapter<String> adapter_ISO15693block = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, list_ISO15693block);
		//fm4_spinner.setAdapter(adapter);
		fm4_sector1.setAdapter(adapter_sector);
		fm4_sector2.setAdapter(adapter_sector);
		fm4_block1.setAdapter(adapter_block);
		fm4_block2.setAdapter(adapter_block);
		
		fm4_block3.setAdapter(adapter_Felicablock);
		fm4_block4.setAdapter(adapter_ISO15693block);
		
		fm4_sector2.setSelection(1);
		
		fm4_value1.setText("100");
		fm4_cpuapdutext.setText("0084000004");
		fm4_ISO14443Bapdutext.setText("0084000004");

		fm4_rdg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int CheckedId) {
				// TODO Auto-generated method stub
				if (CheckedId == fm4_Akey1.getId())
					fm4_index1 = 0;
				else if (CheckedId == fm4_Bkey1.getId())
					fm4_index1 = 1;
				else
					fm4_index1 = 0;
			}
		});
		fm4_rdg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int CheckedId) {
				// TODO Auto-generated method stub
				if (CheckedId == fm4_Akey2.getId())
					fm4_index2 = 0;
				else if (CheckedId == fm4_Bkey2.getId())
					fm4_index2 = 1;
				else
					fm4_index2 = 0;
			}
		});
		
		onInitButtonSetup();
		
	//	refreshSerialPort();
	}

	@Override
	/*public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}*/
	
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.rfid__dev, menu);
		return true;
	}

	/*public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}*/
	


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int iret;
		int[] tmpmoney = new int[4];
		String tmpstr = new String();
		switch(v.getId())
		{
		//case R.id.fmrfid_setRefresh:
		//	refreshSerialPort();
		//	break;
		case R.id.fmrfid_setRefresh:
		
			mRFIDPacket=new RFIDPnPacket((UsbManager) getSystemService(Context.USB_SERVICE), this,
					ACTION_USB_PERMISSION);
			if(!mRFIDPacket.mUSBOp.UsbFeatureSupported())
			{
				Dialog dialog = new AlertDialog.Builder(RFID_DEV.this)
						.setTitle("提示")
						.setMessage("您的手机不支持USB HOST，请更换其他手机再试！")
						.setPositiveButton("确认",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface arg0,
											int arg1) {
										System.exit(0);
									}
								}).create();
				dialog.setCanceledOnTouchOutside(false);
				dialog.show();
				
				
			}
			onRefreshButtonSetup();
			
			//iret=mRFIDPacket.PS_RF_Set(0);
			/*String myString=mRFIDPacket.PS_RF_Set(1);
			new AlertDialog.Builder(this)
			 .setTitle("标题") 
			 .setMessage(myString)
			  .setPositiveButton("确定", null) 
			  .show(); */
		/*	if(iret<0)
			{
				new AlertDialog.Builder(this)
				 .setTitle("标题") 
				 .setMessage("打开设备失败了")
				  .setPositiveButton("确定", null) 
				  .show(); 
				
			}else{
				new AlertDialog.Builder(this)
				 .setTitle("标题") 
				 .setMessage("打开设备成功了")
				  .setPositiveButton("确定", null) 
				  .show(); 	
				
			}*/
			
			break;
		
		case R.id.fmrfid_setinit:
			fm4_OperationIndex=0;
			mTask = new myTask();
            mTask.execute();
            //mRFIDPacket.SetBaud();
            break;
		case R.id.fmrfid_search:
			fm4_OperationIndex=2;
			mTask = new myTask();
            mTask.execute();
			/*Arrays.fill(myCardNoData,0);
			myTaskResult=mRFIDPacket.PN_RF_M1_Search(myCardNoData);
			if(myTaskResult>=0)
			{
				
				String tmpstr = new String();
				tmpstr = USBOp.convert2String(myCardNoData, myTaskResult);
				fm4_accno.setText(tmpstr);
				new AlertDialog.Builder(this)
				 .setTitle("标题") 
				 .setMessage(tmpstr)
				  .setPositiveButton("确定", null) 
				  .show(); 
				
			}
			else
			{
				String tmpstr = new String();
				tmpstr=myTaskResult+"";
				new AlertDialog.Builder(this)
				 .setTitle("标题") 
				 .setMessage(tmpstr)
				  .setPositiveButton("确定", null) 
				  .show(); 
				
			}*/
			
            break;
		case R.id.fmrfid_readcard:
			fm4_OperationIndex=3;
			myBlock = (int) (fm4_sector1.getSelectedItemId() * 4 + fm4_block1
					.getSelectedItemId());
			Arrays.fill(myKey, 0);
			myKey = USBOp.convertStringToIntArr(fm4_keytext.getText().toString());
			mTask = new myTask();
            mTask.execute();
			break;
		case R.id.fmrfid_writecard:
			fm4_OperationIndex=4;
			myBlock = (int) (fm4_sector1.getSelectedItemId() * 4 + fm4_block1
					.getSelectedItemId());
			Arrays.fill(myKey, 0);
			Arrays.fill(myCardData, 0);
			myKey = USBOp.convertStringToIntArr(fm4_keytext.getText().toString());
			if(fm4_block1.getSelectedItemId()==1)
			{
				if (fm4_blocktext2.getText().length() > 0)
					myCardData = USBOp.convertStringToIntArr(fm4_blocktext2.getText()
							.toString());	
				else
				{
					Toast.makeText(RFID_DEV.this, "Please input card data!", Toast.LENGTH_SHORT).show();
					break;
				}
			}
			else if(fm4_block1.getSelectedItemId()==2)
			{
				if (fm4_blocktext3.getText().length() > 0)
					myCardData = USBOp.convertStringToIntArr(fm4_blocktext3.getText()
							.toString());	
				else
				{
					Toast.makeText(RFID_DEV.this, "Please input card data!", Toast.LENGTH_SHORT).show();
					break;
				}
				
			}
			else if(fm4_block1.getSelectedItemId()==2)
			{
				if (fm4_blocktext4.getText().length() > 0)
					myCardData = USBOp.convertStringToIntArr(fm4_blocktext4.getText()
							.toString());	
				else
				{
					Toast.makeText(RFID_DEV.this, "Please input card data!", Toast.LENGTH_SHORT).show();
					break;
				}
				
			}
			else
			{
				if (fm4_blocktext1.getText().length() > 0)
					myCardData = USBOp.convertStringToIntArr(fm4_blocktext1.getText()
							.toString());	
				else
				{
					Toast.makeText(RFID_DEV.this, "Please input card data!", Toast.LENGTH_SHORT).show();
					break;
				}
				
			}
			mTask = new myTask();
            mTask.execute();
			break;
			
		case R.id.fmrfid_init:
			fm4_OperationIndex=5;
			myAmount=Long.parseLong(fm4_value1.getText().toString());
			Arrays.fill(tmpmoney, 0);
			tmpmoney[0] = (int) myAmount % 256;
			tmpmoney[1] = (int) (myAmount / 256) % 256;
			tmpmoney[2] = (int) (myAmount / (256 * 256)) % 256;
			tmpmoney[3] = (int) (myAmount / (256 * 256 * 256)) % 256;
			tmpstr = USBOp.convert2String(tmpmoney, 4);
			fm4_value2.setText(tmpstr);
			myBlock=(int) (fm4_sector2.getSelectedItemId() * 4 + fm4_block2.getSelectedItemId());
			Arrays.fill(myKey, 0);
			myKey = USBOp.convertStringToIntArr(fm4_keytext1.getText().toString());
			
			mTask = new myTask();
            mTask.execute();
			break;
		case R.id.fmrfid_increase:
			fm4_OperationIndex=6;
			myAmount=Long.parseLong(fm4_value1.getText().toString());
			Arrays.fill(tmpmoney, 0);
			tmpmoney[0] = (int) myAmount % 256;
			tmpmoney[1] = (int) (myAmount / 256) % 256;
			tmpmoney[2] = (int) (myAmount / (256 * 256)) % 256;
			tmpmoney[3] = (int) (myAmount / (256 * 256 * 256)) % 256;
			tmpstr = USBOp.convert2String(tmpmoney, 4);
			myBlock=(int) (fm4_sector2.getSelectedItemId() * 4 + fm4_block2.getSelectedItemId());
			Arrays.fill(myKey, 0);
			myKey = USBOp.convertStringToIntArr(fm4_keytext1.getText().toString());
			mTask = new myTask();
            mTask.execute();
			break;
		case R.id.fmrfid_drease:
			fm4_OperationIndex=7;
			myAmount=Long.parseLong(fm4_value1.getText().toString());
			Arrays.fill(tmpmoney, 0);
			tmpmoney[0] = (int) myAmount % 256;
			tmpmoney[1] = (int) (myAmount / 256) % 256;
			tmpmoney[2] = (int) (myAmount / (256 * 256)) % 256;
			tmpmoney[3] = (int) (myAmount / (256 * 256 * 256)) % 256;
			tmpstr = USBOp.convert2String(tmpmoney, 4);
			myBlock=(int) (fm4_sector2.getSelectedItemId() * 4 + fm4_block2.getSelectedItemId());
			Arrays.fill(myKey, 0);
			myKey = USBOp.convertStringToIntArr(fm4_keytext1.getText().toString());
			mTask = new myTask();
            mTask.execute();
			break;
		case R.id.fmrfid_balance:
			fm4_OperationIndex=8;
			myBlock=(int) (fm4_sector2.getSelectedItemId() * 4 + fm4_block2.getSelectedItemId());
			Arrays.fill(myKey, 0);
			myKey = USBOp.convertStringToIntArr(fm4_keytext1.getText().toString());
			mTask = new myTask();
            mTask.execute();
            break;
		case R.id.fmrfid_cpusearch:
			fm4_OperationIndex=9;
			mTask = new myTask();
            mTask.execute();
			break;
		case R.id.fmrfid_cpuapdu:
			fm4_OperationIndex=10;
			Arrays.fill(myApduData, 0);
			myApduData = USBOp.convertStringToIntArr(fm4_cpuapdutext.getText().toString());
			myApduDataLen=fm4_cpuapdutext.getText().length()/2;
			mTask = new myTask();
            mTask.execute();
			break;
		///////14443B CPU///////////////////////////////////
		case R.id.fmrfid_ISO14443Bsearch:
			fm4_OperationIndex=11;
			mTask = new myTask();
            mTask.execute();
			break;
		case R.id.fmrfid_ISO14443Bapdu:
			fm4_OperationIndex=12;
			Arrays.fill(myApduData, 0);
			myApduData = USBOp.convertStringToIntArr(fm4_ISO14443Bapdutext.getText().toString());
			myApduDataLen=fm4_ISO14443Bapdutext.getText().length()/2;
			mTask = new myTask();
            mTask.execute();
			break;
		///////FELICA///////////////////////////////////
		case R.id.fmrfid_Felicasearch:
			fm4_OperationIndex=13;
			mTask = new myTask();
            mTask.execute();
			break;
		case R.id.fmrfid_FelicaReadBlock:
			fm4_OperationIndex=14;
			myBlock=(int) fm4_block3.getSelectedItemId();
			mTask = new myTask();
            mTask.execute();
			break;
		///////ISO15693///////////////////////////////////
		case R.id.fmrfid_ISO15693search:
			fm4_OperationIndex=15;
			mTask = new myTask();
            mTask.execute();
			break;
		case R.id.fmrfid_ISO15693ReadBlock:
			fm4_OperationIndex=16;
			myBlock=(int) fm4_block4.getSelectedItemId();
			mTask = new myTask();
            mTask.execute();
			break;
			
			
		case R.id.fmrfid_setopen:
			//如果用户选了 不授权或者设备没有权限
			//mRFIDPacket.mUSBOp.
			//mRFIDPacket.SetBaud();
			onOpenButtonSetup();
			break;
		case R.id.fmrfid_setsleep:
			fm4_OperationIndex=1;
			mTask = new myTask();
            mTask.execute();
            break;
			
		case R.id.fmrfid_cancel:
			System.exit(0);
			break;
		case R.id.fmrfid_cpucancel:
			System.exit(0);
			break;
		case R.id.fmrfid_setclose:
			System.exit(0);
			break;
		case R.id.fmrfid_ISO14443Bcancel:
			System.exit(0);
			break;
		case R.id.fmrfid_Felicacancel:
			System.exit(0);
			break;
		case R.id.fmrfid_ISO15693cancel:
			System.exit(0);
			break;
		default:
			break;
			
		
		}
		
	}
	private void onInitButtonSetup()
	{
		fm4_Refresh.setEnabled(true);
		fm4_Open.setEnabled(false);
		fm4_Init.setEnabled(false);
		fm4_Sleep.setEnabled(false);
		fm4_search.setEnabled(false);
		fm4_read.setEnabled(false);
		fm4_write.setEnabled(false);
		fm4_Initialize.setEnabled(false);
		fm4_Increase.setEnabled(false);
		fm4_Decrease.setEnabled(false);
		fm4_Balance.setEnabled(false);
		fm4_CPUSearch.setEnabled(false);
		fm4_CPUAPDU.setEnabled(false);
		fm4_ISO14443BSearch.setEnabled(false);
		fm4_ISO14443BAPDU.setEnabled(false);
		fm4_FelicaSearch.setEnabled(false);
		fm4_FelicaReadBlock.setEnabled(false);
		fm4_ISO15693Search.setEnabled(false);
		fm4_ISO15693ReadBlock.setEnabled(false);
		
	}
	private void onRefreshButtonSetup()
	{
		fm4_Refresh.setEnabled(false);
		fm4_Open.setEnabled(true);
		fm4_Init.setEnabled(false);
		fm4_Sleep.setEnabled(false);
		fm4_search.setEnabled(false);
		fm4_read.setEnabled(false);
		fm4_write.setEnabled(false);
		fm4_Initialize.setEnabled(false);
		fm4_Increase.setEnabled(false);
		fm4_Decrease.setEnabled(false);
		fm4_Balance.setEnabled(false);
		fm4_CPUSearch.setEnabled(false);
		fm4_CPUAPDU.setEnabled(false);	
		fm4_ISO14443BSearch.setEnabled(false);
		fm4_ISO14443BAPDU.setEnabled(false);
		fm4_FelicaSearch.setEnabled(false);
		fm4_FelicaReadBlock.setEnabled(false);
		fm4_ISO15693Search.setEnabled(false);
		fm4_ISO15693ReadBlock.setEnabled(false);
		
	}
	private void onOpenButtonSetup()
	{
		fm4_Refresh.setEnabled(false);
		fm4_Open.setEnabled(false);
		fm4_Init.setEnabled(true);
		fm4_Sleep.setEnabled(false);
		fm4_search.setEnabled(false);
		fm4_read.setEnabled(false);
		fm4_write.setEnabled(false);
		fm4_Initialize.setEnabled(false);
		fm4_Increase.setEnabled(false);
		fm4_Decrease.setEnabled(false);
		fm4_Balance.setEnabled(false);
		fm4_CPUSearch.setEnabled(false);
		fm4_CPUAPDU.setEnabled(false);	
		fm4_ISO14443BSearch.setEnabled(false);
		fm4_ISO14443BAPDU.setEnabled(false);
		fm4_FelicaSearch.setEnabled(false);
		fm4_FelicaReadBlock.setEnabled(false);
		fm4_ISO15693Search.setEnabled(false);
		fm4_ISO15693ReadBlock.setEnabled(false);
		
	}
	private void onWakeupButtonSetup()
	{
		fm4_Refresh.setEnabled(false);
		fm4_Open.setEnabled(false);
		fm4_Init.setEnabled(false);
		fm4_Sleep.setEnabled(true);
		fm4_search.setEnabled(true);
		fm4_read.setEnabled(true);
		fm4_write.setEnabled(true);
		fm4_Initialize.setEnabled(true);
		fm4_Increase.setEnabled(true);
		fm4_Decrease.setEnabled(true);
		fm4_Balance.setEnabled(true);
		fm4_CPUSearch.setEnabled(true);
		fm4_CPUAPDU.setEnabled(true);	
		fm4_ISO14443BSearch.setEnabled(true);
		fm4_ISO14443BAPDU.setEnabled(true);
		fm4_FelicaSearch.setEnabled(true);
		fm4_FelicaReadBlock.setEnabled(true);
		fm4_ISO15693Search.setEnabled(true);
		fm4_ISO15693ReadBlock.setEnabled(true);
	}
	
}
