2018-09-04 18:51:09 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2018-10-29 08:09:56 +00:00
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-10-29 08:09:56 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-10-29 08:09:56 +00:00
|
|
|
|
2020-01-05 17:32:53 +00:00
|
|
|
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
2018-10-29 08:09:56 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2018-09-04 18:51:09 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.provider.Settings;
|
2018-12-16 14:08:50 +00:00
|
|
|
import android.util.Base64;
|
2019-06-19 08:15:47 +00:00
|
|
|
import android.widget.Toast;
|
2018-09-04 18:51:09 +00:00
|
|
|
|
2019-07-21 15:38:24 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-08-13 08:27:17 +00:00
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
import androidx.fragment.app.FragmentTransaction;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.lifecycle.Lifecycle;
|
2019-04-29 18:26:29 +00:00
|
|
|
import androidx.lifecycle.LifecycleObserver;
|
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
import com.android.billingclient.api.AcknowledgePurchaseParams;
|
|
|
|
import com.android.billingclient.api.AcknowledgePurchaseResponseListener;
|
2018-09-04 18:51:09 +00:00
|
|
|
import com.android.billingclient.api.BillingClient;
|
|
|
|
import com.android.billingclient.api.BillingClientStateListener;
|
|
|
|
import com.android.billingclient.api.BillingFlowParams;
|
2019-05-29 19:23:56 +00:00
|
|
|
import com.android.billingclient.api.BillingResult;
|
|
|
|
import com.android.billingclient.api.ConsumeParams;
|
|
|
|
import com.android.billingclient.api.ConsumeResponseListener;
|
2018-09-04 18:51:09 +00:00
|
|
|
import com.android.billingclient.api.Purchase;
|
2019-07-21 15:38:24 +00:00
|
|
|
import com.android.billingclient.api.PurchaseHistoryRecord;
|
|
|
|
import com.android.billingclient.api.PurchaseHistoryResponseListener;
|
2018-09-04 18:51:09 +00:00
|
|
|
import com.android.billingclient.api.PurchasesUpdatedListener;
|
2019-04-20 11:08:51 +00:00
|
|
|
import com.android.billingclient.api.SkuDetails;
|
|
|
|
import com.android.billingclient.api.SkuDetailsParams;
|
|
|
|
import com.android.billingclient.api.SkuDetailsResponseListener;
|
2018-09-04 18:51:09 +00:00
|
|
|
|
2018-12-16 14:08:50 +00:00
|
|
|
import java.security.KeyFactory;
|
2018-09-04 18:51:09 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
2018-12-16 14:08:50 +00:00
|
|
|
import java.security.PublicKey;
|
|
|
|
import java.security.Signature;
|
|
|
|
import java.security.spec.X509EncodedKeySpec;
|
2019-04-29 18:26:29 +00:00
|
|
|
import java.util.ArrayList;
|
2019-05-31 16:14:20 +00:00
|
|
|
import java.util.Date;
|
2019-04-20 11:08:51 +00:00
|
|
|
import java.util.HashMap;
|
2018-09-04 18:51:09 +00:00
|
|
|
import java.util.List;
|
2019-04-20 11:08:51 +00:00
|
|
|
import java.util.Map;
|
2018-09-04 18:51:09 +00:00
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
public class ActivityBilling extends ActivityBase implements PurchasesUpdatedListener, FragmentManager.OnBackStackChangedListener {
|
2018-09-04 18:51:09 +00:00
|
|
|
private BillingClient billingClient = null;
|
2019-04-20 11:08:51 +00:00
|
|
|
private Map<String, SkuDetails> skuDetails = new HashMap<>();
|
2019-04-29 18:26:29 +00:00
|
|
|
private List<IBillingListener> listeners = new ArrayList<>();
|
2018-09-04 18:51:09 +00:00
|
|
|
|
|
|
|
static final String ACTION_PURCHASE = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE";
|
2019-07-21 15:38:24 +00:00
|
|
|
static final String ACTION_PURCHASE_CHECK = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE_CHECK";
|
2018-09-04 18:51:09 +00:00
|
|
|
|
2019-12-21 20:34:18 +00:00
|
|
|
private final static long MAX_SKU_CACHE_DURATION = 24 * 3600 * 1000L; // milliseconds
|
|
|
|
private final static long MAX_SKU_NOACK_DURATION = 24 * 3600 * 1000L; // milliseconds
|
2019-08-03 15:37:13 +00:00
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
@Override
|
2019-08-13 08:27:17 +00:00
|
|
|
@SuppressLint("MissingSuperCall")
|
2018-09-04 18:51:09 +00:00
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2019-08-13 08:27:17 +00:00
|
|
|
onCreate(savedInstanceState, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState, boolean standalone) {
|
2018-09-04 18:51:09 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
if (standalone) {
|
|
|
|
setContentView(R.layout.activity_billing);
|
|
|
|
|
|
|
|
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
|
|
|
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
|
|
|
|
fragmentTransaction.commit();
|
|
|
|
|
2019-08-21 18:32:20 +00:00
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
getSupportFragmentManager().addOnBackStackChangedListener(this);
|
|
|
|
}
|
|
|
|
|
2019-09-10 07:05:21 +00:00
|
|
|
if (Helper.isPlayStoreInstall()) {
|
2019-04-29 18:26:29 +00:00
|
|
|
Log.i("IAB start");
|
2019-05-29 19:23:56 +00:00
|
|
|
billingClient = BillingClient.newBuilder(this)
|
|
|
|
.enablePendingPurchases()
|
|
|
|
.setListener(this)
|
|
|
|
.build();
|
2018-09-04 18:51:09 +00:00
|
|
|
billingClient.startConnection(billingClientStateListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
@Override
|
|
|
|
public void onBackStackChanged() {
|
2019-08-21 18:32:20 +00:00
|
|
|
if (getSupportFragmentManager().getBackStackEntryCount() == 0)
|
2019-08-13 08:27:17 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
|
|
|
|
IntentFilter iff = new IntentFilter();
|
|
|
|
iff.addAction(ACTION_PURCHASE);
|
2019-07-21 15:38:24 +00:00
|
|
|
iff.addAction(ACTION_PURCHASE_CHECK);
|
2018-09-04 18:51:09 +00:00
|
|
|
lbm.registerReceiver(receiver, iff);
|
|
|
|
|
|
|
|
if (billingClient != null && billingClient.isReady())
|
|
|
|
queryPurchases();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
2019-08-13 08:27:17 +00:00
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
|
|
|
|
lbm.unregisterReceiver(receiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
if (billingClient != null)
|
|
|
|
billingClient.endConnection();
|
2019-08-13 08:27:17 +00:00
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2019-07-21 15:38:24 +00:00
|
|
|
@NonNull
|
2019-05-29 19:23:56 +00:00
|
|
|
static String getSkuPro() {
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
return "android.test.purchased";
|
|
|
|
else
|
|
|
|
return BuildConfig.APPLICATION_ID + ".pro";
|
|
|
|
}
|
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
private static String getChallenge(Context context) throws NoSuchAlgorithmException {
|
|
|
|
String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
|
|
|
return Helper.sha256(android_id);
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
private static String getResponse(Context context) throws NoSuchAlgorithmException {
|
|
|
|
return Helper.sha256(BuildConfig.APPLICATION_ID + getChallenge(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean activatePro(Context context, Uri data) throws NoSuchAlgorithmException {
|
|
|
|
String challenge = getChallenge(context);
|
|
|
|
String response = data.getQueryParameter("response");
|
|
|
|
Log.i("IAB challenge=" + challenge);
|
|
|
|
Log.i("IAB response=" + response);
|
|
|
|
String expected = getResponse(context);
|
|
|
|
if (expected.equals(response)) {
|
|
|
|
Log.i("IAB response valid");
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
prefs.edit()
|
|
|
|
.putBoolean("pro", true)
|
|
|
|
.putBoolean("play_store", false)
|
|
|
|
.apply();
|
|
|
|
|
2020-01-01 19:52:23 +00:00
|
|
|
WidgetUnified.updateData(context);
|
2019-08-13 08:27:17 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Log.i("IAB response invalid");
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
static boolean isPro(Context context) {
|
2019-12-07 16:02:42 +00:00
|
|
|
if (BuildConfig.DEBUG && false)
|
2019-08-13 08:27:17 +00:00
|
|
|
return true;
|
|
|
|
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("pro", false);
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 10:15:25 +00:00
|
|
|
private BroadcastReceiver receiver = new BroadcastReceiver() {
|
2018-09-04 18:51:09 +00:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2019-07-27 06:02:00 +00:00
|
|
|
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
|
2019-06-19 09:51:15 +00:00
|
|
|
if (ACTION_PURCHASE.equals(intent.getAction()))
|
|
|
|
onPurchase(intent);
|
2019-07-21 15:38:24 +00:00
|
|
|
else if (ACTION_PURCHASE_CHECK.equals(intent.getAction()))
|
|
|
|
onPurchaseCheck(intent);
|
2019-06-19 09:51:15 +00:00
|
|
|
}
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void onPurchase(Intent intent) {
|
2019-09-10 07:05:21 +00:00
|
|
|
if (Helper.isPlayStoreInstall()) {
|
2019-04-20 11:08:51 +00:00
|
|
|
BillingFlowParams.Builder flowParams = BillingFlowParams.newBuilder();
|
2019-05-29 19:23:56 +00:00
|
|
|
if (skuDetails.containsKey(getSkuPro())) {
|
|
|
|
Log.i("IAB purchase SKU=" + skuDetails.get(getSkuPro()));
|
|
|
|
flowParams.setSkuDetails(skuDetails.get(getSkuPro()));
|
2019-04-20 11:08:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
BillingResult result = billingClient.launchBillingFlow(this, flowParams.build());
|
|
|
|
if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
|
2019-09-25 10:09:56 +00:00
|
|
|
reportError(result, "IAB launch billing flow");
|
2018-09-04 18:51:09 +00:00
|
|
|
} else
|
2019-08-13 08:27:17 +00:00
|
|
|
try {
|
2020-02-06 21:45:35 +00:00
|
|
|
Uri uri = Uri.parse(BuildConfig.PRO_FEATURES_URI + "?challenge=" + getChallenge(this));
|
|
|
|
Helper.view(this, uri, true);
|
2019-08-13 08:27:17 +00:00
|
|
|
} catch (NoSuchAlgorithmException ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
Log.unexpectedError(getSupportFragmentManager(), ex);
|
2019-08-13 08:27:17 +00:00
|
|
|
}
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 15:38:24 +00:00
|
|
|
private void onPurchaseCheck(Intent intent) {
|
|
|
|
billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP, new PurchaseHistoryResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onPurchaseHistoryResponse(BillingResult result, List<PurchaseHistoryRecord> records) {
|
|
|
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
|
|
|
for (PurchaseHistoryRecord record : records)
|
|
|
|
Log.i("IAB history=" + record.toString());
|
|
|
|
|
|
|
|
queryPurchases();
|
|
|
|
|
|
|
|
ToastEx.makeText(ActivityBilling.this, R.string.title_setup_done, Toast.LENGTH_LONG).show();
|
|
|
|
} else
|
2019-09-25 10:09:56 +00:00
|
|
|
reportError(result, "IAB history");
|
2019-07-21 15:38:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
private BillingClientStateListener billingClientStateListener = new BillingClientStateListener() {
|
|
|
|
private int backoff = 4; // seconds
|
|
|
|
|
|
|
|
@Override
|
2019-05-29 19:23:56 +00:00
|
|
|
public void onBillingSetupFinished(BillingResult result) {
|
|
|
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
2019-07-21 15:38:24 +00:00
|
|
|
for (IBillingListener listener : listeners)
|
|
|
|
listener.onConnected();
|
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
backoff = 4;
|
|
|
|
queryPurchases();
|
|
|
|
} else
|
2019-09-25 10:09:56 +00:00
|
|
|
reportError(result, "IAB connected");
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBillingServiceDisconnected() {
|
2019-07-21 15:38:24 +00:00
|
|
|
for (IBillingListener listener : listeners)
|
|
|
|
listener.onDisconnected();
|
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
backoff *= 2;
|
2019-09-25 10:09:56 +00:00
|
|
|
retry(backoff);
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-25 10:09:56 +00:00
|
|
|
private void retry(int backoff) {
|
|
|
|
Log.i("IAB connect retry in " + backoff + " s");
|
|
|
|
|
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!billingClient.isReady())
|
|
|
|
billingClient.startConnection(billingClientStateListener);
|
|
|
|
}
|
|
|
|
}, backoff * 1000L);
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
@Override
|
2019-05-29 19:23:56 +00:00
|
|
|
public void onPurchasesUpdated(BillingResult result, @Nullable List<Purchase> purchases) {
|
|
|
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
|
2018-09-04 18:51:09 +00:00
|
|
|
checkPurchases(purchases);
|
|
|
|
else
|
2019-09-25 10:09:56 +00:00
|
|
|
reportError(result, "IAB purchases updated");
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void queryPurchases() {
|
|
|
|
Purchase.PurchasesResult result = billingClient.queryPurchases(BillingClient.SkuType.INAPP);
|
2019-05-29 19:23:56 +00:00
|
|
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
|
2018-09-04 18:51:09 +00:00
|
|
|
checkPurchases(result.getPurchasesList());
|
|
|
|
else
|
2019-09-25 10:09:56 +00:00
|
|
|
reportError(result.getBillingResult(), "IAB query purchases");
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 18:26:29 +00:00
|
|
|
interface IBillingListener {
|
2019-07-21 15:38:24 +00:00
|
|
|
void onConnected();
|
|
|
|
|
|
|
|
void onDisconnected();
|
|
|
|
|
2019-04-29 18:26:29 +00:00
|
|
|
void onSkuDetails(String sku, String price);
|
2019-05-29 19:23:56 +00:00
|
|
|
|
|
|
|
void onPurchasePending(String sku);
|
|
|
|
|
|
|
|
void onPurchased(String sku);
|
2019-07-02 06:50:16 +00:00
|
|
|
|
|
|
|
void onError(String message);
|
2019-04-29 18:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void addBillingListener(final IBillingListener listener, LifecycleOwner owner) {
|
2019-07-02 06:50:16 +00:00
|
|
|
Log.i("IAB adding billing listener=" + listener);
|
2019-04-29 18:26:29 +00:00
|
|
|
listeners.add(listener);
|
|
|
|
|
2019-07-02 06:50:16 +00:00
|
|
|
if (billingClient != null)
|
2019-07-21 15:38:24 +00:00
|
|
|
if (billingClient.isReady()) {
|
|
|
|
listener.onConnected();
|
2019-07-02 06:50:16 +00:00
|
|
|
queryPurchases();
|
2019-08-13 08:27:17 +00:00
|
|
|
} else
|
2019-07-21 15:38:24 +00:00
|
|
|
listener.onDisconnected();
|
2019-04-29 18:26:29 +00:00
|
|
|
|
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
public void onDestroyed() {
|
2019-07-02 06:50:16 +00:00
|
|
|
Log.i("IAB removing billing listener=" + listener);
|
2019-04-29 18:26:29 +00:00
|
|
|
listeners.remove(listener);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
private void checkPurchases(List<Purchase> purchases) {
|
2019-07-16 12:24:32 +00:00
|
|
|
Log.i("IAB purchases=" + (purchases == null ? null : purchases.size()));
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
List<String> query = new ArrayList<>();
|
|
|
|
query.add(getSkuPro());
|
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
if (purchases != null) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
2019-08-03 15:37:13 +00:00
|
|
|
if (prefs.getBoolean("play_store", true)) {
|
|
|
|
long cached = prefs.getLong(getSkuPro() + ".cached", 0);
|
|
|
|
if (cached + MAX_SKU_CACHE_DURATION < new Date().getTime()) {
|
2019-08-05 15:48:07 +00:00
|
|
|
Log.i("IAB cache expired=" + new Date(cached));
|
2019-08-03 15:37:13 +00:00
|
|
|
editor.remove("pro");
|
|
|
|
} else
|
2019-08-05 15:48:07 +00:00
|
|
|
Log.i("IAB caching until=" + new Date(cached + MAX_SKU_CACHE_DURATION));
|
2019-08-03 15:37:13 +00:00
|
|
|
}
|
2018-12-16 14:08:50 +00:00
|
|
|
|
|
|
|
for (Purchase purchase : purchases)
|
|
|
|
try {
|
2019-05-29 19:23:56 +00:00
|
|
|
query.remove(purchase.getSku());
|
2019-07-21 15:38:24 +00:00
|
|
|
|
2019-05-31 16:14:20 +00:00
|
|
|
long time = purchase.getPurchaseTime();
|
2019-12-21 20:34:18 +00:00
|
|
|
Log.i("IAB SKU=" + purchase.getSku() +
|
|
|
|
" purchased=" + isPurchased(purchase) +
|
|
|
|
" valid=" + isPurchaseValid(purchase) +
|
|
|
|
" time=" + new Date(time));
|
2019-05-29 19:23:56 +00:00
|
|
|
|
|
|
|
//if (new Date().getTime() - purchase.getPurchaseTime() > 3 * 60 * 1000L) {
|
|
|
|
// consumePurchase(purchase);
|
|
|
|
// continue;
|
|
|
|
//}
|
|
|
|
|
|
|
|
for (IBillingListener listener : listeners)
|
2019-12-21 20:34:18 +00:00
|
|
|
if (isPurchaseValid(purchase))
|
2019-05-29 19:23:56 +00:00
|
|
|
listener.onPurchased(purchase.getSku());
|
|
|
|
else
|
|
|
|
listener.onPurchasePending(purchase.getSku());
|
|
|
|
|
2019-12-21 20:34:18 +00:00
|
|
|
if (isPurchased(purchase)) {
|
2019-07-21 15:38:24 +00:00
|
|
|
byte[] decodedKey = Base64.decode(getString(R.string.public_key), Base64.DEFAULT);
|
|
|
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
|
|
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
|
|
|
|
Signature sig = Signature.getInstance("SHA1withRSA");
|
|
|
|
sig.initVerify(publicKey);
|
|
|
|
sig.update(purchase.getOriginalJson().getBytes());
|
|
|
|
if (sig.verify(Base64.decode(purchase.getSignature(), Base64.DEFAULT))) {
|
2019-12-21 20:34:18 +00:00
|
|
|
Log.i("IAB valid signature");
|
2019-07-21 15:38:24 +00:00
|
|
|
if (getSkuPro().equals(purchase.getSku())) {
|
2019-12-21 20:34:18 +00:00
|
|
|
if (isPurchaseValid(purchase)) {
|
2019-07-21 15:38:24 +00:00
|
|
|
editor.putBoolean("pro", true);
|
2019-12-21 20:34:18 +00:00
|
|
|
editor.putLong(purchase.getSku() + ".cached", new Date().getTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!purchase.isAcknowledged())
|
2019-12-01 15:21:29 +00:00
|
|
|
acknowledgePurchase(purchase, 0);
|
2019-07-21 15:38:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Log.w("IAB invalid signature");
|
|
|
|
editor.putBoolean("pro", false);
|
2019-09-25 10:09:56 +00:00
|
|
|
reportError(null, "Invalid purchase");
|
2018-12-16 14:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
reportError(null, Log.formatThrowable(ex, false));
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
2018-12-16 14:08:50 +00:00
|
|
|
|
2018-09-04 18:51:09 +00:00
|
|
|
editor.apply();
|
2019-08-06 08:52:32 +00:00
|
|
|
|
2020-01-01 19:52:23 +00:00
|
|
|
WidgetUnified.updateData(this);
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|
2019-05-29 19:23:56 +00:00
|
|
|
|
|
|
|
if (query.size() > 0)
|
|
|
|
querySkus(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void querySkus(List<String> query) {
|
|
|
|
Log.i("IAB query SKUs");
|
|
|
|
SkuDetailsParams.Builder builder = SkuDetailsParams.newBuilder();
|
|
|
|
builder.setSkusList(query);
|
|
|
|
builder.setType(BillingClient.SkuType.INAPP);
|
|
|
|
billingClient.querySkuDetailsAsync(builder.build(),
|
|
|
|
new SkuDetailsResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onSkuDetailsResponse(BillingResult result, List<SkuDetails> skuDetailsList) {
|
|
|
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
|
|
|
for (SkuDetails skuDetail : skuDetailsList) {
|
|
|
|
Log.i("IAB SKU detail=" + skuDetail);
|
|
|
|
skuDetails.put(skuDetail.getSku(), skuDetail);
|
|
|
|
for (IBillingListener listener : listeners)
|
|
|
|
listener.onSkuDetails(skuDetail.getSku(), skuDetail.getPrice());
|
|
|
|
}
|
2019-08-06 07:59:49 +00:00
|
|
|
} else
|
2019-09-25 10:09:56 +00:00
|
|
|
reportError(result, "IAB query SKUs");
|
2019-05-29 19:23:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void consumePurchase(final Purchase purchase) {
|
|
|
|
Log.i("IAB SKU=" + purchase.getSku() + " consuming");
|
|
|
|
ConsumeParams params = ConsumeParams.newBuilder()
|
|
|
|
.setPurchaseToken(purchase.getPurchaseToken())
|
|
|
|
.build();
|
|
|
|
billingClient.consumeAsync(params, new ConsumeResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onConsumeResponse(BillingResult result, String purchaseToken) {
|
2019-08-06 07:59:49 +00:00
|
|
|
if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
|
2019-09-25 10:09:56 +00:00
|
|
|
reportError(result, "IAB consumed SKU=" + purchase.getSku());
|
2019-05-29 19:23:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-01 15:21:29 +00:00
|
|
|
private void acknowledgePurchase(final Purchase purchase, int retry) {
|
2019-05-29 19:23:56 +00:00
|
|
|
Log.i("IAB acknowledging purchase SKU=" + purchase.getSku());
|
|
|
|
AcknowledgePurchaseParams params =
|
|
|
|
AcknowledgePurchaseParams.newBuilder()
|
|
|
|
.setPurchaseToken(purchase.getPurchaseToken())
|
|
|
|
.build();
|
|
|
|
billingClient.acknowledgePurchase(params, new AcknowledgePurchaseResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onAcknowledgePurchaseResponse(BillingResult result) {
|
|
|
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityBilling.this);
|
2019-08-03 15:37:13 +00:00
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
editor.putBoolean("pro", true);
|
2019-12-21 20:34:18 +00:00
|
|
|
editor.putLong(purchase.getSku() + ".cached", new Date().getTime());
|
2019-08-03 15:37:13 +00:00
|
|
|
editor.apply();
|
2019-06-02 05:57:29 +00:00
|
|
|
|
|
|
|
for (IBillingListener listener : listeners)
|
|
|
|
listener.onPurchased(purchase.getSku());
|
2019-08-06 08:52:32 +00:00
|
|
|
|
2020-01-01 19:52:23 +00:00
|
|
|
WidgetUnified.updateData(ActivityBilling.this);
|
2019-12-01 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
if (retry < 3) {
|
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
acknowledgePurchase(purchase, retry + 1);
|
|
|
|
}
|
|
|
|
}, (retry + 1) * 10 * 1000L);
|
|
|
|
} else
|
|
|
|
reportError(result, "IAB acknowledged SKU=" + purchase.getSku());
|
|
|
|
}
|
2019-05-29 19:23:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:34:18 +00:00
|
|
|
private boolean isPurchased(Purchase purchase) {
|
|
|
|
return (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isPurchaseValid(Purchase purchase) {
|
|
|
|
return (isPurchased(purchase) &&
|
|
|
|
(purchase.isAcknowledged() ||
|
|
|
|
purchase.getPurchaseTime() + MAX_SKU_NOACK_DURATION > new Date().getTime()));
|
|
|
|
}
|
|
|
|
|
2019-09-25 10:09:56 +00:00
|
|
|
private void reportError(BillingResult result, String stage) {
|
|
|
|
String message;
|
|
|
|
if (result == null)
|
|
|
|
message = stage;
|
|
|
|
else {
|
|
|
|
String debug = result.getDebugMessage();
|
|
|
|
message = getBillingResponseText(result) + (debug == null ? "" : " " + debug) + " " + stage;
|
|
|
|
|
|
|
|
// https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponse#service_disconnected
|
|
|
|
if (result.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_DISCONNECTED)
|
|
|
|
retry(60);
|
|
|
|
}
|
|
|
|
|
2019-12-01 15:12:53 +00:00
|
|
|
EntityLog.log(this, message);
|
2019-09-25 10:09:56 +00:00
|
|
|
|
2019-07-02 06:50:16 +00:00
|
|
|
for (IBillingListener listener : listeners)
|
|
|
|
listener.onError(message);
|
|
|
|
}
|
|
|
|
|
2019-09-25 10:09:56 +00:00
|
|
|
private static String getBillingResponseText(BillingResult result) {
|
2019-05-29 19:23:56 +00:00
|
|
|
switch (result.getResponseCode()) {
|
|
|
|
case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Billing API version is not supported for the type requested
|
|
|
|
return "BILLING_UNAVAILABLE";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Invalid arguments provided to the API.
|
|
|
|
return "DEVELOPER_ERROR";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.ERROR:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Fatal error during the API action
|
|
|
|
return "ERROR";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Requested feature is not supported by Play Store on the current device.
|
|
|
|
return "FEATURE_NOT_SUPPORTED";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Failure to purchase since item is already owned
|
|
|
|
return "ITEM_ALREADY_OWNED";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.ITEM_NOT_OWNED:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Failure to consume since item is not owned
|
|
|
|
return "ITEM_NOT_OWNED";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Requested product is not available for purchase
|
|
|
|
return "ITEM_UNAVAILABLE";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.OK:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Success
|
|
|
|
return "OK";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.SERVICE_DISCONNECTED:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Play Store service is not connected now - potentially transient state.
|
|
|
|
return "SERVICE_DISCONNECTED";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
|
2019-05-12 16:43:37 +00:00
|
|
|
// Network connection is down
|
|
|
|
return "SERVICE_UNAVAILABLE";
|
|
|
|
|
2019-09-25 10:09:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.SERVICE_TIMEOUT:
|
|
|
|
// The request has reached the maximum timeout before Google Play responds.
|
|
|
|
return "SERVICE_TIMEOUT";
|
|
|
|
|
2019-05-29 19:23:56 +00:00
|
|
|
case BillingClient.BillingResponseCode.USER_CANCELED:
|
2019-05-12 16:43:37 +00:00
|
|
|
// User pressed back or canceled a dialog
|
|
|
|
return "USER_CANCELED";
|
|
|
|
|
|
|
|
default:
|
2019-05-29 19:23:56 +00:00
|
|
|
return Integer.toString(result.getResponseCode());
|
2019-05-12 16:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 18:51:09 +00:00
|
|
|
}
|