Java tutorial
package com.cwp.cmoneycharge.activity; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.ComponentName; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.speech.RecognitionListener; import android.speech.SpeechRecognizer; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.DatePicker; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.Spinner; import android.widget.Toast; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.speech.VoiceRecognitionService; import com.cwp.chart.manager.SystemBarTintManager; import com.cwp.cmoneycharge.Effectstype; import com.cwp.cmoneycharge.R; import com.cwp.cmoneycharge.api.Constant; import com.cwp.cmoneycharge.app.CrashApplication; import com.cwp.cmoneycharge.app.SysApplication; import com.cwp.cmoneycharge.utils.DialogShowUtil; import com.cwp.cmoneycharge.utils.DigitUtil; import com.cwp.cmoneycharge.utils.KeyboardUtil; import com.cwp.cmoneycharge.widget.NiftyDialogBuilder; import com.cwp.pattern.activity.UnlockGesturePasswordActivity; import com.example.testpic.activity.PublishedActivity; import com.example.testpic.utils.Bimp; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.List; import cwp.moneycharge.dao.IncomeDAO; import cwp.moneycharge.dao.ItypeDAO; import cwp.moneycharge.dao.PayDAO; import cwp.moneycharge.dao.PtypeDAO; import cwp.moneycharge.model.Tb_income; import cwp.moneycharge.model.Tb_pay; /** * ? */ public class AddPayActivity extends Activity implements OnClickListener, RecognitionListener { protected static final int DATE_DIALOG_ID = 0;// ?? static String type = "pay"; String VoiceDefault = ""; protected static String typemode = "add"; EditText txtMoney, txtTime, txtAddress, txtMark;// EditText Spinner spType;// Spinner Button btnSaveButton;// Button?? Button btnCancelButton;// Button?? Button btnVoice;// Button? int userid; int Selection = 0; Bundle bundle = null; String[] strInfos = null;// String strno, strType;// ?????? private FrameLayout corporation_fl, address_fl = null; private RadioButton rb1 = null; private RadioButton rb2 = null; private ImageView left_back;//? private Effectstype effect; // Dialog NiftyDialogBuilder dialogBuilder = null; Boolean firstin = true; private int mYear;// private int mMonth;// private int mDay;// private ArrayAdapter<String> adapter; private String[] spdata; String[] number = { "", "", "", "", "", "", "", "", "", "?", "??" }; String[] money = { "", "?", "" }; String[] money2 = { "??", "", "?", "", "" }; String[] voice_pay = { "", "?" }; String[] voice_income = { "?", "" }; String[] VoiceSave = new String[6]; static DialogShowUtil dialogShowUtil; PtypeDAO ptypeDAO = new PtypeDAO(AddPayActivity.this); ItypeDAO itypeDAO = new ItypeDAO(AddPayActivity.this); PayDAO payDAO = new PayDAO(AddPayActivity.this);// PayDAO IncomeDAO incomeDAO = new IncomeDAO(AddPayActivity.this);// IncomeDAO List<String> spdatalist, spdatalist2; private SystemBarTintManager mTintManager; private ImageView btn_loacte; private ImageView addphoto; protected String textphoto = ""; private int incount = 0; private boolean keycount = true; private FrameLayout bottom_empty; private LinearLayout bottom_full; private KeyboardUtil keyBoard; //TODO private LocationClient mLocationClient; private BDLocationListener mLocationListener = new MyLocationListener(); private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) { return; } StringBuilder sb = new StringBuilder(1024); sb.append(location.getAddrStr()); Log.d("adds", sb.toString()); txtAddress.setText(sb.toString()); } } //TODO private static final String TAG = "Sdk2Api"; private static final int REQUEST_UI = 1; public static final int STATUS_None = 0; public static final int STATUS_WaitingReady = 2; public static final int STATUS_Ready = 3; public static final int STATUS_Speaking = 4; public static final int STATUS_Recognition = 5; private SpeechRecognizer speechRecognizer; private int status = STATUS_None; private long speechEndTime = -1; private static final int EVENT_ERROR = 11; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add);// SysApplication.getInstance().addActivity(this); // ?this super.onStart();// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); findViewById(R.id.top).setVisibility(View.VISIBLE); } mTintManager = new SystemBarTintManager(this); mTintManager.setStatusBarTintEnabled(true); mTintManager.setStatusBarTintResource(R.color.statusbar_bg); txtMoney = (EditText) findViewById(R.id.txtMoney);// ?? txtTime = (EditText) findViewById(R.id.txtTime);// ? txtAddress = (EditText) findViewById(R.id.txtAddress);// ? txtMark = (EditText) findViewById(R.id.txtMark);// ? spType = (Spinner) findViewById(R.id.spType);// ? btnSaveButton = (Button) findViewById(R.id.btnSave);// ?? btnCancelButton = (Button) findViewById(R.id.btnCancel);// ?? btnVoice = (Button) findViewById(R.id.btnVoice);// ? rb1 = (RadioButton) findViewById(R.id.payout_tab_rb); rb2 = (RadioButton) findViewById(R.id.income_tab_rb); left_back = (ImageView) findViewById(R.id.example_left3); btn_loacte = (ImageView) findViewById(R.id.btn_loacte); addphoto = (ImageView) findViewById(R.id.addphoto); bottom_empty = (FrameLayout) findViewById(R.id.bottom_empty); bottom_full = (LinearLayout) findViewById(R.id.bottom_full); //? //? mLocationClient = new LocationClient(getApplicationContext()); mLocationClient.registerLocationListener(mLocationListener); //TODO speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, new ComponentName(this, VoiceRecognitionService.class)); speechRecognizer.setRecognitionListener(this); dialogShowUtil = new DialogShowUtil(this, this, VoiceSave, type, // ?dialog VoiceDefault); btn_loacte.setOnClickListener(this); // ? // ???? bottom_empty.setOnClickListener(new OnClickListener() { public void onClick(View v) { keyBoard.hideKeyboard(); bottom_empty.setVisibility(View.GONE); bottom_full.setVisibility(View.VISIBLE); } }); // addphoto.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(AddPayActivity.this, PublishedActivity.class); intent.putExtra("cwp.id", userid); startActivityForResult(intent, 102); } }); left_back.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = null; type = "pay"; typemode = "add"; if (bundle.containsKey("cwp.frament3")) { intent = new Intent(AddPayActivity.this, MainActivity.class); intent.putExtra("cwp.Fragment", "3");// ? } else { intent = new Intent(AddPayActivity.this, MainActivity.class); } intent.putExtra("cwp.id", userid); startActivity(intent); finish();// } }); // mRecognitionListener = new DialogRecognitionListener() { // ? // // @Override // public void onResults(Bundle results) { // ArrayList<String> rs = results != null ? results // .getStringArrayList(RESULTS_RECOGNITION) : null; // if (rs != null && rs.size() > 0) { // Recognition(rs.get(0)); // ? // // Toast.makeText(AddPayActivity.this, rs.get(0), // // Toast.LENGTH_SHORT).show(); // } // } // }; corporation_fl = (FrameLayout) findViewById(R.id.corporation_fl); address_fl = (FrameLayout) findViewById(R.id.address_fl); //checkBox rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (rb1.isChecked()) { // type = "pay"; } else // { type = "income"; } updatetype(); } }); final Calendar c = Calendar.getInstance();// ?? mYear = c.get(Calendar.YEAR);// ? mMonth = c.get(Calendar.MONTH);// ? mDay = c.get(Calendar.DAY_OF_MONTH);// ? } //TODO ? private void initBaiDuMap() { //?? LocationClientOption option = new LocationClientOption(); option.setIsNeedAddress(true); option.setOpenGps(true); option.setAddrType("all"); mLocationClient.setLocOption(option); mLocationClient.start(); //mLocationClient.requestLocation(); if (mLocationClient != null && mLocationClient.isStarted()) { Log.d("MainActivity", "??"); mLocationClient.requestLocation(); } else { Log.d("MainActivity", "LocClient is null or not started"); } } @TargetApi(19) private void setTranslucentStatus(boolean on) { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); } private void initData(int userid) { // ?? if (typemode == "add") { // ? if (type == "pay") { // rb1.setChecked(true); spdatalist = ptypeDAO.getPtypeName(userid); txtMoney.setTextColor(Color.parseColor("#5ea98d")); } else { // rb2.setChecked(true); spdatalist = itypeDAO.getItypeName(userid); txtMoney.setTextColor(Color.parseColor("#ffff0000")); } } else { // ? bottom_empty.setVisibility(View.GONE); bottom_full.setVisibility(View.VISIBLE); rb1.setOnCheckedChangeListener(null); btnSaveButton.setText(""); // ? btnCancelButton.setText(""); // ? CharSequence textreAddres; String textreMark; if (type == "pay") { // rb1.setChecked(true); rb1.setClickable(false); rb2.setClickable(false); // ? spdatalist = ptypeDAO.getPtypeName(userid); // ???Tb_pay Tb_pay tb_pay = payDAO.find(userid, Integer.parseInt(strno)); txtMoney.setText(tb_pay.getMoney2());// ? txtMoney.setTextColor(Color.parseColor("#5ea98d")); txtTime.setText(tb_pay.getTime());// Selection = tb_pay.getType() - 1; initphotodata(tb_pay.getPhoto()); textreAddres = tb_pay.getAddress(); textreMark = tb_pay.getMark(); txtAddress.setText(textreAddres);// txtMark.setText(textreMark);// } else { // // ? rb2.setChecked(true); rb1.setClickable(false); rb2.setClickable(false); spdatalist = itypeDAO.getItypeName(userid); // ???Tb_pay Tb_income tb_income = incomeDAO.find(userid, Integer.parseInt(strno)); txtMoney.setText(tb_income.getMoney2());// ? txtMoney.setTextColor(Color.parseColor("#ffff0000")); txtTime.setText(tb_income.getTime());// Selection = tb_income.getType() - 1; // initphotodata(tb_income.getPhoto()); textreAddres = tb_income.getHandler(); textreMark = tb_income.getMark(); txtAddress.setText(textreAddres);// txtMark.setText(textreMark);// } } } private void initphotodata(String photo) { // ?? if ((incount == 0) && (!photo.equals(""))) { String[] photoall = photo.split(","); for (int i = 0; i < photoall.length / 2; i++) { if (Bimp.drr.size() < 9) { Bimp.drr.add(photoall[i]); } } for (int i = photoall.length / 2; i < photoall.length; i++) { if (Bimp.smdrr.size() < 9) { Bimp.smdrr.add(photoall[i]); } } textphoto = photo; initphoto(); incount++; } } @SuppressWarnings("deprecation") private void initphoto() {// ? try { if (textphoto.equals("")) { addphoto.setImageResource(R.drawable.addphoto_btn); } else if (Bimp.getbitmap(Bimp.smdrr.get(0)) == null) { Toast.makeText(AddPayActivity.this, "?", Toast.LENGTH_SHORT).show(); } else { addphoto.setImageBitmap(Bimp.getbitmap(Bimp.smdrr.get(0))); } } catch (IOException e) { e.printStackTrace(); } } private void updatetype() { // initData(userid); spdata = spdatalist.toArray(new String[spdatalist.size()]);// tb_itypeid? adapter = new ArrayAdapter<String>(AddPayActivity.this, R.layout.spinner, spdata); // ?? spType.setAdapter(adapter); if (Selection > 0) { spType.setSelection(Selection);// } } @Override protected void onStart() { // ?onstart super.onStart();// updateDisplay();// ? Intent intentr = getIntent(); userid = intentr.getIntExtra("cwp.id", 100000001); bundle = intentr.getExtras();// ??Bundle if (bundle.containsKey("cwp.message")) { strInfos = bundle.getStringArray("cwp.message");// ?Bundle? strno = strInfos[0];// id strType = strInfos[1];// typemode = "ModifyInPActivity"; if (strType.equals("btnininfo")) { // type = "income"; } else { type = "pay"; } } keyBoard = new KeyboardUtil(this, this, txtMoney, typemode); // if (bundle.containsKey("cwp.voice")) { // ? if (firstin) { bottom_empty.setVisibility(View.GONE); bottom_full.setVisibility(View.VISIBLE); dialogShowUtil.dialogShow("rotatebottom", "first", "", ""); firstin = false; } } if (bundle.containsKey("cwp.photo")) {// ?? if (firstin) { bottom_empty.setVisibility(View.GONE); bottom_full.setVisibility(View.VISIBLE); Intent intent = new Intent(AddPayActivity.this, PublishedActivity.class); intent.putExtra("cwp.id", userid); intent.putExtra("cwp.photo", "photo"); startActivityForResult(intent, 102); firstin = false; } } if (bundle.containsKey("keyboard")) { // ? if (keycount) { InputMethodManager imm = (InputMethodManager) getSystemService( AddPayActivity.this.INPUT_METHOD_SERVICE); // imm.hideSoftInputFromWindow(txtMoney.getWindowToken(), 0); // ?? keyBoard.showKeyboard(); keycount = false; } } updatetype(); txtTime.setOnTouchListener(new OnTouchListener() { // ?? @Override public boolean onTouch(View v, MotionEvent event) { showDialog(DATE_DIALOG_ID);// ? return false; } }); txtMoney.setOnTouchListener(new OnTouchListener() { // ? @Override public boolean onTouch(View v, MotionEvent event) { InputMethodManager imm = (InputMethodManager) getSystemService( AddPayActivity.this.INPUT_METHOD_SERVICE); // imm.hideSoftInputFromWindow(txtMoney.getWindowToken(), 0); // ?? keyBoard.showKeyboard(); return false; } }); //TODO btnVoice.setOnClickListener(new OnClickListener() {// ? @Override public void onClick(View v) { dialogShowUtil.dialogShow("rotatebottom", "first", "", ""); //VoiceRecognition(); } }); // ?? btnSaveButton.setOnClickListener(new OnClickListener() { private String textreAddres; private String textreMark; @SuppressLint("NewApi") @Override public void onClick(View arg0) { textreAddres = txtAddress.getText().toString(); textreMark = txtMark.getText().toString(); if (textphoto == null) { textphoto = ""; } if (typemode == "add") { // ? String strMoney = txtMoney.getText().toString();// ?? if (type == "pay") { // if (!strMoney.isEmpty()) {// ?? // InaccountDAO PayDAO payDAO = new PayDAO(AddPayActivity.this); // Tb_inaccount Tb_pay tb_pay = new Tb_pay(userid, payDAO.getMaxNo(userid) + 1, get2Double(strMoney), setTimeFormat(null), (spType.getSelectedItemPosition() + 1), textreAddres, textreMark, textphoto); payDAO.add(tb_pay);// ? Toast.makeText(AddPayActivity.this, "???", Toast.LENGTH_SHORT).show(); gotoback(); } else { Toast.makeText(AddPayActivity.this, "??", Toast.LENGTH_SHORT) .show(); } } else { // if (!strMoney.isEmpty()) {// ?? // InaccountDAO IncomeDAO incomeDAO = new IncomeDAO(AddPayActivity.this); // Tb_inaccount Tb_income tb_income = new Tb_income(userid, payDAO.getMaxNo(userid) + 1, get2Double(strMoney), setTimeFormat(null), (spType.getSelectedItemPosition() + 1), // txtInhandler.getText().toString(), textreAddres, textreMark, textphoto, ""); incomeDAO.add(tb_income);// ? // ??? Toast.makeText(AddPayActivity.this, "???", Toast.LENGTH_SHORT).show(); gotoback(); } else { Toast.makeText(AddPayActivity.this, "??", Toast.LENGTH_SHORT) .show(); } } } else { // ? if (type == "pay") { // if (!txtMoney.getText().toString().isEmpty()) {// ?? Tb_pay tb_pay = new Tb_pay(); // Tb_pay tb_pay.set_id(userid); // userid tb_pay.setNo(Integer.parseInt(strno)); // ? tb_pay.setMoney(get2Double(txtMoney.getText().toString()));// ? tb_pay.setTime(setTimeFormat(txtTime.getText().toString()));// tb_pay.setType(spType.getSelectedItemPosition() + 1);// tb_pay.setAddress(textreAddres);// tb_pay.setMark(textreMark);// tb_pay.setPhoto(textphoto);// payDAO.update(tb_pay);// ? Toast.makeText(AddPayActivity.this, "???", Toast.LENGTH_SHORT) .show(); gotoback(); } else { Toast.makeText(AddPayActivity.this, "??", Toast.LENGTH_SHORT) .show(); } } else { // if (!txtMoney.getText().toString().isEmpty()) {// ?? Tb_income tb_income = new Tb_income();// Tb_income tb_income.set_id(userid);// ? tb_income.setNo(Integer.parseInt(strno));// ? tb_income.setMoney(get2Double(txtMoney.getText().toString()));// ? tb_income.setTime(setTimeFormat(txtTime.getText().toString()));// tb_income.setType(spType.getSelectedItemPosition() + 1);// tb_income.setHandler(textreAddres);// tb_income.setMark(textreMark);// tb_income.setPhoto(textphoto);// incomeDAO.update(tb_income);// ? Toast.makeText(AddPayActivity.this, "???", Toast.LENGTH_SHORT) .show(); gotoback(); } else { Toast.makeText(AddPayActivity.this, "??", Toast.LENGTH_SHORT) .show(); } } } } }); btnCancelButton.setOnClickListener(new OnClickListener() {// ??? @Override public void onClick(View arg0) { if (typemode == "add") { // ? txtMoney.setText("");// ? txtMoney.setHint("0.00");// ??? txtTime.setText("");// txtAddress.setText("");// ? txtMark.setText("");// // txtInhandler.setText("");// spType.setSelection(0);// gotoback(); } else { // ? if (type == "pay") { // payDAO.detele(userid, Integer.parseInt(strno));// ??? gotoback(); } else { // incomeDAO.detele(userid, Integer.parseInt(strno));// ??? gotoback(); } Toast.makeText(AddPayActivity.this, "???", Toast.LENGTH_SHORT).show(); } } }); } // ?2?? public static double get2Double(String strMoney) { // ?? Double a = Double.parseDouble(strMoney); DecimalFormat df = new DecimalFormat("0.00"); return new Double(df.format(a)); } @Override protected Dialog onCreateDialog(int id)// ?onCreateDialog { switch (id) { case DATE_DIALOG_ID:// ? return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); } return null; } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year;// mMonth = monthOfYear;// mDay = dayOfMonth;// updateDisplay();// } }; private void updateDisplay() { // txtTime.setText(new StringBuilder().append(mYear).append("-").append(mMonth + 1).append("-").append(mDay)); } // ? private String setTimeFormat(String newtxtTime) { String date; if (typemode == "add") { date = txtTime.getText().toString(); } else { date = newtxtTime; } int y, m, d; String sm, sd; int i = 0, j = 0, k = 0; for (i = 0; i < date.length(); i++) { if (date.substring(i, i + 1).equals("-") && j == 0) j = i; else if (date.substring(i, i + 1).equals("-")) k = i; } y = Integer.valueOf(date.substring(0, j)); m = Integer.valueOf(date.substring(j + 1, k)); d = Integer.valueOf(date.substring(k + 1)); if (m < 10) { sm = "0" + String.valueOf(m); } else sm = String.valueOf(m); if (d < 10) { sd = "0" + String.valueOf(d); } else sd = String.valueOf(d); return String.valueOf(y) + "-" + sm + "-" + sd; } /** * */ public void VoiceRecognition() { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(AddPayActivity.this); boolean api = sp.getBoolean("api", false); if (api) { switch (status) { case STATUS_None: start(); status = STATUS_WaitingReady; break; case STATUS_WaitingReady: cancel(); status = STATUS_None; break; case STATUS_Ready: cancel(); status = STATUS_None; break; case STATUS_Speaking: stop(); status = STATUS_Recognition; break; case STATUS_Recognition: cancel(); status = STATUS_None; break; } } else { start(); } } public void VoiceSuccess() { // ?? if (DialogShowUtil.dialoggettype() != null) { type = DialogShowUtil.dialoggettype(); } VoiceDefault = DialogShowUtil.dialogVoiceDefault(); String textreMark = txtMark.getText().toString(); if (typemode == "add") { // ? if (type == "pay") { // rb1.setChecked(true); // corporation_fl.setVisibility(View.GONE); // address_fl.setVisibility(View.VISIBLE); spdatalist = ptypeDAO.getPtypeName(userid); txtMoney.setText(VoiceSave[1]);// ? txtMoney.setTextColor(Color.parseColor("#5ea98d")); if (VoiceDefault == "notype") { // spType.setSelection(Integer.parseInt(VoiceSave[5]));// } else { spType.setSelection(Integer.parseInt(VoiceSave[0]));// } txtMark.setText(textreMark + " " + VoiceSave[2]);// } else { // rb2.setChecked(true); // corporation_fl.setVisibility(View.VISIBLE); // address_fl.setVisibility(View.GONE); spdatalist = ptypeDAO.getPtypeName(userid); txtMoney.setText(VoiceSave[1]);// ? txtMoney.setTextColor(Color.parseColor("#ffff0000")); if (VoiceDefault == "notype") { // spType.setSelection(Integer.parseInt(VoiceSave[5]));// } else { spType.setSelection(Integer.parseInt(VoiceSave[4]));// } txtMark.setText(textreMark + " " + VoiceSave[2]);// } } else { // ? if (type == "pay") { // rb1.setChecked(true); // ? spdatalist = ptypeDAO.getPtypeName(userid); spdata = spdatalist.toArray(new String[spdatalist.size()]);// tb_itypeid? adapter = new ArrayAdapter<String>(AddPayActivity.this, R.layout.spinner, spdata); // ?? spType.setAdapter(adapter); txtMoney.setText(VoiceSave[1]);// ? txtMoney.setTextColor(Color.parseColor("#5ea98d")); if (VoiceDefault == "notype") { // spType.setSelection(Integer.parseInt(VoiceSave[5]));// } else { spType.setSelection(Integer.parseInt(VoiceSave[0]));// } txtMark.setText(textreMark + " " + VoiceSave[2]);// } else { // // ? rb2.setChecked(true); spdatalist = itypeDAO.getItypeName(userid); spdata = spdatalist.toArray(new String[spdatalist.size()]);// tb_itypeid? adapter = new ArrayAdapter<String>(AddPayActivity.this, R.layout.spinner, spdata); // ?? spType.setAdapter(adapter); txtMoney.setText(VoiceSave[1]);// ? txtMoney.setTextColor(Color.parseColor("#ffff0000")); if (VoiceDefault == "notype") { // spType.setSelection(Integer.parseInt(VoiceSave[5]));// } else { spType.setSelection(Integer.parseInt(VoiceSave[4]));// } txtMark.setText(textreMark + " " + VoiceSave[2]);// } } } /* * ? * * @param VoiceSave[0] * * @param VoiceSave[1] ? * * @param VoiceSave[3] ???? * * @param VoiceSave[4] * * @param VoiceSave[5] "" */ private void Recognition(String t) { int mfirst = 100, mend = 0, temp = 0; Boolean ismoney = false, intype = false, outtype = false; Boolean voice_ptype = false, voice_intype = false; String w = "", strmoney = "", inname = "1", outname = "2"; spdatalist = ptypeDAO.getPtypeName(userid); spdatalist2 = itypeDAO.getItypeName(userid); VoiceSave[2] = t; for (int i = 0; i < spdatalist.size(); i++) { // ?? if (t.indexOf(spdatalist.get(i).toString()) > -1) { type = "pay"; intype = true; inname = spdatalist.get(i).toString(); VoiceSave[0] = Integer.toString(i); // VoiceSave[0] } } for (int i = 0; i < voice_pay.length; i++) { // ??? if (t.indexOf(voice_pay[i]) > -1) { voice_ptype = true; } } for (int i = 0; i < voice_income.length; i++) { // ??? if (t.indexOf(voice_income[i]) > -1) { voice_intype = true; } } for (int i = 0; i < spdatalist2.size(); i++) { // ?? if (t.indexOf(spdatalist2.get(i).toString()) > -1) { type = "income"; outtype = true; outname = spdatalist2.get(i).toString(); VoiceSave[4] = Integer.toString(i); // VoiceSave[4] } } for (int i = 0; i < number.length; i++) { // ??? if (t.indexOf(number[i]) > -1) { temp = t.indexOf(number[i]); if (temp < mfirst) { mfirst = temp; } } } for (int i = 0; i < money.length; i++) { // ??? if (t.indexOf(money[i]) > -1) { temp = t.indexOf(money[i]); if (temp > -1 && temp >= mend) { mend = temp; } } } for (int i = 0; i < money2.length; i++) { // ??? if (t.indexOf(money2[i]) > -1) { temp = t.indexOf(money2[i]); if (temp > -1 && temp >= mend) { mend = temp; } mend = mend + 1; } } if (!(mfirst == 100 || mend == 0)) { // ? ismoney = true; strmoney = t.substring(mfirst, mend); // ???? char[] chs = strmoney.toCharArray(); List<String> num = Arrays.asList(number); List<String> mon = Arrays.asList(money); List<String> mon2 = Arrays.asList(money2); for (int l = 0; l < chs.length; l++) if (!num.contains(String.valueOf(chs[l]))) if (!mon.contains(String.valueOf(chs[l]))) if (!mon2.contains(String.valueOf(chs[l]))) ismoney = false; if (ismoney) { DigitUtil Util = new DigitUtil(); VoiceSave[1] = Integer.toString(Util.parse(strmoney)); // ?? } } if (intype && outtype) { // ??/ if (outname.equals(inname)) { if (ismoney) { if (voice_intype) { type = "income"; dialogShowUtil.dialogShow("rotatebottom", "OK", t, w); } else if (voice_ptype) { type = "pay"; dialogShowUtil.dialogShow("rotatebottom", "OK", t, w); } else { VoiceSave[3] = outname; // VoiceSave[3]???? dialogShowUtil.dialogShow("shake", "judge", t, w); // ?? } } else { w = "??\n??<?>\n"; dialogShowUtil.dialogShow("shake", "wrong", t, w); } } else { w = "**??\n??\n"; // ? dialogShowUtil.dialogShow("shake", "wrong", t, w); } } else { if (!((intype || outtype) || ismoney)) { // ??? w = "**??\n??<>" + listToString(spdatalist, '') + "" + listToString(spdatalist2, '') + "\n\n**??\n??<?>"; dialogShowUtil.dialogShow("shake", "wrong", t, w); } else if ((intype || outtype) && (!ismoney)) { w = "??\n??<?>\n?"; dialogShowUtil.dialogShow("shake", "wrong", t, w); } else if ((!(intype || outtype)) && ismoney) { for (int i = 0; i < spdatalist.size(); i++) { // ?? if ("".indexOf(spdatalist.get(i).toString()) > -1) { VoiceSave[5] = Integer.toString(i); VoiceSave[3] = ""; } } w = "**??\n??<>" + listToString(spdatalist, '') + "\n\n\n<>??\n"; dialogShowUtil.dialogShow("shake", "notype", t, w); } else { dialogShowUtil.dialogShow("rotatebottom", "OK", t, w); } } } public String listToString(List list, char separator) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.size(); i++) { sb.append(list.get(i)); if (i < list.size() - 1) { sb.append(separator); } } return sb.toString(); } private boolean gotoback() { // Intent intent = null; type = "pay"; typemode = "add"; Bimp.drr = new ArrayList<String>(); Bimp.smdrr = new ArrayList<String>(); Bimp.bmp = new ArrayList<Bitmap>(); Bimp.max = 0; Bimp.flag = 0; if (bundle.containsKey("cwp.frament3")) { intent = new Intent(AddPayActivity.this, MainActivity.class); intent.putExtra("cwp.Fragment", "3");// ? } else if (bundle.containsKey("cwp.search")) { this.setResult(3); this.finish(); return true; } else { intent = new Intent(AddPayActivity.this, MainActivity.class); intent.putExtra("cwp.Fragment", "1"); } intent.putExtra("cwp.id", userid); startActivity(intent); finish(); return true; } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // //? gotoback(); } return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { super.onDestroy(); speechRecognizer.destroy(); mLocationClient.unRegisterLocationListener(mLocationListener); } protected void onResume() { super.onResume(); SharedPreferences sp = this.getSharedPreferences("preferences", MODE_WORLD_READABLE); CrashApplication myApplaction = (CrashApplication) getApplication(); if ((myApplaction.isLocked) && (sp.getString("gesturepw", "").equals(""))) {// ???? Intent intent = new Intent(this, UnlockGesturePasswordActivity.class); startActivity(intent); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_loacte: //TODO ? initBaiDuMap(); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { onResults(data.getExtras()); } switch (requestCode) { case 102: if (resultCode == 3 || resultCode == 0) { if ((Bimp.drr.size() != 0) && (Bimp.smdrr.size() != 0)) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < Bimp.drr.size(); i++) { sb.append(Bimp.drr.get(i) + ","); } for (int i = 0; i < Bimp.drr.size(); i++) { sb.append(Bimp.smdrr.get(i) + ","); } textphoto = sb.toString().substring(0, sb.length() - 1); initphoto(); } else { textphoto = ""; initphoto(); } } } } public static void showVoiveDialog() { dialogShowUtil.dialogShow("rotatebottom", "first", "", ""); } public void bindParams(Intent intent) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); if (sp.getBoolean("tips_sound", true)) { intent.putExtra(Constant.EXTRA_SOUND_START, R.raw.bdspeech_recognition_start); intent.putExtra(Constant.EXTRA_SOUND_END, R.raw.bdspeech_speech_end); intent.putExtra(Constant.EXTRA_SOUND_SUCCESS, R.raw.bdspeech_recognition_success); intent.putExtra(Constant.EXTRA_SOUND_ERROR, R.raw.bdspeech_recognition_error); intent.putExtra(Constant.EXTRA_SOUND_CANCEL, R.raw.bdspeech_recognition_cancel); } if (sp.contains(Constant.EXTRA_INFILE)) { String tmp = sp.getString(Constant.EXTRA_INFILE, "").replaceAll(",.*", "").trim(); intent.putExtra(Constant.EXTRA_INFILE, tmp); } if (sp.getBoolean(Constant.EXTRA_OUTFILE, false)) { intent.putExtra(Constant.EXTRA_OUTFILE, "sdcard/outfile.pcm"); } if (sp.contains(Constant.EXTRA_SAMPLE)) { String tmp = sp.getString(Constant.EXTRA_SAMPLE, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_SAMPLE, Integer.parseInt(tmp)); } } if (sp.contains(Constant.EXTRA_LANGUAGE)) { String tmp = sp.getString(Constant.EXTRA_LANGUAGE, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_LANGUAGE, tmp); } } if (sp.contains(Constant.EXTRA_NLU)) { String tmp = sp.getString(Constant.EXTRA_NLU, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_NLU, tmp); } } if (sp.contains(Constant.EXTRA_VAD)) { String tmp = sp.getString(Constant.EXTRA_VAD, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_VAD, tmp); } } String prop = null; if (sp.contains(Constant.EXTRA_PROP)) { String tmp = sp.getString(Constant.EXTRA_PROP, "").replaceAll(",.*", "").trim(); if (null != tmp && !"".equals(tmp)) { intent.putExtra(Constant.EXTRA_PROP, Integer.parseInt(tmp)); prop = tmp; } } // offline asr { intent.putExtra(Constant.EXTRA_OFFLINE_ASR_BASE_FILE_PATH, "/sdcard/easr/s_1"); intent.putExtra(Constant.EXTRA_LICENSE_FILE_PATH, "/sdcard/easr/license-tmp-20150530.txt"); if (null != prop) { int propInt = Integer.parseInt(prop); if (propInt == 10060) { intent.putExtra(Constant.EXTRA_OFFLINE_LM_RES_FILE_PATH, "/sdcard/easr/s_2_Navi"); } else if (propInt == 20000) { intent.putExtra(Constant.EXTRA_OFFLINE_LM_RES_FILE_PATH, "/sdcard/easr/s_2_InputMethod"); } } intent.putExtra(Constant.EXTRA_OFFLINE_SLOT_DATA, buildTestSlotData()); } } private String buildTestSlotData() { JSONObject slotData = new JSONObject(); JSONArray name = new JSONArray().put("?").put(""); JSONArray song = new JSONArray().put("").put("?"); JSONArray artist = new JSONArray().put("?").put("?"); JSONArray app = new JSONArray().put("").put(""); JSONArray usercommand = new JSONArray().put("?").put(""); try { slotData.put(Constant.EXTRA_OFFLINE_SLOT_NAME, name); slotData.put(Constant.EXTRA_OFFLINE_SLOT_SONG, song); slotData.put(Constant.EXTRA_OFFLINE_SLOT_ARTIST, artist); slotData.put(Constant.EXTRA_OFFLINE_SLOT_APP, app); slotData.put(Constant.EXTRA_OFFLINE_SLOT_USERCOMMAND, usercommand); } catch (JSONException e) { } return slotData.toString(); } // private void start() { Intent intent = new Intent(); bindParams(intent); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); { String args = sp.getString("args", ""); if (null != args) { intent.putExtra("args", args); } } boolean api = sp.getBoolean("api", false); if (api) { speechEndTime = -1; speechRecognizer.startListening(intent); } else { intent.setAction("com.baidu.action.RECOGNIZE_SPEECH"); startActivityForResult(intent, REQUEST_UI); } } //? private void stop() { speechRecognizer.stopListening(); } //? private void cancel() { speechRecognizer.cancel(); } // @Override public void onReadyForSpeech(Bundle params) { // status = STATUS_Ready; } @Override public void onBeginningOfSpeech() { //?? status = STATUS_Speaking; } @Override public void onRmsChanged(float rmsdB) { } @Override public void onBufferReceived(byte[] buffer) { //?? } @Override public void onEndOfSpeech() { //??? status = STATUS_Recognition; } @Override public void onError(int error) { //? status = STATUS_None; StringBuilder sb = new StringBuilder(); switch (error) { case SpeechRecognizer.ERROR_AUDIO: sb.append(""); break; case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: sb.append(""); break; case SpeechRecognizer.ERROR_CLIENT: sb.append(""); break; case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: sb.append("???"); break; case SpeechRecognizer.ERROR_NETWORK: sb.append(""); break; case SpeechRecognizer.ERROR_NO_MATCH: sb.append("?"); break; case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: sb.append(""); break; case SpeechRecognizer.ERROR_SERVER: sb.append("?"); break; case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: sb.append(""); break; } sb.append(":" + error); Toast.makeText(AddPayActivity.this, "" + sb.toString(), Toast.LENGTH_SHORT).show(); } @Override public void onResults(Bundle results) { //? status = STATUS_None; ArrayList<String> nbest = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); //print("?" + Arrays.toString(nbest.toArray(new String[nbest.size()]))); String json_res = results.getString("origin_result"); try { Toast.makeText(AddPayActivity.this, "origin_result=\n" + new JSONObject(json_res).toString(4), Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(AddPayActivity.this, "origin_result=[warning: bad json]\n" + json_res, Toast.LENGTH_SHORT).show(); } } @Override public void onPartialResults(Bundle partialResults) { //? ArrayList<String> nbest = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (nbest.size() > 0) { Toast.makeText(AddPayActivity.this, "~" + Arrays.toString(nbest.toArray(new String[0])), Toast.LENGTH_SHORT) .show(); //Toast.makeText().setText(nbest.get(0)); } } @Override public void onEvent(int eventType, Bundle params) { //? switch (eventType) { case EVENT_ERROR: String reason = params.get("reason") + ""; Log.d("Add", "EVENT_ERROR, " + reason); break; case VoiceRecognitionService.EVENT_ENGINE_SWITCH: int type = params.getInt("engine_type"); Log.d("Add", "*?" + (type == 0 ? "" : "")); break; } } }