Search in sources :

Example 11 with InAppSubscription

use of net.osmand.plus.inapp.InAppPurchases.InAppSubscription in project Osmand by osmandapp.

the class InAppPurchaseHelperImpl method fetchInAppPurchase.

private void fetchInAppPurchase(@NonNull InAppPurchase inAppPurchase, @NonNull ProductInfo productInfo, @Nullable InAppPurchaseData purchaseData) {
    if (purchaseData != null) {
        inAppPurchase.setPurchaseState(InAppPurchase.PurchaseState.PURCHASED);
        inAppPurchase.setPurchaseInfo(ctx, getPurchaseInfo(purchaseData));
    } else {
        inAppPurchase.setPurchaseState(InAppPurchase.PurchaseState.NOT_PURCHASED);
        inAppPurchase.restorePurchaseInfo(ctx);
    }
    inAppPurchase.setPrice(productInfo.getPrice());
    inAppPurchase.setPriceCurrencyCode(productInfo.getCurrency());
    if (productInfo.getMicrosPrice() > 0) {
        inAppPurchase.setPriceValue(productInfo.getMicrosPrice() / 1000000d);
    }
    String subscriptionPeriod = productInfo.getSubPeriod();
    if (!Algorithms.isEmpty(subscriptionPeriod)) {
        if (inAppPurchase instanceof InAppSubscription) {
            try {
                ((InAppSubscription) inAppPurchase).setSubscriptionPeriodString(subscriptionPeriod);
            } catch (ParseException e) {
                LOG.error(e);
            }
        }
    }
    if (inAppPurchase instanceof InAppSubscription) {
        String introductoryPrice = productInfo.getSubSpecialPrice();
        String introductoryPricePeriod = productInfo.getSubPeriod();
        int introductoryPriceCycles = productInfo.getSubSpecialPeriodCycles();
        long introductoryPriceAmountMicros = productInfo.getSubSpecialPriceMicros();
        if (!Algorithms.isEmpty(introductoryPrice)) {
            InAppSubscription s = (InAppSubscription) inAppPurchase;
            try {
                s.setIntroductoryInfo(new InAppSubscriptionIntroductoryInfo(s, introductoryPrice, introductoryPriceAmountMicros, introductoryPricePeriod, introductoryPriceCycles));
            } catch (ParseException e) {
                LOG.error(e);
            }
        }
    }
}
Also used : InAppSubscriptionIntroductoryInfo(net.osmand.plus.inapp.InAppPurchases.InAppSubscriptionIntroductoryInfo) InAppSubscription(net.osmand.plus.inapp.InAppPurchases.InAppSubscription) ParseException(java.text.ParseException)

Example 12 with InAppSubscription

use of net.osmand.plus.inapp.InAppPurchases.InAppSubscription in project Osmand by osmandapp.

the class InAppPurchaseHelperImpl method getSkuDetailsResponseListener.

// Listener that's called when we finish querying the items and subscriptions we own
private SkuDetailsResponseListener getSkuDetailsResponseListener(boolean userRequested) {
    return new SkuDetailsResponseListener() {

        @NonNull
        private List<String> getAllOwnedSubscriptionSkus() {
            List<String> result = new ArrayList<>();
            BillingManager billingManager = getBillingManager();
            if (billingManager != null) {
                for (Purchase p : billingManager.getPurchases()) {
                    if (getInAppPurchases().getInAppSubscriptionBySku(p.getSku()) != null) {
                        result.add(p.getSku());
                    }
                }
            }
            for (Entry<String, SubscriptionStateHolder> entry : subscriptionStateMap.entrySet()) {
                SubscriptionState state = entry.getValue().state;
                if (state == SubscriptionState.PAUSED || state == SubscriptionState.ON_HOLD) {
                    String sku = entry.getKey();
                    if (!result.contains(sku)) {
                        result.add(sku);
                    }
                }
            }
            return result;
        }

        @Override
        public void onSkuDetailsResponse(@NonNull BillingResult billingResult, List<SkuDetails> skuDetailsList) {
            logDebug("Query sku details finished.");
            // Have we been disposed of in the meantime? If so, quit.
            if (getBillingManager() == null) {
                stop(true);
                return;
            }
            // Is it a failure?
            if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
                logError("Failed to query inventory: " + billingResult.getResponseCode());
                notifyError(InAppPurchaseTaskType.REQUEST_INVENTORY, billingResult.getDebugMessage());
                stop(true);
                return;
            }
            logDebug("Query sku details was successful.");
            /*
				 * Check for items we own. Notice that for each purchase, we check
				 * the developer payload to see if it's correct! See
				 * verifyDeveloperPayload().
				 */
            List<String> allOwnedSubscriptionSkus = getAllOwnedSubscriptionSkus();
            for (InAppSubscription s : getSubscriptions().getAllSubscriptions()) {
                if (hasDetails(s.getSku())) {
                    Purchase purchase = getPurchase(s.getSku());
                    SkuDetails liveUpdatesDetails = getSkuDetails(s.getSku());
                    if (liveUpdatesDetails != null) {
                        fetchInAppPurchase(s, liveUpdatesDetails, purchase);
                    }
                    allOwnedSubscriptionSkus.remove(s.getSku());
                }
            }
            for (String sku : allOwnedSubscriptionSkus) {
                Purchase purchase = getPurchase(sku);
                SkuDetails liveUpdatesDetails = getSkuDetails(sku);
                if (liveUpdatesDetails != null) {
                    InAppSubscription s = getSubscriptions().upgradeSubscription(sku);
                    if (s == null) {
                        s = new InAppPurchaseLiveUpdatesOldSubscription(liveUpdatesDetails);
                    }
                    fetchInAppPurchase(s, liveUpdatesDetails, purchase);
                }
            }
            InAppPurchase fullVersion = getFullVersion();
            if (hasDetails(fullVersion.getSku())) {
                Purchase purchase = getPurchase(fullVersion.getSku());
                SkuDetails fullPriceDetails = getSkuDetails(fullVersion.getSku());
                if (fullPriceDetails != null) {
                    fetchInAppPurchase(fullVersion, fullPriceDetails, purchase);
                }
            }
            InAppPurchase depthContours = getDepthContours();
            if (hasDetails(depthContours.getSku())) {
                Purchase purchase = getPurchase(depthContours.getSku());
                SkuDetails depthContoursDetails = getSkuDetails(depthContours.getSku());
                if (depthContoursDetails != null) {
                    fetchInAppPurchase(depthContours, depthContoursDetails, purchase);
                }
            }
            InAppPurchase contourLines = getContourLines();
            if (hasDetails(contourLines.getSku())) {
                Purchase purchase = getPurchase(contourLines.getSku());
                SkuDetails contourLinesDetails = getSkuDetails(contourLines.getSku());
                if (contourLinesDetails != null) {
                    fetchInAppPurchase(contourLines, contourLinesDetails, purchase);
                }
            }
            Purchase fullVersionPurchase = getPurchase(fullVersion.getSku());
            boolean fullVersionPurchased = fullVersionPurchase != null;
            if (fullVersionPurchased) {
                ctx.getSettings().FULL_VERSION_PURCHASED.set(true);
            }
            Purchase depthContoursPurchase = getPurchase(depthContours.getSku());
            boolean depthContoursPurchased = depthContoursPurchase != null;
            if (depthContoursPurchased) {
                ctx.getSettings().DEPTH_CONTOURS_PURCHASED.set(true);
            }
            // Do we have the live updates?
            boolean subscribedToLiveUpdates = false;
            boolean subscribedToOsmAndPro = false;
            boolean subscribedToMaps = false;
            List<Purchase> subscriptionPurchases = new ArrayList<>();
            for (InAppSubscription s : getSubscriptions().getAllSubscriptions()) {
                Purchase purchase = getPurchase(s.getSku());
                if (purchase != null || s.getState().isActive()) {
                    if (purchase != null) {
                        subscriptionPurchases.add(purchase);
                    }
                    if (!subscribedToLiveUpdates && purchases.isLiveUpdatesSubscription(s)) {
                        subscribedToLiveUpdates = true;
                    }
                    if (!subscribedToOsmAndPro && purchases.isOsmAndProSubscription(s)) {
                        subscribedToOsmAndPro = true;
                    }
                    if (!subscribedToMaps && purchases.isMapsSubscription(s)) {
                        subscribedToMaps = true;
                    }
                }
            }
            if (!subscribedToLiveUpdates && ctx.getSettings().LIVE_UPDATES_PURCHASED.get()) {
                ctx.getSettings().LIVE_UPDATES_PURCHASED.set(false);
            } else if (subscribedToLiveUpdates) {
                ctx.getSettings().LIVE_UPDATES_PURCHASED.set(true);
            }
            if (!subscribedToOsmAndPro && ctx.getSettings().OSMAND_PRO_PURCHASED.get()) {
                ctx.getSettings().OSMAND_PRO_PURCHASED.set(false);
            } else if (subscribedToOsmAndPro) {
                ctx.getSettings().OSMAND_PRO_PURCHASED.set(true);
            }
            if (!subscribedToMaps && ctx.getSettings().OSMAND_MAPS_PURCHASED.get()) {
                ctx.getSettings().OSMAND_MAPS_PURCHASED.set(false);
            } else if (subscribedToMaps) {
                ctx.getSettings().OSMAND_MAPS_PURCHASED.set(true);
            }
            if (!subscribedToLiveUpdates && !subscribedToOsmAndPro && !subscribedToMaps) {
                onSubscriptionExpired();
            }
            lastValidationCheckTime = System.currentTimeMillis();
            logDebug("User " + (subscribedToLiveUpdates ? "HAS" : "DOES NOT HAVE") + " live updates purchased.");
            logDebug("User " + (subscribedToOsmAndPro ? "HAS" : "DOES NOT HAVE") + " OsmAnd Pro purchased.");
            logDebug("User " + (subscribedToMaps ? "HAS" : "DOES NOT HAVE") + " Maps purchased.");
            OsmandSettings settings = ctx.getSettings();
            settings.INAPPS_READ.set(true);
            List<Purchase> tokensToSend = new ArrayList<>();
            if (subscriptionPurchases.size() > 0) {
                List<String> tokensSent = Arrays.asList(settings.BILLING_PURCHASE_TOKENS_SENT.get().split(";"));
                for (Purchase purchase : subscriptionPurchases) {
                    if (needRestoreUserInfo()) {
                        restoreUserInfo(purchase);
                    }
                    if (!tokensSent.contains(purchase.getSku())) {
                        tokensToSend.add(purchase);
                    }
                }
            }
            List<PurchaseInfo> purchaseInfoList = new ArrayList<>();
            for (Purchase purchase : tokensToSend) {
                purchaseInfoList.add(getPurchaseInfo(purchase));
            }
            onSkuDetailsResponseDone(purchaseInfoList, userRequested);
        }

        private void onSubscriptionExpired() {
            if (!isDepthContoursPurchased(ctx)) {
                ctx.getSettings().getCustomRenderBooleanProperty("depthContours").set(false);
            }
        }
    };
}
Also used : SkuDetails(com.android.billingclient.api.SkuDetails) InAppPurchase(net.osmand.plus.inapp.InAppPurchases.InAppPurchase) Purchase(com.android.billingclient.api.Purchase) InAppPurchaseLiveUpdatesOldSubscription(net.osmand.plus.inapp.InAppPurchasesImpl.InAppPurchaseLiveUpdatesOldSubscription) InAppPurchase(net.osmand.plus.inapp.InAppPurchases.InAppPurchase) ArrayList(java.util.ArrayList) BillingManager(net.osmand.plus.inapp.util.BillingManager) InAppSubscription(net.osmand.plus.inapp.InAppPurchases.InAppSubscription) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) SubscriptionState(net.osmand.plus.inapp.InAppPurchases.InAppSubscription.SubscriptionState) PurchaseInfo(net.osmand.plus.inapp.InAppPurchases.PurchaseInfo) NonNull(androidx.annotation.NonNull) BillingResult(com.android.billingclient.api.BillingResult) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with InAppSubscription

use of net.osmand.plus.inapp.InAppPurchases.InAppSubscription in project Osmand by osmandapp.

the class InAppPurchaseHelperImpl method fetchInAppPurchase.

private void fetchInAppPurchase(@NonNull InAppPurchase inAppPurchase, @NonNull Product product, @Nullable Receipt receipt) {
    if (receipt != null) {
        inAppPurchase.setPurchaseState(InAppPurchase.PurchaseState.PURCHASED);
        inAppPurchase.setPurchaseInfo(ctx, getPurchaseInfo(product.getSku(), receipt));
    } else {
        inAppPurchase.setPurchaseState(InAppPurchase.PurchaseState.NOT_PURCHASED);
        inAppPurchase.restorePurchaseInfo(ctx);
    }
    String price = product.getPrice();
    inAppPurchase.setPrice(price);
    double priceValue = 0;
    String countryCode = userData.getAmazonMarketplace();
    String currencyCode = !Algorithms.isEmpty(countryCode) ? Currency.getInstance(new Locale("", countryCode)).getCurrencyCode() : "";
    Pattern regex = Pattern.compile("\\d[\\d,.]+");
    Matcher finder = regex.matcher(price);
    if (finder.find()) {
        try {
            String rawPrice = finder.group(0);
            if (!Algorithms.isEmpty(rawPrice)) {
                priceValue = Double.parseDouble(rawPrice.trim().replaceAll(",", "."));
            }
        // do something with value
        } catch (Exception e) {
            priceValue = 0;
        }
    }
    inAppPurchase.setPriceCurrencyCode(currencyCode);
    if (priceValue > 0) {
        inAppPurchase.setPriceValue(priceValue);
    }
    if (inAppPurchase instanceof InAppSubscription) {
        InAppSubscription s = (InAppSubscription) inAppPurchase;
        s.restoreState(ctx);
        s.restoreExpireTime(ctx);
        SubscriptionStateHolder stateHolder = subscriptionStateMap.get(s.getSku());
        if (stateHolder != null) {
            s.setState(ctx, stateHolder.state);
            s.setExpireTime(ctx, stateHolder.expireTime);
        }
        String subscriptionPeriod = null;
        if (product.getSku().contains(".annual")) {
            subscriptionPeriod = "P1Y";
        } else if (product.getSku().contains(".monthly")) {
            subscriptionPeriod = "P1M";
        }
        if (!Algorithms.isEmpty(subscriptionPeriod)) {
            try {
                s.setSubscriptionPeriodString(subscriptionPeriod);
            } catch (ParseException e) {
                LOG.error(e);
            }
        }
    }
}
Also used : Locale(java.util.Locale) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InAppSubscription(net.osmand.plus.inapp.InAppPurchases.InAppSubscription) ParseException(java.text.ParseException) ParseException(java.text.ParseException)

Example 14 with InAppSubscription

use of net.osmand.plus.inapp.InAppPurchases.InAppSubscription in project Osmand by osmandapp.

the class LiveUpdatesFragment method getSupportRegionName.

public static String getSupportRegionName(OsmandApplication app, InAppPurchaseHelper purchaseHelper) {
    OsmandSettings settings = app.getSettings();
    String countryName = settings.BILLING_USER_COUNTRY.get();
    if (purchaseHelper != null) {
        List<InAppSubscription> subscriptions = purchaseHelper.getSubscriptions().getVisibleSubscriptions();
        boolean donationSupported = false;
        for (InAppSubscription s : subscriptions) {
            if (s.isDonationSupported()) {
                donationSupported = true;
                break;
            }
        }
        if (donationSupported) {
            if (Algorithms.isEmpty(countryName)) {
                if (OsmandSettings.BILLING_USER_DONATION_NONE_PARAMETER.equals(settings.BILLING_USER_COUNTRY_DOWNLOAD_NAME.get())) {
                    countryName = app.getString(R.string.osmand_team);
                } else {
                    countryName = app.getString(R.string.shared_string_world);
                }
            }
        } else {
            countryName = app.getString(R.string.osmand_team);
        }
    } else {
        countryName = app.getString(R.string.osmand_team);
    }
    return countryName;
}
Also used : InAppSubscription(net.osmand.plus.inapp.InAppPurchases.InAppSubscription) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings)

Aggregations

InAppSubscription (net.osmand.plus.inapp.InAppPurchases.InAppSubscription)14 ArrayList (java.util.ArrayList)7 ParseException (java.text.ParseException)4 InAppPurchase (net.osmand.plus.inapp.InAppPurchases.InAppPurchase)4 OsmandSettings (net.osmand.plus.settings.backend.OsmandSettings)3 NonNull (androidx.annotation.NonNull)2 BillingResult (com.android.billingclient.api.BillingResult)2 Purchase (com.android.billingclient.api.Purchase)2 SkuDetails (com.android.billingclient.api.SkuDetails)2 SkuDetailsResponseListener (com.android.billingclient.api.SkuDetailsResponseListener)2 List (java.util.List)2 InAppPurchases (net.osmand.plus.inapp.InAppPurchases)2 InAppSubscriptionIntroductoryInfo (net.osmand.plus.inapp.InAppPurchases.InAppSubscriptionIntroductoryInfo)2 PurchaseInfo (net.osmand.plus.inapp.InAppPurchases.PurchaseInfo)2 InAppPurchaseLiveUpdatesOldSubscription (net.osmand.plus.inapp.InAppPurchasesImpl.InAppPurchaseLiveUpdatesOldSubscription)2 BillingManager (net.osmand.plus.inapp.util.BillingManager)2 Activity (android.app.Activity)1 Spannable (android.text.Spannable)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1