package kd.scan;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.hardware.barcode.Scanner;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class code_template extends Activity{
//模板标准变量,不允许修改{
public Handler handler;
public Handler sqlhandler;
public sqlhelper sqlhelp;
public myapplication sqlapp;
msgbox showmsg=null;
public String strUUID;
DisplayMetrics dm = new DisplayMetrics();
private MediaPlayer mediaPlayer,mediaPlayerErr;
private boolean playBeep;
private static final float BEEP_VOLUME = 0.10f;//控制音量大小
private boolean vibrate;
private boolean go;
private String strCode;//最后一次扫描的条码内容
private int kk;
private int jj;
private int intBeepCount;//qk 响声的次数
private TabHost mytabhost;
private boolean blnIsBusy;//标记是否查询完,没有不进行下一次查询
private kd.scan.Control_Waitbar xml_template_ct_wait;
long lngSeconds=0;
private String strUserID;
private String strUserName;
//模板标准变量,不允许修改}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Window window;
window = getWindow();
requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//屏幕常亮
getWindowManager().getDefaultDisplay().getMetrics(dm);
showmsg=new msgbox(this);
setContentView(R.layout.xml_template );
xml_template_ct_wait=(kd.scan.Control_Waitbar)findViewById(R.id.xml_template_ct_wait);
mytabhost=(TabHost)findViewById(android.R.id.tabhost);
mytabhost.setup();
mytabhost.addTab(mytabhost.newTabSpec("tab1").setIndicator("页签1").setContent(R.id.xml_template_tab1));
mytabhost.addTab(mytabhost.newTabSpec("tab2").setIndicator("页签2").setContent(R.id.xml_template_tab2));
mytabhost.addTab(mytabhost.newTabSpec("tab3").setIndicator("页签3").setContent(R.id.xml_template_tab3));
mytabhost.addTab(mytabhost.newTabSpec("tab4").setIndicator("页签4").setContent(R.id.xml_template_tab4));
mytabhost.addTab(mytabhost.newTabSpec("tab5").setIndicator("页签5").setContent(R.id.xml_template_tab5));
handler=new Handler()
{
public void handleMessage(Message msg)
{
try{
switch(msg.what )
{
case Scanner.BARCODE_READ: {//扫描返回
playBeepSoundAndVibrate();
String strtext=(String)msg.obj;
strtext=strtext.replace("'", "");//源头防注入
onScanResult(strtext);
break;
}
case 2://savebill返回
blnIsBusy=false;
showWait(false,"");
String[] strData1;
strData1=msg.getData().getStringArray("strData");
if(strData1[1].equals("-1") | strData1[1].equals("-999"))
{
showmsg.dialog_yes_only("异常", strData1[2], "");
}
else
{
//todo 此处添加保存成功后(提交)代码
}
break;
case 0: //
blnIsBusy=false;
showWait(false,"");
String[] strData;
strData=msg.getData().getStringArray("strData");
if(strData[1].equals("-1") | strData[1].equals("-999"))
{
showmsg.dialog_yes_only("异常", strData[2], "");
}
else
{
//todo 此处添加查询返回后代码,根据msg.arg1区分是哪个函数调用的查询
}
break;
case 1: //基本没有上,update返回,其实查询就够了
Long rows;
String errmsg;
rows=msg.getData().getLong("returnRows");
errmsg=msg.getData().getString("errmsg");
if(rows==-1)
{
Log.d("qqkk","error-1:"+errmsg);
}else if(rows==-999){
Log.d("qqkk","error-999:"+errmsg);
}
else
{
Log.d("qqkk","OK:"+rows.toString());
}
break;
}
}catch(Exception ex)
{
showmsg.dialog_yes_only("异常", ex.getMessage(), "");
}
// process incoming messages here
}
};
sqlapp=(myapplication)getApplicationContext();
strUUID=sqlapp.strUUID;
sqlhelp=sqlapp.sqlhelp;//全局变量保存数据库操作线程
sqlhelp.superHd=handler;
strUserID=sqlapp.strUserID;
strUserName=sqlapp.strUserName;
//测试时,暂用管理员
if(strUserID.equals("0")){//16394 Administrator
strUserID="16394";
strUserName="Administrator";
}
showWait(true,"等待控件,请稍后...");
}
//TODO 自定义代码区********************************************************************************
//提交数据
private void Submit(int Type){
//组合数据,传递给webserevice,调用条码打印组件打印
try{
lngSeconds= System.currentTimeMillis(); //获取开始时间
if( !SubmitSafeCheck()){
showWait(false,"");
return;
}
showWait(true, "数据保存中...");
String strHead="";//表头
String strBillClasstypeid="-992";//-992 固定资产盘点数据保存
String strPrintType=String.valueOf(Type);//预留控制参数0
String strEntry="";//表体
String strRow="";//表体的一行
// //组织表体数据
// for(int i=0;i<ada4._ds.RowCount();i++){
// strRow=ada4.getValue(i, "资产编码")+"\\"+ada4.getValue(i, "资产名称")+"\\"+ada4.getValue(i, "型号")
// +"\\"+ada4.getValue(i, "部门ID")+"\\"+ada4.getValue(i, "部门编码")+"\\"+ada4.getValue(i, "部门名称")
// +"\\"+ada4.getValue(i, "使用人")+"\\"+ada4.getValue(i, "责任人");
//
// if(strEntry.equals("")){
// strEntry=strRow;
// }else{
// strEntry=strEntry+"|"+strRow;
// }
// }
//组织表头数据
// strHead=strBillClasstypeid+"\\" +strPrintType
// +"\\"+assetcheck_et_schemaname.getText().toString().replace("'", "")//invoice no
// +"\\"+strUserName ;
Log.d("cc", (System.currentTimeMillis()-lngSeconds)/1000+"秒");
if(strEntry.equals("")){
showmsg.dialog_toast("表体没有或者没有选中数据");
showWait(false,"");
return;
}
savebill(strHead,strEntry,2,0);
}catch(Exception e){
showWait(false,"");
showmsg.dialog_yes_only("异常", e.getMessage(), "");
}
}
private boolean SubmitSafeCheck(){
// todo 添加安全检测代码
try{
}catch(Exception e){
showmsg.dialog_yes_only("异常","SubmitSafeCheck:"+e.getMessage(), "");
return false;
}
return true;
}
private void savebill(String strhead,String strentry,int intWhat,int intarg1)
{
try{
if(!blnIsBusy){
blnIsBusy=true;
Message msg=sqlhelp.myhandler.obtainMessage();
Bundle data=new Bundle();
// strSQL="select isnull(a.fitemid,'')+' ' fstockid,isnull(a.fname,'')+' ' fstockname,isnull(a.fnumber,'')+' ' fstocknumber,isnull(b.fspid,'')+' ' fstockplaceid,isnull(b.fname,'') +' ' fstockplacename,isnull(b.ffullnumber,'') +' ' fstockplacenumber "
// + " from t_stock a left join t_stockplace b on a.fspgroupid=b.fspgroupid "
// +" where a.fdeleted=0 and b.fdeleted=0 and a.fnumber='"+strStockNo+"' and b.ffullnumber='"+strStockPlaceNo+"'";
data.putString("strhead", strhead);
data.putString("strentry", strentry);
data.putString("strYear", strUUID);
msg.setData(data);
msg.what =intWhat;
msg.arg1=intarg1;//通过arg1来区分是哪个函数调用的
sqlhelp.myhandler.sendMessage(msg);
}
}catch(Exception ex){
showmsg.dialog_yes_only("异常", "savebill:"+ex.getMessage(), "");
}
}
//把选中的行的内容填写到编辑页签
private void setEditText(int intRow)
{
if(intRow>=0){
}else{//清空内容
}
}
private void showData(String[] strData){
try{
String strBarcode="";
List<String> ll=new ArrayList<String>();
DataSet _ds=new DataSet();
_ds.SetDataSource(strData);
if(_ds.RowCount()==0){
showmsg.dialog_toast("没有数据");
setEditText(-1);
}
}catch(Exception e){
showmsg.dialog_yes_only("异常","showdata4:"+e.getMessage(), "");
}
}
//检测条码是否重复扫描
private String checkBarCodeRepeat(DataSet _ds){
//
// String strBarCode="";
// for(int i=0;i<ada4._ds.RowCount();i++){
// strBarCode=ada4.getValue(i, "资产编码");
// for(int j=0;j<_ds.RowCount();j++){
// if(strBarCode.equals(_ds.GetValue(j, "资产编码"))){
// return _ds.GetValue(j, "资产编码");
// }
// }
// }
return "";
}
//基础资料选择
private void ItemSelect(int requestCode,int Itemclass){
Intent tt=new Intent(this,baseinfolookup.class );
tt.putExtra("itemclassid", Itemclass);//
startActivityForResult(tt,requestCode); //如果用R.id.btitemnofrom,因为数据太长,系统会报错:can only use lower 16 bits for requestCode
}
//处理扫描返回的内容
private void onScanResult(String strScanCode){
String strStockNo="",strStockPlaceNo="";
String strBZcode="";//包装条码
int intIndex=0;
try{
if(strScanCode.startsWith("CK") & strScanCode.indexOf("|")>0)//仓库仓位条码格式CK004|CW004_000_00_00
{
}
else if(strScanCode.startsWith("CMD")){//扫描了命令条码
if(strScanCode.equals("CMD提交")){
}
}
else//其它情况都认为是扫描了条码,其它情况包括:扫描了仓库仓位,也有可能扫描了职员等
{
// String strSQL="";
// strCode=strScanCode;
// strSQL= " ";
// showWait(true,"卡片信息读取中...");
// SendSQLHandler(strSQL,0,1);
}
}catch(Exception ex){
showmsg.dialog_yes_only("异常", "onScanResult:"+ex.getMessage(), "");
}
}
private void SendSQLHandler(String strSQL,int intWhat,int intarg1)
{
try{
if(!blnIsBusy){
blnIsBusy=true;
Message msg=sqlhelp.myhandler.obtainMessage();
Bundle data=new Bundle();
// strSQL="select isnull(a.fitemid,'')+' ' fstockid,isnull(a.fname,'')+' ' fstockname,isnull(a.fnumber,'')+' ' fstocknumber,isnull(b.fspid,'')+' ' fstockplaceid,isnull(b.fname,'') +' ' fstockplacename,isnull(b.ffullnumber,'') +' ' fstockplacenumber "
// + " from t_stock a left join t_stockplace b on a.fspgroupid=b.fspgroupid "
// +" where a.fdeleted=0 and b.fdeleted=0 and a.fnumber='"+strStockNo+"' and b.ffullnumber='"+strStockPlaceNo+"'";
data.putString("strSQL", strSQL);
data.putString("strYear", strUUID);
msg.setData(data);
msg.what =intWhat;
msg.arg1=intarg1;//通过arg1来区分是哪个函数调用的
sqlhelp.myhandler.sendMessage(msg);
}
}catch(Exception ex){
showmsg.dialog_yes_only("异常", "SendSQLHandler:"+ex.getMessage(), "");
}
}
//显示或者隐藏进度条
private void showWait(boolean blnShow,String strmsg){
if(blnShow){
xml_template_ct_wait.setVisibility(View.VISIBLE);
xml_template_ct_wait.setText(strmsg);
}else{
xml_template_ct_wait.setVisibility(View.INVISIBLE);
xml_template_ct_wait.setText("");
}
}
//***************系统函数区***********************************************************************************
protected void onResume() {
try{
playBeep = true;
AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
playBeep = false;
}
initBeepSound();
initBeepSoundErr();
vibrate = true;
super.onResume();
sqlhelp.superHd=handler;//切回本activity执行sql需要的handler
}catch(Exception ex)
{
showmsg.dialog_yes_only("异常", "onResume:"+ex.getMessage(), "");
}
}
protected void onStart() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
try{
Scanner.m_handler=handler;
//初始化扫描头
Scanner.InitSCA();
super.onStart();
}catch(Exception e){
showmsg.dialog_yes_only("异常",e.getMessage(), "");
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(event.getRepeatCount()==0){
if(keyCode==4)
{
// finish();
}
else if((keyCode==220)) // KEYCODE_BRIGHTNESS_DOWN
{
if(!blnIsBusy) {
Scanner.Read(); }
}
}
return super.onKeyDown(keyCode, event);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
String scantext;
String strStockinfo;
String[] strdata;
String strisreturn="";
try{
//todo 此处添加调用其他activity返回后响应代码,常用于基础资料查询返回和日期选择返回
}catch(Exception e)
{
showmsg.dialog_err_only("异常", "onActivityResult:"+e.getMessage(), "");
}
}
public boolean onCreateOptionsMenu(Menu menu) {
setIconEnable(menu, true); //android 4.0显示菜单图标有问题,需用反射
menu.add( 1,R.string.xml_template_out,0,R.string.xml_template_out).setIcon(android.R.drawable.stat_sys_upload);
menu.add( 1,R.string.xml_template_in,0,R.string.xml_template_in).setIcon(android.R.drawable.stat_sys_download);
//menu.add( 1,R.string.xml_template_print,0,R.string.xml_template_print).setIcon(android.R.drawable.ic_menu_view);
menu.add( 1,R.string.xml_template_print,0,R.string.xml_template_print).setIcon(R.drawable.print);
menu.add( 1,R.string.xml_template_del,0,R.string.xml_template_del).setIcon(android.R.drawable.ic_menu_delete);
// menu.add( 1,R.string.xml_template_del,0,R.string.xml_template_del).setIcon(R.drawable.agt);
menu.add( 1,R.string.xml_template_del,0,R.string.xml_template_reset).setIcon(android.R.drawable.ic_menu_rotate);
menu.add( 1,R.string.xml_template_save,0,R.string.xml_template_save).setIcon(android.R.drawable.ic_menu_save);
return true;
}
private void setIconEnable(Menu menu, boolean enable)
{
try
{ //利用反射
Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
m.setAccessible(true);
//MenuBuilder实现Menu接口,创建菜单时,传进来的menu其实就是MenuBuilder对象(java的多态特征)
m.invoke(menu, enable);
} catch (Exception e)
{
e.printStackTrace();
}
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
try{
if(item.getGroupId()==1)
{
switch(item.getItemId())
{
case R.string.xml_template_save://保存数据
//todo
showmsg.dialog_err_only("异常", "保存失败", "" );
break;
}
}
}catch(Exception e)
{
showmsg.dialog_yes_only("异常", e.getMessage(), "");
}
return super.onMenuItemSelected(featureId, item);
}
private void initBeepSound() {
if (playBeep && mediaPlayer == null) {
// The volume on STREAM_SYSTEM is not adjustable, and users found it
// too loud,
// so we now play on the music stream.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(beepListener);
// AssetFileDescriptor file = getResources().openRawResourceFd(
// R.raw.beep);
AssetFileDescriptor file = getResources().openRawResourceFd(
R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(),
file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
} catch (IOException e) {
mediaPlayer = null;
}
}
}
private void initBeepSoundErr() {
if (playBeep && mediaPlayerErr == null) {
// The volume on STREAM_SYSTEM is not adjustable, and users found it
// too loud,
// so we now play on the music stream.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayerErr = new MediaPlayer();
mediaPlayerErr.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayerErr.setOnCompletionListener(beepListener);
// AssetFileDescriptor file = getResources().openRawResourceFd(
// R.raw.beep);
AssetFileDescriptor file = getResources().openRawResourceFd(
R.raw.err1);
try {
mediaPlayerErr.setDataSource(file.getFileDescriptor(),
file.getStartOffset(), file.getLength());
file.close();
mediaPlayerErr.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayerErr.prepare();
} catch (IOException e) {
mediaPlayerErr = null;
}
}
}
private static final long VIBRATE_DURATION = 200L;//震动时间 qk
private void playBeepSoundAndVibrate() {
if (playBeep && mediaPlayer != null) {
mediaPlayer.start();
}
if (vibrate) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(VIBRATE_DURATION);
}
}
private void playBeepSoundAndVibrateErr() {
if (playBeep && mediaPlayerErr != null) {
mediaPlayerErr.start();
}
if (vibrate) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(VIBRATE_DURATION);
}
}
/**
* When the beep has finished playing, rewind to queue up another one.
*/
private final OnCompletionListener beepListener = new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.seekTo(0);
if(intBeepCount>0)
{
mediaPlayer.start();//qk 连续响几次
intBeepCount--;
}
}
};
}
--
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<kd.scan.Control_Waitbar
android:id="@+id/xml_template_ct_wait"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
></kd.scan.Control_Waitbar>
<!-- <LinearLayout -->
<!-- android:id="@+id/xml_template_ll_wait" -->
<!-- android:layout_width="300dp" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:orientation="horizontal" -->
<!-- android:background="@drawable/waitbar" -->
<!-- android:layout_gravity="center" -->
<!-- android:visibility="visible" -->
<!-- > -->
<!-- <ProgressBar -->
<!-- android:id="@+id/xml_template_wait" -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:layout_gravity="center_vertical" -->
<!-- /> -->
<!-- <TextView -->
<!-- android:id="@+id/xml_template_tv_waitmsg" -->
<!-- android:layout_width="wrap_content" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:layout_gravity="center_vertical" -->
<!-- android:textColor="@color/white" -->
<!-- android:text="代码模板,请稍后..." -->
<!-- /> -->
<!-- </LinearLayout> -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- *******************************************xml_template_tab1********************************************* -->
<RelativeLayout
android:id="@+id/xml_template_tab1"
android:background="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
<!-- *********************************xml_template_tab2*************************************** -->
<LinearLayout
android:id="@+id/xml_template_tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
<!-- *********************************xml_template_tab3*************************************** -->
<LinearLayout
android:id="@+id/xml_template_tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
<!-- *********************************xml_template_tab4*************************************** -->
<LinearLayout
android:id="@+id/xml_template_tab4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
<!-- *********************************xml_template_tab5*************************************** -->
<LinearLayout
android:id="@+id/xml_template_tab5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>