From 4c6a3eb25315b9383d0d1ab6e442e1d5251cb252 Mon Sep 17 00:00:00 2001 From: serggl Date: Wed, 10 Sep 2014 00:09:22 +0400 Subject: [PATCH] fix identation --- .../anjlab/android/iab/v3/BillingBase.java | 10 +- .../anjlab/android/iab/v3/BillingCache.java | 72 +-- .../android/iab/v3/BillingProcessor.java | 415 +++++++++--------- .../anjlab/android/iab/v3/PurchaseInfo.java | 12 +- .../com/anjlab/android/iab/v3/SkuDetails.java | 46 +- .../android/iab/v3/TransactionDetails.java | 36 +- 6 files changed, 289 insertions(+), 302 deletions(-) diff --git a/library/src/com/anjlab/android/iab/v3/BillingBase.java b/library/src/com/anjlab/android/iab/v3/BillingBase.java index 48d7183b..159c3d26 100644 --- a/library/src/com/anjlab/android/iab/v3/BillingBase.java +++ b/library/src/com/anjlab/android/iab/v3/BillingBase.java @@ -26,7 +26,7 @@ class BillingBase { private WeakReference contextReference; public BillingBase(Activity context) { - contextReference = new WeakReference(context); + contextReference = new WeakReference(context); } public Activity getContext() { @@ -38,10 +38,10 @@ protected String getPreferencesBaseKey() { } private SharedPreferences getPreferences() { - if(contextReference.get() != null) - return PreferenceManager.getDefaultSharedPreferences(contextReference.get()); - return null; - } + if (contextReference.get() != null) + return PreferenceManager.getDefaultSharedPreferences(contextReference.get()); + return null; + } public void release() { if (contextReference != null) diff --git a/library/src/com/anjlab/android/iab/v3/BillingCache.java b/library/src/com/anjlab/android/iab/v3/BillingCache.java index 6d1cd6a6..5fd50396 100644 --- a/library/src/com/anjlab/android/iab/v3/BillingCache.java +++ b/library/src/com/anjlab/android/iab/v3/BillingCache.java @@ -24,16 +24,16 @@ import java.util.regex.Pattern; class BillingCache extends BillingBase { - private static final String ENTRY_DELIMITER = "#####"; - private static final String LINE_DELIMITER = ">>>>>"; + private static final String ENTRY_DELIMITER = "#####"; + private static final String LINE_DELIMITER = ">>>>>"; - private HashMap data; - private String cacheKey; + private HashMap data; + private String cacheKey; public BillingCache(Activity context, String key) { super(context); - data = new HashMap(); - cacheKey = key; + data = new HashMap(); + cacheKey = key; load(); } @@ -42,21 +42,21 @@ private String getPreferencesCacheKey() { } private void load() { - for(String entry : loadString(getPreferencesCacheKey(), "").split(Pattern.quote(ENTRY_DELIMITER))) { - if (!TextUtils.isEmpty(entry)) { - String[] parts = entry.split(Pattern.quote(LINE_DELIMITER)); - if (parts.length > 1) - data.put(parts[0], new PurchaseInfo(parts[1], parts[2])); - } + for (String entry : loadString(getPreferencesCacheKey(), "").split(Pattern.quote(ENTRY_DELIMITER))) { + if (!TextUtils.isEmpty(entry)) { + String[] parts = entry.split(Pattern.quote(LINE_DELIMITER)); + if (parts.length > 1) + data.put(parts[0], new PurchaseInfo(parts[1], parts[2])); + } } } private void flush() { - ArrayList output = new ArrayList(); - for(String productId : data.keySet()) { - PurchaseInfo info = data.get(productId); - output.add(productId + LINE_DELIMITER + info.responseData + LINE_DELIMITER + info.signature); - } + ArrayList output = new ArrayList(); + for (String productId : data.keySet()) { + PurchaseInfo info = data.get(productId); + output.add(productId + LINE_DELIMITER + info.responseData + LINE_DELIMITER + info.signature); + } saveString(getPreferencesCacheKey(), TextUtils.join(ENTRY_DELIMITER, output)); } @@ -64,32 +64,32 @@ public boolean includesProduct(String productId) { return data != null && data.containsKey(productId); } - public PurchaseInfo getDetails(String productId) { - return data.containsKey(productId) ? data.get(productId) : null; - } + public PurchaseInfo getDetails(String productId) { + return data.containsKey(productId) ? data.get(productId) : null; + } - public void put(String productId, String details, String signature) { - if (!data.containsKey(productId)) { - data.put(productId, new PurchaseInfo(details, signature)); - flush(); - } - } + public void put(String productId, String details, String signature) { + if (!data.containsKey(productId)) { + data.put(productId, new PurchaseInfo(details, signature)); + flush(); + } + } - public void remove(String productId) { - if (data.containsKey(productId)) { - data.remove(productId); - flush(); - } - } + public void remove(String productId) { + if (data.containsKey(productId)) { + data.remove(productId); + flush(); + } + } public void clear() { - data.clear(); + data.clear(); flush(); } - public List getContents() { - return new ArrayList(data.keySet()); - } + public List getContents() { + return new ArrayList(data.keySet()); + } @Override public String toString() { diff --git a/library/src/com/anjlab/android/iab/v3/BillingProcessor.java b/library/src/com/anjlab/android/iab/v3/BillingProcessor.java index 16118b66..bd534669 100644 --- a/library/src/com/anjlab/android/iab/v3/BillingProcessor.java +++ b/library/src/com/anjlab/android/iab/v3/BillingProcessor.java @@ -43,18 +43,21 @@ public class BillingProcessor extends BillingBase { */ public static interface IBillingHandler { void onProductPurchased(String productId, TransactionDetails details); + void onPurchaseHistoryRestored(); + void onBillingError(int errorCode, Throwable error); + void onBillingInitialized(); } private static final int PURCHASE_FLOW_REQUEST_CODE = 2061984; - private static final String LOG_TAG = "iabv3"; - private static final String SETTINGS_VERSION = ".v2_6"; + private static final String LOG_TAG = "iabv3"; + private static final String SETTINGS_VERSION = ".v2_6"; private static final String RESTORE_KEY = ".products.restored" + SETTINGS_VERSION; - private static final String MANAGED_PRODUCTS_CACHE_KEY = ".products.cache" + SETTINGS_VERSION; - private static final String SUBSCRIPTIONS_CACHE_KEY = ".subscriptions.cache" + SETTINGS_VERSION; - private static final String PURCHASE_PAYLOAD_CACHE_KEY = ".purchase.last" + SETTINGS_VERSION; + private static final String MANAGED_PRODUCTS_CACHE_KEY = ".products.cache" + SETTINGS_VERSION; + private static final String SUBSCRIPTIONS_CACHE_KEY = ".subscriptions.cache" + SETTINGS_VERSION; + private static final String PURCHASE_PAYLOAD_CACHE_KEY = ".purchase.last" + SETTINGS_VERSION; private IInAppBillingService billingService; private String contextPackageName; @@ -74,10 +77,10 @@ public void onServiceConnected(ComponentName name, IBinder service) { billingService = IInAppBillingService.Stub.asInterface(service); if (!isPurchaseHistoryRestored() && loadOwnedPurchasesFromGoogle()) { setPurchaseHistoryRestored(); - if(eventHandler != null) + if (eventHandler != null) eventHandler.onPurchaseHistoryRestored(); } - if(eventHandler != null) + if (eventHandler != null) eventHandler.onBillingInitialized(); } }; @@ -87,16 +90,15 @@ public BillingProcessor(Activity context, String licenseKey, IBillingHandler han signatureBase64 = licenseKey; eventHandler = handler; contextPackageName = context.getApplicationContext().getPackageName(); - cachedProducts = new BillingCache(context, MANAGED_PRODUCTS_CACHE_KEY); - cachedSubscriptions = new BillingCache(context, SUBSCRIPTIONS_CACHE_KEY); + cachedProducts = new BillingCache(context, MANAGED_PRODUCTS_CACHE_KEY); + cachedSubscriptions = new BillingCache(context, SUBSCRIPTIONS_CACHE_KEY); bindPlayServices(); } private void bindPlayServices() { try { - getContext().bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), serviceConnection, Context.BIND_AUTO_CREATE); - } - catch (Exception e) { + getContext().bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), serviceConnection, Context.BIND_AUTO_CREATE); + } catch (Exception e) { Log.e(LOG_TAG, e.toString()); } } @@ -106,8 +108,7 @@ public void release() { if (serviceConnection != null && getContext() != null) { try { getContext().unbindService(serviceConnection); - } - catch (Exception e) { + } catch (Exception e) { Log.e(LOG_TAG, e.toString()); } billingService = null; @@ -120,92 +121,87 @@ public boolean isInitialized() { return billingService != null; } - public boolean isPurchased(String productId) { - return cachedProducts.includesProduct(productId); - } - - public boolean isSubscribed(String productId) { - return cachedSubscriptions.includesProduct(productId); - } - - public List listOwnedProducts() { - return cachedProducts.getContents(); - } - - public List listOwnedSubscriptions() { - return cachedSubscriptions.getContents(); - } - - private boolean loadPurchasesByType(String type, BillingCache cacheStorage) { - if (!isInitialized()) - return false; - try { - Bundle bundle = billingService.getPurchases(Constants.GOOGLE_API_VERSION, contextPackageName, type, null); - if (bundle.getInt(Constants.RESPONSE_CODE) == Constants.BILLING_RESPONSE_RESULT_OK) { - cacheStorage.clear(); - ArrayList purchaseList = bundle.getStringArrayList(Constants.INAPP_PURCHASE_DATA_LIST); - ArrayList signatureList = bundle.getStringArrayList(Constants.RESPONSE_INAPP_SIGNATURE); - for (int i=0; i i ? signatureList.get(i) : null; - cacheStorage.put(purchase.getString(Constants.RESPONSE_PRODUCT_ID), jsonData, signature); - } - } - return true; - } - catch (Exception e) { - if(eventHandler != null) + public boolean isPurchased(String productId) { + return cachedProducts.includesProduct(productId); + } + + public boolean isSubscribed(String productId) { + return cachedSubscriptions.includesProduct(productId); + } + + public List listOwnedProducts() { + return cachedProducts.getContents(); + } + + public List listOwnedSubscriptions() { + return cachedSubscriptions.getContents(); + } + + private boolean loadPurchasesByType(String type, BillingCache cacheStorage) { + if (!isInitialized()) + return false; + try { + Bundle bundle = billingService.getPurchases(Constants.GOOGLE_API_VERSION, contextPackageName, type, null); + if (bundle.getInt(Constants.RESPONSE_CODE) == Constants.BILLING_RESPONSE_RESULT_OK) { + cacheStorage.clear(); + ArrayList purchaseList = bundle.getStringArrayList(Constants.INAPP_PURCHASE_DATA_LIST); + ArrayList signatureList = bundle.getStringArrayList(Constants.RESPONSE_INAPP_SIGNATURE); + for (int i = 0; i < purchaseList.size(); i++) { + String jsonData = purchaseList.get(i); + JSONObject purchase = new JSONObject(jsonData); + String signature = signatureList != null && signatureList.size() > i ? signatureList.get(i) : null; + cacheStorage.put(purchase.getString(Constants.RESPONSE_PRODUCT_ID), jsonData, signature); + } + } + return true; + } catch (Exception e) { + if (eventHandler != null) eventHandler.onBillingError(Constants.BILLING_ERROR_FAILED_LOAD_PURCHASES, e); - Log.e(LOG_TAG, e.toString()); - } - return false; - } + Log.e(LOG_TAG, e.toString()); + } + return false; + } public boolean loadOwnedPurchasesFromGoogle() { - return isInitialized() && - loadPurchasesByType(Constants.PRODUCT_TYPE_MANAGED, cachedProducts) && - loadPurchasesByType(Constants.PRODUCT_TYPE_SUBSCRIPTION, cachedSubscriptions); + return isInitialized() && + loadPurchasesByType(Constants.PRODUCT_TYPE_MANAGED, cachedProducts) && + loadPurchasesByType(Constants.PRODUCT_TYPE_SUBSCRIPTION, cachedSubscriptions); } - public boolean purchase(String productId) { - return purchase(productId, Constants.PRODUCT_TYPE_MANAGED); - } + public boolean purchase(String productId) { + return purchase(productId, Constants.PRODUCT_TYPE_MANAGED); + } - public boolean subscribe(String productId) { - return purchase(productId, Constants.PRODUCT_TYPE_SUBSCRIPTION); - } + public boolean subscribe(String productId) { + return purchase(productId, Constants.PRODUCT_TYPE_SUBSCRIPTION); + } private boolean purchase(String productId, String purchaseType) { if (!isInitialized() || TextUtils.isEmpty(productId) || TextUtils.isEmpty(purchaseType)) return false; try { String purchasePayload = purchaseType + ":" + UUID.randomUUID().toString(); - savePurchasePayload(purchasePayload); + savePurchasePayload(purchasePayload); Bundle bundle = billingService.getBuyIntent(Constants.GOOGLE_API_VERSION, contextPackageName, productId, purchaseType, purchasePayload); if (bundle != null) { - int response = bundle.getInt(Constants.RESPONSE_CODE); - if (response == Constants.BILLING_RESPONSE_RESULT_OK) { + int response = bundle.getInt(Constants.RESPONSE_CODE); + if (response == Constants.BILLING_RESPONSE_RESULT_OK) { PendingIntent pendingIntent = bundle.getParcelable(Constants.BUY_INTENT); if (getContext() != null) getContext().startIntentSenderForResult(pendingIntent.getIntentSender(), PURCHASE_FLOW_REQUEST_CODE, new Intent(), 0, 0, 0); - else - if(eventHandler != null) - eventHandler.onBillingError(Constants.BILLING_ERROR_LOST_CONTEXT, null); - } - else if (response == Constants.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) { - if (!isPurchased(productId) && !isSubscribed(productId)) - loadOwnedPurchasesFromGoogle(); - if(eventHandler != null) { - TransactionDetails details = getPurchaseTransactionDetails(productId); - if (details == null) - details = getSubscriptionTransactionDetails(productId); - eventHandler.onProductPurchased(productId, details); - } - } - else - if(eventHandler != null) - eventHandler.onBillingError(Constants.BILLING_ERROR_FAILED_TO_INITIALIZE_PURCHASE, null); + else if (eventHandler != null) + eventHandler.onBillingError(Constants.BILLING_ERROR_LOST_CONTEXT, null); + } else if (response == Constants.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) { + if (!isPurchased(productId) && !isSubscribed(productId)) + loadOwnedPurchasesFromGoogle(); + if (eventHandler != null) { + TransactionDetails details = getPurchaseTransactionDetails(productId); + if (details == null) + details = getSubscriptionTransactionDetails(productId); + eventHandler.onProductPurchased(productId, details); + } + } else if (eventHandler != null) + eventHandler.onBillingError(Constants.BILLING_ERROR_FAILED_TO_INITIALIZE_PURCHASE, null); } return true; } catch (Exception e) { @@ -214,147 +210,139 @@ else if (response == Constants.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) { return false; } - private TransactionDetails getPurchaseTransactionDetails(String productId, BillingCache cache) { - PurchaseInfo details = cache.getDetails(productId); - if (details != null && !TextUtils.isEmpty(details.responseData)) { - try { - return new TransactionDetails(details); - } catch (JSONException e) { - Log.e(LOG_TAG, "Failed to load saved purchase details for " + productId); - } - } - return null; - } - - public boolean consumePurchase(String productId) { - if (!isInitialized()) - return false; - try { - TransactionDetails transactionDetails = getPurchaseTransactionDetails(productId, cachedProducts); - if (transactionDetails != null && !TextUtils.isEmpty(transactionDetails.purchaseToken)) { - int response = billingService.consumePurchase(Constants.GOOGLE_API_VERSION, contextPackageName, transactionDetails.purchaseToken); - if (response == Constants.BILLING_RESPONSE_RESULT_OK) { - cachedProducts.remove(productId); - Log.d(LOG_TAG, "Successfully consumed " + productId + " purchase."); - return true; - } - else { - if(eventHandler != null) - eventHandler.onBillingError(response, null); - Log.e(LOG_TAG, String.format("Failed to consume %s: error %d", productId, response)); - } - } - } - catch (Exception e) { - Log.e(LOG_TAG, e.toString()); - } - return false; - } - - private SkuDetails getSkuDetails(String productId, String purchaseType) { - if (billingService != null) { - try { - ArrayList skuList = new ArrayList(); - skuList.add(productId); - Bundle products = new Bundle(); - products.putStringArrayList(Constants.PRODUCTS_LIST, skuList); - Bundle skuDetails = billingService.getSkuDetails(Constants.GOOGLE_API_VERSION, contextPackageName, purchaseType, products); - int response = skuDetails.getInt(Constants.RESPONSE_CODE); - if (response == Constants.BILLING_RESPONSE_RESULT_OK) { - for (String responseLine : skuDetails.getStringArrayList(Constants.DETAILS_LIST)) { - JSONObject object = new JSONObject(responseLine); - String responseProductId = object.getString(Constants.RESPONSE_PRODUCT_ID); - if (productId.equals(responseProductId)) - return new SkuDetails(object); - } - } - else { - if(eventHandler != null) - eventHandler.onBillingError(response, null); - Log.e(LOG_TAG, String.format("Failed to retrieve info for %s: error %d", productId, response)); - } - } catch (Exception e) { - Log.e(LOG_TAG, String.format("Failed to call getSkuDetails %s", e.toString())); - } - } - return null; - } - - public SkuDetails getPurchaseListingDetails(String productId) { - return getSkuDetails(productId, Constants.PRODUCT_TYPE_MANAGED); - } - - public SkuDetails getSubscriptionListingDetails(String productId) { - return getSkuDetails(productId, Constants.PRODUCT_TYPE_SUBSCRIPTION); - } - - public TransactionDetails getPurchaseTransactionDetails(String productId) { - return getPurchaseTransactionDetails(productId, cachedProducts); - } - - public TransactionDetails getSubscriptionTransactionDetails(String productId) { - return getPurchaseTransactionDetails(productId, cachedSubscriptions); - } + private TransactionDetails getPurchaseTransactionDetails(String productId, BillingCache cache) { + PurchaseInfo details = cache.getDetails(productId); + if (details != null && !TextUtils.isEmpty(details.responseData)) { + try { + return new TransactionDetails(details); + } catch (JSONException e) { + Log.e(LOG_TAG, "Failed to load saved purchase details for " + productId); + } + } + return null; + } + + public boolean consumePurchase(String productId) { + if (!isInitialized()) + return false; + try { + TransactionDetails transactionDetails = getPurchaseTransactionDetails(productId, cachedProducts); + if (transactionDetails != null && !TextUtils.isEmpty(transactionDetails.purchaseToken)) { + int response = billingService.consumePurchase(Constants.GOOGLE_API_VERSION, contextPackageName, transactionDetails.purchaseToken); + if (response == Constants.BILLING_RESPONSE_RESULT_OK) { + cachedProducts.remove(productId); + Log.d(LOG_TAG, "Successfully consumed " + productId + " purchase."); + return true; + } else { + if (eventHandler != null) + eventHandler.onBillingError(response, null); + Log.e(LOG_TAG, String.format("Failed to consume %s: error %d", productId, response)); + } + } + } catch (Exception e) { + Log.e(LOG_TAG, e.toString()); + } + return false; + } + + private SkuDetails getSkuDetails(String productId, String purchaseType) { + if (billingService != null) { + try { + ArrayList skuList = new ArrayList(); + skuList.add(productId); + Bundle products = new Bundle(); + products.putStringArrayList(Constants.PRODUCTS_LIST, skuList); + Bundle skuDetails = billingService.getSkuDetails(Constants.GOOGLE_API_VERSION, contextPackageName, purchaseType, products); + int response = skuDetails.getInt(Constants.RESPONSE_CODE); + if (response == Constants.BILLING_RESPONSE_RESULT_OK) { + for (String responseLine : skuDetails.getStringArrayList(Constants.DETAILS_LIST)) { + JSONObject object = new JSONObject(responseLine); + String responseProductId = object.getString(Constants.RESPONSE_PRODUCT_ID); + if (productId.equals(responseProductId)) + return new SkuDetails(object); + } + } else { + if (eventHandler != null) + eventHandler.onBillingError(response, null); + Log.e(LOG_TAG, String.format("Failed to retrieve info for %s: error %d", productId, response)); + } + } catch (Exception e) { + Log.e(LOG_TAG, String.format("Failed to call getSkuDetails %s", e.toString())); + } + } + return null; + } + + public SkuDetails getPurchaseListingDetails(String productId) { + return getSkuDetails(productId, Constants.PRODUCT_TYPE_MANAGED); + } + + public SkuDetails getSubscriptionListingDetails(String productId) { + return getSkuDetails(productId, Constants.PRODUCT_TYPE_SUBSCRIPTION); + } + + public TransactionDetails getPurchaseTransactionDetails(String productId) { + return getPurchaseTransactionDetails(productId, cachedProducts); + } + + public TransactionDetails getSubscriptionTransactionDetails(String productId) { + return getPurchaseTransactionDetails(productId, cachedSubscriptions); + } public boolean handleActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode != PURCHASE_FLOW_REQUEST_CODE) - return false; - int responseCode = data.getIntExtra(Constants.RESPONSE_CODE, Constants.BILLING_RESPONSE_RESULT_OK); - Log.d(LOG_TAG, String.format("resultCode = %d, responseCode = %d", resultCode, responseCode)); - String purchasePayload = getPurchasePayload(); + if (requestCode != PURCHASE_FLOW_REQUEST_CODE) + return false; + int responseCode = data.getIntExtra(Constants.RESPONSE_CODE, Constants.BILLING_RESPONSE_RESULT_OK); + Log.d(LOG_TAG, String.format("resultCode = %d, responseCode = %d", resultCode, responseCode)); + String purchasePayload = getPurchasePayload(); if (resultCode == Activity.RESULT_OK && responseCode == Constants.BILLING_RESPONSE_RESULT_OK && !TextUtils.isEmpty(purchasePayload)) { String purchaseData = data.getStringExtra(Constants.INAPP_PURCHASE_DATA); - String dataSignature = data.getStringExtra(Constants.RESPONSE_INAPP_SIGNATURE); + String dataSignature = data.getStringExtra(Constants.RESPONSE_INAPP_SIGNATURE); try { JSONObject purchase = new JSONObject(purchaseData); - String productId = purchase.getString(Constants.RESPONSE_PRODUCT_ID); - String developerPayload = purchase.getString(Constants.RESPONSE_PAYLOAD); - if (developerPayload == null) - developerPayload = ""; - boolean purchasedSubscription = purchasePayload.startsWith(Constants.PRODUCT_TYPE_SUBSCRIPTION); + String productId = purchase.getString(Constants.RESPONSE_PRODUCT_ID); + String developerPayload = purchase.getString(Constants.RESPONSE_PAYLOAD); + if (developerPayload == null) + developerPayload = ""; + boolean purchasedSubscription = purchasePayload.startsWith(Constants.PRODUCT_TYPE_SUBSCRIPTION); if (purchasePayload.equals(developerPayload)) { - if (verifyPurchaseSignature(purchaseData, dataSignature)) { - BillingCache cache = purchasedSubscription ? cachedSubscriptions : cachedProducts; - cache.put(productId, purchaseData, dataSignature); - if(eventHandler != null) - eventHandler.onProductPurchased(productId, new TransactionDetails(new PurchaseInfo(purchaseData, dataSignature))); - } - else { - Log.e(LOG_TAG, "Public key signature doesn't match!"); - if(eventHandler != null) - eventHandler.onBillingError(Constants.BILLING_ERROR_INVALID_SIGNATURE, null); - } - } - else { + if (verifyPurchaseSignature(purchaseData, dataSignature)) { + BillingCache cache = purchasedSubscription ? cachedSubscriptions : cachedProducts; + cache.put(productId, purchaseData, dataSignature); + if (eventHandler != null) + eventHandler.onProductPurchased(productId, new TransactionDetails(new PurchaseInfo(purchaseData, dataSignature))); + } else { + Log.e(LOG_TAG, "Public key signature doesn't match!"); + if (eventHandler != null) + eventHandler.onBillingError(Constants.BILLING_ERROR_INVALID_SIGNATURE, null); + } + } else { Log.e(LOG_TAG, String.format("Payload mismatch: %s != %s", purchasePayload, developerPayload)); - if(eventHandler != null) + if (eventHandler != null) eventHandler.onBillingError(Constants.BILLING_ERROR_INVALID_SIGNATURE, null); } - } - catch (Exception e) { - Log.e(LOG_TAG, e.toString()); - if(eventHandler != null) + } catch (Exception e) { + Log.e(LOG_TAG, e.toString()); + if (eventHandler != null) eventHandler.onBillingError(Constants.BILLING_ERROR_OTHER_ERROR, null); } - } - else { - if(eventHandler != null) + } else { + if (eventHandler != null) eventHandler.onBillingError(Constants.BILLING_ERROR_OTHER_ERROR, null); } return true; } - private boolean verifyPurchaseSignature(String purchaseData, String dataSignature) { - if (!TextUtils.isEmpty(signatureBase64)) { - try { - return Security.verifyPurchase(signatureBase64, purchaseData, dataSignature); - } - catch (Exception e) { - return false; - } - } - return true; - } + private boolean verifyPurchaseSignature(String purchaseData, String dataSignature) { + if (!TextUtils.isEmpty(signatureBase64)) { + try { + return Security.verifyPurchase(signatureBase64, purchaseData, dataSignature); + } catch (Exception e) { + return false; + } + } + return true; + } private boolean isPurchaseHistoryRestored() { return loadBoolean(getPreferencesBaseKey() + RESTORE_KEY, false); @@ -364,12 +352,11 @@ public void setPurchaseHistoryRestored() { saveBoolean(getPreferencesBaseKey() + RESTORE_KEY, true); } - private void savePurchasePayload(String value) { - saveString(getPreferencesBaseKey() + PURCHASE_PAYLOAD_CACHE_KEY, value); - } - - private String getPurchasePayload() { - return loadString(getPreferencesBaseKey() + PURCHASE_PAYLOAD_CACHE_KEY, null); - } + private void savePurchasePayload(String value) { + saveString(getPreferencesBaseKey() + PURCHASE_PAYLOAD_CACHE_KEY, value); + } + private String getPurchasePayload() { + return loadString(getPreferencesBaseKey() + PURCHASE_PAYLOAD_CACHE_KEY, null); + } } diff --git a/library/src/com/anjlab/android/iab/v3/PurchaseInfo.java b/library/src/com/anjlab/android/iab/v3/PurchaseInfo.java index 4e146d06..04a14e76 100644 --- a/library/src/com/anjlab/android/iab/v3/PurchaseInfo.java +++ b/library/src/com/anjlab/android/iab/v3/PurchaseInfo.java @@ -25,12 +25,12 @@ */ public class PurchaseInfo { - public final String responseData; + public final String responseData; - public final String signature; + public final String signature; - PurchaseInfo(String responseData, String signature) { - this.responseData = responseData; - this.signature = signature; - } + PurchaseInfo(String responseData, String signature) { + this.responseData = responseData; + this.signature = signature; + } } diff --git a/library/src/com/anjlab/android/iab/v3/SkuDetails.java b/library/src/com/anjlab/android/iab/v3/SkuDetails.java index 917d17c1..c61e806a 100644 --- a/library/src/com/anjlab/android/iab/v3/SkuDetails.java +++ b/library/src/com/anjlab/android/iab/v3/SkuDetails.java @@ -20,35 +20,35 @@ public class SkuDetails { - public final String productId; + public final String productId; - public final String title; + public final String title; - public final String description; + public final String description; - public final boolean isSubscription; + public final boolean isSubscription; - public final String currency; + public final String currency; - public final Double priceValue; + public final Double priceValue; - public final String priceText; + public final String priceText; - public SkuDetails(JSONObject source) throws JSONException { - String responseType = source.getString(Constants.RESPONSE_TYPE); - if (responseType == null) - responseType = Constants.PRODUCT_TYPE_MANAGED; - productId = source.getString(Constants.RESPONSE_PRODUCT_ID); - title = source.getString(Constants.RESPONSE_TITLE); - description = source.getString(Constants.RESPONSE_DESCRIPTION); - isSubscription = responseType.equalsIgnoreCase(Constants.PRODUCT_TYPE_SUBSCRIPTION); - currency = source.getString(Constants.RESPONSE_PRICE_CURRENCY); - priceValue = source.getDouble(Constants.RESPONSE_PRICE_MICROS) / 1000000; - priceText = source.getString(Constants.RESPONSE_PRICE); - } + public SkuDetails(JSONObject source) throws JSONException { + String responseType = source.getString(Constants.RESPONSE_TYPE); + if (responseType == null) + responseType = Constants.PRODUCT_TYPE_MANAGED; + productId = source.getString(Constants.RESPONSE_PRODUCT_ID); + title = source.getString(Constants.RESPONSE_TITLE); + description = source.getString(Constants.RESPONSE_DESCRIPTION); + isSubscription = responseType.equalsIgnoreCase(Constants.PRODUCT_TYPE_SUBSCRIPTION); + currency = source.getString(Constants.RESPONSE_PRICE_CURRENCY); + priceValue = source.getDouble(Constants.RESPONSE_PRICE_MICROS) / 1000000; + priceText = source.getString(Constants.RESPONSE_PRICE); + } - @Override - public String toString() { - return String.format("%s: %s(%s) %f in %s (%s)", productId, title, description, priceValue, currency, priceText); - } + @Override + public String toString() { + return String.format("%s: %s(%s) %f in %s (%s)", productId, title, description, priceValue, currency, priceText); + } } diff --git a/library/src/com/anjlab/android/iab/v3/TransactionDetails.java b/library/src/com/anjlab/android/iab/v3/TransactionDetails.java index 247eb711..13842d5b 100644 --- a/library/src/com/anjlab/android/iab/v3/TransactionDetails.java +++ b/library/src/com/anjlab/android/iab/v3/TransactionDetails.java @@ -22,27 +22,27 @@ public class TransactionDetails { - public final String productId; + public final String productId; - public final String orderId; + public final String orderId; - public final String purchaseToken; + public final String purchaseToken; + + public final Date purchaseTime; - public final Date purchaseTime; - public final PurchaseInfo purchaseInfo; - public TransactionDetails(PurchaseInfo info) throws JSONException { - JSONObject source = new JSONObject(info.responseData); - purchaseInfo = info; - productId = source.getString(Constants.RESPONSE_PRODUCT_ID); - orderId = source.getString(Constants.RESPONSE_ORDER_ID); - purchaseToken = source.getString(Constants.RESPONSE_PURCHASE_TOKEN); - purchaseTime = new Date(source.getInt(Constants.RESPONSE_PURCHASE_TIME)); - } - - @Override - public String toString() { - return String.format("%s purchased at %s(%s). Token: %s, Signature: %s", productId, purchaseTime, orderId, purchaseToken, purchaseInfo.signature); - } + public TransactionDetails(PurchaseInfo info) throws JSONException { + JSONObject source = new JSONObject(info.responseData); + purchaseInfo = info; + productId = source.getString(Constants.RESPONSE_PRODUCT_ID); + orderId = source.getString(Constants.RESPONSE_ORDER_ID); + purchaseToken = source.getString(Constants.RESPONSE_PURCHASE_TOKEN); + purchaseTime = new Date(source.getInt(Constants.RESPONSE_PURCHASE_TIME)); + } + + @Override + public String toString() { + return String.format("%s purchased at %s(%s). Token: %s, Signature: %s", productId, purchaseTime, orderId, purchaseToken, purchaseInfo.signature); + } }