mirror of https://github.com/M66B/FairEmail.git
Fixed IAB stub
This commit is contained in:
parent
abf709f44c
commit
5f6bd38809
|
@ -31,7 +31,6 @@ import android.os.Handler;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
@ -53,8 +52,6 @@ import com.android.billingclient.api.BillingResult;
|
||||||
import com.android.billingclient.api.ConsumeParams;
|
import com.android.billingclient.api.ConsumeParams;
|
||||||
import com.android.billingclient.api.ConsumeResponseListener;
|
import com.android.billingclient.api.ConsumeResponseListener;
|
||||||
import com.android.billingclient.api.Purchase;
|
import com.android.billingclient.api.Purchase;
|
||||||
import com.android.billingclient.api.PurchaseHistoryRecord;
|
|
||||||
import com.android.billingclient.api.PurchaseHistoryResponseListener;
|
|
||||||
import com.android.billingclient.api.PurchasesUpdatedListener;
|
import com.android.billingclient.api.PurchasesUpdatedListener;
|
||||||
import com.android.billingclient.api.SkuDetails;
|
import com.android.billingclient.api.SkuDetails;
|
||||||
import com.android.billingclient.api.SkuDetailsParams;
|
import com.android.billingclient.api.SkuDetailsParams;
|
||||||
|
@ -66,20 +63,20 @@ import java.security.PublicKey;
|
||||||
import java.security.Signature;
|
import java.security.Signature;
|
||||||
import java.security.spec.X509EncodedKeySpec;
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedListener,*/ FragmentManager.OnBackStackChangedListener {
|
public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedListener,*/ FragmentManager.OnBackStackChangedListener {
|
||||||
|
private boolean standalone = false;
|
||||||
//private BillingClient billingClient = null;
|
//private BillingClient billingClient = null;
|
||||||
//private Map<String, SkuDetails> skuDetails = new HashMap<>();
|
|
||||||
private List<IBillingListener> listeners = new ArrayList<>();
|
private List<IBillingListener> listeners = new ArrayList<>();
|
||||||
|
|
||||||
static final String ACTION_PURCHASE = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE";
|
static final String ACTION_PURCHASE = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE";
|
||||||
static final String ACTION_PURCHASE_CHECK = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE_CHECK";
|
static final String ACTION_PURCHASE_CONSUME = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE_CONSUME";
|
||||||
static final String ACTION_PURCHASE_ERROR = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE_ERROR";
|
static final String ACTION_PURCHASE_ERROR = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE_ERROR";
|
||||||
|
|
||||||
|
private static final String SKU_TEST = "android.test.purchased";
|
||||||
private final static long MAX_SKU_CACHE_DURATION = 24 * 3600 * 1000L; // milliseconds
|
private final static long MAX_SKU_CACHE_DURATION = 24 * 3600 * 1000L; // milliseconds
|
||||||
private final static long MAX_SKU_NOACK_DURATION = 24 * 3600 * 1000L; // milliseconds
|
private final static long MAX_SKU_NOACK_DURATION = 24 * 3600 * 1000L; // milliseconds
|
||||||
|
|
||||||
|
@ -92,6 +89,8 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
protected void onCreate(Bundle savedInstanceState, boolean standalone) {
|
protected void onCreate(Bundle savedInstanceState, boolean standalone) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
this.standalone = standalone;
|
||||||
|
|
||||||
if (standalone) {
|
if (standalone) {
|
||||||
setContentView(R.layout.activity_billing);
|
setContentView(R.layout.activity_billing);
|
||||||
|
|
||||||
|
@ -104,7 +103,7 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
getSupportFragmentManager().addOnBackStackChangedListener(this);
|
getSupportFragmentManager().addOnBackStackChangedListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Helper.isPlayStoreInstall()) {
|
if (Helper.isPlayStoreInstall() || isTesting(this)) {
|
||||||
Log.i("IAB start");
|
Log.i("IAB start");
|
||||||
//billingClient = BillingClient.newBuilder(this)
|
//billingClient = BillingClient.newBuilder(this)
|
||||||
// .enablePendingPurchases()
|
// .enablePendingPurchases()
|
||||||
|
@ -124,12 +123,14 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
|
if (standalone) {
|
||||||
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
|
||||||
IntentFilter iff = new IntentFilter();
|
IntentFilter iff = new IntentFilter();
|
||||||
iff.addAction(ACTION_PURCHASE);
|
iff.addAction(ACTION_PURCHASE);
|
||||||
iff.addAction(ACTION_PURCHASE_CHECK);
|
iff.addAction(ACTION_PURCHASE_CONSUME);
|
||||||
iff.addAction(ACTION_PURCHASE_ERROR);
|
iff.addAction(ACTION_PURCHASE_ERROR);
|
||||||
lbm.registerReceiver(receiver, iff);
|
lbm.registerReceiver(receiver, iff);
|
||||||
|
}
|
||||||
|
|
||||||
//if (billingClient != null && billingClient.isReady())
|
//if (billingClient != null && billingClient.isReady())
|
||||||
// queryPurchases();
|
// queryPurchases();
|
||||||
|
@ -139,9 +140,11 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
|
|
||||||
|
if (standalone) {
|
||||||
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
|
||||||
lbm.unregisterReceiver(receiver);
|
lbm.unregisterReceiver(receiver);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
|
@ -154,11 +157,16 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
@NonNull
|
@NonNull
|
||||||
static String getSkuPro() {
|
static String getSkuPro() {
|
||||||
if (BuildConfig.DEBUG)
|
if (BuildConfig.DEBUG)
|
||||||
return "android.test.purchased";
|
return SKU_TEST;
|
||||||
else
|
else
|
||||||
return BuildConfig.APPLICATION_ID + ".pro";
|
return BuildConfig.APPLICATION_ID + ".pro";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static boolean isTesting(Context context) {
|
||||||
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
return (BuildConfig.DEBUG && prefs.getBoolean("test_iab", false));
|
||||||
|
}
|
||||||
|
|
||||||
private static String getChallenge(Context context) throws NoSuchAlgorithmException {
|
private static String getChallenge(Context context) throws NoSuchAlgorithmException {
|
||||||
String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
||||||
return Helper.sha256(android_id);
|
return Helper.sha256(android_id);
|
||||||
|
@ -208,7 +216,7 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
|
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
|
||||||
if (ACTION_PURCHASE.equals(intent.getAction()))
|
if (ACTION_PURCHASE.equals(intent.getAction()))
|
||||||
onPurchase(intent);
|
onPurchase(intent);
|
||||||
else if (ACTION_PURCHASE_CHECK.equals(intent.getAction()))
|
else if (ACTION_PURCHASE_CONSUME.equals(intent.getAction()))
|
||||||
;//onPurchaseCheck(intent);
|
;//onPurchaseCheck(intent);
|
||||||
else if (ACTION_PURCHASE_ERROR.equals(intent.getAction()))
|
else if (ACTION_PURCHASE_ERROR.equals(intent.getAction()))
|
||||||
;//onPurchaseError(intent);
|
;//onPurchaseError(intent);
|
||||||
|
@ -217,16 +225,36 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
};
|
};
|
||||||
|
|
||||||
private void onPurchase(Intent intent) {
|
private void onPurchase(Intent intent) {
|
||||||
if (Helper.isPlayStoreInstall()) {
|
if (Helper.isPlayStoreInstall() || isTesting(this)) {
|
||||||
//BillingFlowParams.Builder flowParams = BillingFlowParams.newBuilder();
|
String skuPro = getSkuPro();
|
||||||
//if (skuDetails.containsKey(getSkuPro())) {
|
Log.i("IAB purchase SKU=" + skuPro);
|
||||||
// Log.i("IAB purchase SKU=" + skuDetails.get(getSkuPro()));
|
/*
|
||||||
// flowParams.setSkuDetails(skuDetails.get(getSkuPro()));
|
SkuDetailsParams.Builder builder = SkuDetailsParams.newBuilder();
|
||||||
//}
|
builder.setSkusList(Arrays.asList(skuPro));
|
||||||
|
builder.setType(BillingClient.SkuType.INAPP);
|
||||||
|
billingClient.querySkuDetailsAsync(builder.build(),
|
||||||
|
new SkuDetailsResponseListener() {
|
||||||
|
@Override
|
||||||
|
public void onSkuDetailsResponse(@NonNull BillingResult r, List<SkuDetails> skuDetailsList) {
|
||||||
|
if (r.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
||||||
|
if (skuDetailsList.size() == 0)
|
||||||
|
reportError(null, "Unknown SKU=" + skuPro);
|
||||||
|
else {
|
||||||
|
SkuDetails skuDetails = skuDetailsList.get(0);
|
||||||
|
Log.i("IAB purchase details=" + skuDetails);
|
||||||
|
|
||||||
//BillingResult result = billingClient.launchBillingFlow(this, flowParams.build());
|
BillingFlowParams.Builder flowParams = BillingFlowParams.newBuilder();
|
||||||
//if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
|
flowParams.setSkuDetails(skuDetails);
|
||||||
// reportError(result, "IAB launch billing flow");
|
|
||||||
|
BillingResult result = billingClient.launchBillingFlow(ActivityBilling.this, flowParams.build());
|
||||||
|
if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
|
||||||
|
reportError(result, "IAB launch billing flow");
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
reportError(r, "IAB query SKUs");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
*/
|
||||||
} else
|
} else
|
||||||
try {
|
try {
|
||||||
Uri uri = Uri.parse(BuildConfig.PRO_FEATURES_URI +
|
Uri uri = Uri.parse(BuildConfig.PRO_FEATURES_URI +
|
||||||
|
@ -238,21 +266,13 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
private void onPurchaseCheck(Intent intent) {
|
private void onPurchaseConsume(Intent intent) {
|
||||||
billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP, new PurchaseHistoryResponseListener() {
|
Purchase.PurchasesResult result = billingClient.queryPurchases(BillingClient.SkuType.INAPP);
|
||||||
@Override
|
|
||||||
public void onPurchaseHistoryResponse(BillingResult result, List<PurchaseHistoryRecord> records) {
|
|
||||||
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
||||||
for (PurchaseHistoryRecord record : records)
|
for (Purchase purchase : result.getPurchasesList())
|
||||||
Log.i("IAB history=" + record.toString());
|
consumePurchase(purchase);
|
||||||
|
|
||||||
queryPurchases();
|
|
||||||
|
|
||||||
ToastEx.makeText(ActivityBilling.this, R.string.title_setup_done, Toast.LENGTH_LONG).show();
|
|
||||||
} else
|
} else
|
||||||
reportError(result, "IAB history");
|
reportError(result.getBillingResult(), "IAB onPurchaseConsume");
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPurchaseError(Intent intent) {
|
private void onPurchaseError(Intent intent) {
|
||||||
|
@ -291,11 +311,17 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
private void retry(int backoff) {
|
private void retry(int backoff) {
|
||||||
Log.i("IAB connect retry in " + backoff + " s");
|
Log.i("IAB connect retry in " + backoff + " s");
|
||||||
|
|
||||||
new Handler().postDelayed(new Runnable() {
|
getMainHandler().postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (!billingClient.isReady())
|
try {
|
||||||
|
boolean ready = billingClient.isReady();
|
||||||
|
Log.i("IAB ready=" + ready);
|
||||||
|
if (!ready)
|
||||||
billingClient.startConnection(billingClientStateListener);
|
billingClient.startConnection(billingClientStateListener);
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, backoff * 1000L);
|
}, backoff * 1000L);
|
||||||
}
|
}
|
||||||
|
@ -325,7 +351,7 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
|
|
||||||
void onPurchasePending(String sku);
|
void onPurchasePending(String sku);
|
||||||
|
|
||||||
void onPurchased(String sku);
|
void onPurchased(String sku, boolean purchased);
|
||||||
|
|
||||||
void onError(String message);
|
void onError(String message);
|
||||||
}
|
}
|
||||||
|
@ -379,14 +405,9 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
" time=" + new Date(time));
|
" time=" + new Date(time));
|
||||||
Log.i("IAB json=" + purchase.getOriginalJson());
|
Log.i("IAB json=" + purchase.getOriginalJson());
|
||||||
|
|
||||||
//if (new Date().getTime() - purchase.getPurchaseTime() > 3 * 60 * 1000L) {
|
|
||||||
// consumePurchase(purchase);
|
|
||||||
// continue;
|
|
||||||
//}
|
|
||||||
|
|
||||||
for (IBillingListener listener : listeners)
|
for (IBillingListener listener : listeners)
|
||||||
if (isPurchaseValid(purchase))
|
if (isPurchaseValid(purchase))
|
||||||
listener.onPurchased(purchase.getSku());
|
listener.onPurchased(purchase.getSku(), true);
|
||||||
else
|
else
|
||||||
listener.onPurchasePending(purchase.getSku());
|
listener.onPurchasePending(purchase.getSku());
|
||||||
|
|
||||||
|
@ -397,7 +418,8 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
Signature sig = Signature.getInstance("SHA1withRSA");
|
Signature sig = Signature.getInstance("SHA1withRSA");
|
||||||
sig.initVerify(publicKey);
|
sig.initVerify(publicKey);
|
||||||
sig.update(purchase.getOriginalJson().getBytes());
|
sig.update(purchase.getOriginalJson().getBytes());
|
||||||
if (sig.verify(Base64.decode(purchase.getSignature(), Base64.DEFAULT))) {
|
if (SKU_TEST.equals(purchase.getSku()) ||
|
||||||
|
sig.verify(Base64.decode(purchase.getSignature(), Base64.DEFAULT))) {
|
||||||
Log.i("IAB valid signature");
|
Log.i("IAB valid signature");
|
||||||
if (getSkuPro().equals(purchase.getSku())) {
|
if (getSkuPro().equals(purchase.getSku())) {
|
||||||
if (isPurchaseValid(purchase)) {
|
if (isPurchaseValid(purchase)) {
|
||||||
|
@ -435,11 +457,10 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
billingClient.querySkuDetailsAsync(builder.build(),
|
billingClient.querySkuDetailsAsync(builder.build(),
|
||||||
new SkuDetailsResponseListener() {
|
new SkuDetailsResponseListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onSkuDetailsResponse(BillingResult result, List<SkuDetails> skuDetailsList) {
|
public void onSkuDetailsResponse(@NonNull BillingResult result, List<SkuDetails> skuDetailsList) {
|
||||||
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
||||||
for (SkuDetails skuDetail : skuDetailsList) {
|
for (SkuDetails skuDetail : skuDetailsList) {
|
||||||
Log.i("IAB SKU detail=" + skuDetail);
|
Log.i("IAB SKU detail=" + skuDetail);
|
||||||
skuDetails.put(skuDetail.getSku(), skuDetail);
|
|
||||||
for (IBillingListener listener : listeners)
|
for (IBillingListener listener : listeners)
|
||||||
listener.onSkuDetails(skuDetail.getSku(), skuDetail.getPrice());
|
listener.onSkuDetails(skuDetail.getSku(), skuDetail.getPrice());
|
||||||
}
|
}
|
||||||
|
@ -450,15 +471,18 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
}
|
}
|
||||||
|
|
||||||
private void consumePurchase(final Purchase purchase) {
|
private void consumePurchase(final Purchase purchase) {
|
||||||
Log.i("IAB SKU=" + purchase.getSku() + " consuming");
|
Log.i("IAB consuming SKU=" + purchase.getSku());
|
||||||
ConsumeParams params = ConsumeParams.newBuilder()
|
ConsumeParams params = ConsumeParams.newBuilder()
|
||||||
.setPurchaseToken(purchase.getPurchaseToken())
|
.setPurchaseToken(purchase.getPurchaseToken())
|
||||||
.build();
|
.build();
|
||||||
billingClient.consumeAsync(params, new ConsumeResponseListener() {
|
billingClient.consumeAsync(params, new ConsumeResponseListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onConsumeResponse(BillingResult result, String purchaseToken) {
|
public void onConsumeResponse(@NonNull BillingResult result, @NonNull String purchaseToken) {
|
||||||
if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
||||||
reportError(result, "IAB consumed SKU=" + purchase.getSku());
|
for (IBillingListener listener : listeners)
|
||||||
|
listener.onPurchased(purchase.getSku(), false);
|
||||||
|
} else
|
||||||
|
reportError(result, "IAB consuming SKU=" + purchase.getSku());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -471,7 +495,7 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
.build();
|
.build();
|
||||||
billingClient.acknowledgePurchase(params, new AcknowledgePurchaseResponseListener() {
|
billingClient.acknowledgePurchase(params, new AcknowledgePurchaseResponseListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onAcknowledgePurchaseResponse(BillingResult result) {
|
public void onAcknowledgePurchaseResponse(@NonNull BillingResult result) {
|
||||||
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityBilling.this);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityBilling.this);
|
||||||
SharedPreferences.Editor editor = prefs.edit();
|
SharedPreferences.Editor editor = prefs.edit();
|
||||||
|
@ -480,7 +504,7 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
editor.apply();
|
editor.apply();
|
||||||
|
|
||||||
for (IBillingListener listener : listeners)
|
for (IBillingListener listener : listeners)
|
||||||
listener.onPurchased(purchase.getSku());
|
listener.onPurchased(purchase.getSku(), true);
|
||||||
|
|
||||||
WidgetUnified.updateData(ActivityBilling.this);
|
WidgetUnified.updateData(ActivityBilling.this);
|
||||||
} else {
|
} else {
|
||||||
|
@ -505,6 +529,7 @@ public class ActivityBilling extends ActivityBase implements /*PurchasesUpdatedL
|
||||||
private boolean isPurchaseValid(Purchase purchase) {
|
private boolean isPurchaseValid(Purchase purchase) {
|
||||||
return (isPurchased(purchase) &&
|
return (isPurchased(purchase) &&
|
||||||
(purchase.isAcknowledged() ||
|
(purchase.isAcknowledged() ||
|
||||||
|
SKU_TEST.equals(purchase.getSku()) ||
|
||||||
purchase.getPurchaseTime() + MAX_SKU_NOACK_DURATION > new Date().getTime()));
|
purchase.getPurchaseTime() + MAX_SKU_NOACK_DURATION > new Date().getTime()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue