Search in sources :

Example 1 with RequestResponse

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

the class InAppPurchaseHelper method sendTokens.

protected void sendTokens(@NonNull final List<PurchaseInfo> purchaseInfoList, final OnRequestResultListener listener) {
    final String userId = ctx.getSettings().BILLING_USER_ID.get();
    final String token = ctx.getSettings().BILLING_USER_TOKEN.get();
    final String email = ctx.getSettings().BILLING_USER_EMAIL.get();
    try {
        String url = "https://osmand.net/subscription/purchased";
        String userOperation = "Sending purchase info...";
        final List<Request> requests = new ArrayList<>();
        for (PurchaseInfo info : purchaseInfoList) {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("userid", userId);
            parameters.put("sku", info.getSku());
            parameters.put("orderId", info.getOrderId());
            parameters.put("purchaseToken", info.getPurchaseToken());
            parameters.put("email", email);
            parameters.put("token", token);
            addUserInfo(parameters);
            requests.add(new Request(url, parameters, userOperation, true, true));
        }
        AndroidNetworkUtils.sendRequestsAsync(ctx, requests, new OnSendRequestsListener() {

            @Override
            public void onRequestSending(@NonNull Request request) {
            }

            @Override
            public void onRequestSent(@NonNull RequestResponse response) {
            }

            @Override
            public void onRequestsSent(@NonNull List<RequestResponse> results) {
                for (RequestResponse rr : results) {
                    String sku = rr.getRequest().getParameters().get("sku");
                    PurchaseInfo info = getPurchaseInfo(sku);
                    if (info != null) {
                        updateSentTokens(info);
                        String result = rr.getResponse();
                        if (result != null) {
                            try {
                                JSONObject obj = new JSONObject(result);
                                if (obj.has("error")) {
                                    complain("SendToken Error: " + obj.getString("error") + " (response=" + result + " google=" + info.toString() + ")");
                                }
                            } catch (JSONException e) {
                                logError("SendToken", e);
                                complain("SendToken Error: " + (e.getMessage() != null ? e.getMessage() : "JSONException") + " (response=" + result + " google=" + info.toString() + ")");
                            }
                        }
                    }
                }
                if (listener != null) {
                    listener.onResult("OK", null, null);
                }
            }

            private void updateSentTokens(@NonNull PurchaseInfo info) {
                String tokensSentStr = ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.get();
                Set<String> tokensSent = new HashSet<>(Arrays.asList(tokensSentStr.split(";")));
                tokensSent.add(info.getSku());
                ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.set(TextUtils.join(";", tokensSent));
            }

            @Nullable
            private PurchaseInfo getPurchaseInfo(String sku) {
                for (PurchaseInfo info : purchaseInfoList) {
                    if (info.getSku().equals(sku)) {
                        return info;
                    }
                }
                return null;
            }
        });
    } catch (Exception e) {
        logError("SendToken Error", e);
        if (listener != null) {
            listener.onResult("Error", null, null);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) OnSendRequestsListener(net.osmand.plus.utils.AndroidNetworkUtils.OnSendRequestsListener) Request(net.osmand.plus.utils.AndroidNetworkUtils.Request) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) RequestResponse(net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse) JSONException(org.json.JSONException) ParseException(java.text.ParseException) JSONObject(org.json.JSONObject) PurchaseInfo(net.osmand.plus.inapp.InAppPurchases.PurchaseInfo) Nullable(androidx.annotation.Nullable)

Example 2 with RequestResponse

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

the class BaseDeleteFilesCommand method onPostExecute.

@Override
protected void onPostExecute(Object o) {
    List<OnDeleteFilesListener> listeners = getListeners();
    if (!listeners.isEmpty()) {
        Map<RemoteFile, String> errors = new HashMap<>();
        for (DeleteRemoteFileTask task : tasks) {
            boolean success;
            String message = null;
            RequestResponse response = task.response;
            if (response != null) {
                String errorStr = response.getError();
                if (!Algorithms.isEmpty(errorStr)) {
                    message = new BackupError(errorStr).toString();
                    success = false;
                } else {
                    String responseStr = response.getResponse();
                    try {
                        JSONObject result = new JSONObject(responseStr);
                        if (result.has("status") && "ok".equals(result.getString("status"))) {
                            success = true;
                        } else {
                            message = "Unknown error";
                            success = false;
                        }
                    } catch (JSONException e) {
                        message = "Json parsing error";
                        success = false;
                    }
                }
                if (!success) {
                    errors.put(task.remoteFile, message);
                }
            }
        }
        for (OnDeleteFilesListener listener : listeners) {
            listener.onFilesDeleteDone(errors);
        }
    }
    if (listener != null) {
        getListeners().remove(listener);
    }
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONException(org.json.JSONException) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) RequestResponse(net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse) RemoteFile(net.osmand.plus.backup.RemoteFile) BackupError(net.osmand.plus.backup.BackupError)

Aggregations

HashMap (java.util.HashMap)2 RequestResponse (net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Nullable (androidx.annotation.Nullable)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 BackupError (net.osmand.plus.backup.BackupError)1 OnDeleteFilesListener (net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener)1 RemoteFile (net.osmand.plus.backup.RemoteFile)1 PurchaseInfo (net.osmand.plus.inapp.InAppPurchases.PurchaseInfo)1 OnSendRequestsListener (net.osmand.plus.utils.AndroidNetworkUtils.OnSendRequestsListener)1 Request (net.osmand.plus.utils.AndroidNetworkUtils.Request)1