discord-jadx/app/src/main/java/com/discord/utilities/billing/GooglePlayBillingManager.java

551 lines
22 KiB
Java

package com.discord.utilities.billing;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import c.a.d.l;
import c.d.a.a.b;
import c.d.a.a.c;
import c.d.a.a.d;
import c.d.a.a.e;
import c.d.a.a.f;
import c.d.a.a.g;
import c.d.b.a.a;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.SkuDetails;
import com.discord.app.AppActivity;
import com.discord.app.AppLog;
import com.discord.stores.StoreGooglePlaySkuDetails;
import com.discord.stores.StoreStream;
import com.discord.utilities.KotlinExtensionsKt;
import com.discord.utilities.logging.Logger;
import com.discord.utilities.rx.ActivityLifecycleCallbacks;
import com.discord.utilities.rx.ObservableExtensionsKt;
import d0.t.n;
import d0.t.o;
import d0.t.u;
import d0.z.d.m;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import kotlin.NoWhenBranchMatchedException;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import rx.Observable;
/* compiled from: GooglePlayBillingManager.kt */
public final class GooglePlayBillingManager implements e, b, g, d {
private static final long DEFAULT_BACKOFF_TIME_MS = 1000;
public static final GooglePlayBillingManager INSTANCE = new GooglePlayBillingManager();
public static final String PLAY_STORE_SUBSCRIPTION_DEEPLINK_URL = "https://play.google.com/store/account/subscriptions?sku=%s&package=%s";
public static final String PLAY_STORE_SUBSCRIPTION_URL = "https://play.google.com/store/account/subscriptions";
private static AtomicLong backoffTimeMs = new AtomicLong(1000);
private static BillingClient billingClient;
private static Map<String, InAppSkuToConsume> inAppSkusToConsume = new HashMap();
private static boolean isAuthenticated;
private static AtomicBoolean isReconnecting = new AtomicBoolean(false);
/* compiled from: GooglePlayBillingManager.kt */
public static final class GooglePlayBillingManagerLifecycleListener extends ActivityLifecycleCallbacks {
@Override // com.discord.utilities.rx.ActivityLifecycleCallbacks
public void onActivityCreated(AppActivity appActivity, Bundle bundle) {
m.checkNotNullParameter(appActivity, "activity");
super.onActivityCreated(appActivity, bundle);
l lVar = l.g;
if (appActivity.g(l.f)) {
GooglePlayBillingManager googlePlayBillingManager = GooglePlayBillingManager.INSTANCE;
if (GooglePlayBillingManager.access$isAuthenticated$p(googlePlayBillingManager)) {
googlePlayBillingManager.onActivityCreated();
}
}
}
@Override // com.discord.utilities.rx.ActivityLifecycleCallbacks
public void onActivityDestroyed(AppActivity appActivity) {
m.checkNotNullParameter(appActivity, "activity");
super.onActivityDestroyed(appActivity);
l lVar = l.g;
if (appActivity.g(l.f)) {
GooglePlayBillingManager.INSTANCE.onActivityDestroyed();
}
}
@Override // com.discord.utilities.rx.ActivityLifecycleCallbacks
public void onActivityResumed(AppActivity appActivity) {
m.checkNotNullParameter(appActivity, "activity");
super.onActivityResumed(appActivity);
l lVar = l.g;
if (appActivity.g(l.f)) {
GooglePlayBillingManager googlePlayBillingManager = GooglePlayBillingManager.INSTANCE;
if (GooglePlayBillingManager.access$isAuthenticated$p(googlePlayBillingManager)) {
googlePlayBillingManager.queryPurchases();
}
}
}
}
/* compiled from: GooglePlayBillingManager.kt */
public static final class InAppSkuToConsume {
private final String paymentGatewaySkuId;
private final Long skuId;
private final InAppSkuType type;
public InAppSkuToConsume(InAppSkuType inAppSkuType, Long l, String str) {
m.checkNotNullParameter(inAppSkuType, "type");
m.checkNotNullParameter(str, "paymentGatewaySkuId");
this.type = inAppSkuType;
this.skuId = l;
this.paymentGatewaySkuId = str;
}
public static /* synthetic */ InAppSkuToConsume copy$default(InAppSkuToConsume inAppSkuToConsume, InAppSkuType inAppSkuType, Long l, String str, int i, Object obj) {
if ((i & 1) != 0) {
inAppSkuType = inAppSkuToConsume.type;
}
if ((i & 2) != 0) {
l = inAppSkuToConsume.skuId;
}
if ((i & 4) != 0) {
str = inAppSkuToConsume.paymentGatewaySkuId;
}
return inAppSkuToConsume.copy(inAppSkuType, l, str);
}
public final InAppSkuType component1() {
return this.type;
}
public final Long component2() {
return this.skuId;
}
public final String component3() {
return this.paymentGatewaySkuId;
}
public final InAppSkuToConsume copy(InAppSkuType inAppSkuType, Long l, String str) {
m.checkNotNullParameter(inAppSkuType, "type");
m.checkNotNullParameter(str, "paymentGatewaySkuId");
return new InAppSkuToConsume(inAppSkuType, l, str);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof InAppSkuToConsume)) {
return false;
}
InAppSkuToConsume inAppSkuToConsume = (InAppSkuToConsume) obj;
return m.areEqual(this.type, inAppSkuToConsume.type) && m.areEqual(this.skuId, inAppSkuToConsume.skuId) && m.areEqual(this.paymentGatewaySkuId, inAppSkuToConsume.paymentGatewaySkuId);
}
public final String getPaymentGatewaySkuId() {
return this.paymentGatewaySkuId;
}
public final Long getSkuId() {
return this.skuId;
}
public final InAppSkuType getType() {
return this.type;
}
public int hashCode() {
InAppSkuType inAppSkuType = this.type;
int i = 0;
int hashCode = (inAppSkuType != null ? inAppSkuType.hashCode() : 0) * 31;
Long l = this.skuId;
int hashCode2 = (hashCode + (l != null ? l.hashCode() : 0)) * 31;
String str = this.paymentGatewaySkuId;
if (str != null) {
i = str.hashCode();
}
return hashCode2 + i;
}
public String toString() {
StringBuilder K = a.K("InAppSkuToConsume(type=");
K.append(this.type);
K.append(", skuId=");
K.append(this.skuId);
K.append(", paymentGatewaySkuId=");
return a.C(K, this.paymentGatewaySkuId, ")");
}
}
public final /* synthetic */ class WhenMappings {
public static final /* synthetic */ int[] $EnumSwitchMapping$0;
public static final /* synthetic */ int[] $EnumSwitchMapping$1;
public static final /* synthetic */ int[] $EnumSwitchMapping$2 = new int[1];
static {
InAppSkuType.values();
int[] iArr = new int[1];
$EnumSwitchMapping$0 = iArr;
InAppSkuType inAppSkuType = InAppSkuType.PREMIUM_GIFT;
iArr[inAppSkuType.ordinal()] = 1;
InAppSkuType.values();
int[] iArr2 = new int[1];
$EnumSwitchMapping$1 = iArr2;
iArr2[inAppSkuType.ordinal()] = 1;
InAppSkuType.values();
}
}
private GooglePlayBillingManager() {
}
public static final /* synthetic */ AtomicLong access$getBackoffTimeMs$p(GooglePlayBillingManager googlePlayBillingManager) {
return backoffTimeMs;
}
public static final /* synthetic */ BillingClient access$getBillingClient$p(GooglePlayBillingManager googlePlayBillingManager) {
BillingClient billingClient2 = billingClient;
if (billingClient2 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
return billingClient2;
}
public static final /* synthetic */ boolean access$isAuthenticated$p(GooglePlayBillingManager googlePlayBillingManager) {
return isAuthenticated;
}
public static final /* synthetic */ AtomicBoolean access$isReconnecting$p(GooglePlayBillingManager googlePlayBillingManager) {
return isReconnecting;
}
public static final /* synthetic */ void access$setAuthenticated$p(GooglePlayBillingManager googlePlayBillingManager, boolean z2) {
isAuthenticated = z2;
}
public static final /* synthetic */ void access$setBackoffTimeMs$p(GooglePlayBillingManager googlePlayBillingManager, AtomicLong atomicLong) {
backoffTimeMs = atomicLong;
}
public static final /* synthetic */ void access$setBillingClient$p(GooglePlayBillingManager googlePlayBillingManager, BillingClient billingClient2) {
billingClient = billingClient2;
}
public static final /* synthetic */ void access$setReconnecting$p(GooglePlayBillingManager googlePlayBillingManager, AtomicBoolean atomicBoolean) {
isReconnecting = atomicBoolean;
}
private final void handleConsumeEnd(String str) {
InAppSkuToConsume inAppSkuToConsume = inAppSkusToConsume.get(str);
if (inAppSkuToConsume != null) {
inAppSkuToConsume.getType();
}
}
private final void handleConsumeFailure(String str) {
InAppSkuToConsume inAppSkuToConsume = inAppSkusToConsume.get(str);
if (inAppSkuToConsume != null) {
StoreStream.Companion.getGooglePlayPurchases().trackPaymentFlowFailed(inAppSkuToConsume.getPaymentGatewaySkuId());
}
}
private final void handleConsumeStart(Purchase purchase, InAppSkuToConsume inAppSkuToConsume) {
Map<String, InAppSkuToConsume> map = inAppSkusToConsume;
String a = purchase.a();
m.checkNotNullExpressionValue(a, "purchase.purchaseToken");
map.put(a, inAppSkuToConsume);
if (inAppSkuToConsume.getType().ordinal() == 0) {
KotlinExtensionsKt.getExhaustive(Unit.a);
return;
}
throw new NoWhenBranchMatchedException();
}
private final void handleConsumeSuccess(String str) {
InAppSkuToConsume inAppSkuToConsume = inAppSkusToConsume.get(str);
if (inAppSkuToConsume != null) {
StoreStream.Companion.getGooglePlayPurchases().trackPaymentFlowCompleted(inAppSkuToConsume.getPaymentGatewaySkuId());
if (inAppSkuToConsume.getType().ordinal() == 0) {
KotlinExtensionsKt.getExhaustive(Unit.a);
return;
}
throw new NoWhenBranchMatchedException();
}
}
private final void queryInAppSkuDetails() {
BillingClient billingClient2 = billingClient;
if (billingClient2 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
if (billingClient2.c()) {
List<GooglePlayInAppSku> skus = GooglePlayInAppSkus.INSTANCE.getSkus();
ArrayList arrayList = new ArrayList(o.collectionSizeOrDefault(skus, 10));
for (GooglePlayInAppSku googlePlayInAppSku : skus) {
arrayList.add(googlePlayInAppSku.getPaymentGatewaySkuId());
}
ArrayList arrayList2 = new ArrayList(arrayList);
f fVar = new f();
fVar.a = "inapp";
fVar.b = arrayList2;
m.checkNotNullExpressionValue(fVar, "SkuDetailsParams.newBuil…kuNames)\n .build()");
BillingClient billingClient3 = billingClient;
if (billingClient3 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
billingClient3.f(fVar, this);
}
}
private final void reconnect() {
if (isReconnecting.compareAndSet(false, true)) {
long j = backoffTimeMs.get();
Observable<Long> d02 = Observable.d0(j, TimeUnit.MILLISECONDS);
m.checkNotNullExpressionValue(d02, "Observable\n .ti…s, TimeUnit.MILLISECONDS)");
ObservableExtensionsKt.appSubscribe$default(d02, GooglePlayBillingManager.class, (Context) null, (Function1) null, (Function1) null, (Function0) null, (Function0) null, new GooglePlayBillingManager$reconnect$1(j), 62, (Object) null);
}
}
public final void consumePurchase(Purchase purchase, InAppSkuType inAppSkuType, Long l) {
m.checkNotNullParameter(purchase, "purchase");
m.checkNotNullParameter(inAppSkuType, "inAppSkuType");
BillingClient billingClient2 = billingClient;
if (billingClient2 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
if (billingClient2.c()) {
String a = purchase.a();
if (a != null) {
c cVar = new c();
cVar.a = a;
m.checkNotNullExpressionValue(cVar, "ConsumeParams.newBuilder…se.purchaseToken).build()");
BillingClient billingClient3 = billingClient;
if (billingClient3 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
billingClient3.a(cVar, this);
String b = purchase.b();
m.checkNotNullExpressionValue(b, "purchase.sku");
handleConsumeStart(purchase, new InAppSkuToConsume(inAppSkuType, l, b));
return;
}
throw new IllegalArgumentException("Purchase token must be set");
}
}
public final void init(Application application) {
m.checkNotNullParameter(application, "application");
Context applicationContext = application.getApplicationContext();
if (applicationContext != null) {
c.d.a.a.a aVar = new c.d.a.a.a(null, applicationContext, this);
m.checkNotNullExpressionValue(aVar, "BillingClient.newBuilder…chases()\n .build()");
billingClient = aVar;
application.registerActivityLifecycleCallbacks(new GooglePlayBillingManagerLifecycleListener());
ObservableExtensionsKt.appSubscribe$default(StoreStream.Companion.getAuthentication().observeIsAuthed$app_productionBetaRelease(), GooglePlayBillingManager.class, (Context) null, (Function1) null, (Function1) null, (Function0) null, (Function0) null, GooglePlayBillingManager$init$1.INSTANCE, 62, (Object) null);
return;
}
throw new IllegalArgumentException("Please provide a valid Context.");
}
public final int launchBillingFlow(Activity activity, BillingFlowParams billingFlowParams) {
m.checkNotNullParameter(activity, "activity");
m.checkNotNullParameter(billingFlowParams, "params");
BillingClient billingClient2 = billingClient;
if (billingClient2 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
if (!billingClient2.c()) {
return -1;
}
BillingClient billingClient3 = billingClient;
if (billingClient3 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
BillingResult d = billingClient3.d(activity, billingFlowParams);
m.checkNotNullExpressionValue(d, "billingClient.launchBillingFlow(activity, params)");
return d.a;
}
public final void onActivityCreated() {
BillingClient billingClient2 = billingClient;
if (billingClient2 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
if (!billingClient2.c()) {
BillingClient billingClient3 = billingClient;
if (billingClient3 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
billingClient3.g(this);
}
}
public final void onActivityDestroyed() {
BillingClient billingClient2 = billingClient;
if (billingClient2 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
if (billingClient2.c()) {
BillingClient billingClient3 = billingClient;
if (billingClient3 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
billingClient3.b();
}
}
@Override // c.d.a.a.b
public void onBillingServiceDisconnected() {
if (!isReconnecting.get()) {
reconnect();
}
}
@Override // c.d.a.a.b
public void onBillingSetupFinished(BillingResult billingResult) {
m.checkNotNullParameter(billingResult, "billingResult");
if (billingResult.a == 0) {
querySkuDetails();
queryInAppSkuDetails();
queryPurchases();
backoffTimeMs.set(1000);
}
}
@Override // c.d.a.a.d
public void onConsumeResponse(BillingResult billingResult, String str) {
m.checkNotNullParameter(billingResult, "billingResult");
m.checkNotNullParameter(str, "purchaseToken");
if (billingResult.a == 0) {
handleConsumeSuccess(str);
} else {
handleConsumeFailure(str);
AppLog appLog = AppLog.g;
StringBuilder O = a.O("Failed to consume purchase. ", "Billing Response Code: ");
O.append(billingResult.a);
O.append(", ");
O.append("Purchase Token: ");
O.append(str);
Logger.e$default(appLog, O.toString(), null, null, 6, null);
}
handleConsumeEnd(str);
}
@Override // c.d.a.a.e
public void onPurchasesUpdated(BillingResult billingResult, List<? extends Purchase> list) {
m.checkNotNullParameter(billingResult, "billingResult");
if (billingResult.a != 0) {
StoreStream.Companion.getGooglePlayPurchases().updatePendingDowngrade(null);
}
int i = billingResult.a;
boolean z2 = true;
if (i == 0) {
if (list != null && !list.isEmpty()) {
z2 = false;
}
if (z2) {
StoreStream.Companion.getGooglePlayPurchases().downgradePurchase();
}
StoreStream.Companion.getGooglePlayPurchases().processPurchases(list);
} else if (i == 1) {
AppLog.i("onPurchasesUpdated: User canceled the purchase");
} else if (i == 5) {
Logger.e$default(AppLog.g, "onPurchasesUpdated: Google Play doesn't recognize this app config. Verify the SKU product ID and the signed APK you are using.", null, null, 6, null);
} else if (i == 7) {
AppLog.i("onPurchasesUpdated: The user already owns this item");
}
}
@Override // c.d.a.a.g
public void onSkuDetailsResponse(BillingResult billingResult, List<? extends SkuDetails> list) {
m.checkNotNullParameter(billingResult, "billingResult");
int i = billingResult.a;
String str = billingResult.b;
m.checkNotNullExpressionValue(str, "billingResult.debugMessage");
switch (i) {
case -2:
case 1:
case 7:
case 8:
StoreStream.Companion.getGooglePlaySkuDetails().handleError();
AppLog appLog = AppLog.g;
Logger.e$default(appLog, "onSkuDetailsResponse: " + i + ' ' + str, null, null, 6, null);
return;
case -1:
case 2:
case 3:
case 4:
case 5:
case 6:
StoreStream.Companion.getGooglePlaySkuDetails().handleError();
return;
case 0:
StoreGooglePlaySkuDetails googlePlaySkuDetails = StoreStream.Companion.getGooglePlaySkuDetails();
if (list == null) {
list = n.emptyList();
}
googlePlaySkuDetails.updateSkuDetails(list);
return;
default:
return;
}
}
public final void queryPurchases() {
BillingClient billingClient2 = billingClient;
if (billingClient2 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
if (billingClient2.c()) {
HashSet hashSet = new HashSet();
BillingClient billingClient3 = billingClient;
if (billingClient3 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
Purchase.a e = billingClient3.e("subs");
m.checkNotNullExpressionValue(e, "billingClient.queryPurch…llingClient.SkuType.SUBS)");
List<Purchase> list = e.a;
if (list != null) {
hashSet.addAll(list);
}
BillingClient billingClient4 = billingClient;
if (billingClient4 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
Purchase.a e2 = billingClient4.e("inapp");
m.checkNotNullExpressionValue(e2, "billingClient.queryPurch…lingClient.SkuType.INAPP)");
List<Purchase> list2 = e2.a;
if (list2 != null) {
hashSet.addAll(list2);
}
StoreStream.Companion.getGooglePlayPurchases().processPurchases(u.toList(hashSet));
}
}
public final void querySkuDetails() {
BillingClient billingClient2 = billingClient;
if (billingClient2 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
if (billingClient2.c()) {
ArrayList arrayList = new ArrayList(GooglePlaySku.Companion.getALL_SKU_NAMES());
f fVar = new f();
fVar.a = "subs";
fVar.b = arrayList;
m.checkNotNullExpressionValue(fVar, "SkuDetailsParams.newBuil…U_NAMES)\n .build()");
BillingClient billingClient3 = billingClient;
if (billingClient3 == null) {
m.throwUninitializedPropertyAccessException("billingClient");
}
billingClient3.f(fVar, this);
}
}
}