Fix live subscriptions
This commit is contained in:
parent
b406f76920
commit
8fbcdb8353
3 changed files with 95 additions and 54 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String> 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<Void, Void, String> {
|
||||
|
||||
RequestInventoryTask() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
try {
|
||||
Map<String, String> 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<String> 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) {
|
||||
|
|
|
@ -160,7 +160,7 @@ public class InAppPurchases {
|
|||
List<InAppSubscription> 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<InAppSubscription> 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<String, InAppSubscription> discounts = new ConcurrentHashMap<>();
|
||||
private Map<String, InAppSubscription> 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<InAppSubscription> getDiscounts() {
|
||||
return new ArrayList<>(discounts.values());
|
||||
private List<InAppSubscription> 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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue