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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations