From 8fbcdb83536ab4a9495ef795fadf3b5508ef2515 Mon Sep 17 00:00:00 2001 From: crimean Date: Wed, 14 Nov 2018 20:06:09 +0300 Subject: [PATCH] Fix live subscriptions --- .../osmand/plus/helpers/DiscountHelper.java | 14 +-- .../plus/inapp/InAppPurchaseHelper.java | 91 +++++++++++++++---- .../net/osmand/plus/inapp/InAppPurchases.java | 44 ++++----- 3 files changed, 95 insertions(+), 54 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/DiscountHelper.java b/OsmAnd/src/net/osmand/plus/helpers/DiscountHelper.java index 5d257297c3..b14e8d9198 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/DiscountHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/DiscountHelper.java @@ -30,7 +30,6 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.chooseplan.ChoosePlanDialogFragment; import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchases.InAppPurchase; -import net.osmand.plus.inapp.InAppPurchases.InAppSubscription; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.search.QuickSearchHelper; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController; @@ -63,6 +62,7 @@ public class DiscountHelper { private static final String SHOW_POI_PREFIX = "osmand-show-poi:"; private static final String OPEN_ACTIVITY = "open_activity"; + @SuppressLint("HardwareIds") public static void checkAndDisplay(final MapActivity mapActivity) { OsmandApplication app = mapActivity.getMyApplication(); OsmandSettings settings = app.getSettings(); @@ -130,15 +130,9 @@ public class DiscountHelper { if (data.url.startsWith(INAPP_PREFIX) && data.url.length() > INAPP_PREFIX.length()) { String inAppSku = data.url.substring(INAPP_PREFIX.length()); InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper(); - if (purchaseHelper != null) { - if (purchaseHelper.isPurchased(inAppSku) || InAppPurchaseHelper.isSubscribedToLiveUpdates(app)) { - return; - } else { - InAppSubscription discountSubscription = purchaseHelper.getLiveUpdates().applyDiscountSubscription(inAppSku); - if (discountSubscription != null && discountSubscription.fetchRequired()) { - purchaseHelper.requestInventory(); - } - } + if (purchaseHelper != null + && purchaseHelper.isPurchased(inAppSku) || InAppPurchaseHelper.isSubscribedToLiveUpdates(app)) { + return; } } diff --git a/OsmAnd/src/net/osmand/plus/inapp/InAppPurchaseHelper.java b/OsmAnd/src/net/osmand/plus/inapp/InAppPurchaseHelper.java index 549aa55f3b..b4e641a1e6 100644 --- a/OsmAnd/src/net/osmand/plus/inapp/InAppPurchaseHelper.java +++ b/OsmAnd/src/net/osmand/plus/inapp/InAppPurchaseHelper.java @@ -32,6 +32,7 @@ import net.osmand.plus.liveupdates.CountrySelectionFragment; import net.osmand.plus.liveupdates.CountrySelectionFragment.CountryItem; import net.osmand.util.Algorithms; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -240,25 +241,7 @@ public class InAppPurchaseHelper { public void requestInventory() { notifyShowProgress(InAppPurchaseTaskType.REQUEST_INVENTORY); - exec(InAppPurchaseTaskType.REQUEST_INVENTORY, new InAppRunnable() { - @Override - public boolean run(InAppPurchaseHelper helper) { - logDebug("Setup successful. Querying inventory."); - List skus = new ArrayList<>(); - for (InAppPurchase purchase : purchases.getAllInAppPurchases()) { - skus.add(purchase.getSku()); - } - try { - mHelper.queryInventoryAsync(true, skus, mGotInventoryListener); - return false; - } catch (Exception e) { - logError("queryInventoryAsync Error", e); - notifyDismissProgress(InAppPurchaseTaskType.REQUEST_INVENTORY); - stop(true); - } - return true; - } - }); + new RequestInventoryTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); } public void purchaseFullVersion(final Activity activity) { @@ -392,7 +375,7 @@ public class InAppPurchaseHelper { for (String sku : allOwnedSubscriptionSkus) { Purchase purchase = inventory.getPurchase(sku); SkuDetails liveUpdatesDetails = inventory.getSkuDetails(sku); - InAppSubscription s = getLiveUpdates().applyDiscountSubscription(sku); + InAppSubscription s = getLiveUpdates().upgradeSubscription(sku); if (s == null) { s = new InAppPurchaseLiveUpdatesOldSubscription(liveUpdatesDetails); } @@ -615,6 +598,74 @@ public class InAppPurchaseHelper { } } + @SuppressLint("StaticFieldLeak") + private class RequestInventoryTask extends AsyncTask { + + RequestInventoryTask() { + } + + @Override + protected String doInBackground(Void... params) { + try { + Map parameters = new HashMap<>(); + parameters.put("androidPackage", ctx.getPackageName()); + parameters.put("version", Version.getFullVersion(ctx)); + parameters.put("lang", ctx.getLanguage() + ""); + + return AndroidNetworkUtils.sendRequest(ctx, + "https://osmand.net/api/subscriptions/active", + parameters, "Requesting active subscriptions...", false, false); + + } catch (Exception e) { + logError("sendRequest Error", e); + } + return null; + } + + @Override + protected void onPostExecute(String response) { + logDebug("Response=" + response); + if (response != null) { + try { + /* + { "monthly" : { "sku": "osm_live_subscription_monthly_free_v1" }, "quarterly" : { "sku": "osm_live_subscription_3_months_free_v1" }, "annual" : { "sku": "osm_live_subscription_annual_free_v1" } } + */ + JSONObject obj = new JSONObject(response); + JSONArray names = obj.names(); + for (int i = 0; i < names.length(); i++) { + String skuType = names.getString(i); + JSONObject subObj = obj.getJSONObject(skuType); + String sku = subObj.getString("sku"); + if (!Algorithms.isEmpty(sku)) { + getLiveUpdates().upgradeSubscription(sku); + } + } + } catch (JSONException e) { + logError("Json parsing error", e); + } + } + exec(InAppPurchaseTaskType.REQUEST_INVENTORY, new InAppRunnable() { + @Override + public boolean run(InAppPurchaseHelper helper) { + logDebug("Setup successful. Querying inventory."); + Set skus = new HashSet<>(); + for (InAppPurchase purchase : purchases.getAllInAppPurchases()) { + skus.add(purchase.getSku()); + } + try { + mHelper.queryInventoryAsync(true, new ArrayList<>(skus), mGotInventoryListener); + return false; + } catch (Exception e) { + logError("queryInventoryAsync Error", e); + notifyDismissProgress(InAppPurchaseTaskType.REQUEST_INVENTORY); + stop(true); + } + return true; + } + }); + } + } + // Callback for when a purchase is finished private OnIabPurchaseFinishedListener mPurchaseFinishedListener = new OnIabPurchaseFinishedListener() { public void onIabPurchaseFinished(IabResult result, Purchase purchase) { diff --git a/OsmAnd/src/net/osmand/plus/inapp/InAppPurchases.java b/OsmAnd/src/net/osmand/plus/inapp/InAppPurchases.java index c4e98d88ab..c6de16d177 100644 --- a/OsmAnd/src/net/osmand/plus/inapp/InAppPurchases.java +++ b/OsmAnd/src/net/osmand/plus/inapp/InAppPurchases.java @@ -160,7 +160,7 @@ public class InAppPurchases { List res = new ArrayList<>(); for (InAppSubscription s : getSubscriptions()) { res.add(s); - res.addAll(s.getDiscounts()); + res.addAll(s.getUpgrades()); } return res; } @@ -173,16 +173,16 @@ public class InAppPurchases { res.add(s); added = true; } else { - for (InAppSubscription discount : s.getDiscounts()) { - if (discount.isPurchased()) { - res.add(discount); + for (InAppSubscription upgrade : s.getUpgrades()) { + if (upgrade.isPurchased()) { + res.add(upgrade); added = true; } } } if (!added) { - for (InAppSubscription discount : s.getDiscounts()) { - res.add(discount); + for (InAppSubscription upgrade : s.getUpgrades()) { + res.add(upgrade); added = true; } } @@ -208,12 +208,12 @@ public class InAppPurchases { } @Nullable - public InAppSubscription applyDiscountSubscription(String sku) { + public InAppSubscription upgradeSubscription(String sku) { List subscriptions = getAllSubscriptions(); for (InAppSubscription s : subscriptions) { - InAppSubscription discount = s.applyDiscountSubscription(sku); - if (discount != null) { - return discount; + InAppSubscription upgrade = s.upgradeSubscription(sku); + if (upgrade != null) { + return upgrade; } } return null; @@ -408,10 +408,10 @@ public class InAppPurchases { public static abstract class InAppSubscription extends InAppPurchase { - private Map discounts = new ConcurrentHashMap<>(); + private Map upgrades = new ConcurrentHashMap<>(); private String skuNoVersion; private String subscriptionPeriod; - protected boolean discount = false; + private boolean upgrade = false; InAppSubscription(@NonNull String skuNoVersion, int version) { super(skuNoVersion + "_v" + version); @@ -424,20 +424,20 @@ public class InAppPurchases { } @NonNull - public List getDiscounts() { - return new ArrayList<>(discounts.values()); + private List getUpgrades() { + return new ArrayList<>(upgrades.values()); } @Nullable - public InAppSubscription applyDiscountSubscription(@NonNull String sku) { + InAppSubscription upgradeSubscription(@NonNull String sku) { InAppSubscription s = null; - if (!discount) { - s = discounts.get(sku); + if (!upgrade) { + s = upgrades.get(sku); if (s == null) { s = newInstance(sku); if (s != null) { - s.discount = true; - discounts.put(sku, s); + s.upgrade = true; + upgrades.put(sku, s); } } } @@ -448,7 +448,7 @@ public class InAppPurchases { if (isPurchased()) { return true; } else { - for (InAppSubscription s : getDiscounts()) { + for (InAppSubscription s : getUpgrades()) { if (s.isPurchased()) { return true; } @@ -457,10 +457,6 @@ public class InAppPurchases { return false; } - public boolean isDiscount() { - return discount; - } - public String getSkuNoVersion() { return skuNoVersion; }