Small refactoring
This commit is contained in:
parent
82f2138c07
commit
6efb6f27b3
2 changed files with 10 additions and 41 deletions
|
@ -6,7 +6,6 @@ import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.android.billingclient.api.AccountIdentifiers;
|
|
||||||
import com.android.billingclient.api.BillingClient;
|
import com.android.billingclient.api.BillingClient;
|
||||||
import com.android.billingclient.api.BillingResult;
|
import com.android.billingclient.api.BillingResult;
|
||||||
import com.android.billingclient.api.Purchase;
|
import com.android.billingclient.api.Purchase;
|
||||||
|
@ -200,7 +199,6 @@ public class InAppPurchaseHelperImpl extends InAppPurchaseHelper {
|
||||||
|
|
||||||
BillingManager billingManager = getBillingManager();
|
BillingManager billingManager = getBillingManager();
|
||||||
if (billingManager != null) {
|
if (billingManager != null) {
|
||||||
setupUserInfo(billingManager);
|
|
||||||
billingManager.initiatePurchaseFlow(activity, skuDetails);
|
billingManager.initiatePurchaseFlow(activity, skuDetails);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("BillingManager disposed");
|
throw new IllegalStateException("BillingManager disposed");
|
||||||
|
@ -228,7 +226,6 @@ public class InAppPurchaseHelperImpl extends InAppPurchaseHelper {
|
||||||
}
|
}
|
||||||
BillingManager billingManager = getBillingManager();
|
BillingManager billingManager = getBillingManager();
|
||||||
if (billingManager != null) {
|
if (billingManager != null) {
|
||||||
setupUserInfo(billingManager);
|
|
||||||
billingManager.initiatePurchaseFlow(activity, skuDetails);
|
billingManager.initiatePurchaseFlow(activity, skuDetails);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("BillingManager disposed");
|
throw new IllegalStateException("BillingManager disposed");
|
||||||
|
@ -462,43 +459,27 @@ public class InAppPurchaseHelperImpl extends InAppPurchaseHelper {
|
||||||
};
|
};
|
||||||
|
|
||||||
private void restoreUserInfo(OsmandSettings settings, Purchase purchase) {
|
private void restoreUserInfo(OsmandSettings settings, Purchase purchase) {
|
||||||
boolean restored = restoreUserInfoUsingPayload(settings, purchase);
|
boolean restored = restoreUserInfoFromString(settings, purchase.getDeveloperPayload());
|
||||||
if (!restored) {
|
if (!restored) {
|
||||||
restoreUserInfoUsingAccountIdentifiers(settings, purchase);
|
restoreUserInfoFromString(settings, purchase.getAccountIdentifiers().getObfuscatedAccountId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean restoreUserInfoUsingPayload(OsmandSettings settings, Purchase purchase) {
|
private boolean restoreUserInfoFromString(OsmandSettings settings, String userInfo) {
|
||||||
String payload = purchase.getDeveloperPayload();
|
if (Algorithms.isEmpty(userInfo)) {
|
||||||
if (Algorithms.isEmpty(payload)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String[] arr = payload.split(" ");
|
String[] arr = userInfo.split(" ");
|
||||||
if (arr.length > 0) {
|
if (arr.length > 0 && !Algorithms.isEmpty(settings.BILLING_USER_ID.get())) {
|
||||||
settings.BILLING_USER_ID.set(arr[0]);
|
settings.BILLING_USER_ID.set(arr[0]);
|
||||||
}
|
}
|
||||||
if (arr.length > 1) {
|
if (arr.length > 1 && !Algorithms.isEmpty(settings.BILLING_USER_TOKEN.get())) {
|
||||||
token = arr[1];
|
token = arr[1];
|
||||||
settings.BILLING_USER_TOKEN.set(token);
|
settings.BILLING_USER_TOKEN.set(token);
|
||||||
}
|
}
|
||||||
return needRestoreUserInfo(settings);
|
return needRestoreUserInfo(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void restoreUserInfoUsingAccountIdentifiers(OsmandSettings settings, Purchase purchase) {
|
|
||||||
AccountIdentifiers accountInfo = purchase.getAccountIdentifiers();
|
|
||||||
if (accountInfo != null) {
|
|
||||||
String userId = accountInfo.getObfuscatedAccountId();
|
|
||||||
String userToken = accountInfo.getObfuscatedProfileId();
|
|
||||||
if (Algorithms.isEmpty(settings.BILLING_USER_ID.get()) && !Algorithms.isEmpty(userId)) {
|
|
||||||
settings.BILLING_USER_ID.set(userId);
|
|
||||||
}
|
|
||||||
if (Algorithms.isEmpty(settings.BILLING_USER_TOKEN.get()) && !Algorithms.isEmpty(userToken)) {
|
|
||||||
token = userToken;
|
|
||||||
settings.BILLING_USER_TOKEN.set(userToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean needRestoreUserInfo(OsmandSettings settings) {
|
private boolean needRestoreUserInfo(OsmandSettings settings) {
|
||||||
return Algorithms.isEmpty(settings.BILLING_USER_ID.get()) || Algorithms.isEmpty(settings.BILLING_USER_TOKEN.get());
|
return Algorithms.isEmpty(settings.BILLING_USER_ID.get()) || Algorithms.isEmpty(settings.BILLING_USER_TOKEN.get());
|
||||||
}
|
}
|
||||||
|
@ -568,7 +549,9 @@ public class InAppPurchaseHelperImpl extends InAppPurchaseHelper {
|
||||||
if (AndroidUtils.isActivityNotDestroyed(a) && skuDetails != null) {
|
if (AndroidUtils.isActivityNotDestroyed(a) && skuDetails != null) {
|
||||||
BillingManager billingManager = getBillingManager();
|
BillingManager billingManager = getBillingManager();
|
||||||
if (billingManager != null) {
|
if (billingManager != null) {
|
||||||
setupUserInfo(billingManager);
|
OsmandSettings settings = ctx.getSettings();
|
||||||
|
String userCredential = settings.BILLING_USER_ID.get() + settings.BILLING_USER_TOKEN.get();
|
||||||
|
billingManager.setObfuscatedAccountId(userCredential);
|
||||||
billingManager.initiatePurchaseFlow(a, skuDetails);
|
billingManager.initiatePurchaseFlow(a, skuDetails);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("BillingManager disposed");
|
throw new IllegalStateException("BillingManager disposed");
|
||||||
|
@ -620,12 +603,6 @@ public class InAppPurchaseHelperImpl extends InAppPurchaseHelper {
|
||||||
onPurchaseDone(getPurchaseInfo(purchase));
|
onPurchaseDone(getPurchaseInfo(purchase));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupUserInfo(BillingManager billingManager) {
|
|
||||||
OsmandSettings settings = ctx.getSettings();
|
|
||||||
billingManager.setObfuscatedAccountId(settings.BILLING_USER_ID.get());
|
|
||||||
billingManager.setObfuscatedProfileId(settings.BILLING_USER_TOKEN.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isBillingManagerExists() {
|
protected boolean isBillingManagerExists() {
|
||||||
return getBillingManager() != null;
|
return getBillingManager() != null;
|
||||||
|
|
|
@ -59,7 +59,6 @@ public class BillingManager implements PurchasesUpdatedListener {
|
||||||
// Public key for verifying signature, in base64 encoding
|
// Public key for verifying signature, in base64 encoding
|
||||||
private String mSignatureBase64;
|
private String mSignatureBase64;
|
||||||
private String mObfuscatedAccountId;
|
private String mObfuscatedAccountId;
|
||||||
private String mObfuscatedProfileId;
|
|
||||||
|
|
||||||
private final BillingUpdatesListener mBillingUpdatesListener;
|
private final BillingUpdatesListener mBillingUpdatesListener;
|
||||||
private final List<Purchase> mPurchases = new ArrayList<>();
|
private final List<Purchase> mPurchases = new ArrayList<>();
|
||||||
|
@ -152,9 +151,6 @@ public class BillingManager implements PurchasesUpdatedListener {
|
||||||
if (!TextUtils.isEmpty(mObfuscatedAccountId)) {
|
if (!TextUtils.isEmpty(mObfuscatedAccountId)) {
|
||||||
paramsBuilder.setObfuscatedAccountId(mObfuscatedAccountId);
|
paramsBuilder.setObfuscatedAccountId(mObfuscatedAccountId);
|
||||||
}
|
}
|
||||||
if (!TextUtils.isEmpty(mObfuscatedProfileId)) {
|
|
||||||
paramsBuilder.setObfuscatedProfileId(mObfuscatedProfileId);
|
|
||||||
}
|
|
||||||
if (oldSku != null && purchaseToken != null) {
|
if (oldSku != null && purchaseToken != null) {
|
||||||
paramsBuilder.setOldSku(oldSku, purchaseToken);
|
paramsBuilder.setOldSku(oldSku, purchaseToken);
|
||||||
}
|
}
|
||||||
|
@ -409,10 +405,6 @@ public class BillingManager implements PurchasesUpdatedListener {
|
||||||
mObfuscatedAccountId = obfuscatedAccountId;
|
mObfuscatedAccountId = obfuscatedAccountId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setObfuscatedProfileId(String obfuscatedProfileId) {
|
|
||||||
mObfuscatedProfileId = obfuscatedProfileId;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void executeServiceRequest(Runnable runnable) {
|
private void executeServiceRequest(Runnable runnable) {
|
||||||
if (mIsServiceConnected) {
|
if (mIsServiceConnected) {
|
||||||
runnable.run();
|
runnable.run();
|
||||||
|
|
Loading…
Reference in a new issue