Skip to content

Commit

Permalink
Optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuguohua committed Dec 12, 2024
1 parent 8c59afe commit 0fec767
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ public static void removeProductDetails(int tag, int productDetailsId) {
public static void removePurchase(int tag, int purchaseID) {
GoogleBilling billing = googleBillings.get(tag);
if (billing != null) {
billing.removeProductDetails(purchaseID);
billing.removePurchase(purchaseID);
}
}

public static ProductDetails getPurchase(int tag, int purchaseId) {
public static Purchase getPurchase(int tag, int purchaseId) {
GoogleBilling billing = googleBillings.get(tag);
if (billing != null) {
return billing.getProductDetails(purchaseId);
return billing.getPurchase(purchaseId);
}
return null;
}
Expand Down
8 changes: 5 additions & 3 deletions native/vendor/google/billing/GoogleBilling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ bool BillingClient::isReady() const {
void BillingClient::queryProductDetailsAsync(QueryProductDetailsParams* params, se::Object* listener) {
int listenerId = addListener(listener);

std::vector<std::string> productIDs;
std::vector<std::string> productIds;
std::vector<std::string> productTypes;
productIds.reserve(16);
productTypes.reserve(16);
for (auto product : params->_productList) {
productIDs.push_back(product->_productID);
productIds.push_back(product->_productId);
productTypes.push_back(product->_productType);
}
GoogleBillingHelper::queryProductDetailsAsync(_tag, listenerId, productIDs, productTypes);
GoogleBillingHelper::queryProductDetailsAsync(_tag, listenerId, productIds, productTypes);
}

void BillingClient::launchBillingFlow(BillingFlowParams* params) {
Expand Down
68 changes: 31 additions & 37 deletions native/vendor/google/billing/GoogleBillingHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
#include "cocos/bindings/manual/jsb_conversions.h"
#include "cocos/bindings/manual/jsb_global_init.h"
#include "vendor/google/billing/GoogleBilling.h"
#include "vendor/google/billing/JniBilling.h"
#include "vendor/google/billing/GoogleBillingManager.h"
#include "vendor/google/billing/result-values/Purchase.h"
#include "vendor/google/billing/JniBilling.h"
#include "vendor/google/billing/build-params/AcknowledgePurchaseParams.h"
#include "vendor/google/billing/result-values/ProductDetails.h"
#include "vendor/google/billing/build-params/ConsumeParams.h"
#include "vendor/google/billing/result-values/ProductDetails.h"
#include "vendor/google/billing/result-values/Purchase.h"
namespace {

#ifndef JCLS_BILLING
Expand All @@ -48,7 +48,7 @@ namespace {
}; // namespace

namespace cc {

namespace {
void invokeJsCallback(se::Object* obj, const char* jsFunctionName, const se::ValueArray& args) {
if (!obj) {
return;
Expand All @@ -63,7 +63,7 @@ void invokeJsCallback(se::Object* obj, const char* jsFunctionName, const se::Val
}

template <typename... Args>
void callJSfunc(se::Object* obj, const char* jsFunctionName, Args&&... inargs) { // NOLINT(readability-identifier-naming)
void callJSfunc(se::Object* obj, const char* jsFunctionName, Args&&... inargs) { // NOLINT(readability-identifier-naming)
if (!se::ScriptEngine::getInstance()->isValid()) {
return;
}
Expand All @@ -90,6 +90,7 @@ template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, Billing
template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, AlternativeBillingOnlyReportingDetails*&&);
template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, ExternalOfferReportingDetails*&&);
template void callJSfunc(se::Object* obj, const char*, InAppMessageResult*&&);
} // namespace

int GoogleBillingHelper::createGoogleBilling(BillingClient::Builder* builder) {
int tag = JniHelper::callStaticIntMethod(JCLS_BILLING, "newTag");
Expand All @@ -103,7 +104,7 @@ int GoogleBillingHelper::createGoogleBilling(BillingClient::Builder* builder) {
}

void GoogleBillingHelper::removeGoogleBilling(int tag) {
JniHelper::callStaticIntMethod(JCLS_BILLING, "removeGoogleBilling", tag);
JniHelper::callStaticIntMethod(JCLS_BILLING, "removeGoogleBilling", tag);
}

void GoogleBillingHelper::removeProductDetails(int tag, int productDetailsID) {
Expand All @@ -114,7 +115,6 @@ void GoogleBillingHelper::removePurchase(int tag, int purchaseID) {
JniHelper::callStaticVoidMethod(JCLS_BILLING, "removePurchase", tag, purchaseID);
}


void GoogleBillingHelper::startConnection(int tag, int callbackId) {
JniHelper::callStaticVoidMethod(JCLS_BILLING, "startConnection", tag, callbackId);
}
Expand All @@ -135,7 +135,7 @@ void GoogleBillingHelper::queryProductDetailsAsync(int tag, int callbackId, cons
JniHelper::callStaticVoidMethod(JCLS_BILLING, "queryProductDetailsAsync", tag, callbackId, productIds, productTypes);
}

void GoogleBillingHelper::queryPurchasesAsync(int tag, int callbackId, const std::string& productType) {
void GoogleBillingHelper::queryPurchasesAsync(int tag, int callbackId, const std::string& productType) {
JniHelper::callStaticVoidMethod(JCLS_BILLING, "queryPurchasesAsync", tag, callbackId, productType);
}

Expand Down Expand Up @@ -173,12 +173,7 @@ BillingResult* GoogleBillingHelper::showInAppMessages(int tag, int callbackId, c
if (cc::JniHelper::getStaticMethodInfo(t, JCLS_BILLING, "showInAppMessages", "(II[I)Lcom/android/billingclient/api/BillingResult;")) {
const int size = inAppMessageCategoryId.size();
jintArray result = env->NewIntArray(size);
jint* buf = new jint[size];
for (int i = 0; i < size; ++i) {
buf[i] = inAppMessageCategoryId[i];
}
env->SetIntArrayRegion(result, 0, size, buf);
delete[] buf;
env->SetIntArrayRegion(result, 0, inAppMessageCategoryId.size(), inAppMessageCategoryId.data());
jobject obj = t.env->CallStaticObjectMethod(t.classID, t.methodID, tag, callbackId, result);
return cc::JniBilling::toBillingResult(env, obj);
}
Expand Down Expand Up @@ -224,23 +219,23 @@ void GoogleBillingHelper::onBillingServiceDisconnected(JNIEnv* env, jclass clazz
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
if (it != billingClient->_listeners.end()) {
cc::callJSfunc(it->second.get(), "onBillingServiceDisconnected");
billingClient->_listeners.erase(it);
}
}
}

void GoogleBillingHelper::onProductDetailsResponse(JNIEnv* env, jclass clazz,
jint tag,
jint callbackId,
jobject billingResultObj,
jobject productDetailsListObj,
jint startID) {
jint tag,
jint callbackId,
jobject billingResultObj,
jobject productDetailsListObj,
jint startID) {
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
if (it != billingClient->_listeners.end()) {
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
std::vector<cc::ProductDetails*> productDetailsList = cc::JniBilling::toProductDetailList(env, productDetailsListObj, tag, startID);
cc::callJSfunc(it->second.get(), "onProductDetailsResponse", billingResult, productDetailsList);
Expand All @@ -250,10 +245,10 @@ void GoogleBillingHelper::onProductDetailsResponse(JNIEnv* env, jclass clazz,
}

void GoogleBillingHelper::onPurchasesUpdated(JNIEnv* env, jclass clazz,
jint tag,
jobject billingResultObj,
jobject purchasesListObj,
jint startID) {
jint tag,
jobject billingResultObj,
jobject purchasesListObj,
jint startID) {
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient && billingClient->_purchasesUpdatedListener) {
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
Expand All @@ -265,21 +260,21 @@ void GoogleBillingHelper::onPurchasesUpdated(JNIEnv* env, jclass clazz,
}
}

void GoogleBillingHelper::userSelectedAlternativeBilling(JNIEnv *env, jclass clazz, jint tag, jobject userChoiceDetailsObj) {
void GoogleBillingHelper::userSelectedAlternativeBilling(JNIEnv* env, jclass clazz, jint tag, jobject userChoiceDetailsObj) {
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient && billingClient->_userChoiceBillingListener) {
auto* userChoiceDetails = cc::JniBilling::toUserChoiceDetails(env, userChoiceDetailsObj);
cc::callJSfunc(billingClient->_userChoiceBillingListener.get(), "userSelectedAlternativeBilling", userChoiceDetails);
}
}
}

void GoogleBillingHelper::onConsumeResponse(JNIEnv* env, jclass clazz, jint tag, jint callbackId, jobject billingResultObj, jstring purchaseToken) {
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
auto *billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
cc::callJSfunc(it->second.get(), "onConsumeResponse", billingResult, cc::StringUtils::getStringUTFCharsJNI(env,static_cast<jstring>(purchaseToken)));
if (it != billingClient->_listeners.end()) {
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
cc::callJSfunc(it->second.get(), "onConsumeResponse", billingResult, cc::StringUtils::getStringUTFCharsJNI(env, static_cast<jstring>(purchaseToken)));
billingClient->_listeners.erase(it);
}
}
Expand All @@ -293,7 +288,7 @@ void GoogleBillingHelper::onQueryPurchasesResponse(JNIEnv* env, jclass clazz, ji
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
if (it != billingClient->_listeners.end()) {
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
std::vector<cc::Purchase*> purchasesList = cc::JniBilling::toPurchaseList(env, purchasesListObj, tag, startID);
cc::callJSfunc(it->second.get(), "onQueryPurchasesResponse", billingResult, purchasesList);
Expand All @@ -306,7 +301,7 @@ void GoogleBillingHelper::onBillingConfigResponse(JNIEnv* env, jclass clazz, jin
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
if (it != billingClient->_listeners.end()) {
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
cc::BillingConfig* billingConfig = nullptr;
if (billingConfigObj) {
Expand All @@ -322,7 +317,7 @@ void GoogleBillingHelper::onAlternativeBillingOnlyTokenResponse(JNIEnv* env, jcl
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
if (it != billingClient->_listeners.end()) {
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
cc::AlternativeBillingOnlyReportingDetails* toAlternativeBillingOnlyReporting = nullptr;
if (alternativeBillingOnlyReportingDetailsObj) {
Expand All @@ -338,7 +333,7 @@ void GoogleBillingHelper::onExternalOfferReportingDetailsResponse(JNIEnv* env, j
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
if (it != billingClient->_listeners.end()) {
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
cc::ExternalOfferReportingDetails* externalOfferReportingDetails = nullptr;
if (externalOfferReportingDetailsObj) {
Expand Down Expand Up @@ -370,7 +365,7 @@ void GoogleBillingHelper::onInAppMessageResponse(JNIEnv* env, jclass clazz, jint
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
if (it != billingClient->_listeners.end()) {
auto* inAppMessageResult = cc::JniBilling::toInAppMessageResult(env, inAppMessageResultObj);
cc::callJSfunc(it->second.get(), "onInAppMessageResponse", inAppMessageResult);
billingClient->_listeners.erase(it);
Expand All @@ -382,13 +377,12 @@ void GoogleBillingHelper::responseOnlyWithBillingResult(const std::string& funct
auto* billingClient = GoogleBillingManager::getInstance()->getBillingClient(tag);
if (billingClient) {
auto it = billingClient->_listeners.find(callbackId);
if(it != billingClient->_listeners.end()) {
if (it != billingClient->_listeners.end()) {
auto* billingResult = cc::JniBilling::toBillingResult(env, billingResultObj);
cc::callJSfunc(it->second.get(), functionName.c_str(), billingResult);
billingClient->_listeners.erase(it);
}
}
}


} // namespace cc
10 changes: 5 additions & 5 deletions native/vendor/google/billing/JniBilling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ BillingResult* JniBilling::callFunctionAndReturnBillingResult(const std::string&
return nullptr;
}

jobject JniBilling::newSubscriptionUpdateParamsObject(BillingFlowParams::SubscriptionUpdateParams* params) {
jobject JniBilling::newSubscriptionUpdateParamsObject(const BillingFlowParams::SubscriptionUpdateParams* params) {
if(!params) {
return nullptr;
}
Expand All @@ -383,7 +383,7 @@ jobject JniBilling::newSubscriptionUpdateParamsObject(BillingFlowParams::Subscri
return env->CallObjectMethod(builder, buildMethodIdMethodId);
}

jobject JniBilling::newProductDetailsParamsObject(int tag, BillingFlowParams::ProductDetailsParams* params) {
jobject JniBilling::newProductDetailsParamsObject(int tag, const BillingFlowParams::ProductDetailsParams* params) {
auto* env = JniHelper::getEnv();
cc::JniMethodInfo t;
cc::JniHelper::getStaticMethodInfo(t, "com/android/billingclient/api/BillingFlowParams$ProductDetailsParams", "newBuilder", "()Lcom/android/billingclient/api/BillingFlowParams$ProductDetailsParams$Builder;");
Expand Down Expand Up @@ -419,7 +419,7 @@ jobject JniBilling::newProductDetailsParamsListObject(int tag, std::vector<Billi
return list;
}

jobject JniBilling::newBillingFlowParamsObject(int tag, BillingFlowParams* params) {
jobject JniBilling::newBillingFlowParamsObject(int tag, const BillingFlowParams* params) {
JNIEnv *env = cc::JniHelper::getEnv();
cc::JniMethodInfo t;
cc::JniHelper::getStaticMethodInfo(t, "com/android/billingclient/api/BillingFlowParams", "newBuilder", "()Lcom/android/billingclient/api/BillingFlowParams$Builder;");
Expand Down Expand Up @@ -449,7 +449,7 @@ jobject JniBilling::newBillingFlowParamsObject(int tag, BillingFlowParams* param
return env->CallObjectMethod(builder, buildMethodId);
}

jobject JniBilling::newPendingPurchasesParamsObject(PendingPurchasesParams* params) {
jobject JniBilling::newPendingPurchasesParamsObject(const PendingPurchasesParams* params) {
auto* env = JniHelper::getEnv();
cc::JniMethodInfo t;
cc::JniHelper::getStaticMethodInfo(t, "com/android/billingclient/api/PendingPurchasesParams", "newBuilder", "()Lcom/android/billingclient/api/PendingPurchasesParams$Builder;");
Expand Down Expand Up @@ -487,7 +487,7 @@ jobject JniBilling::newUserChoiceBillingListenerObj(int tag) {
return newCustomListenerObject(tag, "BillingClientUserChoiceBillingListener");
}

jobject JniBilling::newBillingClientBuilderObject(int tag, BillingClient::Builder* params) {
jobject JniBilling::newBillingClientBuilderObject(int tag, const BillingClient::Builder* params) {
auto* env = JniHelper::getEnv();
cc::JniMethodInfo t;
auto *javaGameActivity = cc::JniHelper::getActivity();
Expand Down
10 changes: 5 additions & 5 deletions native/vendor/google/billing/JniBilling.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ class CC_DLL JniBilling {
static UserChoiceDetails* toUserChoiceDetails(JNIEnv* env, jobject obj);


static jobject newBillingFlowParamsObject(int tag, BillingFlowParams* params);
static jobject newBillingClientBuilderObject(int tag, BillingClient::Builder* params);
static jobject newBillingFlowParamsObject(int tag, const BillingFlowParams* params);
static jobject newBillingClientBuilderObject(int tag, const BillingClient::Builder* params);
private:
static jobject newSubscriptionUpdateParamsObject(BillingFlowParams::SubscriptionUpdateParams* params);
static jobject newProductDetailsParamsObject(int tag, BillingFlowParams::ProductDetailsParams* params);
static jobject newSubscriptionUpdateParamsObject(const BillingFlowParams::SubscriptionUpdateParams* params);
static jobject newProductDetailsParamsObject(int tag, const BillingFlowParams::ProductDetailsParams* params);
static jobject newProductDetailsParamsListObject(int tag, std::vector<BillingFlowParams::ProductDetailsParams*> listParams);

static jobject newPurchaseUpdateListenerObject(int tag);
static jobject newUserChoiceBillingListenerObj(int tag);
static jobject newPendingPurchasesParamsObject(PendingPurchasesParams* params);
static jobject newPendingPurchasesParamsObject(const PendingPurchasesParams* params);
static jobject newCustomListenerObject(int tag, const std::string& functionName);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
#include "base/RefCounted.h"

namespace cc {
class AcknowledgePurchaseParams {
class AcknowledgePurchaseParams : public cc::RefCounted {
public:
class Builder {
class Builder : public cc::RefCounted {
public:
Builder& setPurchaseToken(const std::string& purchaseToken) {
_purchaseToken = purchaseToken;
return *this;
}

AcknowledgePurchaseParams* build() {
return new AcknowledgePurchaseParams(_purchaseToken);
return new AcknowledgePurchaseParams(std::move(_purchaseToken));
}

private:
Expand All @@ -56,8 +56,7 @@ class AcknowledgePurchaseParams {
}

private:
AcknowledgePurchaseParams(const std::string& purchaseToken) {
_purchaseToken = purchaseToken;
AcknowledgePurchaseParams(const std::string&& purchaseToken): _purchaseToken(purchaseToken) {
}

std::string _purchaseToken;
Expand Down
Loading

0 comments on commit 0fec767

Please sign in to comment.