use of net.osmand.plus.inapp.InAppPurchases.InAppPurchase 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.InAppPurchases.InAppPurchase in project Osmand by osmandapp.
the class PurchasingUtils method getOneTimePaymentButton.
@Nullable
public static OneTimePaymentButton getOneTimePaymentButton(OsmandApplication app) {
InAppPurchase purchase = getPlanTypePurchase(app);
if (purchase == null)
return null;
OneTimePaymentButton btn = new OneTimePaymentButton(purchase.getSku(), purchase);
btn.setTitle(app.getString(R.string.in_app_purchase_desc));
btn.setPrice(purchase.getPrice(app));
return btn;
}
use of net.osmand.plus.inapp.InAppPurchases.InAppPurchase in project Osmand by osmandapp.
the class InAppPurchaseHelperImpl method execImpl.
@Override
protected void execImpl(@NonNull final InAppPurchaseTaskType taskType, @NonNull final InAppCommand runnable) {
billingManager = new BillingManager(ctx, BASE64_ENCODED_PUBLIC_KEY, new BillingManager.BillingUpdatesListener() {
@Override
public void onBillingClientSetupFinished() {
logDebug("Setup finished.");
BillingManager billingManager = getBillingManager();
// Have we been disposed of in the meantime? If so, quit.
if (billingManager == null) {
stop(true);
return;
}
if (!billingManager.isServiceConnected()) {
// Oh noes, there was a problem.
// complain("Problem setting up in-app billing: " + result);
notifyError(taskType, billingManager.getBillingClientResponseMessage());
stop(true);
return;
}
runnable.run(InAppPurchaseHelperImpl.this);
}
@Override
public void onConsumeFinished(String token, BillingResult billingResult) {
}
@Override
public void onPurchasesUpdated(final List<Purchase> purchases) {
BillingManager billingManager = getBillingManager();
// Have we been disposed of in the meantime? If so, quit.
if (billingManager == null) {
stop(true);
return;
}
if (activeTask == InAppPurchaseTaskType.REQUEST_INVENTORY) {
List<String> skuInApps = new ArrayList<>();
for (InAppPurchase purchase : getInAppPurchases().getAllInAppPurchases(false)) {
skuInApps.add(purchase.getSku());
}
for (Purchase p : purchases) {
skuInApps.add(p.getSku());
}
billingManager.querySkuDetailsAsync(BillingClient.SkuType.INAPP, skuInApps, new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, final List<SkuDetails> skuDetailsListInApps) {
// Is it a failure?
if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
logError("Failed to query inapps sku details: " + billingResult.getResponseCode());
notifyError(InAppPurchaseTaskType.REQUEST_INVENTORY, billingResult.getDebugMessage());
stop(true);
return;
}
List<String> skuSubscriptions = new ArrayList<>();
for (InAppSubscription subscription : getInAppPurchases().getAllInAppSubscriptions()) {
skuSubscriptions.add(subscription.getSku());
}
for (Purchase p : purchases) {
skuSubscriptions.add(p.getSku());
}
skuSubscriptions.addAll(subscriptionStateMap.keySet());
BillingManager billingManager = getBillingManager();
// Have we been disposed of in the meantime? If so, quit.
if (billingManager == null) {
stop(true);
return;
}
billingManager.querySkuDetailsAsync(BillingClient.SkuType.SUBS, skuSubscriptions, new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, final List<SkuDetails> skuDetailsListSubscriptions) {
// Is it a failure?
if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
logError("Failed to query subscriptipons sku details: " + billingResult.getResponseCode());
notifyError(InAppPurchaseTaskType.REQUEST_INVENTORY, billingResult.getDebugMessage());
stop(true);
return;
}
List<SkuDetails> skuDetailsList = new ArrayList<>(skuDetailsListInApps);
skuDetailsList.addAll(skuDetailsListSubscriptions);
InAppPurchaseHelperImpl.this.skuDetailsList = skuDetailsList;
getSkuDetailsResponseListener(runnable.userRequested()).onSkuDetailsResponse(billingResult, skuDetailsList);
}
});
}
});
}
for (Purchase purchase : purchases) {
InAppSubscription subscription = getSubscriptions().getSubscriptionBySku(purchase.getSku());
if (!purchase.isAcknowledged() || (subscription != null && !subscription.isPurchased())) {
onPurchaseFinished(purchase);
}
}
}
@Override
public void onPurchaseCanceled() {
stop(true);
}
});
}
use of net.osmand.plus.inapp.InAppPurchases.InAppPurchase in project Osmand by osmandapp.
the class InAppPurchaseHelperImpl method purchaseFullVersion.
@Override
public void purchaseFullVersion(@NonNull Activity activity) throws UnsupportedOperationException {
notifyShowProgress(InAppPurchaseTaskType.PURCHASE_FULL_VERSION);
InAppPurchase fullVersion = purchases.getFullVersion();
if (fullVersion != null) {
exec(InAppPurchaseTaskType.PURCHASE_FULL_VERSION, new PurchaseInAppCommand(new WeakReference<>(activity), fullVersion.getSku()));
}
}
use of net.osmand.plus.inapp.InAppPurchases.InAppPurchase in project Osmand by osmandapp.
the class InAppPurchaseHelperImpl method getRequestInventoryCommand.
@Override
protected InAppCommand getRequestInventoryCommand(boolean userRequested) {
return new InAppCommand() {
@Override
protected void commandDone() {
super.commandDone();
inventoryRequested = false;
}
@Override
public void run(InAppPurchaseHelper helper) {
logDebug("Setup successful. Querying inventory.");
try {
productInfos = new ArrayList<>();
obtainOwnedSubscriptions();
} catch (Exception e) {
logError("queryInventoryAsync Error", e);
notifyDismissProgress(InAppPurchaseTaskType.REQUEST_INVENTORY);
stop(true);
commandDone();
}
}
private void obtainOwnedSubscriptions() {
if (uiActivity != null) {
IapRequestHelper.obtainOwnedPurchases(getIapClient(), IapClient.PriceType.IN_APP_SUBSCRIPTION, null, new IapApiCallback<OwnedPurchasesResult>() {
@Override
public void onSuccess(OwnedPurchasesResult result) {
ownedSubscriptions = result;
obtainOwnedInApps(null);
}
@Override
public void onFail(Exception e) {
logError("obtainOwnedSubscriptions exception", e);
ExceptionHandle.handle((Activity) uiActivity, e);
commandDone();
}
});
} else {
commandDone();
}
}
private void obtainOwnedInApps(final String continuationToken) {
if (uiActivity != null) {
// Query users' purchased non-consumable products.
IapRequestHelper.obtainOwnedPurchases(getIapClient(), IapClient.PriceType.IN_APP_NONCONSUMABLE, continuationToken, new IapApiCallback<OwnedPurchasesResult>() {
@Override
public void onSuccess(OwnedPurchasesResult result) {
ownedInApps.add(result);
if (result != null && !TextUtils.isEmpty(result.getContinuationToken())) {
obtainOwnedInApps(result.getContinuationToken());
} else {
obtainSubscriptionsInfo();
}
}
@Override
public void onFail(Exception e) {
logError("obtainOwnedInApps exception", e);
ExceptionHandle.handle((Activity) uiActivity, e);
commandDone();
}
});
} else {
commandDone();
}
}
private void obtainSubscriptionsInfo() {
if (uiActivity != null) {
Set<String> productIds = new HashSet<>();
List<InAppSubscription> subscriptions = purchases.getSubscriptions().getAllSubscriptions();
for (InAppSubscription s : subscriptions) {
productIds.add(s.getSku());
}
productIds.addAll(ownedSubscriptions.getItemList());
IapRequestHelper.obtainProductInfo(getIapClient(), new ArrayList<>(productIds), IapClient.PriceType.IN_APP_SUBSCRIPTION, new IapApiCallback<ProductInfoResult>() {
@Override
public void onSuccess(final ProductInfoResult result) {
if (result != null && result.getProductInfoList() != null) {
productInfos.addAll(result.getProductInfoList());
}
obtainInAppsInfo();
}
@Override
public void onFail(Exception e) {
int errorCode = ExceptionHandle.handle((Activity) uiActivity, e);
if (ExceptionHandle.SOLVED != errorCode) {
LOG.error("Unknown error");
}
commandDone();
}
});
} else {
commandDone();
}
}
private void obtainInAppsInfo() {
if (uiActivity != null) {
Set<String> productIds = new HashSet<>();
for (InAppPurchase purchase : getInAppPurchases().getAllInAppPurchases(false)) {
productIds.add(purchase.getSku());
}
for (OwnedPurchasesResult result : ownedInApps) {
productIds.addAll(result.getItemList());
}
IapRequestHelper.obtainProductInfo(getIapClient(), new ArrayList<>(productIds), IapClient.PriceType.IN_APP_NONCONSUMABLE, new IapApiCallback<ProductInfoResult>() {
@Override
public void onSuccess(ProductInfoResult result) {
if (result != null && result.getProductInfoList() != null) {
productInfos.addAll(result.getProductInfoList());
}
processInventory();
}
@Override
public void onFail(Exception e) {
int errorCode = ExceptionHandle.handle((Activity) uiActivity, e);
if (ExceptionHandle.SOLVED != errorCode) {
LOG.error("Unknown error");
}
commandDone();
}
});
} else {
commandDone();
}
}
private void processInventory() {
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!
*/
List<String> allOwnedSubscriptionSkus = ownedSubscriptions.getItemList();
for (InAppSubscription s : getSubscriptions().getAllSubscriptions()) {
if (hasDetails(s.getSku())) {
InAppPurchaseData purchaseData = getPurchaseData(s.getSku());
ProductInfo liveUpdatesInfo = getProductInfo(s.getSku());
if (liveUpdatesInfo != null) {
fetchInAppPurchase(s, liveUpdatesInfo, purchaseData);
}
allOwnedSubscriptionSkus.remove(s.getSku());
}
}
for (String sku : allOwnedSubscriptionSkus) {
InAppPurchaseData purchaseData = getPurchaseData(sku);
ProductInfo liveUpdatesInfo = getProductInfo(sku);
if (liveUpdatesInfo != null) {
InAppSubscription s = getSubscriptions().upgradeSubscription(sku);
if (s == null) {
s = new InAppPurchaseLiveUpdatesOldSubscription(liveUpdatesInfo);
}
fetchInAppPurchase(s, liveUpdatesInfo, purchaseData);
}
}
InAppPurchase fullVersion = getFullVersion();
if (hasDetails(fullVersion.getSku())) {
InAppPurchaseData purchaseData = getPurchaseData(fullVersion.getSku());
ProductInfo fullPriceDetails = getProductInfo(fullVersion.getSku());
if (fullPriceDetails != null) {
fetchInAppPurchase(fullVersion, fullPriceDetails, purchaseData);
}
}
InAppPurchase depthContours = getDepthContours();
if (hasDetails(depthContours.getSku())) {
InAppPurchaseData purchaseData = getPurchaseData(depthContours.getSku());
ProductInfo depthContoursDetails = getProductInfo(depthContours.getSku());
if (depthContoursDetails != null) {
fetchInAppPurchase(depthContours, depthContoursDetails, purchaseData);
}
}
InAppPurchase contourLines = getContourLines();
if (hasDetails(contourLines.getSku())) {
InAppPurchaseData purchaseData = getPurchaseData(contourLines.getSku());
ProductInfo contourLinesDetails = getProductInfo(contourLines.getSku());
if (contourLinesDetails != null) {
fetchInAppPurchase(contourLines, contourLinesDetails, purchaseData);
}
}
if (getPurchaseData(fullVersion.getSku()) != null) {
ctx.getSettings().FULL_VERSION_PURCHASED.set(true);
}
if (getPurchaseData(depthContours.getSku()) != null) {
ctx.getSettings().DEPTH_CONTOURS_PURCHASED.set(true);
}
if (getPurchaseData(contourLines.getSku()) != null) {
ctx.getSettings().CONTOUR_LINES_PURCHASED.set(true);
}
// Do we have the live updates?
boolean subscribedToLiveUpdates = false;
boolean subscribedToOsmAndPro = false;
boolean subscribedToMaps = false;
List<InAppPurchaseData> subscriptionPurchases = new ArrayList<>();
for (InAppSubscription s : getSubscriptions().getAllSubscriptions()) {
InAppPurchaseData purchaseData = getPurchaseData(s.getSku());
if (purchaseData != null || s.getState().isActive()) {
if (purchaseData != null) {
subscriptionPurchases.add(purchaseData);
}
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<InAppPurchaseData> tokensToSend = new ArrayList<>();
if (subscriptionPurchases.size() > 0) {
List<String> tokensSent = Arrays.asList(settings.BILLING_PURCHASE_TOKENS_SENT.get().split(";"));
for (InAppPurchaseData purchase : subscriptionPurchases) {
if ((Algorithms.isEmpty(settings.BILLING_USER_ID.get()) || Algorithms.isEmpty(settings.BILLING_USER_TOKEN.get())) && !Algorithms.isEmpty(purchase.getDeveloperPayload())) {
String payload = purchase.getDeveloperPayload();
if (!Algorithms.isEmpty(payload)) {
String[] arr = payload.split(" ");
if (arr.length > 0) {
settings.BILLING_USER_ID.set(arr[0]);
}
if (arr.length > 1) {
token = arr[1];
settings.BILLING_USER_TOKEN.set(token);
}
}
}
if (!tokensSent.contains(purchase.getProductId())) {
tokensToSend.add(purchase);
}
}
}
List<PurchaseInfo> purchaseInfoList = new ArrayList<>();
for (InAppPurchaseData purchase : tokensToSend) {
purchaseInfoList.add(getPurchaseInfo(purchase));
}
onSkuDetailsResponseDone(purchaseInfoList, userRequested);
}
private void onSubscriptionExpired() {
if (!isDepthContoursPurchased(ctx)) {
ctx.getSettings().getCustomRenderBooleanProperty("depthContours").set(false);
}
}
};
}
Aggregations