Search in sources :

Example 1 with InAppPurchaseHelper

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

the class OsmandInAppPurchaseActivity method initInAppPurchaseHelper.

private void initInAppPurchaseHelper() {
    deinitInAppPurchaseHelper();
    OsmandApplication app = getMyApplication();
    OsmandSettings settings = app.getSettings();
    if (purchaseHelper == null) {
        InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper();
        if (settings.isInternetConnectionAvailable() && isInAppPurchaseAllowed() && Version.isInAppPurchaseSupported()) {
            this.purchaseHelper = purchaseHelper;
        }
    }
    if (purchaseHelper != null) {
        final WeakReference<OsmandInAppPurchaseActivity> activityRef = new WeakReference<>(this);
        purchaseHelper.isInAppPurchaseSupported(this, new InAppPurchaseInitCallback() {

            @Override
            public void onSuccess() {
                OsmandInAppPurchaseActivity activity = activityRef.get();
                if (!activityDestroyed && AndroidUtils.isActivityNotDestroyed(activity)) {
                    purchaseHelper.setUiActivity(activity);
                    if (purchaseHelper.needRequestInventory()) {
                        purchaseHelper.requestInventory(false);
                    }
                }
            }

            @Override
            public void onFail() {
            }
        });
    } else if (isInAppPurchaseAllowed() && settings.isInternetConnectionAvailable()) {
        InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper();
        if (purchaseHelper != null && purchaseHelper.needRequestPromo()) {
            purchaseHelper.checkPromoAsync(null);
        }
    }
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) WeakReference(java.lang.ref.WeakReference) InAppPurchaseHelper(net.osmand.plus.inapp.InAppPurchaseHelper) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings) InAppPurchaseInitCallback(net.osmand.plus.inapp.InAppPurchaseHelper.InAppPurchaseInitCallback)

Example 2 with InAppPurchaseHelper

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

the class DiscountHelper method processDiscountResponse.

@SuppressLint("SimpleDateFormat")
private static void processDiscountResponse(String response, MapActivity mapActivity) {
    try {
        OsmandApplication app = mapActivity.getMyApplication();
        JSONObject obj = new JSONObject(response);
        ControllerData data = ControllerData.parse(app, obj);
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm");
        Date start = df.parse(obj.getString("start"));
        Date end = df.parse(obj.getString("end"));
        int showStartFrequency = obj.getInt("show_start_frequency");
        double showDayFrequency = obj.getDouble("show_day_frequency");
        int maxTotalShow = obj.getInt("max_total_show");
        JSONObject application = obj.getJSONObject("application");
        boolean showChristmasDialog = obj.optBoolean("show_christmas_dialog", false);
        if (!validateUrl(app, data.url)) {
            return;
        }
        if (data.oneOfConditions != null) {
            boolean oneOfConditionsMatch = false;
            try {
                Conditions conditions = new Conditions(app);
                JSONArray conditionsArr = data.oneOfConditions;
                for (int i = 0; i < conditionsArr.length(); i++) {
                    JSONObject conditionObj = conditionsArr.getJSONObject(i);
                    JSONArray conditionArr = conditionObj.getJSONArray("condition");
                    if (conditionArr.length() > 0) {
                        boolean conditionMatch = true;
                        for (int k = 0; k < conditionArr.length(); k++) {
                            JSONObject o = conditionArr.getJSONObject(k);
                            conditionMatch = conditions.matchesCondition(o);
                            if (!conditionMatch) {
                                break;
                            }
                        }
                        oneOfConditionsMatch |= conditionMatch;
                    }
                }
            } catch (JSONException e) {
            // ignore
            }
            if (!oneOfConditionsMatch) {
                return;
            }
        }
        String appName = app.getPackageName();
        Date date = new Date();
        if (application.has(appName) && application.getBoolean(appName) && date.after(start) && date.before(end)) {
            OsmandSettings settings = app.getSettings();
            int discountId = getDiscountId(data.message, start);
            boolean discountChanged = settings.DISCOUNT_ID.get() != discountId;
            if (discountChanged) {
                settings.DISCOUNT_TOTAL_SHOW.set(0);
            }
            // show after every N (getNumberOfStarts()) starts or show after every N (double show_day_frequency) frequency
            if (discountChanged || (app.getAppInitializer().getNumberOfStarts() - settings.DISCOUNT_SHOW_NUMBER_OF_STARTS.get() >= showStartFrequency || System.currentTimeMillis() - settings.DISCOUNT_SHOW_DATETIME_MS.get() > 1000L * 60 * 60 * 24 * showDayFrequency)) {
                if (settings.DISCOUNT_TOTAL_SHOW.get() < maxTotalShow) {
                    settings.DISCOUNT_ID.set(discountId);
                    settings.DISCOUNT_TOTAL_SHOW.set(settings.DISCOUNT_TOTAL_SHOW.get() + 1);
                    settings.DISCOUNT_SHOW_NUMBER_OF_STARTS.set(app.getAppInitializer().getNumberOfStarts());
                    settings.DISCOUNT_SHOW_DATETIME_MS.set(System.currentTimeMillis());
                    if (showChristmasDialog) {
                        mapActivity.showXMasDialog();
                    } else {
                        InAppPurchaseHelper purchaseHelper = mapActivity.getPurchaseHelper();
                        if (purchaseHelper != null) {
                            purchaseHelper.requestInventory(false);
                        }
                        showDiscountBanner(mapActivity, data);
                    }
                }
            }
        }
    } catch (Exception e) {
        logError("JSON parsing error: ", e);
    }
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Date(java.util.Date) SuppressLint(android.annotation.SuppressLint) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) InAppPurchaseHelper(net.osmand.plus.inapp.InAppPurchaseHelper) SimpleDateFormat(java.text.SimpleDateFormat) SuppressLint(android.annotation.SuppressLint)

Example 3 with InAppPurchaseHelper

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

the class DiscountHelper method openUrl.

public static void openUrl(final MapActivity mapActivity, String url) {
    if (url.startsWith(INAPP_PREFIX)) {
        OsmandApplication app = mapActivity.getMyApplication();
        InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper();
        if (purchaseHelper != null) {
            InAppPurchase fullVersion = purchaseHelper.getFullVersion();
            if (fullVersion != null && url.contains(fullVersion.getSku())) {
                app.logEvent("in_app_purchase_redirect");
                try {
                    purchaseHelper.purchaseFullVersion(mapActivity);
                } catch (UnsupportedOperationException e) {
                    LOG.error("purchaseFullVersion is not supported", e);
                }
            } else {
                InAppPurchases purchases = purchaseHelper.getInAppPurchases();
                for (InAppPurchase p : purchaseHelper.getSubscriptions().getAllSubscriptions()) {
                    if (url.contains(p.getSku())) {
                        if (purchases.isMapsSubscription(p)) {
                            MapsPlusPlanFragment.showInstance(mapActivity, p.getSku());
                        } else if (purchases.isOsmAndProSubscription(p)) {
                            OsmAndProPlanFragment.showInstance(mapActivity, p.getSku());
                        } else {
                            ChoosePlanFragment.showDefaultInstance(mapActivity);
                        }
                        break;
                    }
                }
            }
        }
    } else if (url.startsWith(SEARCH_QUERY_PREFIX)) {
        String query = url.substring(SEARCH_QUERY_PREFIX.length());
        if (!query.isEmpty()) {
            mapActivity.showQuickSearch(query);
        }
    } else if (url.startsWith(SHOW_POI_PREFIX)) {
        String names = url.substring(SHOW_POI_PREFIX.length());
        if (!names.isEmpty()) {
            OsmandApplication app = mapActivity.getMyApplication();
            MapPoiTypes poiTypes = app.getPoiTypes();
            Map<PoiCategory, LinkedHashSet<String>> acceptedTypes = new LinkedHashMap<>();
            for (String name : names.split(",")) {
                AbstractPoiType abstractType = poiTypes.getAnyPoiTypeByKey(name);
                if (abstractType instanceof PoiCategory) {
                    acceptedTypes.put((PoiCategory) abstractType, null);
                } else if (abstractType instanceof PoiType) {
                    PoiType type = (PoiType) abstractType;
                    PoiCategory category = type.getCategory();
                    LinkedHashSet<String> set = acceptedTypes.get(category);
                    if (set == null) {
                        set = new LinkedHashSet<>();
                        acceptedTypes.put(category, set);
                    }
                    set.add(type.getKeyName());
                }
            }
            if (!acceptedTypes.isEmpty()) {
                PoiUIFilter filter = new PoiUIFilter("", null, acceptedTypes, app);
                filter.setName(filter.getTypesName());
                showPoiFilter(mapActivity, filter);
            }
        }
    } else if (url.equals(OPEN_ACTIVITY)) {
        if (mData != null && mData.activityJson != null) {
            openActivity(mapActivity, mData.activityJson);
        }
    } else if (url.startsWith(SHOW_CHOOSE_PLAN_PREFIX)) {
        String planType = url.substring(SHOW_CHOOSE_PLAN_PREFIX.length()).trim();
        showDialogForPlanType(mapActivity, planType);
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        AndroidUtils.startActivityIfSafe(mapActivity, intent);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OsmandApplication(net.osmand.plus.OsmandApplication) InAppPurchase(net.osmand.plus.inapp.InAppPurchases.InAppPurchase) AbstractPoiType(net.osmand.osm.AbstractPoiType) PoiType(net.osmand.osm.PoiType) InAppPurchases(net.osmand.plus.inapp.InAppPurchases) Intent(android.content.Intent) AbstractPoiType(net.osmand.osm.AbstractPoiType) MapPoiTypes(net.osmand.osm.MapPoiTypes) LinkedHashMap(java.util.LinkedHashMap) PoiUIFilter(net.osmand.plus.poi.PoiUIFilter) PoiCategory(net.osmand.osm.PoiCategory) InAppPurchaseHelper(net.osmand.plus.inapp.InAppPurchaseHelper)

Example 4 with InAppPurchaseHelper

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

the class BackupHelper method checkSubscriptions.

void checkSubscriptions(@Nullable OnUpdateSubscriptionListener listener) {
    boolean subscriptionActive = false;
    InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper();
    if (purchaseHelper != null) {
        OperationLog operationLog = new OperationLog("checkSubscriptions", DEBUG);
        String error = "";
        try {
            subscriptionActive = purchaseHelper.checkBackupSubscriptions();
        } catch (Exception e) {
            error = e.getMessage();
        }
        operationLog.finishOperation(subscriptionActive + " " + error);
    }
    if (subscriptionActive) {
        if (listener != null) {
            listener.onUpdateSubscription(STATUS_SUCCESS, "Subscriptions have been checked successfully", null);
        }
    } else {
        updateOrderId(listener);
    }
}
Also used : InAppPurchaseHelper(net.osmand.plus.inapp.InAppPurchaseHelper) OperationLog(net.osmand.OperationLog) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with InAppPurchaseHelper

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

the class BackupHelper method getOrderId.

@Nullable
public String getOrderId() {
    InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper();
    InAppSubscription purchasedSubscription = purchaseHelper.getAnyPurchasedOsmAndProSubscription();
    return purchasedSubscription != null ? purchasedSubscription.getOrderId() : null;
}
Also used : InAppPurchaseHelper(net.osmand.plus.inapp.InAppPurchaseHelper) InAppSubscription(net.osmand.plus.inapp.InAppPurchases.InAppSubscription) Nullable(androidx.annotation.Nullable)

Aggregations

InAppPurchaseHelper (net.osmand.plus.inapp.InAppPurchaseHelper)9 OsmandApplication (net.osmand.plus.OsmandApplication)5 Intent (android.content.Intent)2 View (android.view.View)2 OsmandSettings (net.osmand.plus.settings.backend.OsmandSettings)2 JSONException (org.json.JSONException)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 Bundle (android.os.Bundle)1 ViewGroup (android.view.ViewGroup)1 EditText (android.widget.EditText)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 FragmentManager (androidx.fragment.app.FragmentManager)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 WeakReference (java.lang.ref.WeakReference)1 SimpleDateFormat (java.text.SimpleDateFormat)1