From 34b2a0583e27064c156d1cf0111f97b3164d7abf Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 2 Jul 2019 08:50:16 +0200 Subject: [PATCH] IAB improvements --- .../eu/faircode/email/ActivityBilling.java | 43 ++++++++++++------- .../java/eu/faircode/email/FragmentPro.java | 19 ++++++++ app/src/main/res/layout/fragment_pro.xml | 2 + 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityBilling.java b/app/src/main/java/eu/faircode/email/ActivityBilling.java index 36e4a3641e..4a20fa7dbf 100644 --- a/app/src/main/java/eu/faircode/email/ActivityBilling.java +++ b/app/src/main/java/eu/faircode/email/ActivityBilling.java @@ -168,7 +168,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL String text = getBillingResponseText(result); Log.i("IAB launch billing flow response=" + text); if (result.getResponseCode() != BillingClient.BillingResponseCode.OK) - Toast.makeText(this, text, Toast.LENGTH_LONG).show(); + notifyError(text); } else Helper.view(this, getIntentPro()); } @@ -178,8 +178,8 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL Uri data = intent.getParcelableExtra("uri"); String challenge = getChallenge(); String response = data.getQueryParameter("response"); - Log.i("Challenge=" + challenge); - Log.i("Response=" + response); + Log.i("IAB challenge=" + challenge); + Log.i("IAB response=" + response); String expected = getResponse(); if (expected.equals(response)) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); @@ -187,10 +187,10 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL .putBoolean("pro", true) .putBoolean("play_store", false) .apply(); - Log.i("Response valid"); + Log.i("IAB response valid"); Toast.makeText(this, R.string.title_pro_valid, Toast.LENGTH_LONG).show(); } else { - Log.i("Response invalid"); + Log.i("IAB response invalid"); Toast.makeText(this, R.string.title_pro_invalid, Toast.LENGTH_LONG).show(); } } catch (NoSuchAlgorithmException ex) { @@ -211,7 +211,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL backoff = 4; queryPurchases(); } else - Toast.makeText(ActivityBilling.this, text, Toast.LENGTH_LONG).show(); + notifyError(text); } @Override @@ -236,7 +236,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) checkPurchases(purchases); else - Toast.makeText(this, text, Toast.LENGTH_LONG).show(); + notifyError(text); } private void queryPurchases() { @@ -247,7 +247,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) checkPurchases(result.getPurchasesList()); else - Toast.makeText(this, text, Toast.LENGTH_LONG).show(); + notifyError(text); } interface IBillingListener { @@ -256,19 +256,24 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL void onPurchasePending(String sku); void onPurchased(String sku); + + void onError(String message); } void addBillingListener(final IBillingListener listener, LifecycleOwner owner) { - Log.i("Adding billing listener=" + listener); + Log.i("IAB adding billing listener=" + listener); listeners.add(listener); - if (billingClient != null && billingClient.isReady()) - queryPurchases(); + if (billingClient != null) + if (billingClient.isReady()) + queryPurchases(); + else + billingClient.startConnection(billingClientStateListener); owner.getLifecycle().addObserver(new LifecycleObserver() { @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public void onDestroyed() { - Log.i("Removing billing listener=" + listener); + Log.i("IAB removing billing listener=" + listener); listeners.remove(listener); } }); @@ -314,19 +319,20 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL if (sig.verify(Base64.decode(purchase.getSignature(), Base64.DEFAULT))) { if (getSkuPro().equals(purchase.getSku())) { if (purchase.isAcknowledged()) { + Log.i("IAB valid signature"); editor.putBoolean("pro", true); - Log.i("IAB pro features activated"); } else acknowledgePurchase(purchase); } } else { - Log.w("Invalid signature"); - Toast.makeText(this, R.string.title_pro_invalid, Toast.LENGTH_LONG).show(); + Log.w("IAB invalid signature"); + editor.putBoolean("pro", false); + notifyError("Invalid purchase"); } } catch (Throwable ex) { Log.e(ex); - Toast.makeText(this, Helper.formatThrowable(ex, false), Toast.LENGTH_LONG).show(); + notifyError(Helper.formatThrowable(ex, false)); } editor.apply(); @@ -395,6 +401,11 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL }); } + private void notifyError(String message) { + for (IBillingListener listener : listeners) + listener.onError(message); + } + private static String getBillingResponseText(BillingResult result) { String debug = result.getDebugMessage(); return _getBillingResponseText(result) + (debug == null ? "" : " " + debug); diff --git a/app/src/main/java/eu/faircode/email/FragmentPro.java b/app/src/main/java/eu/faircode/email/FragmentPro.java index c9d4abc69d..733565339c 100644 --- a/app/src/main/java/eu/faircode/email/FragmentPro.java +++ b/app/src/main/java/eu/faircode/email/FragmentPro.java @@ -21,6 +21,7 @@ package eu.faircode.email; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; import android.text.Html; import android.text.method.LinkMovementMethod; @@ -35,6 +36,8 @@ import androidx.annotation.Nullable; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; +import com.google.android.material.snackbar.Snackbar; + public class FragmentPro extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener { private TextView tvPending; private TextView tvActivated; @@ -107,6 +110,22 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha tvPending.setVisibility(View.GONE); } } + + @Override + public void onError(String message) { + final Intent support = new Intent( + Intent.ACTION_VIEW, + Uri.parse("https://contact.faircode.eu/?product=fairemailsupport")); + Snackbar snackbar = Snackbar.make(getView(), message, Snackbar.LENGTH_LONG); + if (support.resolveActivity(getContext().getPackageManager()) != null) + snackbar.setAction(R.string.title_setup_help, new View.OnClickListener() { + @Override + public void onClick(View view) { + startActivity(support); + } + }); + snackbar.show(); + } }); } diff --git a/app/src/main/res/layout/fragment_pro.xml b/app/src/main/res/layout/fragment_pro.xml index 809031f39b..6e62e9e8f7 100644 --- a/app/src/main/res/layout/fragment_pro.xml +++ b/app/src/main/res/layout/fragment_pro.xml @@ -24,6 +24,7 @@ android:layout_height="wrap_content" android:text="@string/title_pro_pending" android:textAppearance="@style/TextAppearance.AppCompat.Medium" + android:textColor="?android:textColorPrimary" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -33,6 +34,7 @@ android:layout_height="wrap_content" android:text="@string/title_pro_activated" android:textAppearance="@style/TextAppearance.AppCompat.Medium" + android:textColor="?android:textColorPrimary" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvPending" />