Skip to content

Commit

Permalink
Optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuguohua committed Dec 11, 2024
1 parent 353e905 commit 891d7b0
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 64 deletions.
33 changes: 18 additions & 15 deletions @types/jsb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ declare namespace jsb {
getId(): string;
getOfferToken(): string;
getType(): string;
toStr(): string;
toString(): string;
equals(product: UserChoiceDetailsProduct): boolean;
}
export class UserChoiceDetails {
Expand Down Expand Up @@ -502,6 +502,7 @@ declare namespace jsb {
*/
export class ProductDetails {
static RecurrenceMode: typeof jsb.RecurrenceMode;

equals(other: ProductDetails): boolean;
/**
* @en Hash code
Expand Down Expand Up @@ -688,7 +689,7 @@ declare namespace jsb {
* @en To string
* @zh 转换成字符串
*/
toStr(): string;
toString(): string;

/**
* @en Returns account identifiers that were provided when the purchase was made.
Expand Down Expand Up @@ -744,7 +745,8 @@ declare namespace jsb {
* @en Results related to in-app messaging.
* @zh 与应用程序内消息相关的结果。
*/
export interface InAppMessageResult {
export class InAppMessageResult {
static InAppMessageResponseCode: typeof InAppMessageResponseCode;
/**
* @en Response code for the in-app messaging API call.
* @zh 应用内消息传递 API 调用的响应代码。
Expand All @@ -763,9 +765,8 @@ declare namespace jsb {
build(): BillingResult;
}
export class BillingResult {
static Builder: {
new(): BillingResultBuilder
};
private constructor();
static Builder: BillingResultBuilder;
getResponseCode(): number;
getDebugMessage(): string;
toString(): string;
Expand Down Expand Up @@ -800,22 +801,22 @@ declare namespace jsb {
public static newBuilder(): PendingPurchasesParamsBuilder;
}

export interface ProductBuilder {
setProductId: (productID: string) => ProductBuilder;
setProductType: (productType: string) => ProductBuilder;
build: () => Product;
export interface QueryProductDetailsParamsProductBuilder {
setProductId: (productID: string) => QueryProductDetailsParamsProductBuilder;
setProductType: (productType: string) => QueryProductDetailsParamsProductBuilder;
build: () => QueryProductDetailsParamsProduct;
}
export class Product {
export class QueryProductDetailsParamsProduct {
private constructor();
public static newBuilder(): ProductBuilder;
public static newBuilder(): QueryProductDetailsParamsProductBuilder;
}

export interface QueryProductDetailsParamsBuilder {
setProductList: (products: Product[]) => QueryProductDetailsParamsBuilder;
setProductList: (products: QueryProductDetailsParamsProduct[]) => QueryProductDetailsParamsBuilder;
build: () => QueryProductDetailsParams;
}
export class QueryProductDetailsParams {
static Product: typeof jsb.Product;
static Product: typeof jsb.QueryProductDetailsParamsProduct;
private constructor();
public static newBuilder(): QueryProductDetailsParamsBuilder;
}
Expand Down Expand Up @@ -1308,6 +1309,7 @@ declare namespace jsb {
}
export class ProductDetailsParams {
private constructor();
static Builder: ProductDetailsParamsBuilder;
static newBuilder: () => ProductDetailsParamsBuilder;
}

Expand All @@ -1319,7 +1321,8 @@ declare namespace jsb {
}
export class SubscriptionUpdateParams {
private constructor();
static newBuilder(): () => SubscriptionUpdateParamsBuilder;
static Builder: SubscriptionUpdateParamsBuilder;
static newBuilder: () => SubscriptionUpdateParamsBuilder;
static ReplacementMode: typeof jsb.ReplacementMode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ public final class GoogleBilling {
public GoogleBilling(int tag, BillingClient.Builder builder) {
assert tag >= 0 && builder != null;
this._tag = tag;
_billingClient = builder.build();
try {
_billingClient = builder.build();
} catch (RuntimeException e) {
Log.e(TAG, e.getMessage());
}
}

public void removeProductDetails(int productDetailsId) {
Expand Down
54 changes: 48 additions & 6 deletions native/vendor/google/billing/GoogleBilling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
****************************************************************************/

#include "vendor/google/billing/GoogleBilling.h"
#include "cocos/bindings/jswrapper/SeApi.h"
#include "platform/java/jni/JniHelper.h"
#include "platform/java/jni/JniImp.h"
#include "cocos/bindings/jswrapper/SeApi.h"
#include "vendor/google/billing/GoogleBillingHelper.h"
#include "vendor/google/billing/GoogleBillingManager.h"
#include "vendor/google/billing/build-params/AcknowledgePurchaseParams.h"
Expand All @@ -39,21 +39,40 @@

namespace cc {

#define ADD_JS_OBJ_REF(obj) \
do { \
obj->root(); \
obj->incRef(); \
} while (0)

#define DEL_JS_OBJ_REF(obj) \
do { \
if (obj) { \
obj->unroot(); \
obj->decRef(); \
} \
} while (0)

BillingClient::Builder& BillingClient::Builder::enableUserChoiceBilling(se::Object* listener) {
listener->root();
listener->incRef();
if (!listener) {
CC_LOG_WARNING("Can't set an empty listener.");
return *this;
}
ADD_JS_OBJ_REF(listener);
_userChoiceBillingListener = listener;
return *this;
}

BillingClient::Builder& BillingClient::Builder::setListener(se::Object* listener) {
listener->root();
listener->incRef();
if (!listener) {
CC_LOG_WARNING("Can't set an empty listener.");
return *this;
}
ADD_JS_OBJ_REF(listener);
_purchasesUpdatedListener = listener;
return *this;
}


BillingClient::BillingClient(Builder* builder) {
this->_enableAlternativeBillingOnly = builder->_enableAlternativeBillingOnly;
this->_enableExternalOffer = builder->_enableExternalOffer;
Expand All @@ -68,10 +87,33 @@ BillingClient::BillingClient(Builder* builder) {
delete builder;
}

void BillingClient::RemoveJsObject(std::vector<se::Object*>* listeners) {
for (auto* listener : *listeners) {
DEL_JS_OBJ_REF(listener);
}
listeners->clear();
}

BillingClient::~BillingClient() {
CC_ASSERT(_tag >= 0);
GoogleBillingHelper::removeGoogleBilling(_tag);
GoogleBillingManager::getInstance()->removeGoogleBilling(_tag);

DEL_JS_OBJ_REF(_purchasesUpdatedListener);
DEL_JS_OBJ_REF(_userChoiceBillingListener);
RemoveJsObject(&_billingClientStateListeners);
RemoveJsObject(&_productDetailsResponseListeners);
RemoveJsObject(&_consumeResponseListeners);
RemoveJsObject(&_acknowledgePurchaseResponseListeners);
RemoveJsObject(&_queryPurchasesResponseListeners);
RemoveJsObject(&_alternativeBillingOnlyReportingDetailsListeners);
RemoveJsObject(&_alternativeBillingOnlyAvailabilityListeners);
RemoveJsObject(&_externalOfferReportingDetailsListeners);
RemoveJsObject(&_externalOfferAvailabilityListeners);
RemoveJsObject(&_alternativeBillingOnlyInformationDialogListeners);
RemoveJsObject(&_externalOfferInformationDialogListeners);
RemoveJsObject(&_inappListeners);
RemoveJsObject(&_billingConfigListeners);
}

void BillingClient::startConnection(se::Object* listener) {
Expand Down
14 changes: 6 additions & 8 deletions native/vendor/google/billing/GoogleBilling.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ class CC_DLL BillingClient : public cc::RefCounted {
bool _enableAlternativeBillingOnly;
bool _enableExternalOffer;
PendingPurchasesParams* _pendingPurchasesParams;
se::Object* _purchasesUpdatedListener;
se::Object* _userChoiceBillingListener;
se::Object* _purchasesUpdatedListener{nullptr};
se::Object* _userChoiceBillingListener{nullptr};
};

static Builder* newBuilder() {
return new Builder();
}
Expand All @@ -94,7 +95,6 @@ class CC_DLL BillingClient : public cc::RefCounted {
void acknowledgePurchase(AcknowledgePurchaseParams* params, se::Object* listener);
void queryPurchasesAsync(QueryPurchasesParams* parmas, se::Object* listener);
void getBillingConfigAsync(GetBillingConfigParams* params, se::Object* listener);

void createAlternativeBillingOnlyReportingDetailsAsync(se::Object* listener);
void isAlternativeBillingOnlyAvailableAsync(se::Object* listener);
void createExternalOfferReportingDetailsAsync(se::Object* listener);
Expand All @@ -106,31 +106,29 @@ class CC_DLL BillingClient : public cc::RefCounted {
private:
BillingClient(Builder* builder);
~BillingClient();
void RemoveJsObject(std::vector<se::Object*>* listeners);

private:
friend class GoogleBillingHelper;
int _tag{-1};
bool _enableAlternativeBillingOnly{false};
bool _enableExternalOffer{false};
PendingPurchasesParams* _pendingPurchasesParams{nullptr};

se::Object* _purchasesUpdatedListener{nullptr};
se::Object* _userChoiceBillingListener{nullptr};
PendingPurchasesParams* _pendingPurchasesParams{nullptr};

std::vector<se::Object*> _billingClientStateListeners;
std::vector<se::Object*> _productDetailsResponseListeners;
std::vector<se::Object*> _consumeResponseListeners;
std::vector<se::Object*> _acknowledgePurchaseResponseListeners;
std::vector<se::Object*> _queryPurchasesResponseListeners;

std::vector<se::Object*> _alternativeBillingOnlyReportingDetailsListeners;
std::vector<se::Object*> _alternativeBillingOnlyAvailabilityListeners;
std::vector<se::Object*> _externalOfferReportingDetailsListeners;
std::vector<se::Object*> _externalOfferAvailabilityListeners;

std::vector<se::Object*> _alternativeBillingOnlyInformationDialogListeners;
std::vector<se::Object*> _externalOfferInformationDialogListeners;
std::vector<se::Object*> _inappListeners;

std::vector<se::Object*> _billingConfigListeners;
};

Expand Down
2 changes: 1 addition & 1 deletion native/vendor/google/billing/GoogleBillingHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ BillingResult* GoogleBillingHelper::showInAppMessages(int tag, int callbackId, c
void GoogleBillingHelper::launchBillingFlow(int tag, BillingFlowParams* params) {
cc::JniMethodInfo t1;
cc::JniHelper::getStaticMethodInfo(t1, JCLS_BILLING, "launchBillingFlow", "(ILcom/android/billingclient/api/BillingFlowParams;)V");
t1.env->CallStaticVoidMethod(t1.classID, t1.methodID, tag, JniBilling::newBillingFlowParamsObject(params));
t1.env->CallStaticVoidMethod(t1.classID, t1.methodID, tag, JniBilling::newBillingFlowParamsObject(tag, params));
}

void GoogleBillingHelper::consumeAsync(int tag, int callbackId, ConsumeParams* purchase) {
Expand Down
14 changes: 7 additions & 7 deletions native/vendor/google/billing/JniBilling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ cc::UserChoiceDetails::Product* JniBilling::toUserChoiceDetailsProduct(JNIEnv* e
product->_id = callStringMethod(env, clazz, obj, "getId");
product->_offerToken = callStringMethod(env, clazz, obj, "getOfferToken");
product->_type = callStringMethod(env, clazz, obj, "getType");
product->_toStr = callStringMethod(env, clazz, obj, "toString");
product->_toString = callStringMethod(env, clazz, obj, "toString");
return product;
}

Expand Down Expand Up @@ -382,7 +382,7 @@ jobject JniBilling::newSubscriptionUpdateParamsObject(BillingFlowParams::Subscri
return env->CallObjectMethod(builder, buildMethodIdMethodId);
}

jobject JniBilling::newProductDetailsParamsObject(BillingFlowParams::ProductDetailsParams* params) {
jobject JniBilling::newProductDetailsParamsObject(int tag, 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 All @@ -396,27 +396,27 @@ jobject JniBilling::newProductDetailsParamsObject(BillingFlowParams::ProductDeta

cc::JniMethodInfo t2;
cc::JniHelper::getStaticMethodInfo(t2, JCLS_BILLING, "getProductDetailsObject", "(II)Lcom/android/billingclient/api/ProductDetails;");
jobject productDetailsObject = t2.env->CallStaticObjectMethod(t2.classID, t2.methodID, 0, params->_productDetails->_id);
jobject productDetailsObject = t2.env->CallStaticObjectMethod(t2.classID, t2.methodID, tag, params->_productDetails->_id);
jmethodID setProductDetailsMethodId = env->GetMethodID(builderClass, "setProductDetails", "(Lcom/android/billingclient/api/ProductDetails;)Lcom/android/billingclient/api/BillingFlowParams$ProductDetailsParams$Builder;");
env->CallObjectMethod(builder, setProductDetailsMethodId, productDetailsObject);

jmethodID buildMethodId = env->GetMethodID(builderClass, "build", "()Lcom/android/billingclient/api/BillingFlowParams$ProductDetailsParams;");
return env->CallObjectMethod(builder, buildMethodId);
}

jobject JniBilling::newProductDetailsParamsListObject(std::vector<BillingFlowParams::ProductDetailsParams*> listParams) {
jobject JniBilling::newProductDetailsParamsListObject(int tag, std::vector<BillingFlowParams::ProductDetailsParams*> listParams) {
JNIEnv *env = cc::JniHelper::getEnv();
jclass listClass = env->FindClass("java/util/ArrayList");
jmethodID methodInit = env->GetMethodID(listClass, "<init>", "()V"); /* 无参构造 */
jobject list = env->NewObjectA(listClass, methodInit, 0);
jmethodID methodAdd = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
for(auto param : listParams) {
env->CallBooleanMethod(list, methodAdd, newProductDetailsParamsObject(param));
env->CallBooleanMethod(list, methodAdd, newProductDetailsParamsObject(tag, param));
}
return list;
}

jobject JniBilling::newBillingFlowParamsObject(BillingFlowParams* params) {
jobject JniBilling::newBillingFlowParamsObject(int tag, 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 All @@ -432,7 +432,7 @@ jobject JniBilling::newBillingFlowParamsObject(BillingFlowParams* params) {
jmethodID setObfuscatedProfileIdMethodId = env->GetMethodID(builderClass, "setObfuscatedProfileId", "(Ljava/lang/String;)Lcom/android/billingclient/api/BillingFlowParams$Builder;");
env->CallObjectMethod(builder, setObfuscatedProfileIdMethodId, cc::StringUtils::newStringUTFJNI(env, params->_obfuscatedProfileId));

jobject listObjs = JniBilling::newProductDetailsParamsListObject(params->_productDetailsParamsList);
jobject listObjs = JniBilling::newProductDetailsParamsListObject(tag, params->_productDetailsParamsList);
jmethodID setProductDetailsParamsListMethodId = env->GetMethodID(builderClass, "setProductDetailsParamsList", "(Ljava/util/List;)Lcom/android/billingclient/api/BillingFlowParams$Builder;");
env->CallObjectMethod(builder, setProductDetailsParamsListMethodId, listObjs);

Expand Down
6 changes: 3 additions & 3 deletions native/vendor/google/billing/JniBilling.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ class CC_DLL JniBilling {
static UserChoiceDetails* toUserChoiceDetails(JNIEnv* env, jobject obj);


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

static jobject newPurchaseUpdateListenerObject(int tag);
static jobject newUserChoiceBillingListenerObj(int tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class GetBillingConfigParams {
}

private:
GetBillingConfigParams() {
}
GetBillingConfigParams() = default;
};


Expand Down
4 changes: 2 additions & 2 deletions native/vendor/google/billing/result-values/ProductDetails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ ProductDetails::~ProductDetails() {
delete _oneTimePurchaseOfferDetails;
_oneTimePurchaseOfferDetails = nullptr;
}
for (auto* ptr : _subscriptionOfferDetails) {
delete ptr;
for (auto* details : _subscriptionOfferDetails) {
delete details;
}
_subscriptionOfferDetails.clear();
GoogleBillingHelper::removeProductDetails(_tag, _id);
Expand Down
2 changes: 1 addition & 1 deletion native/vendor/google/billing/result-values/Purchase.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class CC_DLL Purchase : public cc::RefCounted {
}
~Purchase() override;
bool equals(const Purchase& other) const {
return this->_hashCode == other._hashCode;
return this->_originalJson == other._originalJson && this->_signature == other._signature;
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class UserChoiceDetails : public cc::RefCounted {
std::string getType() const {
return _offerToken;
}
std::string toStr() const {
return _toStr;
std::string toString() const {
return _toString;
}
bool equals(const Product& p) {
if (&p == this) {
Expand All @@ -62,7 +62,7 @@ class UserChoiceDetails : public cc::RefCounted {
std::string _id;
std::string _type;
std::string _offerToken;
std::string _toStr;
std::string _toString;
};

~UserChoiceDetails() {
Expand Down
Loading

0 comments on commit 891d7b0

Please sign in to comment.