package com.apiprintdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jolimark.JmPrinter;

@SuppressLint("CutPasteId")
public class TestActivity extends Activity {
	protected static final int LENGTH_SHORT = 0;
	public static final int LENGTH_LONG = 1;
	private Button backButton = null;
	private Button feedkButton = null;	
	private Button exitSet = null;	
	private Button StatusCheck = null;
	private Button btnGetBlackValue = null;
	private Button btnSetBlackValue = null;
	private Button btnSetCharWH = null;
	private Button  btnSetCharWH_xi,btnSetCharWH_zhong;
	private Button btnSetCharWHcancel;
	private Button btnPrintPinTest ;
	
	private JmPrinter mPrinter;
	int globalSign; 
    int setAutoSign; //自动进纸有效 按键次数标记
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		//JmPrinter.initLibrarys(this);//初始化
		mPrinter = new JmPrinter();//建立实例
		
		exitSet = (Button) this.findViewById(R.id.button8);	
		StatusCheck   = (Button) this.findViewById(R.id.btn_StatusCheck);
		backButton = (Button) this.findViewById(R.id.btn_backPaper);
		feedkButton = (Button) this.findViewById(R.id.btn_feedkPaper);		
		btnGetBlackValue = (Button) this.findViewById(R.id.btn_GetBlackValue);
		btnSetBlackValue = (Button) this.findViewById(R.id.btn_SetBlackValue);
		btnSetCharWH = (Button) this.findViewById(R.id.btn_SetCharWH);
		btnSetCharWHcancel= (Button) this.findViewById(R.id.btn_SetCharWHcancel);

		btnSetCharWH_xi   = (Button) this.findViewById(R.id.btn_SetCharWH_xi);
		btnSetCharWH_zhong  = (Button) this.findViewById(R.id.btn_SetCharWH_zhong);		
		btnPrintPinTest = (Button) this.findViewById(R.id.btn_PrintPinTest);


		// 注册按钮监听		

		backButton.setOnClickListener(new ClickListener());		
		feedkButton.setOnClickListener(new ClickListener());	
		StatusCheck.setOnClickListener(new ClickListener());
		exitSet.setOnClickListener(new ClickListener());	
		btnGetBlackValue.setOnClickListener(new ClickListener());
		btnSetBlackValue.setOnClickListener(new ClickListener());
		btnSetCharWH.setOnClickListener(new ClickListener());
		btnSetCharWHcancel.setOnClickListener(new ClickListener());
	
		btnSetCharWH_xi.setOnClickListener(new ClickListener());
		btnSetCharWH_zhong.setOnClickListener(new ClickListener());
		btnPrintPinTest.setOnClickListener(new ClickListener());
			
		
	}

	// 按钮监听实现
	//功能：监听设置界面 button按钮 的监听类
	public class ClickListener implements View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			//final EditText editText = (EditText) findViewById(R.id.editText10); //状态提示窗
			
			switch (v.getId()) {
			case R.id.button8: {   //退出设置
				TestActivity.this.finish(); 
				break;
			}			

			case R.id.btn_backPaper: {  // 退纸
				boolean retnVale =mPrinter.Back(22);
				if( !retnVale )
				{ 
				  	Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				}		
				break;
			}
			case R.id.btn_feedkPaper: {   //    进纸
				boolean retnVale = mPrinter.Feed(22);
				if( !retnVale )
				{
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				} 			 
				break;
			}
			case R.id.btn_StatusCheck: {   //  打印机状态检测
				byte[] BeginValue ={0};
				
				boolean retnVale = mPrinter.GetStatus(BeginValue);
				
				if( !retnVale ){
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				}else{
					Toast.makeText(TestActivity.this,
							"status:"+Integer.toBinaryString(BeginValue[0]&0xff) , LENGTH_LONG).show();
				} 			
				break;
			}
			case R.id.btn_GetBlackValue: {   //读取黑标调整值(浓度)
				int[] BlackValue ={0};
				boolean retnVale = mPrinter.GetBlackValue(BlackValue);
				if( !retnVale )
				{
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				}else{
					Toast.makeText(TestActivity.this,
							Integer.toString(BlackValue[0]) , LENGTH_LONG).show();
					} 			 
				break;
			}
			case R.id.btn_SetBlackValue: {   //设置黑标浓度（黑标调整）
				// 参数范围：16＜?＜128  
				boolean retnVale = mPrinter.SetBlackValue(127);
				if( !retnVale )
				{
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				}else{
					Toast.makeText(TestActivity.this,
							"设置黑标浓度成功" , LENGTH_SHORT).show();
				} 			 
				break;
			}
			
			case R.id.btn_SetCharWH: {   //打印机打印 字符的倍高/倍宽功能设定          （ 倍=双倍，即打印机打印字体大小为常规的双倍大小）
//				 * 参数: BitValue Bit0: 中文倍宽 1:有效；0：无效 (掩码: 0x01)，
//				 *               Bit1: 中文倍高 1:有效；0：无效 (掩码: 0x02) 
//				 *               Bit2: 西文倍宽 1:有效；0：无效 (掩码: 0x04) 
//				 *               Bit3: 西文倍高 1:有效；0：无效 (掩码: 0x08) 
				byte  BitValue =0x01+0x02+0x04+0x08;//中西文倍宽倍高都有效
				boolean retnVale = mPrinter.SetCharWH(BitValue);
				if( !retnVale )
				{
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				}else{
					Toast.makeText(TestActivity.this,
							"字符的大字体打印 设定成功" , LENGTH_SHORT).show();
				}		 
				break;
			}
			case R.id.btn_SetCharWH_zhong: {   //  
				byte  BitValue =0x01+0x02;//仅中文倍宽倍高都有效
				boolean retnVale = mPrinter.SetCharWH(BitValue);
				if( !retnVale )
				{
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				}else{
					Toast.makeText(TestActivity.this,
							"中文大字体 设定成功" , LENGTH_SHORT).show();
				}		 
				break;
			}
			case R.id.btn_SetCharWH_xi: {   //  
				byte  BitValue =0x04+0x08;//仅西文倍宽倍高有效
				boolean retnVale = mPrinter.SetCharWH(BitValue);
				if( !retnVale )
				{
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				}else{
					Toast.makeText(TestActivity.this,
							"西文大字体 设定成功" , LENGTH_SHORT).show();
				}		 
				break;
			}
			case R.id.btn_SetCharWHcancel: {   // 取消字符的倍高/倍宽 
 
				byte  BitValue =0;//参数的二进制所有位都为0，就是设置成常规打印字体大小。
				boolean retnVale = mPrinter.SetCharWH(BitValue);
				if( !retnVale )
				{
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				}else{
					Toast.makeText(TestActivity.this,
							"常规字体设定 成功" , LENGTH_SHORT).show();
				}		 
				break;
			}

			case R.id.btn_PrintPinTest: {   // 打印针测试：打印针出针测试，检测打印机的打印针是否正常。

				boolean retnVale = mPrinter.PrintPinTest();
				if( !retnVale )
				{
					Toast.makeText(TestActivity.this,
							mPrinter.GetLastPrintErr() , LENGTH_SHORT).show();
				} else{
					Toast.makeText(TestActivity.this,
							"打印针测试 完毕" , LENGTH_SHORT).show();
				}			 
				break;
			}
	
			}//end switch   btn_SetVolLimit
		}

	}
	@Override
    protected void onDestroy() {
        super.onDestroy();
        mPrinter = null;  
         
    }

}
