Java tutorial
/**************************************************************************** Copyright (c) 2008-2010 Ricardo Quesada Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2011 Zynga Inc. Copyright (c) 2013-2014 Chukong Technologies Inc. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ package com.junyou.jyxxx.vivo.cpp; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Random; import java.util.UUID; import mm.purchasesdk.OnPurchaseListener; import mm.purchasesdk.Purchase; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.cocos2dx.lib.Cocos2dxActivity; import org.json.JSONObject; import com.umeng.analytics.mobclick.game.MobClickCppHelper; import com.vivo.sdkplugin.aidl.VivoUnionManager; import com.bbk.payment.payment.OnVivoSinglePayResultListener; import com.junyou.jyxxx.vivo.R; import com.junyou.jyxxx.vivo.cpp.NetworkRequestAgent; import android.R.bool; import android.app.ProgressDialog; import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.Toast; public class AppActivity extends Cocos2dxActivity { public static final String TAG = "TBU_DEBUG"; private int m_tradeId; private String m_orderNum; //?? private String m_vivo_order;//vivo?? private int m_payMethod = 1; //? 1,2vivo? public static AppActivity instance; private VivoUnionManager mVivoUnionManager; Handler mHandler = null; Handler mHandler2 = null; public static native void eventSuccess(int tradeId, int[] propIds, int[] propNums); public static native void eventFail(final String tradeId); public static native void eventClose(final String tradeId); //------MMSDK-------- private IAPListener mListener; private ProgressDialog mProgressDialog; private String mPaycode; private int mProductNum = 1; public static Purchase mPurchase; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* "000023" {} //360? "000002" {} // "000005" {} // "000007" {} //91 "000009" {} // "000116" {} //? "000225" {} //pp(UC?) "000800" {} //? "150001" {} //? "150002" {} // "150003" {} //? "150004" {} //? "150005" {} //? "100061" {} //? "000066" {} //? "000054" {} //??? "000020" {} //OPPO? ???(OPPO) "000084" {} //?? "000008" {} //??? "000368" {} //voio??,(vivo) "000016" {} //? */ MobClickCppHelper.init(AppActivity.this, Constants.APP_KEY_UMENG, "000368"); //todo id // Log.i(TAG, "phoneInfo: " + ComFunction.getDeviceInfo(this)); instance = this; mVivoUnionManager = new VivoUnionManager(AppActivity.this); mVivoUnionManager.initVivoSinglePayment(AppActivity.this, mOnVivoPayResultListener); mVivoUnionManager.singlePaymentInit(AppActivity.this); //vivo ??? mVivoUnionManager.singlePaymentInit(AppActivity.this); //------------------MM mProgressDialog = new ProgressDialog(AppActivity.this); mProgressDialog.setIndeterminate(true); mProgressDialog.setMessage("?..."); IAPHandler iapHandler = new IAPHandler(this); mListener = new IAPListener(this, iapHandler); mPaycode = Constants.MM_LEASE_PAYCODE; mProductNum = 1; mPurchase = Purchase.getInstance(); try { if (null != mPurchase) { mPurchase.setAppInfo(Constants.MM_APPID, Constants.MM_APPKEY); mPurchase.setTimeout(10000, 10000); // (?)???10s } } catch (Exception e1) { e1.printStackTrace(); } try { if (null != mPurchase) { mPurchase.init(AppActivity.this, mListener); } } catch (Exception e) { e.printStackTrace(); return; } showProgressDialog(); //------------------MM mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); // if (ComFunction.isWechatAvilible(AppActivity.this)) // Toast.makeText(AppActivity.this, getResources().getString(R.string.no_wechat), Toast.LENGTH_SHORT).show(); if (ComFunction.networkInfo(AppActivity.this)) { if (msg.what == 0) { try { //vivo Pay // NameValuePair[] nameValuePairs = doPaymentInit(1); // new InitialPayTask().execute(nameValuePairs); //MM Pay MMOrder(AppActivity.this, mListener); } catch (Exception e) { e.printStackTrace(); } } } else { Toast.makeText(AppActivity.this, getResources().getString(R.string.no_net), Toast.LENGTH_SHORT) .show(); } } }; mHandler2 = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 0) { Toast.makeText(AppActivity.this, getResources().getString(R.string.no_wechat), Toast.LENGTH_SHORT).show(); } else if (msg.what == 1) { Toast.makeText(AppActivity.this, getResources().getString(R.string.no_net), Toast.LENGTH_SHORT) .show(); } else if (msg.what == 2) { Toast.makeText(AppActivity.this, getResources().getString(R.string.show_tonextLevelTip), Toast.LENGTH_SHORT).show(); } } }; } OnVivoSinglePayResultListener mOnVivoPayResultListener = new OnVivoSinglePayResultListener() { @Override public void payResult(String transNo, boolean pay_result, String result_code, String pay_msg) { // UtilTool.showDialog(AppActivity.this, "", "?=" + transNo // + ", =" + (pay_result ? "?" : "") + ",??=" // + result_code + "???=" + pay_msg); // Log.i(TAG, "? "+transNo + "," + pay_msg + "," + result_code + ","+ pay_result); //todo ??? ? m_vivo_order = transNo; if (!transNo.isEmpty() && pay_result && "9000".equals(result_code) && !pay_msg.isEmpty()) { Toast.makeText(AppActivity.this, "?!", Toast.LENGTH_SHORT).show(); //todo ? eventSuccess(m_tradeId, ComFunction.getPropId(m_tradeId), ComFunction.getPropNum(m_tradeId)); } else { eventFail(Integer.toString(m_tradeId)); Toast.makeText(AppActivity.this, "!", Toast.LENGTH_SHORT).show(); } } }; //? private boolean queryOrder() { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); NameValuePair[] nameValuePairs = new NameValuePair[7]; Map<String, String> param_map = new HashMap<String, String>(); param_map.put(Constant.PARAM_NOTIFYURL, "http://www.zjhzjykj.com"); DecimalFormat format_d = new DecimalFormat("0.00"); String orderAmount = format_d.format(ComFunction.getMoneyFormTradeId(m_tradeId)); param_map.put(Constant.PARAM_ORDER_AMOUNT, orderAmount); param_map.put(Constant.PARAM_ORDER_DESC, "weixinzhifu"); param_map.put(Constant.PARAM_ORDER_TITLE, getResources().getString(R.string.wx_pay)); param_map.put(Constant.PARAM_ORDER_TIME, format.format(new Date())); param_map.put(Constant.PARAM_STOREID, UtilTool.getMetaData(this, Constant.STORE_ID)); param_map.put(Constant.PARAM_APP_ID, UtilTool.getMetaData(this, Constant.APP_ID)); param_map.put(Constant.PARAM_STOREORDER, m_orderNum); param_map.put(Constant.PARAM_VERSION, "1.0"); String signature = generateSignature(param_map); param_map.put(Constant.PARAM_SIGNATURE, signature); // drop param_map.put(Constant.PARAM_SIGNMETHOD, "MD5"); // drop /* boolean verify =VivoSignUtils.verifySignature(param_map,UtilTool.getMetaData(this, Constant.SIGN_KEY)); if (verify) { Log.i(TAG, "????...."); }else{ Log.i(TAG, "???...."); }*/ int i = 0; nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_ORDER_AMOUNT, orderAmount); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_STOREID, UtilTool.getMetaData(this, Constant.STORE_ID)); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_STOREORDER, m_orderNum); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_VERSION, "1.0"); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_SIGNATURE, signature); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_SIGNMETHOD, "MD5"); nameValuePairs[i++] = new BasicNameValuePair(Constant.RESPONE_VIVO_ORDER, m_vivo_order); // for (int j = 0; j < nameValuePairs.length; j++) { // Log.i(TAG, "?: " + j + "= " + nameValuePairs[j]); // } NetworkRequestAgent networkRequestAgent = new NetworkRequestAgent(); String resultInfo = networkRequestAgent.initialQuery(nameValuePairs); // Log.i(TAG, ": " + resultInfo); return false; } //todo ? // static { // MobClickCppHelper.loadLibrary(); // } private NameValuePair[] doPaymentInit(int arg2) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); String orderNum = UUID.randomUUID().toString().replaceAll("-", ""); m_orderNum = orderNum; // Log.i(TAG, "orderNum: " + orderNum + " ..............."); NameValuePair[] nameValuePairs = new NameValuePair[11]; Map<String, String> param_map = new HashMap<String, String>(); param_map.put(Constant.PARAM_NOTIFYURL, "http://www.zjhzjykj.com"); DecimalFormat format_d = new DecimalFormat("0.00"); // Log.i(TAG, "" + format_d.format(ComFunction.getMoneyFormTradeId(m_tradeId))); String orderAmount = format_d.format(ComFunction.getMoneyFormTradeId(m_tradeId)); param_map.put(Constant.PARAM_ORDER_AMOUNT, orderAmount); param_map.put(Constant.PARAM_ORDER_DESC, "payforjunyouxxx"); param_map.put(Constant.PARAM_ORDER_TITLE, getResources().getString(R.string.jyxxx_pay)); param_map.put(Constant.PARAM_ORDER_TIME, format.format(new Date())); param_map.put(Constant.PARAM_STOREID, UtilTool.getMetaData(this, Constant.STORE_ID)); param_map.put(Constant.PARAM_APP_ID, UtilTool.getMetaData(this, Constant.APP_ID)); param_map.put(Constant.PARAM_STOREORDER, orderNum); param_map.put(Constant.PARAM_VERSION, "1.0"); String signature = generateSignature(param_map); param_map.put(Constant.PARAM_SIGNATURE, signature); // drop param_map.put(Constant.PARAM_SIGNMETHOD, "MD5"); // drop /* boolean verify =VivoSignUtils.verifySignature(param_map,UtilTool.getMetaData(this, Constant.SIGN_KEY)); if (verify) { Log.i(TAG, "???...."); }else{ Log.i(TAG, "??...."); }*/ int i = 0; nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_NOTIFYURL, "http://www.zjhzjykj.com"); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_ORDER_AMOUNT, orderAmount); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_ORDER_DESC, "payforjunyouxxx"); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_ORDER_TITLE, getResources().getString(R.string.jyxxx_pay)); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_ORDER_TIME, format.format(new Date())); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_STOREID, UtilTool.getMetaData(this, Constant.STORE_ID)); // Log.i(TAG, "STORE_ID: " + UtilTool.getMetaData(this, Constant.STORE_ID) + " ................"); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_APP_ID, UtilTool.getMetaData(this, Constant.APP_ID)); // Log.i(TAG, "APP_ID: " + UtilTool.getMetaData(this, Constant.APP_ID) + " ..............."); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_STOREORDER, orderNum); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_VERSION, "1.0"); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_SIGNATURE, signature); nameValuePairs[i++] = new BasicNameValuePair(Constant.PARAM_SIGNMETHOD, "MD5"); // for (int j = 0; j < nameValuePairs.length; j++) { // Log.i(TAG, "nameValuePairs:" + j + "= " + nameValuePairs[j]); // } return nameValuePairs; } public void showToNextToast() { new Thread() { @Override public void run() { super.run(); if (mHandler2 != null) { mHandler2.sendEmptyMessage(2); } } }.start(); } public static Object getInstance() { return instance; } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); // ???,? Session MobClickCppHelper.onResume(this); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); // //???, ? Session MobClickCppHelper.onPause(this); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mVivoUnionManager.cancelVivoSinglePayment(mOnVivoPayResultListener); //??? mVivoUnionManager.singlePaymentExit(AppActivity.this); } public boolean isWechatOrNetAvilable() { if (ComFunction.networkInfo(AppActivity.this)) { if (ComFunction.isWechatAvilible(AppActivity.this)) { return true; } else { new Thread() { @Override public void run() { super.run(); if (mHandler2 != null) { mHandler2.sendEmptyMessage(0); } } }.start(); return false; } } else { new Thread() { @Override public void run() { super.run(); if (mHandler2 != null) { mHandler2.sendEmptyMessage(1); } } }.start(); return false; } } @Override public void onBackPressed() { //TODO: quit game Log.i("TBU_DEBUG", "do quit game!"); } //c++ call public void WXpay(final int tradeID) { m_tradeId = tradeID; m_payMethod = 1; Log.i(TAG, "WXpay success!tradeid==" + m_tradeId); // Log.i(TAG, "id:" + tradeID+ " proId:" + ComFunction.getPropId(trade_id) + " propNum:"+ ComFunction.getPropNum(trade_id)); // for (int i = 0; i < ComFunction.getPropId(trade_id).length; i++) { // Log.i(TAG, "proId:"+ ComFunction.getPropId(trade_id)[i]); // } // for (int i = 0; i < ComFunction.getPropNum(trade_id).length; i++) { // Log.i(TAG, "proNum:"+ ComFunction.getPropNum(trade_id)[i]); // } new Thread() { @Override public void run() { super.run(); if (mHandler != null) { mHandler.sendEmptyMessage(0); } } }.start(); } //vivo public void vivoStagePay(final int tradeID) { m_tradeId = tradeID; m_payMethod = 2; Log.i(TAG, "vivoStagePay!tradeid==" + m_tradeId); new Thread() { @Override public void run() { super.run(); if (mHandler != null) { mHandler.sendEmptyMessage(0); } } }.start(); } private void showPayMessage(int msgId) { Toast.makeText(AppActivity.this, msgId, Toast.LENGTH_SHORT).show(); } private String generateSignature(Map<String, String> param_map) { String result = null; result = VivoSignUtils.getVivoSign(param_map, UtilTool.getMetaData(this, Constant.SIGN_KEY)); return result; } //----------------------------------GetPrepayIdTask------------------------------------// private class InitialPayTask extends AsyncTask<NameValuePair, Integer, String> { private ProgressDialog mProgress = null; @Override protected void onPreExecute() { mProgress = UtilTool.showProgress(AppActivity.this, null, AppActivity.this.getResources().getString(R.string.ini_install), false, true); mProgress.show(); } @Override protected String doInBackground(NameValuePair... nameValuePairs) { NetworkRequestAgent networkRequestAgent = new NetworkRequestAgent(); String resultInfo = networkRequestAgent.initialPayment(nameValuePairs); return resultInfo; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub if (null != mProgress) { mProgress.dismiss(); mProgress = null; } super.onPostExecute(result); if (!result.isEmpty()) { try { // Log.i(TAG, "=" + result); // Toast.makeText(AppActivity.this, getResources().getString(R.string.send_payorder), Toast.LENGTH_SHORT).show(); boolean isSignOk = UtilTool.checkSignatrue(AppActivity.this, result); if (isSignOk) { // Log.i(TAG, "???...."); JSONObject jsonVo = new JSONObject(result); String respCode = jsonVo.getString(Constant.RESPONE_RESP_CODE); if ("200".equals(respCode)) { String transNo = jsonVo.getString(Constant.RESPONE_VIVO_ORDER); String signature = jsonVo.getString(Constant.RESPONE_VIVO_SIGNATURE); Bundle localBundle = new Bundle(); String packageName = AppActivity.this.getPackageName(); DecimalFormat format_d = new DecimalFormat("0.00"); String orderAmount = format_d.format(ComFunction.getMoneyFormTradeId(m_tradeId)); double dorderAmount = Double.valueOf(orderAmount); localBundle.putString(Constants.PAY_PARAM_TRANSNO, transNo); localBundle.putString(Constants.PAY_PARAM_SIGNATURE, signature); localBundle.putString(Constants.PAY_PARAM_PKG, packageName); localBundle.putBoolean(Constants.PAY_PARAM_USE_WEIXIN_PAY, false); localBundle.putString(Constants.PAY_PARAM_USEMODE, "00"); localBundle.putString(Constants.PAY_PARAM_PRODUCT_DEC, "payforjunyouxxx"); localBundle.putString(Constants.PAY_PARAM_PRODUCT_NAME, getResources().getString(R.string.jyxxx_pay)); localBundle.putDouble(Constants.PAY_PARAM_PRICE, dorderAmount); localBundle.putString(Constants.PAY_PARAM_USERID, "test"); if (m_payMethod == 1) { //weichat pay localBundle.putInt("mPaymentType", 1); //? 1 2? mVivoUnionManager.singlePaymentDirectly(AppActivity.this, localBundle); //? } else if (m_payMethod == 2) { //vivo pay mVivoUnionManager.singlePayment(AppActivity.this, localBundle); //vivo } } else { showPayMessage(R.string.ini_install_fail); } } else { showPayMessage(R.string.ini_sign_fail); } } catch (Exception e) { e.printStackTrace(); } } else { // Log.i(TAG, "111"); //Toast.makeText(AppActivity.this, getResources().getString(R.string.pay_faild), Toast.LENGTH_SHORT).show(); showPayMessage(R.string.ini_install_fail); } } @Override protected void onCancelled() { super.onCancelled(); } } //------------------------------------MM------------------------------------// private void showProgressDialog() { if (mProgressDialog == null) { mProgressDialog = new ProgressDialog(AppActivity.this); mProgressDialog.setIndeterminate(true); mProgressDialog.setMessage("?....."); } if (!mProgressDialog.isShowing()) { mProgressDialog.show(); } } public void dismissProgressDialog() { if (mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } } public void MMOrder(Context context, OnPurchaseListener listener) { try { // purchase.order(context, mPaycode, mProductNum, listener); Log.i(TAG, "call MMOrder"); if (null != mPurchase) { /** *1. payCode?2.3 *2. orderCount,???????1????10 *3. nextCycle, ??? *4. Listener??OnPurchaseListener??? *5. data??????64 */ mPurchase.order(context, mPaycode, mProductNum, "null", false, listener); } } catch (Exception e) { e.printStackTrace(); } } }