From a7b63d1b9de34da8d2755241ed5307194ec15b22 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 12 May 2015 16:40:24 +0200 Subject: [PATCH] Added callback for errors for in app purchase manager --- .../purchase/InAppPurchaseActivity.java | 12 +++++++++++ .../purchase/InAppPurchaseHelper.java | 20 +++++++++++++++++++ .../purchase/InAppPurchaseListener.java | 6 ++++++ 3 files changed, 38 insertions(+) diff --git a/src/org/linphone/purchase/InAppPurchaseActivity.java b/src/org/linphone/purchase/InAppPurchaseActivity.java index 3e6f13036..862081da1 100644 --- a/src/org/linphone/purchase/InAppPurchaseActivity.java +++ b/src/org/linphone/purchase/InAppPurchaseActivity.java @@ -39,6 +39,7 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; /** * @author Sylvain Berfini @@ -137,6 +138,17 @@ public class InAppPurchaseActivity extends Activity implements InAppPurchaseList } }); } + + @Override + public void onError(final String error) { + Log.e(error); + mHandler.post(new Runnable() { + @Override + public void run() { + Toast.makeText(InAppPurchaseActivity.this, error, Toast.LENGTH_LONG).show(); + } + }); + } private void displayBuySubscriptionButton(Purchasable item) { View layout = LayoutInflater.from(this).inflate(R.layout.in_app_purchasable, purchasableItemsLayout); diff --git a/src/org/linphone/purchase/InAppPurchaseHelper.java b/src/org/linphone/purchase/InAppPurchaseHelper.java index 29ec45e60..10a1fe035 100644 --- a/src/org/linphone/purchase/InAppPurchaseHelper.java +++ b/src/org/linphone/purchase/InAppPurchaseHelper.java @@ -101,6 +101,12 @@ public class InAppPurchaseHelper { public static final String SERVER_ERROR_UID_ALREADY_IN_USE = "ERROR_UID_ALREADY_IN_USE"; public static final String SERVER_ERROR_SIGNATURE_VERIFICATION_FAILED = "ERROR_SIGNATURE_VERIFICATION_FAILED"; public static final String SERVER_ERROR_ACCOUNT_ALREADY_EXISTS = "ERROR_ACCOUNT_ALREADY_EXISTS"; + public static final String SERVER_ERROR_UNKNOWN_ERROR = "ERROR_UNKNOWN_ERROR"; + + public static final String CLIENT_ERROR_SUBSCRIPTION_PURCHASE_NOT_AVAILABLE = "SUBSCRIPTION_PURCHASE_NOT_AVAILABLE"; + public static final String CLIENT_ERROR_BIND_TO_BILLING_SERVICE_FAILED = "BIND_TO_BILLING_SERVICE_FAILED"; + public static final String CLIENT_ERROR_BILLING_SERVICE_UNAVAILABLE = "BILLING_SERVICE_UNAVAILABLE"; + public static final String CLIENT_ERROR_SERVER_NOT_REACHABLE = "SERVER_NOT_REACHABLE"; private Context mContext; private InAppPurchaseListener mListener; @@ -150,6 +156,7 @@ public class InAppPurchaseHelper { int response = mService.isBillingSupported(API_VERSION, packageName, ITEM_TYPE_SUBS); if (response != RESPONSE_RESULT_OK || mGmailAccount == null) { Log.e("[In-app purchase] Error: Subscriptions aren't supported!"); + mListener.onError(CLIENT_ERROR_SUBSCRIPTION_PURCHASE_NOT_AVAILABLE); } else { mListener.onServiceAvailableForQueries(); } @@ -165,9 +172,11 @@ public class InAppPurchaseHelper { boolean ok = mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE); if (!ok) { Log.e("[In-app purchase] Error: Bind service failed"); + mListener.onError(CLIENT_ERROR_BIND_TO_BILLING_SERVICE_FAILED); } } else { Log.e("[In-app purchase] Error: Billing service unavailable on device."); + mListener.onError(CLIENT_ERROR_BILLING_SERVICE_UNAVAILABLE); } } @@ -205,6 +214,7 @@ public class InAppPurchaseHelper { } } else { Log.e("[In-app purchase] Error: responde code is not ok: " + responseCodeToErrorMessage(response)); + mListener.onError(responseCodeToErrorMessage(response)); } } @@ -259,6 +269,7 @@ public class InAppPurchaseHelper { } } else { Log.e("[In-app purchase] Error: responde code is not ok: " + responseCodeToErrorMessage(response)); + mListener.onError(responseCodeToErrorMessage(response)); } } } while (continuationToken != null); @@ -317,6 +328,7 @@ public class InAppPurchaseHelper { }, purchaseData, signature, username); } else { Log.e("[In-app purchase] Error: resultCode is " + resultCode + " and responseCode is " + responseCodeToErrorMessage(responseCode)); + mListener.onError(responseCodeToErrorMessage(responseCode)); } } } @@ -328,6 +340,7 @@ public class InAppPurchaseHelper { } catch (MalformedURLException e) { Log.e(e); Log.e("[In-app purchase] Can't reach the server !"); + mListener.onError(CLIENT_ERROR_SERVER_NOT_REACHABLE); } if (client != null) { @@ -336,6 +349,7 @@ public class InAppPurchaseHelper { public void onServerError(long id, XMLRPCServerException error) { Log.e(error); Log.e("[In-app purchase] Server can't validate the payload and it's signature !"); + mListener.onError(SERVER_ERROR_SIGNATURE_VERIFICATION_FAILED); } @Override @@ -348,6 +362,7 @@ public class InAppPurchaseHelper { public void onError(long id, XMLRPCException error) { Log.e(error); Log.e("[In-app purchase] Server can't validate the payload and it's signature !"); + mListener.onError(SERVER_ERROR_SIGNATURE_VERIFICATION_FAILED); } }, "recover_account", mGmailAccount, sipIdentity + "@sip.linphone.org"); } @@ -393,6 +408,7 @@ public class InAppPurchaseHelper { longExpire = Long.parseLong(expire); } catch (NumberFormatException nfe) { Log.e("[In-app purchase] Server failure: " + result); + mListener.onError(SERVER_ERROR_UNKNOWN_ERROR); return null; } @@ -419,6 +435,7 @@ public class InAppPurchaseHelper { } catch (MalformedURLException e) { Log.e(e); Log.e("[In-app purchase] Can't reach the server !"); + mListener.onError(CLIENT_ERROR_SERVER_NOT_REACHABLE); } if (client != null) { @@ -427,6 +444,7 @@ public class InAppPurchaseHelper { public void onServerError(long id, XMLRPCServerException error) { Log.e(error); Log.e("[In-app purchase] Server can't validate the payload and it's signature !"); + mListener.onError(SERVER_ERROR_SIGNATURE_VERIFICATION_FAILED); } @Override @@ -440,6 +458,7 @@ public class InAppPurchaseHelper { } catch (NumberFormatException nfe) { Log.e("[In-app purchase] Server failure: " + result); listener.onParsedAndVerifiedSignatureQueryFinished(null); + mListener.onError(SERVER_ERROR_UNKNOWN_ERROR); return; } @@ -454,6 +473,7 @@ public class InAppPurchaseHelper { Log.e(e); } Log.e("[In-app purchase] Server can't validate the payload and it's signature !"); + mListener.onError(SERVER_ERROR_SIGNATURE_VERIFICATION_FAILED); } @Override diff --git a/src/org/linphone/purchase/InAppPurchaseListener.java b/src/org/linphone/purchase/InAppPurchaseListener.java index 6df279e1d..a65bcbb2b 100644 --- a/src/org/linphone/purchase/InAppPurchaseListener.java +++ b/src/org/linphone/purchase/InAppPurchaseListener.java @@ -52,4 +52,10 @@ public interface InAppPurchaseListener { * @param true if the recover has been successful, false otherwise */ void onRecoverAccountSuccessful(boolean success); + + /** + * Callback called when an error occurred. + * @param error the error that occurred + */ + void onError(String error); }