Changes to reflect new xmlrpc api

This commit is contained in:
Sylvain Berfini 2015-04-28 16:02:33 +02:00
parent f4a6bfe593
commit f747635c7b
2 changed files with 45 additions and 5 deletions

View file

@ -92,7 +92,9 @@ public class InAppPurchaseActivity extends Activity implements InAppPurchaseList
@Override @Override
public void onPurchasedItemConfirmationQueryFinished(Purchasable item) { public void onPurchasedItemConfirmationQueryFinished(Purchasable item) {
if (item != null) {
Log.d("[In-app purchase] Item bought, expires " + item.getExpireDate());
}
} }
@Override @Override

View file

@ -21,12 +21,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.regex.Pattern;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.linphone.LinphonePreferences; import org.linphone.LinphonePreferences;
import org.linphone.mediastream.Log; import org.linphone.mediastream.Log;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity; import android.app.Activity;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ComponentName; import android.content.ComponentName;
@ -38,6 +41,7 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Patterns;
import com.android.vending.billing.IInAppBillingService; import com.android.vending.billing.IInAppBillingService;
@ -96,6 +100,7 @@ public class InAppPurchaseHelper {
private IInAppBillingService mService; private IInAppBillingService mService;
private ServiceConnection mServiceConn; private ServiceConnection mServiceConn;
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private String mGmailAccount;
private String responseCodeToErrorMessage(int responseCode) { private String responseCodeToErrorMessage(int responseCode) {
switch (responseCode) { switch (responseCode) {
@ -122,6 +127,8 @@ public class InAppPurchaseHelper {
public InAppPurchaseHelper(Activity context, InAppPurchaseListener listener) { public InAppPurchaseHelper(Activity context, InAppPurchaseListener listener) {
mContext = context; mContext = context;
mListener = listener; mListener = listener;
mGmailAccount = getGmailAccount();
mServiceConn = new ServiceConnection() { mServiceConn = new ServiceConnection() {
@Override @Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
@ -134,7 +141,7 @@ public class InAppPurchaseHelper {
String packageName = mContext.getPackageName(); String packageName = mContext.getPackageName();
try { try {
int response = mService.isBillingSupported(API_VERSION, packageName, ITEM_TYPE_SUBS); int response = mService.isBillingSupported(API_VERSION, packageName, ITEM_TYPE_SUBS);
if (response != RESPONSE_RESULT_OK) { if (response != RESPONSE_RESULT_OK || mGmailAccount == null) {
Log.e("[In-app purchase] Error: Subscriptions aren't supported!"); Log.e("[In-app purchase] Error: Subscriptions aren't supported!");
} else { } else {
mListener.onServiceAvailableForQueries(); mListener.onServiceAvailableForQueries();
@ -296,8 +303,10 @@ public class InAppPurchaseHelper {
verifySignatureAndCreateAccountAsync(new VerifiedSignatureListener() { verifySignatureAndCreateAccountAsync(new VerifiedSignatureListener() {
@Override @Override
public void onParsedAndVerifiedSignatureQueryFinished(Purchasable item) { public void onParsedAndVerifiedSignatureQueryFinished(Purchasable item) {
if (item != null) {
mListener.onPurchasedItemConfirmationQueryFinished(item); mListener.onPurchasedItemConfirmationQueryFinished(item);
} }
}
}, purchaseData, signature); }, purchaseData, signature);
} else { } else {
Log.e("[In-app purchase] Error: resultCode is " + resultCode + " and responseCode is " + responseCodeToErrorMessage(responseCode)); Log.e("[In-app purchase] Error: resultCode is " + resultCode + " and responseCode is " + responseCodeToErrorMessage(responseCode));
@ -309,6 +318,24 @@ public class InAppPurchaseHelper {
mContext.unbindService(mServiceConn); mContext.unbindService(mServiceConn);
} }
private boolean isEmailCorrect(String email) {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
return emailPattern.matcher(email).matches();
}
private String getGmailAccount() {
Account[] accounts = AccountManager.get(mContext).getAccountsByType("com.google");
for (Account account: accounts) {
if (isEmailCorrect(account.name)) {
String possibleEmail = account.name;
return possibleEmail;
}
}
return null;
}
private Purchasable verifySignatureAndGetExpire(String purchasedData, String signature) { private Purchasable verifySignatureAndGetExpire(String purchasedData, String signature) {
XMLRPCClient client = null; XMLRPCClient client = null;
try { try {
@ -319,8 +346,13 @@ public class InAppPurchaseHelper {
if (client != null) { if (client != null) {
try { try {
Object result = client.call("get_expiration_date", purchasedData, signature, "google"); Object result = client.call("get_expiration_date", mGmailAccount, purchasedData, signature, "google");
String expire = (String)result; String expire = (String)result;
if ("-1".equals(expire)) {
Log.e("[In-app purchase] Server failed to validate the payload !");
return null;
}
JSONObject json = new JSONObject(purchasedData); JSONObject json = new JSONObject(purchasedData);
String productId = json.getString(PURCHASE_DETAILS_PRODUCT_ID); String productId = json.getString(PURCHASE_DETAILS_PRODUCT_ID);
Purchasable item = new Purchasable(productId); Purchasable item = new Purchasable(productId);
@ -358,6 +390,12 @@ public class InAppPurchaseHelper {
public void onResponse(long id, Object result) { public void onResponse(long id, Object result) {
try { try {
String expire = (String)result; String expire = (String)result;
if ("-1".equals(expire)) {
Log.e("[In-app purchase] Server failed to validate the payload !");
listener.onParsedAndVerifiedSignatureQueryFinished(null);
return;
}
JSONObject json = new JSONObject(purchasedData); JSONObject json = new JSONObject(purchasedData);
String productId = json.getString(PURCHASE_DETAILS_PRODUCT_ID); String productId = json.getString(PURCHASE_DETAILS_PRODUCT_ID);
Purchasable item = new Purchasable(productId); Purchasable item = new Purchasable(productId);
@ -376,7 +414,7 @@ public class InAppPurchaseHelper {
Log.e(error); Log.e(error);
Log.e("[In-app purchase] Server can't validate the payload and it's signature !"); Log.e("[In-app purchase] Server can't validate the payload and it's signature !");
} }
}, "create_account_from_in_app_purchase", "sylvain@sip.linphone.org", "toto", purchasedData, signature, "google"); }, "create_account_from_in_app_purchase", mGmailAccount, "sylvain@sip.linphone.org", "toto", purchasedData, signature, "google");
} }
} }