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.chooseplan.ChoosePlanDialogFragment;
|
||||||
import net.osmand.plus.inapp.InAppPurchaseHelper;
|
import net.osmand.plus.inapp.InAppPurchaseHelper;
|
||||||
import net.osmand.plus.inapp.InAppPurchases.InAppPurchase;
|
import net.osmand.plus.inapp.InAppPurchases.InAppPurchase;
|
||||||
import net.osmand.plus.inapp.InAppPurchases.InAppSubscription;
|
|
||||||
import net.osmand.plus.poi.PoiUIFilter;
|
import net.osmand.plus.poi.PoiUIFilter;
|
||||||
import net.osmand.plus.search.QuickSearchHelper;
|
import net.osmand.plus.search.QuickSearchHelper;
|
||||||
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController;
|
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 SHOW_POI_PREFIX = "osmand-show-poi:";
|
||||||
private static final String OPEN_ACTIVITY = "open_activity";
|
private static final String OPEN_ACTIVITY = "open_activity";
|
||||||
|
|
||||||
|
@SuppressLint("HardwareIds")
|
||||||
public static void checkAndDisplay(final MapActivity mapActivity) {
|
public static void checkAndDisplay(final MapActivity mapActivity) {
|
||||||
OsmandApplication app = mapActivity.getMyApplication();
|
OsmandApplication app = mapActivity.getMyApplication();
|
||||||
OsmandSettings settings = app.getSettings();
|
OsmandSettings settings = app.getSettings();
|
||||||
|
@ -130,15 +130,9 @@ public class DiscountHelper {
|
||||||
if (data.url.startsWith(INAPP_PREFIX) && data.url.length() > INAPP_PREFIX.length()) {
|
if (data.url.startsWith(INAPP_PREFIX) && data.url.length() > INAPP_PREFIX.length()) {
|
||||||
String inAppSku = data.url.substring(INAPP_PREFIX.length());
|
String inAppSku = data.url.substring(INAPP_PREFIX.length());
|
||||||
InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper();
|
InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper();
|
||||||
if (purchaseHelper != null) {
|
if (purchaseHelper != null
|
||||||
if (purchaseHelper.isPurchased(inAppSku) || InAppPurchaseHelper.isSubscribedToLiveUpdates(app)) {
|
&& purchaseHelper.isPurchased(inAppSku) || InAppPurchaseHelper.isSubscribedToLiveUpdates(app)) {
|
||||||
return;
|
return;
|
||||||
} else {
|
|
||||||
InAppSubscription discountSubscription = purchaseHelper.getLiveUpdates().applyDiscountSubscription(inAppSku);
|
|
||||||
if (discountSubscription != null && discountSubscription.fetchRequired()) {
|
|
||||||
purchaseHelper.requestInventory();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ import net.osmand.plus.liveupdates.CountrySelectionFragment;
|
||||||
import net.osmand.plus.liveupdates.CountrySelectionFragment.CountryItem;
|
import net.osmand.plus.liveupdates.CountrySelectionFragment.CountryItem;
|
||||||
import net.osmand.util.Algorithms;
|
import net.osmand.util.Algorithms;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -240,25 +241,7 @@ public class InAppPurchaseHelper {
|
||||||
|
|
||||||
public void requestInventory() {
|
public void requestInventory() {
|
||||||
notifyShowProgress(InAppPurchaseTaskType.REQUEST_INVENTORY);
|
notifyShowProgress(InAppPurchaseTaskType.REQUEST_INVENTORY);
|
||||||
exec(InAppPurchaseTaskType.REQUEST_INVENTORY, new InAppRunnable() {
|
new RequestInventoryTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
|
||||||
@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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void purchaseFullVersion(final Activity activity) {
|
public void purchaseFullVersion(final Activity activity) {
|
||||||
|
@ -392,7 +375,7 @@ public class InAppPurchaseHelper {
|
||||||
for (String sku : allOwnedSubscriptionSkus) {
|
for (String sku : allOwnedSubscriptionSkus) {
|
||||||
Purchase purchase = inventory.getPurchase(sku);
|
Purchase purchase = inventory.getPurchase(sku);
|
||||||
SkuDetails liveUpdatesDetails = inventory.getSkuDetails(sku);
|
SkuDetails liveUpdatesDetails = inventory.getSkuDetails(sku);
|
||||||
InAppSubscription s = getLiveUpdates().applyDiscountSubscription(sku);
|
InAppSubscription s = getLiveUpdates().upgradeSubscription(sku);
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
s = new InAppPurchaseLiveUpdatesOldSubscription(liveUpdatesDetails);
|
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
|
// Callback for when a purchase is finished
|
||||||
private OnIabPurchaseFinishedListener mPurchaseFinishedListener = new OnIabPurchaseFinishedListener() {
|
private OnIabPurchaseFinishedListener mPurchaseFinishedListener = new OnIabPurchaseFinishedListener() {
|
||||||
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
|
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
|
||||||
|
|
|
@ -160,7 +160,7 @@ public class InAppPurchases {
|
||||||
List<InAppSubscription> res = new ArrayList<>();
|
List<InAppSubscription> res = new ArrayList<>();
|
||||||
for (InAppSubscription s : getSubscriptions()) {
|
for (InAppSubscription s : getSubscriptions()) {
|
||||||
res.add(s);
|
res.add(s);
|
||||||
res.addAll(s.getDiscounts());
|
res.addAll(s.getUpgrades());
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -173,16 +173,16 @@ public class InAppPurchases {
|
||||||
res.add(s);
|
res.add(s);
|
||||||
added = true;
|
added = true;
|
||||||
} else {
|
} else {
|
||||||
for (InAppSubscription discount : s.getDiscounts()) {
|
for (InAppSubscription upgrade : s.getUpgrades()) {
|
||||||
if (discount.isPurchased()) {
|
if (upgrade.isPurchased()) {
|
||||||
res.add(discount);
|
res.add(upgrade);
|
||||||
added = true;
|
added = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!added) {
|
if (!added) {
|
||||||
for (InAppSubscription discount : s.getDiscounts()) {
|
for (InAppSubscription upgrade : s.getUpgrades()) {
|
||||||
res.add(discount);
|
res.add(upgrade);
|
||||||
added = true;
|
added = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,12 +208,12 @@ public class InAppPurchases {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public InAppSubscription applyDiscountSubscription(String sku) {
|
public InAppSubscription upgradeSubscription(String sku) {
|
||||||
List<InAppSubscription> subscriptions = getAllSubscriptions();
|
List<InAppSubscription> subscriptions = getAllSubscriptions();
|
||||||
for (InAppSubscription s : subscriptions) {
|
for (InAppSubscription s : subscriptions) {
|
||||||
InAppSubscription discount = s.applyDiscountSubscription(sku);
|
InAppSubscription upgrade = s.upgradeSubscription(sku);
|
||||||
if (discount != null) {
|
if (upgrade != null) {
|
||||||
return discount;
|
return upgrade;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -408,10 +408,10 @@ public class InAppPurchases {
|
||||||
|
|
||||||
public static abstract class InAppSubscription extends InAppPurchase {
|
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 skuNoVersion;
|
||||||
private String subscriptionPeriod;
|
private String subscriptionPeriod;
|
||||||
protected boolean discount = false;
|
private boolean upgrade = false;
|
||||||
|
|
||||||
InAppSubscription(@NonNull String skuNoVersion, int version) {
|
InAppSubscription(@NonNull String skuNoVersion, int version) {
|
||||||
super(skuNoVersion + "_v" + version);
|
super(skuNoVersion + "_v" + version);
|
||||||
|
@ -424,20 +424,20 @@ public class InAppPurchases {
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public List<InAppSubscription> getDiscounts() {
|
private List<InAppSubscription> getUpgrades() {
|
||||||
return new ArrayList<>(discounts.values());
|
return new ArrayList<>(upgrades.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public InAppSubscription applyDiscountSubscription(@NonNull String sku) {
|
InAppSubscription upgradeSubscription(@NonNull String sku) {
|
||||||
InAppSubscription s = null;
|
InAppSubscription s = null;
|
||||||
if (!discount) {
|
if (!upgrade) {
|
||||||
s = discounts.get(sku);
|
s = upgrades.get(sku);
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
s = newInstance(sku);
|
s = newInstance(sku);
|
||||||
if (s != null) {
|
if (s != null) {
|
||||||
s.discount = true;
|
s.upgrade = true;
|
||||||
discounts.put(sku, s);
|
upgrades.put(sku, s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -448,7 +448,7 @@ public class InAppPurchases {
|
||||||
if (isPurchased()) {
|
if (isPurchased()) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
for (InAppSubscription s : getDiscounts()) {
|
for (InAppSubscription s : getUpgrades()) {
|
||||||
if (s.isPurchased()) {
|
if (s.isPurchased()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -457,10 +457,6 @@ public class InAppPurchases {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDiscount() {
|
|
||||||
return discount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSkuNoVersion() {
|
public String getSkuNoVersion() {
|
||||||
return skuNoVersion;
|
return skuNoVersion;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue