use of net.osmand.OperationLog in project Osmand by osmandapp.
the class BackupHelper method uploadFile.
@Nullable
String uploadFile(@NonNull String fileName, @NonNull String type, @NonNull StreamWriter streamWriter, final long uploadTime, @Nullable final OnUploadFileListener listener) throws UserNotRegisteredException {
checkRegistered();
Map<String, String> params = new HashMap<>();
params.put("deviceid", getDeviceId());
params.put("accessToken", getAccessToken());
params.put("name", fileName);
params.put("type", type);
params.put("clienttime", String.valueOf(uploadTime));
Map<String, String> headers = new HashMap<>();
headers.put("Accept-Encoding", "deflate, gzip");
OperationLog operationLog = new OperationLog("uploadFile", DEBUG);
operationLog.startOperation(type + " " + fileName);
String error = AndroidNetworkUtils.uploadFile(UPLOAD_FILE_URL, streamWriter, fileName, true, params, headers, new AbstractProgress() {
private int work = 0;
private int progress = 0;
private int deltaProgress = 0;
@Override
public void startWork(int work) {
if (listener != null) {
this.work = work > 0 ? work : 1;
listener.onFileUploadStarted(type, fileName, work);
}
}
@Override
public void progress(int deltaWork) {
if (listener != null) {
deltaProgress += deltaWork;
if ((deltaProgress > (work / 100)) || ((progress + deltaProgress) >= work)) {
progress += deltaProgress;
listener.onFileUploadProgress(type, fileName, progress, deltaProgress);
deltaProgress = 0;
}
}
}
@Override
public boolean isInterrupted() {
if (listener != null) {
return listener.isUploadCancelled();
}
return super.isInterrupted();
}
});
if (error == null) {
updateFileUploadTime(type, fileName, uploadTime);
}
if (listener != null) {
listener.onFileUploadDone(type, fileName, uploadTime, error);
}
operationLog.finishOperation(type + " " + fileName + (error != null ? " Error: " + new BackupError(error) : " OK"));
return error;
}
use of net.osmand.OperationLog in project Osmand by osmandapp.
the class BackupHelper method downloadFileList.
void downloadFileList(@Nullable final OnDownloadFileListListener listener) throws UserNotRegisteredException {
checkRegistered();
Map<String, String> params = new HashMap<>();
params.put("deviceid", getDeviceId());
params.put("accessToken", getAccessToken());
params.put("allVersions", "true");
final OperationLog operationLog = new OperationLog("downloadFileList", DEBUG);
operationLog.startOperation();
AndroidNetworkUtils.sendRequest(app, LIST_FILES_URL, params, "Download file list", false, false, (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 res = new JSONObject(resultJson);
String totalZipSize = res.getString("totalZipSize");
String totalFiles = res.getString("totalFiles");
String totalFileVersions = res.getString("totalFileVersions");
JSONArray allFiles = res.getJSONArray("allFiles");
for (int i = 0; i < allFiles.length(); i++) {
remoteFiles.add(new RemoteFile(allFiles.getJSONObject(i)));
}
status = STATUS_SUCCESS;
message = "Total files: " + totalFiles + " " + "Total zip size: " + AndroidUtils.formatSize(app, Long.parseLong(totalZipSize)) + " " + "Total file versions: " + totalFileVersions;
} 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";
}
operationLog.finishOperation("(" + status + "): " + message);
if (listener != null) {
listener.onDownloadFileList(status, message, remoteFiles);
}
});
}
use of net.osmand.OperationLog in project Osmand by osmandapp.
the class BackupHelper method collectLocalFiles.
@SuppressLint("StaticFieldLeak")
void collectLocalFiles(@Nullable final OnCollectLocalFilesListener listener) {
OperationLog operationLog = new OperationLog("collectLocalFiles", DEBUG);
operationLog.startOperation();
AsyncTask<Void, LocalFile, List<LocalFile>> task = new AsyncTask<Void, LocalFile, List<LocalFile>>() {
BackupDbHelper dbHelper;
SQLiteConnection db;
Map<String, UploadedFileInfo> infos;
@Override
protected void onPreExecute() {
dbHelper = app.getBackupHelper().getDbHelper();
db = dbHelper.openConnection(true);
}
@Override
protected List<LocalFile> doInBackground(Void... voids) {
List<LocalFile> result = new ArrayList<>();
infos = dbHelper.getUploadedFileInfoMap();
List<SettingsItem> localItems = getLocalItems();
operationLog.log("getLocalItems");
for (SettingsItem item : localItems) {
String fileName = BackupHelper.getItemFileName(item);
if (item instanceof FileSettingsItem) {
FileSettingsItem fileItem = (FileSettingsItem) item;
File file = fileItem.getFile();
if (file.isDirectory()) {
if (item instanceof GpxSettingsItem) {
continue;
} else if (fileItem.getSubtype() == FileSubtype.VOICE) {
File jsFile = new File(file, file.getName() + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS);
if (jsFile.exists()) {
fileName = jsFile.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, jsFile, jsFile.lastModified());
continue;
}
} else if (fileItem.getSubtype() == FileSubtype.TTS_VOICE) {
String langName = file.getName().replace(IndexConstants.VOICE_PROVIDER_SUFFIX, "");
File jsFile = new File(file, langName + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS);
if (jsFile.exists()) {
fileName = jsFile.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, jsFile, jsFile.lastModified());
continue;
}
} else if (fileItem.getSubtype() == FileSubtype.TILES_MAP) {
continue;
}
List<File> dirs = new ArrayList<>();
dirs.add(file);
Algorithms.collectDirs(file, dirs);
operationLog.log("collectDirs " + file.getName() + " BEGIN");
for (File dir : dirs) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (!f.isDirectory()) {
fileName = f.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, f, f.lastModified());
}
}
}
}
operationLog.log("collectDirs " + file.getName() + " END");
} else if (fileItem.getSubtype() == FileSubtype.TILES_MAP) {
if (file.getName().endsWith(SQLiteTileSource.EXT)) {
createLocalFile(result, item, fileName, file, file.lastModified());
}
} else {
createLocalFile(result, item, fileName, file, file.lastModified());
}
} else {
createLocalFile(result, item, fileName, null, item.getLastModifiedTime());
}
}
return result;
}
private void createLocalFile(@NonNull List<LocalFile> result, @NonNull SettingsItem item, @NonNull String fileName, @Nullable File file, long lastModifiedTime) {
LocalFile localFile = new LocalFile();
localFile.file = file;
localFile.item = item;
localFile.fileName = fileName;
localFile.localModifiedTime = lastModifiedTime;
if (infos != null) {
UploadedFileInfo fileInfo = infos.get(item.getType().name() + "___" + fileName);
if (fileInfo != null) {
localFile.uploadTime = fileInfo.getUploadTime();
String lastMd5 = fileInfo.getMd5Digest();
boolean needM5Digest = item instanceof StreamSettingsItem && ((StreamSettingsItem) item).needMd5Digest() && localFile.uploadTime < lastModifiedTime && !Algorithms.isEmpty(lastMd5);
if (needM5Digest && file != null && file.exists()) {
FileInputStream is = null;
try {
is = new FileInputStream(file);
String md5 = new String(Hex.encodeHex(DigestUtils.md5(is)));
if (md5.equals(lastMd5)) {
item.setLocalModifiedTime(localFile.uploadTime);
localFile.localModifiedTime = localFile.uploadTime;
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
Algorithms.closeStream(is);
}
}
}
}
result.add(localFile);
publishProgress(localFile);
}
private List<SettingsItem> getLocalItems() {
List<ExportSettingsType> types = ExportSettingsType.getEnabledTypes();
Iterator<ExportSettingsType> it = types.iterator();
while (it.hasNext()) {
ExportSettingsType type = it.next();
if (!getBackupTypePref(type).get()) {
it.remove();
}
}
return app.getFileSettingsHelper().getFilteredSettingsItems(types, true, true);
}
@Override
protected void onProgressUpdate(LocalFile... localFiles) {
if (listener != null) {
listener.onFileCollected(localFiles[0]);
}
}
@Override
protected void onPostExecute(List<LocalFile> localFiles) {
if (db != null) {
db.close();
}
operationLog.finishOperation(" Files=" + localFiles.size());
if (listener != null) {
listener.onFilesCollected(localFiles);
}
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of net.osmand.OperationLog in project Osmand by osmandapp.
the class RegisterUserCommand method doInBackground.
@Override
protected Object doInBackground(Object... objects) {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("login", String.valueOf(login));
final String orderId = Algorithms.isEmpty(promoCode) ? getHelper().getOrderId() : promoCode;
if (!Algorithms.isEmpty(orderId)) {
params.put("orderid", orderId);
}
final String deviceId = getApp().getUserAndroidId();
params.put("deviceid", deviceId);
OperationLog operationLog = createOperationLog("registerUser");
AndroidNetworkUtils.sendRequest(getApp(), USER_REGISTER_URL, params, "Register user", false, true, (resultJson, error, resultCode) -> {
int status;
String message;
BackupError backupError = null;
if (!Algorithms.isEmpty(error)) {
backupError = new BackupError(error);
message = "User registration error: " + backupError + "\nEmail=" + email + "\nOrderId=" + orderId + "\nDeviceId=" + deviceId;
status = STATUS_SERVER_ERROR;
} else if (!Algorithms.isEmpty(resultJson)) {
try {
JSONObject result = new JSONObject(resultJson);
if (result.has("status") && "ok".equals(result.getString("status"))) {
message = "You have been registered successfully. Please check for email with activation code.";
status = STATUS_SUCCESS;
} else {
message = "User registration error: unknown";
status = STATUS_SERVER_ERROR;
}
} catch (JSONException e) {
message = "User registration error: json parsing";
status = STATUS_PARSE_JSON_ERROR;
}
} else {
message = "User registration error: empty response";
status = STATUS_EMPTY_RESPONSE_ERROR;
}
publishProgress(status, message, backupError);
operationLog.finishOperation(status + " " + message);
});
return null;
}
use of net.osmand.OperationLog in project Osmand by osmandapp.
the class BackupImporter method collectItems.
@NonNull
CollectItemsResult collectItems(boolean readItems) throws IllegalArgumentException, IOException {
CollectItemsResult result = new CollectItemsResult();
StringBuilder error = new StringBuilder();
OperationLog operationLog = new OperationLog("collectRemoteItems", BackupHelper.DEBUG);
operationLog.startOperation();
try {
backupHelper.downloadFileList((status, message, remoteFiles) -> {
if (status == BackupHelper.STATUS_SUCCESS) {
result.remoteFiles = remoteFiles;
try {
result.items = getRemoteItems(remoteFiles, readItems);
} catch (IOException e) {
error.append(e.getMessage());
}
} else {
error.append(message);
}
});
} catch (UserNotRegisteredException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
operationLog.finishOperation();
if (!Algorithms.isEmpty(error)) {
throw new IOException(error.toString());
}
return result;
}
Aggregations