FairEmail/app/src/main/java/eu/faircode/email/ActivityBilling.java

464 lines
19 KiB
Java
Raw Normal View History

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
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-10-29 08:09:56 +00:00
*/
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-01-10 09:37:13 +00:00
import android.view.View;
2018-09-04 18:51:09 +00:00
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;
2019-04-29 18:26:29 +00:00
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
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;
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;
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
import com.google.android.material.snackbar.Snackbar;
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-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
abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedListener {
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";
static final String ACTION_ACTIVATE_PRO = BuildConfig.APPLICATION_ID + ".ACTIVATE_PRO";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Helper.isPlayStoreInstall(this)) {
2019-04-29 18:26:29 +00:00
Log.i("IAB start");
billingClient = BillingClient.newBuilder(this)
.enablePendingPurchases()
.setListener(this)
.build();
2018-09-04 18:51:09 +00:00
billingClient.startConnection(billingClientStateListener);
}
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
IntentFilter iff = new IntentFilter();
iff.addAction(ACTION_PURCHASE);
iff.addAction(ACTION_ACTIVATE_PRO);
lbm.registerReceiver(receiver, iff);
if (billingClient != null && billingClient.isReady())
queryPurchases();
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.unregisterReceiver(receiver);
}
@Override
protected void onDestroy() {
if (billingClient != null)
billingClient.endConnection();
super.onDestroy();
}
static String getSkuPro() {
if (BuildConfig.DEBUG)
return "android.test.purchased";
else
return BuildConfig.APPLICATION_ID + ".pro";
}
2018-09-04 18:51:09 +00:00
protected Intent getIntentPro() {
if (Helper.isPlayStoreInstall(this))
return null;
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
2019-02-07 19:49:14 +00:00
intent.setData(Uri.parse(BuildConfig.PRO_FEATURES_URI + "?challenge=" + getChallenge()));
2018-09-04 18:51:09 +00:00
return intent;
} catch (NoSuchAlgorithmException ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-09-04 18:51:09 +00:00
return null;
}
}
private String getChallenge() throws NoSuchAlgorithmException {
String android_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
return Helper.sha256(android_id);
}
private String getResponse() throws NoSuchAlgorithmException {
return Helper.sha256(BuildConfig.APPLICATION_ID + getChallenge());
}
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) {
if (ACTION_PURCHASE.equals(intent.getAction()))
onPurchase(intent);
else if (ACTION_ACTIVATE_PRO.equals(intent.getAction()))
onActivatePro(intent);
}
};
private void onPurchase(Intent intent) {
if (Helper.isPlayStoreInstall(this)) {
2019-04-20 11:08:51 +00:00
BillingFlowParams.Builder flowParams = BillingFlowParams.newBuilder();
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
}
BillingResult result = billingClient.launchBillingFlow(this, flowParams.build());
String text = getBillingResponseText(result);
2018-12-24 12:27:45 +00:00
Log.i("IAB launch billing flow response=" + text);
if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
2018-12-07 17:44:20 +00:00
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
2018-09-04 18:51:09 +00:00
} else
Helper.view(this, this, getIntentPro());
2018-09-04 18:51:09 +00:00
}
private void onActivatePro(Intent intent) {
try {
Uri data = intent.getParcelableExtra("uri");
String challenge = getChallenge();
String response = data.getQueryParameter("response");
2018-12-24 12:27:45 +00:00
Log.i("Challenge=" + challenge);
Log.i("Response=" + response);
2018-09-04 18:51:09 +00:00
String expected = getResponse();
if (expected.equals(response)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2019-03-11 11:40:56 +00:00
prefs.edit()
.putBoolean("pro", true)
.putBoolean("play_store", false)
.apply();
2018-12-24 12:27:45 +00:00
Log.i("Response valid");
2019-01-10 09:37:13 +00:00
Snackbar snackbar = Snackbar.make(getVisibleView(), R.string.title_pro_valid, Snackbar.LENGTH_LONG);
snackbar.setAction(R.string.title_check, new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
});
snackbar.show();
2018-09-04 18:51:09 +00:00
} else {
2018-12-24 12:27:45 +00:00
Log.i("Response invalid");
2018-12-07 17:44:20 +00:00
Snackbar.make(getVisibleView(), R.string.title_pro_invalid, Snackbar.LENGTH_LONG).show();
2018-09-04 18:51:09 +00:00
}
} catch (NoSuchAlgorithmException ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
Helper.unexpectedError(this, this, ex);
2018-09-04 18:51:09 +00:00
}
}
private BillingClientStateListener billingClientStateListener = new BillingClientStateListener() {
private int backoff = 4; // seconds
@Override
public void onBillingSetupFinished(BillingResult result) {
String text = getBillingResponseText(result);
2018-12-24 12:27:45 +00:00
Log.i("IAB connected response=" + text);
2018-12-22 08:16:12 +00:00
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
return;
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
2018-09-04 18:51:09 +00:00
backoff = 4;
queryPurchases();
} else
2018-12-07 17:44:20 +00:00
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
2018-09-04 18:51:09 +00:00
}
@Override
public void onBillingServiceDisconnected() {
backoff *= 2;
2018-12-24 12:27:45 +00:00
Log.i("IAB disconnected retry in " + backoff + " s");
2018-09-04 18:51:09 +00:00
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
2018-12-22 08:16:12 +00:00
if (!billingClient.isReady())
2018-09-04 18:51:09 +00:00
billingClient.startConnection(billingClientStateListener);
}
}, backoff * 1000L);
}
};
@Override
public void onPurchasesUpdated(BillingResult result, @Nullable List<Purchase> purchases) {
String text = getBillingResponseText(result);
2018-12-24 12:27:45 +00:00
Log.i("IAB purchases updated response=" + text);
2019-01-22 10:19:49 +00:00
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
return;
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
2018-09-04 18:51:09 +00:00
checkPurchases(purchases);
else
2018-12-07 17:44:20 +00:00
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
2018-09-04 18:51:09 +00:00
}
private void queryPurchases() {
Purchase.PurchasesResult result = billingClient.queryPurchases(BillingClient.SkuType.INAPP);
String text = getBillingResponseText(result.getBillingResult());
2018-12-24 12:27:45 +00:00
Log.i("IAB query purchases response=" + text);
2019-01-22 10:19:49 +00:00
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
2018-09-04 18:51:09 +00:00
checkPurchases(result.getPurchasesList());
else
2018-12-07 17:44:20 +00:00
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
2018-09-04 18:51:09 +00:00
}
2019-04-29 18:26:29 +00:00
interface IBillingListener {
void onSkuDetails(String sku, String price);
void onPurchasePending(String sku);
void onPurchased(String sku);
2019-04-29 18:26:29 +00:00
}
void addBillingListener(final IBillingListener listener, LifecycleOwner owner) {
Log.i("Adding billing listener=" + listener);
listeners.add(listener);
if (billingClient != null && billingClient.isReady())
queryPurchases();
2019-04-29 18:26:29 +00:00
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
Log.i("Removing billing listener=" + listener);
listeners.remove(listener);
}
});
}
2018-09-04 18:51:09 +00:00
private void checkPurchases(List<Purchase> purchases) {
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-03-11 11:40:56 +00:00
if (prefs.getBoolean("play_store", true))
editor.remove("pro");
2018-12-16 14:08:50 +00:00
for (Purchase purchase : purchases)
try {
query.remove(purchase.getSku());
boolean purchased = (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED);
Log.i("IAB SKU=" + purchase.getSku() + " purchased=" + purchased);
//if (new Date().getTime() - purchase.getPurchaseTime() > 3 * 60 * 1000L) {
// consumePurchase(purchase);
// continue;
//}
for (IBillingListener listener : listeners)
if (purchased)
listener.onPurchased(purchase.getSku());
else
listener.onPurchasePending(purchase.getSku());
if (!purchased)
continue;
2018-12-16 14:08:50 +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))) {
if (getSkuPro().equals(purchase.getSku())) {
if (purchase.isAcknowledged()) {
editor.putBoolean("pro", true);
Log.i("IAB pro features activated");
} else
acknowledgePurchase(purchase);
2018-12-16 14:08:50 +00:00
}
2018-12-16 14:08:50 +00:00
} else {
2018-12-24 12:27:45 +00:00
Log.w("Invalid signature");
2018-12-16 14:08:50 +00:00
Snackbar.make(getVisibleView(), R.string.title_pro_invalid, Snackbar.LENGTH_LONG).show();
}
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-12-16 14:08:50 +00:00
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
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();
}
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) {
String text = getBillingResponseText(result);
Log.i("IAB query SKUs response=" + text);
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());
}
}
}
});
}
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) {
String text = getBillingResponseText(result);
Log.i("IAB SKU=" + purchase.getSku() + " consumed response=" + text);
}
});
}
private void acknowledgePurchase(final Purchase purchase) {
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) {
String text = getBillingResponseText(result);
Log.i("IAB acknowledged SKU=" + purchase.getSku() + " response=" + text);
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityBilling.this);
prefs.edit().putBoolean("pro", true).apply();
}
}
});
}
private static String getBillingResponseText(BillingResult result) {
String debug = result.getDebugMessage();
return _getBillingResponseText(result) + (debug == null ? "" : " " + debug);
2018-09-04 18:51:09 +00:00
}
2019-05-12 16:43:37 +00:00
private static String _getBillingResponseText(BillingResult result) {
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";
case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
2019-05-12 16:43:37 +00:00
// Invalid arguments provided to the API.
return "DEVELOPER_ERROR";
case BillingClient.BillingResponseCode.ERROR:
2019-05-12 16:43:37 +00:00
// Fatal error during the API action
return "ERROR";
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";
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";
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";
case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
2019-05-12 16:43:37 +00:00
// Requested product is not available for purchase
return "ITEM_UNAVAILABLE";
case BillingClient.BillingResponseCode.OK:
2019-05-12 16:43:37 +00:00
// Success
return "OK";
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";
case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
2019-05-12 16:43:37 +00:00
// Network connection is down
return "SERVICE_UNAVAILABLE";
case BillingClient.BillingResponseCode.USER_CANCELED:
2019-05-12 16:43:37 +00:00
// User pressed back or canceled a dialog
return "USER_CANCELED";
default:
return Integer.toString(result.getResponseCode());
2019-05-12 16:43:37 +00:00
}
}
2018-09-04 18:51:09 +00:00
}