package com.example.demoactivity;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class inActivity extends Activity
{
	private Button numButton;
	private Button xuliehao;
	private ProgressDialog dialog;
	private EditText editText2;
	private EditText editText3;
	private EditText editText1;
	private Handler handler;
	String Num;
	String Rnum;
	private int UPDATE;

	int t;
	long flag;
	long temp;
	public void onCreate(Bundle savedInstanceState) 
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.in_main);
		
		
		numButton=(Button)findViewById(R.id.numButton2);
		xuliehao=(Button)findViewById(R.id.haoButton1);
		editText2=(EditText)findViewById(R.id.editText2);
		editText3=(EditText)findViewById(R.id.editText3);
		editText1=(EditText)findViewById(R.id.editText1);
		
		
		xuliehao.setOnClickListener(listener);
		numButton.setOnClickListener(listener);
		
		
		 handler = new Handler() {  
		        public void handleMessage(Message msg) {
		        	if(msg.arg1==0)
		        	{
		        		if(msg.what==0)
		        		{
		        			dialog.dismiss();
		        			editText2.setText(Num);
		        		}
		        		else {
		        			dialog.dismiss();
			        		AlertDialog.Builder builder1=new AlertDialog.Builder(inActivity.this);
							builder1.setMessage(R.string.For_serial_number_failed);
							builder1.setIcon(R.drawable.warning);
							builder1.setTitle(R.string.in_main_Warning);
							builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
									
								}
							});
							AlertDialog dialog=builder1.create();
							dialog.show();
						}
		        	}
		        	else if(msg.arg1==1)
		        	{
		        		if(msg.what==0)
		        		{
		        			
		        			dialog.dismiss();
			        			editText1.setText(Rnum);
		        			
		        			
		        		}
		        		else {
		        			dialog.dismiss();
			        		AlertDialog.Builder builder1=new AlertDialog.Builder(inActivity.this);
							builder1.setMessage(R.string.For_random_number_failed);
							builder1.setIcon(R.drawable.warning);
							builder1.setTitle(R.string.in_main_Warning);
							builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
									
								}
							});
			        		
							AlertDialog dialog=builder1.create();
							dialog.show();
						}
		        	}

		        	super.handleMessage(msg);
		        }};

	}

	 private void newThread() {  
	        new Thread() {  
	            public void run() {  
	                
	                    try {
	                    	
	    					byte[] ppDeviceSN=new byte[50];
	    					int[] lpDeviceSNLen=new int[1];
	    					flag=DeviceSerialNumber(ppDeviceSN,lpDeviceSNLen);
	    					Num=new String(ppDeviceSN,0,lpDeviceSNLen[0]);
	    					t=0;
	    					sendMsg(flag,t);
	                    } catch (Exception e) {  
	                        e.printStackTrace();  
	                    }  
	                
	            }  
	        }.start();  
	    }  
	 private void newGetRandomNumber() {  
	        new Thread() {  
	            public void run() {  
	                
	                    try {
	                    	long a=Integer.parseInt(editText3.getText().toString());
	    					byte[] random=new byte[50];
	    					int[] randomLen=new int[1];
	    					temp=RandomNumber(a,randomLen,random);
	    					Rnum=new String(random,0,randomLen[0]);
	    					t=1;
	    					sendMsg(temp,t);
	                    } catch (Exception e) {  
	                        e.printStackTrace();  
	                    }  
	                
	            }  
	        }.start();  
	    }  
	private OnClickListener listener=new OnClickListener()
	{

		public void onClick(View v) {
			if(v.getId()==R.id.haoButton1)
			{
				// TODO Auto-generated method stub
				dialog = new ProgressDialog(inActivity.this);
				dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				dialog.setTitle(R.string.tip);
	            dialog.setMessage(v.getContext().getString(R.string.Are_read_serial_number));
	            dialog.setIndeterminate(false);
	            dialog.setCancelable(true);
	            dialog.show();
	          newThread();
	           
			}
			
			if(v.getId()==R.id.numButton2)
			{
				if("".equals(editText3.getText().toString())||Integer.parseInt(editText3.getText().toString())>32||Integer.parseInt(editText3.getText().toString())<1)
				{
					AlertDialog.Builder builder1=new AlertDialog.Builder(inActivity.this);
					builder1.setMessage(R.string.input_for_the_length_of_the_serial_number);
					builder1.setIcon(R.drawable.warning);
					builder1.setTitle(R.string.in_main_Warning);
					builder1.setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							//dialog.dismiss();  
							dialog.dismiss();
							
						}
					});
	        		
					AlertDialog dialog=builder1.create();
					dialog.show();
				}
				else{
					// TODO Auto-generated method stub
					
					dialog = new ProgressDialog(inActivity.this);
					dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					dialog.setTitle(R.string.tip);
		            dialog.setMessage(v.getContext().getString(R.string.Just_make_random_number));
		            dialog.setIndeterminate(false);
		            dialog.setCancelable(true);
		            dialog.show();
		           
		            newGetRandomNumber();
				}
				
	           
			}
			}
			
		
	};
	
	private void sendMsg(long result,int i) {  
        Message msg = new Message();  
        msg.what=(int) result;
        msg.arg1=i;
        handler.sendMessage(msg);  
    } 

	public static native long aa();
	public static native long DeviceSerialNumber(byte[] ppDeviceSN, int[] lpDeviceSNLen);
	public static native long RandomNumber(long a,int[] randomLen, byte[] random);
	static {
        int version = 0;   
        try {   
           version = Integer.valueOf(android.os.Build.VERSION.SDK);   
        } catch (NumberFormatException e) {   
        }
        if(version < 9)
           System.loadLibrary("Demo8");
        else
           System.loadLibrary("Demo"); 
       }
}
