IAB improvements

This commit is contained in:
M66B 2019-07-02 08:50:16 +02:00
parent ddbe0dcae3
commit 34b2a0583e
3 changed files with 48 additions and 16 deletions

View File

@ -168,7 +168,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
String text = getBillingResponseText(result); String text = getBillingResponseText(result);
Log.i("IAB launch billing flow response=" + text); Log.i("IAB launch billing flow response=" + text);
if (result.getResponseCode() != BillingClient.BillingResponseCode.OK) if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
Toast.makeText(this, text, Toast.LENGTH_LONG).show(); notifyError(text);
} else } else
Helper.view(this, getIntentPro()); Helper.view(this, getIntentPro());
} }
@ -178,8 +178,8 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
Uri data = intent.getParcelableExtra("uri"); Uri data = intent.getParcelableExtra("uri");
String challenge = getChallenge(); String challenge = getChallenge();
String response = data.getQueryParameter("response"); String response = data.getQueryParameter("response");
Log.i("Challenge=" + challenge); Log.i("IAB challenge=" + challenge);
Log.i("Response=" + response); Log.i("IAB response=" + response);
String expected = getResponse(); String expected = getResponse();
if (expected.equals(response)) { if (expected.equals(response)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@ -187,10 +187,10 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
.putBoolean("pro", true) .putBoolean("pro", true)
.putBoolean("play_store", false) .putBoolean("play_store", false)
.apply(); .apply();
Log.i("Response valid"); Log.i("IAB response valid");
Toast.makeText(this, R.string.title_pro_valid, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.title_pro_valid, Toast.LENGTH_LONG).show();
} else { } else {
Log.i("Response invalid"); Log.i("IAB response invalid");
Toast.makeText(this, R.string.title_pro_invalid, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.title_pro_invalid, Toast.LENGTH_LONG).show();
} }
} catch (NoSuchAlgorithmException ex) { } catch (NoSuchAlgorithmException ex) {
@ -211,7 +211,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
backoff = 4; backoff = 4;
queryPurchases(); queryPurchases();
} else } else
Toast.makeText(ActivityBilling.this, text, Toast.LENGTH_LONG).show(); notifyError(text);
} }
@Override @Override
@ -236,7 +236,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
checkPurchases(purchases); checkPurchases(purchases);
else else
Toast.makeText(this, text, Toast.LENGTH_LONG).show(); notifyError(text);
} }
private void queryPurchases() { private void queryPurchases() {
@ -247,7 +247,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
checkPurchases(result.getPurchasesList()); checkPurchases(result.getPurchasesList());
else else
Toast.makeText(this, text, Toast.LENGTH_LONG).show(); notifyError(text);
} }
interface IBillingListener { interface IBillingListener {
@ -256,19 +256,24 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
void onPurchasePending(String sku); void onPurchasePending(String sku);
void onPurchased(String sku); void onPurchased(String sku);
void onError(String message);
} }
void addBillingListener(final IBillingListener listener, LifecycleOwner owner) { void addBillingListener(final IBillingListener listener, LifecycleOwner owner) {
Log.i("Adding billing listener=" + listener); Log.i("IAB adding billing listener=" + listener);
listeners.add(listener); listeners.add(listener);
if (billingClient != null && billingClient.isReady()) if (billingClient != null)
queryPurchases(); if (billingClient.isReady())
queryPurchases();
else
billingClient.startConnection(billingClientStateListener);
owner.getLifecycle().addObserver(new LifecycleObserver() { owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() { public void onDestroyed() {
Log.i("Removing billing listener=" + listener); Log.i("IAB removing billing listener=" + listener);
listeners.remove(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 (sig.verify(Base64.decode(purchase.getSignature(), Base64.DEFAULT))) {
if (getSkuPro().equals(purchase.getSku())) { if (getSkuPro().equals(purchase.getSku())) {
if (purchase.isAcknowledged()) { if (purchase.isAcknowledged()) {
Log.i("IAB valid signature");
editor.putBoolean("pro", true); editor.putBoolean("pro", true);
Log.i("IAB pro features activated");
} else } else
acknowledgePurchase(purchase); acknowledgePurchase(purchase);
} }
} else { } else {
Log.w("Invalid signature"); Log.w("IAB invalid signature");
Toast.makeText(this, R.string.title_pro_invalid, Toast.LENGTH_LONG).show(); editor.putBoolean("pro", false);
notifyError("Invalid purchase");
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
Toast.makeText(this, Helper.formatThrowable(ex, false), Toast.LENGTH_LONG).show(); notifyError(Helper.formatThrowable(ex, false));
} }
editor.apply(); 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) { private static String getBillingResponseText(BillingResult result) {
String debug = result.getDebugMessage(); String debug = result.getDebugMessage();
return _getBillingResponseText(result) + (debug == null ? "" : " " + debug); return _getBillingResponseText(result) + (debug == null ? "" : " " + debug);

View File

@ -21,6 +21,7 @@ package eu.faircode.email;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
@ -35,6 +36,8 @@ import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.google.android.material.snackbar.Snackbar;
public class FragmentPro extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener { public class FragmentPro extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private TextView tvPending; private TextView tvPending;
private TextView tvActivated; private TextView tvActivated;
@ -107,6 +110,22 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
tvPending.setVisibility(View.GONE); 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();
}
}); });
} }

View File

@ -24,6 +24,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/title_pro_pending" android:text="@string/title_pro_pending"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?android:textColorPrimary"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -33,6 +34,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/title_pro_activated" android:text="@string/title_pro_activated"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?android:textColorPrimary"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPending" /> app:layout_constraintTop_toBottomOf="@id/tvPending" />