[android] Fixed review notes

This commit is contained in:
Dmitry Donskoy 2019-12-25 10:09:57 +03:00 committed by Aleksandr Zatsepin
parent 6a2c22c195
commit 985cb5ec0f
4 changed files with 49 additions and 3 deletions

View file

@ -6,6 +6,8 @@ import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import com.google.gson.Gson;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import java.io.File;
import java.util.Objects;
@ -13,6 +15,8 @@ import java.util.Objects;
public class FileUploadWorker extends Worker
{
static final String PARAMS = "params";
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.NETWORK);
private static final String TAG = FileUploadWorker.class.getSimpleName();
public FileUploadWorker(@NonNull Context context, @NonNull WorkerParameters workerParams)
{
@ -27,14 +31,21 @@ public class FileUploadWorker extends Worker
Gson gson = new Gson();
HttpPayload payload = gson.fromJson(rawJson, HttpPayload.class);
LOGGER.d(TAG, "Payload '" + payload + "' going to be uploaded");
File file = new File(payload.getFilePath());
if (!file.exists())
return Result.success();
return Result.failure();
HttpUploader uploader = new HttpUploader(payload);
HttpUploader.Result result = uploader.upload();
LOGGER.d(TAG, "Upload finished with result '" + result + "' ");
file.delete();
return result.getHttpCode() / 100 != 2 ? Result.failure() : Result.success();
if (result.getHttpCode() / 100 != 2)
return Result.retry();
boolean isDeleted = file.delete();
LOGGER.d(TAG, "File deleted: " + isDeleted + " " + payload.getFilePath());
return Result.success();
}
}

View file

@ -1,13 +1,20 @@
package com.mapswithme.util;
import androidx.annotation.NonNull;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
import com.google.gson.Gson;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
public class HttpBackgroundUploader extends AbstractHttpUploader
{
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.NETWORK);
private static final String TAG = HttpBackgroundUploader.class.getSimpleName();
public HttpBackgroundUploader(@NonNull HttpPayload payload)
{
super(payload);
@ -18,8 +25,13 @@ public class HttpBackgroundUploader extends AbstractHttpUploader
Gson gson = new Gson();
String json = gson.toJson(getPayload());
Data.Builder builder = new Data.Builder().putString(FileUploadWorker.PARAMS, json);
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.build();
OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(FileUploadWorker.class)
.setConstraints(constraints)
.setInputData(builder.build()).build();
LOGGER.d(TAG, "Request " + request + "' going to be enqueued");
WorkManager.getInstance().enqueue(request);
}
}

View file

@ -83,4 +83,18 @@ public class HttpPayload
{
return mNeedClientAuth;
}
@Override
public String toString()
{
return "HttpPayload{" +
"mMethod='" + mMethod + '\'' +
", mUrl='" + mUrl + '\'' +
", mParams=" + mParams +
", mHeaders=" + mHeaders +
", mFileKey='" + mFileKey + '\'' +
", mFilePath='" + mFilePath + '\'' +
", mNeedClientAuth=" + mNeedClientAuth +
'}';
}
}

View file

@ -260,6 +260,15 @@ public final class HttpUploader extends AbstractHttpUploader
{
return mDescription;
}
@Override
public String toString()
{
return "Result{" +
"mHttpCode=" + mHttpCode +
", mDescription='" + mDescription + '\'' +
'}';
}
}
@NonNull