Added purchase check

This commit is contained in:
M66B 2019-07-21 17:38:24 +02:00
parent cb6a239716
commit 5d3fb91deb
3 changed files with 100 additions and 21 deletions

View File

@ -31,6 +31,7 @@ import android.provider.Settings;
import android.util.Base64; import android.util.Base64;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.LifecycleObserver;
@ -48,6 +49,8 @@ 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;
@ -70,6 +73,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
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_ACTIVATE_PRO = BuildConfig.APPLICATION_ID + ".ACTIVATE_PRO"; static final String ACTION_ACTIVATE_PRO = BuildConfig.APPLICATION_ID + ".ACTIVATE_PRO";
@Override @Override
@ -93,6 +97,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
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_ACTIVATE_PRO); iff.addAction(ACTION_ACTIVATE_PRO);
lbm.registerReceiver(receiver, iff); lbm.registerReceiver(receiver, iff);
@ -114,6 +119,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
super.onDestroy(); super.onDestroy();
} }
@NonNull
static String getSkuPro() { static String getSkuPro() {
if (BuildConfig.DEBUG) if (BuildConfig.DEBUG)
return "android.test.purchased"; return "android.test.purchased";
@ -150,6 +156,8 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) { if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
if (ACTION_PURCHASE.equals(intent.getAction())) if (ACTION_PURCHASE.equals(intent.getAction()))
onPurchase(intent); onPurchase(intent);
else if (ACTION_PURCHASE_CHECK.equals(intent.getAction()))
onPurchaseCheck(intent);
else if (ACTION_ACTIVATE_PRO.equals(intent.getAction())) else if (ACTION_ACTIVATE_PRO.equals(intent.getAction()))
onActivatePro(intent); onActivatePro(intent);
} }
@ -173,6 +181,26 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
Helper.view(this, getIntentPro()); Helper.view(this, getIntentPro());
} }
private void onPurchaseCheck(Intent intent) {
billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP, new PurchaseHistoryResponseListener() {
@Override
public void onPurchaseHistoryResponse(BillingResult result, List<PurchaseHistoryRecord> records) {
String text = getBillingResponseText(result);
Log.i("IAB history response=" + text);
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
notifyError(text);
}
});
}
private void onActivatePro(Intent intent) { private void onActivatePro(Intent intent) {
try { try {
Uri data = intent.getParcelableExtra("uri"); Uri data = intent.getParcelableExtra("uri");
@ -208,6 +236,9 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
Log.i("IAB connected response=" + text); Log.i("IAB connected response=" + text);
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) { if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
for (IBillingListener listener : listeners)
listener.onConnected();
backoff = 4; backoff = 4;
queryPurchases(); queryPurchases();
} else } else
@ -216,8 +247,12 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
@Override @Override
public void onBillingServiceDisconnected() { public void onBillingServiceDisconnected() {
for (IBillingListener listener : listeners)
listener.onDisconnected();
backoff *= 2; backoff *= 2;
Log.i("IAB disconnected retry in " + backoff + " s"); Log.i("IAB disconnected retry in " + backoff + " s");
new Handler().postDelayed(new Runnable() { new Handler().postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -251,6 +286,10 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
} }
interface IBillingListener { interface IBillingListener {
void onConnected();
void onDisconnected();
void onSkuDetails(String sku, String price); void onSkuDetails(String sku, String price);
void onPurchasePending(String sku); void onPurchasePending(String sku);
@ -265,10 +304,13 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
listeners.add(listener); listeners.add(listener);
if (billingClient != null) if (billingClient != null)
if (billingClient.isReady()) if (billingClient.isReady()) {
listener.onConnected();
queryPurchases(); queryPurchases();
else } else {
listener.onDisconnected();
billingClient.startConnection(billingClientStateListener); billingClient.startConnection(billingClientStateListener);
}
owner.getLifecycle().addObserver(new LifecycleObserver() { owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
@ -294,6 +336,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
for (Purchase purchase : purchases) for (Purchase purchase : purchases)
try { try {
query.remove(purchase.getSku()); query.remove(purchase.getSku());
boolean purchased = (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED); boolean purchased = (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED);
long time = purchase.getPurchaseTime(); long time = purchase.getPurchaseTime();
Log.i("IAB SKU=" + purchase.getSku() + " purchased=" + purchased + " time=" + new Date(time)); Log.i("IAB SKU=" + purchase.getSku() + " purchased=" + purchased + " time=" + new Date(time));
@ -312,25 +355,27 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
if (!purchased) if (!purchased)
continue; continue;
byte[] decodedKey = Base64.decode(getString(R.string.public_key), Base64.DEFAULT); if (getSkuPro().equals(purchase.getSku())) {
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] decodedKey = Base64.decode(getString(R.string.public_key), Base64.DEFAULT);
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey)); KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Signature sig = Signature.getInstance("SHA1withRSA"); PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
sig.initVerify(publicKey); Signature sig = Signature.getInstance("SHA1withRSA");
sig.update(purchase.getOriginalJson().getBytes()); sig.initVerify(publicKey);
if (sig.verify(Base64.decode(purchase.getSignature(), Base64.DEFAULT))) { sig.update(purchase.getOriginalJson().getBytes());
if (getSkuPro().equals(purchase.getSku())) { if (sig.verify(Base64.decode(purchase.getSignature(), Base64.DEFAULT))) {
if (purchase.isAcknowledged()) { if (getSkuPro().equals(purchase.getSku())) {
Log.i("IAB valid signature"); if (purchase.isAcknowledged()) {
editor.putBoolean("pro", true); Log.i("IAB valid signature");
} else editor.putBoolean("pro", true);
acknowledgePurchase(purchase); } else
} acknowledgePurchase(purchase);
}
} else { } else {
Log.w("IAB invalid signature"); Log.w("IAB invalid signature");
editor.putBoolean("pro", false); editor.putBoolean("pro", false);
notifyError("Invalid purchase"); notifyError("Invalid purchase");
}
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);

View File

@ -45,6 +45,7 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
private Button btnPurchase; private Button btnPurchase;
private TextView tvPrice; private TextView tvPrice;
private TextView tvPriceHint; private TextView tvPriceHint;
private Button btnCheck;
@Override @Override
@Nullable @Nullable
@ -59,6 +60,7 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
btnPurchase = view.findViewById(R.id.btnPurchase); btnPurchase = view.findViewById(R.id.btnPurchase);
tvPrice = view.findViewById(R.id.tvPrice); tvPrice = view.findViewById(R.id.tvPrice);
tvPriceHint = view.findViewById(R.id.tvPriceHint); tvPriceHint = view.findViewById(R.id.tvPriceHint);
btnCheck = view.findViewById(R.id.btnCheck);
tvList.setText(HtmlHelper.fromHtml( tvList.setText(HtmlHelper.fromHtml(
"<a href=\"" + BuildConfig.PRO_FEATURES_URI + "\">" + Html.escapeHtml(getString(R.string.title_pro_list)) + "</a>")); "<a href=\"" + BuildConfig.PRO_FEATURES_URI + "\">" + Html.escapeHtml(getString(R.string.title_pro_list)) + "</a>"));
@ -68,16 +70,26 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
@Override @Override
public void onClick(View view) { public void onClick(View view) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(new Intent(ActivityView.ACTION_PURCHASE)); lbm.sendBroadcast(new Intent(ActivityBilling.ACTION_PURCHASE));
} }
}); });
tvPriceHint.setMovementMethod(LinkMovementMethod.getInstance()); tvPriceHint.setMovementMethod(LinkMovementMethod.getInstance());
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.sendBroadcast(new Intent(ActivityBilling.ACTION_PURCHASE_CHECK));
}
});
tvPending.setVisibility(View.GONE); tvPending.setVisibility(View.GONE);
tvActivated.setVisibility(View.GONE); tvActivated.setVisibility(View.GONE);
btnPurchase.setEnabled(false); btnPurchase.setEnabled(false);
tvPrice.setText(null); tvPrice.setText(null);
btnCheck.setEnabled(false);
btnCheck.setVisibility(Helper.isPlayStoreInstall(getContext()) ? View.VISIBLE : View.GONE);
return view; return view;
} }
@ -87,6 +99,16 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
addBillingListener(new ActivityBilling.IBillingListener() { addBillingListener(new ActivityBilling.IBillingListener() {
@Override
public void onConnected() {
btnCheck.setEnabled(true);
}
@Override
public void onDisconnected() {
btnCheck.setEnabled(false);
}
@Override @Override
public void onSkuDetails(String sku, String price) { public void onSkuDetails(String sku, String price) {
if (ActivityBilling.getSkuPro().equals(sku)) { if (ActivityBilling.getSkuPro().equals(sku)) {

View File

@ -97,6 +97,18 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvHint" /> app:layout_constraintTop_toBottomOf="@id/tvHint" />
<Button
android:id="@+id/btnCheck"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="@string/title_check"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPriceHint" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView> </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>