Search in sources :

Example 1 with OnRequestResultListener

use of net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener in project Osmand by osmandapp.

the class InAppPurchaseHelper method onPurchaseDone.

protected void onPurchaseDone(PurchaseInfo info) {
    logDebug("Purchase successful.");
    InAppSubscription subscription = getSubscriptions().getSubscriptionBySku(info.getSku());
    InAppPurchase fullVersion = getFullVersion();
    InAppPurchase depthContours = getDepthContours();
    InAppPurchase contourLines = getContourLines();
    if (subscription != null) {
        final boolean maps = purchases.isMapsSubscription(subscription);
        final boolean liveUpdates = purchases.isLiveUpdatesSubscription(subscription);
        final boolean pro = purchases.isOsmAndProSubscription(subscription);
        // bought live updates
        if (maps) {
            logDebug("Maps subscription purchased.");
        } else if (liveUpdates) {
            logDebug("Live updates subscription purchased.");
        } else if (pro) {
            logDebug("OsmAnd Pro subscription purchased.");
        }
        final String sku = subscription.getSku();
        subscription.setPurchaseState(PurchaseState.PURCHASED);
        subscription.setPurchaseInfo(ctx, info);
        subscription.setState(ctx, SubscriptionState.UNDEFINED);
        logDebug("Sending tokens...");
        sendTokens(Collections.singletonList(info), new OnRequestResultListener() {

            @Override
            public void onResult(@Nullable String result, @Nullable String error, @Nullable Integer resultCode) {
                logDebug("Tokens sent");
                boolean active = false;
                if (liveUpdates || pro) {
                    active = ctx.getSettings().LIVE_UPDATES_PURCHASED.get();
                    ctx.getSettings().LIVE_UPDATES_PURCHASED.set(true);
                    if (pro) {
                        ctx.getSettings().OSMAND_PRO_PURCHASED.set(true);
                    }
                    ctx.getSettings().getCustomRenderBooleanProperty("depthContours").set(true);
                    ctx.getSettings().LIVE_UPDATES_EXPIRED_FIRST_DLG_SHOWN_TIME.set(0L);
                    ctx.getSettings().LIVE_UPDATES_EXPIRED_SECOND_DLG_SHOWN_TIME.set(0L);
                } else if (maps) {
                    active = ctx.getSettings().OSMAND_MAPS_PURCHASED.get();
                    ctx.getSettings().OSMAND_MAPS_PURCHASED.set(true);
                    ctx.getSettings().getCustomRenderBooleanProperty("depthContours").set(true);
                }
                notifyDismissProgress(InAppPurchaseTaskType.PURCHASE_SUBSCRIPTION);
                notifyItemPurchased(sku, active);
                refreshAndroidAuto();
                stop(true);
            }
        });
    } else if (fullVersion != null && info.getSku().equals(fullVersion.getSku())) {
        // bought full version
        fullVersion.setPurchaseState(PurchaseState.PURCHASED);
        fullVersion.setPurchaseInfo(ctx, info);
        logDebug("Full version purchased.");
        showToast(ctx.getString(R.string.full_version_thanks));
        ctx.getSettings().FULL_VERSION_PURCHASED.set(true);
        notifyDismissProgress(InAppPurchaseTaskType.PURCHASE_FULL_VERSION);
        notifyItemPurchased(fullVersion.getSku(), false);
        refreshAndroidAuto();
        stop(true);
    } else if (depthContours != null && info.getSku().equals(depthContours.getSku())) {
        // bought sea depth contours
        depthContours.setPurchaseState(PurchaseState.PURCHASED);
        depthContours.setPurchaseInfo(ctx, info);
        logDebug("Sea depth contours purchased.");
        showToast(ctx.getString(R.string.sea_depth_thanks));
        ctx.getSettings().DEPTH_CONTOURS_PURCHASED.set(true);
        ctx.getSettings().getCustomRenderBooleanProperty("depthContours").set(true);
        notifyDismissProgress(InAppPurchaseTaskType.PURCHASE_DEPTH_CONTOURS);
        notifyItemPurchased(depthContours.getSku(), false);
        stop(true);
    } else if (contourLines != null && info.getSku().equals(contourLines.getSku())) {
        // bought contour lines
        contourLines.setPurchaseState(PurchaseState.PURCHASED);
        contourLines.setPurchaseInfo(ctx, info);
        logDebug("Contours lines purchased.");
        showToast(ctx.getString(R.string.contour_lines_thanks));
        ctx.getSettings().CONTOUR_LINES_PURCHASED.set(true);
        notifyDismissProgress(InAppPurchaseTaskType.PURCHASE_CONTOUR_LINES);
        notifyItemPurchased(contourLines.getSku(), false);
        stop(true);
    } else {
        notifyDismissProgress(activeTask);
        stop(true);
    }
}
Also used : InAppPurchase(net.osmand.plus.inapp.InAppPurchases.InAppPurchase) OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) InAppSubscription(net.osmand.plus.inapp.InAppPurchases.InAppSubscription)

Example 2 with OnRequestResultListener

use of net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener in project Osmand by osmandapp.

the class InAppPurchaseHelper method onSkuDetailsResponseDone.

protected void onSkuDetailsResponseDone(@NonNull List<PurchaseInfo> purchaseInfoList, boolean userRequested) {
    final OnRequestResultListener listener = new OnRequestResultListener() {

        @Override
        public void onResult(@Nullable String result, @Nullable String error, @Nullable Integer resultCode) {
            notifyDismissProgress(InAppPurchaseTaskType.REQUEST_INVENTORY);
            notifyGetItems();
            stop(true);
            logDebug("Initial inapp query finished");
            if (userRequested) {
                showToast(ctx.getString(R.string.purchases_restored));
            }
        }
    };
    if (purchaseInfoList.size() > 0) {
        sendTokens(purchaseInfoList, listener);
    } else {
        listener.onResult("OK", null, null);
    }
}
Also used : OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) Nullable(androidx.annotation.Nullable)

Example 3 with OnRequestResultListener

use of net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener in project Osmand by osmandapp.

the class DeleteAllFilesCommand method getDeleteAllFilesListener.

private OnRequestResultListener getDeleteAllFilesListener() {
    return (resultJson, error, resultCode) -> {
        int status;
        String message;
        List<RemoteFile> remoteFiles = new ArrayList<>();
        if (!Algorithms.isEmpty(error)) {
            status = STATUS_SERVER_ERROR;
            message = "Download file list error: " + new BackupError(error);
        } else if (!Algorithms.isEmpty(resultJson)) {
            try {
                JSONObject result = new JSONObject(resultJson);
                JSONArray files = result.getJSONArray("allFiles");
                for (int i = 0; i < files.length(); i++) {
                    remoteFiles.add(new RemoteFile(files.getJSONObject(i)));
                }
                status = STATUS_SUCCESS;
                message = "OK";
            } catch (JSONException e) {
                status = STATUS_PARSE_JSON_ERROR;
                message = "Download file list error: json parsing";
            }
        } else {
            status = STATUS_EMPTY_RESPONSE_ERROR;
            message = "Download file list error: empty response";
        }
        if (status != STATUS_SUCCESS) {
            publishProgress(status, message);
        } else {
            List<RemoteFile> filesToDelete = new ArrayList<>();
            if (types != null) {
                for (RemoteFile file : remoteFiles) {
                    ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(file);
                    if (types.contains(exportType)) {
                        filesToDelete.add(file);
                    }
                }
            } else {
                filesToDelete.addAll(remoteFiles);
            }
            if (!filesToDelete.isEmpty()) {
                publishProgress(filesToDelete);
                deleteFiles(filesToDelete);
            } else {
                publishProgress(Collections.emptyMap());
            }
        }
    };
}
Also used : BackupHelper(net.osmand.plus.backup.BackupHelper) OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) STATUS_EMPTY_RESPONSE_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_EMPTY_RESPONSE_ERROR) STATUS_SUCCESS(net.osmand.plus.backup.BackupHelper.STATUS_SUCCESS) NonNull(androidx.annotation.NonNull) LIST_FILES_URL(net.osmand.plus.backup.BackupHelper.LIST_FILES_URL) HashMap(java.util.HashMap) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) BackupError(net.osmand.plus.backup.BackupError) AndroidNetworkUtils(net.osmand.plus.utils.AndroidNetworkUtils) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType) ArrayList(java.util.ArrayList) STATUS_PARSE_JSON_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_PARSE_JSON_ERROR) JSONException(org.json.JSONException) List(java.util.List) Nullable(androidx.annotation.Nullable) JSONObject(org.json.JSONObject) STATUS_SERVER_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_SERVER_ERROR) Map(java.util.Map) Algorithms(net.osmand.util.Algorithms) RemoteFile(net.osmand.plus.backup.RemoteFile) Collections(java.util.Collections) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ArrayList(java.util.ArrayList) List(java.util.List) BackupError(net.osmand.plus.backup.BackupError) RemoteFile(net.osmand.plus.backup.RemoteFile) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Example 4 with OnRequestResultListener

use of net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener in project Osmand by osmandapp.

the class DeleteOldFilesCommand method getDeleteOldFilesListener.

private OnRequestResultListener getDeleteOldFilesListener() {
    return (resultJson, error, resultCode) -> {
        int status;
        String message;
        List<RemoteFile> remoteFiles = new ArrayList<>();
        if (!Algorithms.isEmpty(error)) {
            status = STATUS_SERVER_ERROR;
            message = "Download file list error: " + new BackupError(error);
        } else if (!Algorithms.isEmpty(resultJson)) {
            try {
                JSONObject result = new JSONObject(resultJson);
                JSONArray allFiles = result.getJSONArray("allFiles");
                for (int i = 0; i < allFiles.length(); i++) {
                    remoteFiles.add(new RemoteFile(allFiles.getJSONObject(i)));
                }
                JSONArray uniqueFiles = result.getJSONArray("uniqueFiles");
                for (int i = 0; i < uniqueFiles.length(); i++) {
                    remoteFiles.remove(new RemoteFile(uniqueFiles.getJSONObject(i)));
                }
                status = STATUS_SUCCESS;
                message = "OK";
            } catch (JSONException e) {
                status = STATUS_PARSE_JSON_ERROR;
                message = "Download file list error: json parsing";
            }
        } else {
            status = STATUS_EMPTY_RESPONSE_ERROR;
            message = "Download file list error: empty response";
        }
        if (status != STATUS_SUCCESS) {
            publishProgress(status, message);
        } else {
            List<RemoteFile> filesToDelete = new ArrayList<>();
            if (types != null) {
                for (RemoteFile file : remoteFiles) {
                    ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(file);
                    if (types.contains(exportType)) {
                        filesToDelete.add(file);
                    }
                }
            } else {
                filesToDelete.addAll(remoteFiles);
            }
            if (!filesToDelete.isEmpty()) {
                publishProgress(filesToDelete);
                deleteFiles(filesToDelete);
            } else {
                publishProgress(Collections.emptyMap());
            }
        }
    };
}
Also used : BackupHelper(net.osmand.plus.backup.BackupHelper) OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) STATUS_EMPTY_RESPONSE_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_EMPTY_RESPONSE_ERROR) STATUS_SUCCESS(net.osmand.plus.backup.BackupHelper.STATUS_SUCCESS) NonNull(androidx.annotation.NonNull) LIST_FILES_URL(net.osmand.plus.backup.BackupHelper.LIST_FILES_URL) HashMap(java.util.HashMap) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) BackupError(net.osmand.plus.backup.BackupError) AndroidNetworkUtils(net.osmand.plus.utils.AndroidNetworkUtils) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType) ArrayList(java.util.ArrayList) STATUS_PARSE_JSON_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_PARSE_JSON_ERROR) JSONException(org.json.JSONException) List(java.util.List) Nullable(androidx.annotation.Nullable) JSONObject(org.json.JSONObject) STATUS_SERVER_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_SERVER_ERROR) Map(java.util.Map) Algorithms(net.osmand.util.Algorithms) RemoteFile(net.osmand.plus.backup.RemoteFile) Collections(java.util.Collections) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ArrayList(java.util.ArrayList) List(java.util.List) BackupError(net.osmand.plus.backup.BackupError) RemoteFile(net.osmand.plus.backup.RemoteFile) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Example 5 with OnRequestResultListener

use of net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener in project Osmand by osmandapp.

the class CustomRegion method loadDynamicIndexItems.

public void loadDynamicIndexItems(final OsmandApplication app) {
    if (dynamicItemsJson == null && dynamicDownloadItems != null && !Algorithms.isEmpty(dynamicDownloadItems.url) && app.getSettings().isInternetConnectionAvailable()) {
        OnRequestResultListener resultListener = new OnRequestResultListener() {

            @Override
            public void onResult(@Nullable String result, @Nullable String error, @Nullable Integer resultCode) {
                if (!Algorithms.isEmpty(result)) {
                    if ("json".equalsIgnoreCase(dynamicDownloadItems.format)) {
                        dynamicItemsJson = mapJsonItems(result);
                    }
                    app.getDownloadThread().runReloadIndexFilesSilent();
                }
            }
        };
        AndroidNetworkUtils.sendRequestAsync(app, dynamicDownloadItems.getUrl(), null, null, false, false, resultListener);
    }
}
Also used : OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) Nullable(androidx.annotation.Nullable)

Aggregations

OnRequestResultListener (net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener)6 Nullable (androidx.annotation.Nullable)4 NonNull (androidx.annotation.NonNull)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 BackupError (net.osmand.plus.backup.BackupError)2 BackupHelper (net.osmand.plus.backup.BackupHelper)2 LIST_FILES_URL (net.osmand.plus.backup.BackupHelper.LIST_FILES_URL)2 STATUS_EMPTY_RESPONSE_ERROR (net.osmand.plus.backup.BackupHelper.STATUS_EMPTY_RESPONSE_ERROR)2 STATUS_PARSE_JSON_ERROR (net.osmand.plus.backup.BackupHelper.STATUS_PARSE_JSON_ERROR)2 STATUS_SERVER_ERROR (net.osmand.plus.backup.BackupHelper.STATUS_SERVER_ERROR)2 STATUS_SUCCESS (net.osmand.plus.backup.BackupHelper.STATUS_SUCCESS)2 OnDeleteFilesListener (net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener)2 RemoteFile (net.osmand.plus.backup.RemoteFile)2 ExportSettingsType (net.osmand.plus.settings.backend.ExportSettingsType)2 AndroidNetworkUtils (net.osmand.plus.utils.AndroidNetworkUtils)2 Algorithms (net.osmand.util.Algorithms)2