use of net.osmand.plus.utils.AndroidNetworkUtils.Request 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);
}
}
}
use of net.osmand.plus.utils.AndroidNetworkUtils.Request in project Osmand by osmandapp.
the class BaseDeleteFilesCommand method deleteFiles.
protected void deleteFiles(@NonNull List<RemoteFile> remoteFiles) {
Map<String, String> commonParameters = new HashMap<>();
commonParameters.put("deviceid", getHelper().getDeviceId());
commonParameters.put("accessToken", getHelper().getAccessToken());
List<DeleteRemoteFileTask> tasks = new ArrayList<>();
for (RemoteFile remoteFile : remoteFiles) {
Map<String, String> parameters = new HashMap<>(commonParameters);
parameters.put("name", remoteFile.getName());
parameters.put("type", remoteFile.getType());
if (byVersion) {
parameters.put("updatetime", String.valueOf(remoteFile.getUpdatetimems()));
}
Request r = new Request(byVersion ? DELETE_FILE_VERSION_URL : DELETE_FILE_URL, parameters, null, false, true);
tasks.add(new DeleteRemoteFileTask(getApp(), r, remoteFile, byVersion));
}
ThreadPoolTaskExecutor<DeleteRemoteFileTask> executor = new ThreadPoolTaskExecutor<>(new OnThreadPoolTaskExecutorListener<DeleteRemoteFileTask>() {
@Override
public void onTaskStarted(@NonNull DeleteRemoteFileTask task) {
}
@Override
public void onTaskFinished(@NonNull DeleteRemoteFileTask task) {
publishProgress(task);
}
@Override
public void onTasksFinished(@NonNull List<DeleteRemoteFileTask> tasks) {
BaseDeleteFilesCommand.this.tasks = tasks;
}
});
executor.run(tasks);
}
Aggregations