Fix backup sync and progress

This commit is contained in:
max-klaus 2021-04-22 20:35:27 +03:00
parent e6be5dcab5
commit 974ad80734
4 changed files with 73 additions and 17 deletions

View file

@ -38,6 +38,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executor;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@ -92,6 +93,13 @@ public class AndroidNetworkUtils {
public static void sendRequestsAsync(@Nullable final OsmandApplication ctx,
@NonNull final List<Request> requests,
@Nullable final OnSendRequestsListener listener) {
sendRequestsAsync(ctx, requests, listener, AsyncTask.THREAD_POOL_EXECUTOR);
}
public static void sendRequestsAsync(@Nullable final OsmandApplication ctx,
@NonNull final List<Request> requests,
@Nullable final OnSendRequestsListener listener,
final Executor executor) {
new AsyncTask<Void, RequestResponse, List<RequestResponse>>() {
@ -127,7 +135,7 @@ public class AndroidNetworkUtils {
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}.executeOnExecutor(executor, (Void) null);
}
public static void sendRequestAsync(final OsmandApplication ctx,
@ -137,7 +145,18 @@ public class AndroidNetworkUtils {
final boolean toastAllowed,
final boolean post,
final OnRequestResultListener listener) {
sendRequestAsync(ctx, url, parameters, userOperation, toastAllowed, post, listener,
AsyncTask.THREAD_POOL_EXECUTOR);
}
public static void sendRequestAsync(final OsmandApplication ctx,
final String url,
final Map<String, String> parameters,
final String userOperation,
final boolean toastAllowed,
final boolean post,
final OnRequestResultListener listener,
final Executor executor) {
new AsyncTask<Void, Void, String>() {
@Override
@ -156,7 +175,7 @@ public class AndroidNetworkUtils {
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}.executeOnExecutor(executor, (Void) null);
}
public static void downloadFileAsync(final String url,
@ -183,6 +202,14 @@ public class AndroidNetworkUtils {
final @NonNull List<File> files,
final @NonNull Map<String, String> parameters,
final @Nullable OnFilesDownloadCallback callback) {
downloadFilesAsync(url, files, parameters, callback, AsyncTask.THREAD_POOL_EXECUTOR);
}
public static void downloadFilesAsync(final @NonNull String url,
final @NonNull List<File> files,
final @NonNull Map<String, String> parameters,
final @Nullable OnFilesDownloadCallback callback,
final Executor executor) {
new AsyncTask<Void, Object, Map<File, String>>() {
@ -247,7 +274,7 @@ public class AndroidNetworkUtils {
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}.executeOnExecutor(executor, (Void) null);
}
public static String sendRequest(@Nullable OsmandApplication ctx, @NonNull String url,
@ -503,6 +530,16 @@ public class AndroidNetworkUtils {
final @NonNull Map<String, String> parameters,
final @Nullable Map<String, String> headers,
final OnFilesUploadCallback callback) {
uploadFilesAsync(url, files, gzip, parameters, headers, callback, AsyncTask.THREAD_POOL_EXECUTOR);
}
public static void uploadFilesAsync(final @NonNull String url,
final @NonNull List<File> files,
final boolean gzip,
final @NonNull Map<String, String> parameters,
final @Nullable Map<String, String> headers,
final OnFilesUploadCallback callback,
final Executor executor) {
new AsyncTask<Void, Object, Map<File, String>>() {
@ -557,7 +594,7 @@ public class AndroidNetworkUtils {
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}.executeOnExecutor(executor, (Void) null);
}
private static void showToast(OsmandApplication ctx, String message) {

View file

@ -37,6 +37,9 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class BackupHelper {
@ -45,6 +48,9 @@ public class BackupHelper {
private final FavouritesDbHelper favouritesHelper;
private final GpxDbHelper gpxHelper;
private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(1, 1, 0L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
private static final String SERVER_URL = "https://osmand.net";
private static final String USER_REGISTER_URL = SERVER_URL + "/userdata/user-register";
@ -196,7 +202,7 @@ public class BackupHelper {
listener.onRegisterUser(status, message);
}
}
});
}, EXECUTOR);
}
public void registerDevice(String token, @Nullable final OnRegisterDeviceListener listener) {
@ -238,7 +244,7 @@ public class BackupHelper {
listener.onRegisterDevice(status, message);
}
}
});
}, EXECUTOR);
}
public void uploadFiles(@NonNull List<GpxFileInfo> gpxFiles, @Nullable final OnUploadFilesListener listener) throws UserNotRegisteredException {
@ -294,7 +300,7 @@ public class BackupHelper {
listener.onFilesUploadDone(errors);
}
}
});
}, EXECUTOR);
}
public void deleteFiles(@NonNull List<UserFile> userFiles, @Nullable final OnDeleteFilesListener listener) throws UserNotRegisteredException {
@ -349,7 +355,7 @@ public class BackupHelper {
listener.onFilesDeleteDone(errors);
}
}
});
}, EXECUTOR);
}
public void downloadFileList(@Nullable final OnDownloadFileListListener listener) throws UserNotRegisteredException {
@ -391,7 +397,7 @@ public class BackupHelper {
listener.onDownloadFileList(status, message, userFiles);
}
}
});
}, EXECUTOR);
}
public void downloadFiles(@NonNull final Map<File, UserFile> filesMap, @Nullable final OnDownloadFileListener listener) throws UserNotRegisteredException {
@ -432,7 +438,7 @@ public class BackupHelper {
listener.onFilesDownloadDone(errors);
}
}
});
}, EXECUTOR);
}
@SuppressLint("StaticFieldLeak")
@ -514,7 +520,7 @@ public class BackupHelper {
}
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
task.executeOnExecutor(EXECUTOR);
}
@SuppressLint("StaticFieldLeak")
@ -575,6 +581,6 @@ public class BackupHelper {
}
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
task.executeOnExecutor(EXECUTOR);
}
}

View file

@ -11,7 +11,6 @@ import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.GpxDbHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.ProgressImplementation;
import net.osmand.plus.backup.BackupHelper.BackupInfo;
@ -113,7 +112,7 @@ public class BackupTask {
tasks.push(backupTasks[i]);
}
this.runningTasks = tasks;
onTasksInit();
onBackupTasksInit();
}
private void initRestoreTasks() {
@ -123,7 +122,7 @@ public class BackupTask {
tasks.push(restoreTasks[i]);
}
this.runningTasks = tasks;
onTasksInit();
onRestoreTasksInit();
}
private void initData() {
@ -275,14 +274,22 @@ public class BackupTask {
}
}
private void onTasksInit() {
private void onBackupTasksInit() {
Context ctx = contextRef.get();
if (ctx instanceof Activity && AndroidUtils.isActivityNotDestroyed((Activity) ctx) && progress != null) {
if (ctx instanceof Activity && AndroidUtils.isActivityNotDestroyed((Activity) ctx)) {
progress = ProgressImplementation.createProgressDialog(ctx,
"Backup data", "Initializing...", ProgressDialog.STYLE_HORIZONTAL);
}
}
private void onRestoreTasksInit() {
Context ctx = contextRef.get();
if (ctx instanceof Activity && AndroidUtils.isActivityNotDestroyed((Activity) ctx)) {
progress = ProgressImplementation.createProgressDialog(ctx,
"Restore data", "Initializing...", ProgressDialog.STYLE_HORIZONTAL);
}
}
private void onTaskProgressUpdate(Object... objects) {
Context ctx = contextRef.get();
if (ctx instanceof Activity && AndroidUtils.isActivityNotDestroyed((Activity) ctx) && progress != null) {

View file

@ -186,6 +186,7 @@ public class TestBackupActivity extends OsmandActionBarActivity {
@Override
public void onClick(View v) {
if (backupInfo != null) {
buttonBackup.setEnabled(false);
BackupTask task = new BackupTask(backupInfo, TestBackupActivity.this, new OnBackupListener() {
@Override
public void onBackupDone(@Nullable Map<File, String> uploadErrors, @Nullable Map<File, String> downloadErrors,
@ -203,6 +204,7 @@ public class TestBackupActivity extends OsmandActionBarActivity {
a.infoView.setText(description);
a.infoView.requestFocus();
a.prepareBackup();
a.buttonBackup.setEnabled(true);
}
}
});
@ -214,6 +216,7 @@ public class TestBackupActivity extends OsmandActionBarActivity {
@Override
public void onClick(View v) {
if (backupInfo != null) {
buttonRestore.setEnabled(false);
BackupTask task = new BackupTask(backupInfo, TestBackupActivity.this, new OnBackupListener() {
@Override
public void onBackupDone(@Nullable Map<File, String> uploadErrors, @Nullable Map<File, String> downloadErrors,
@ -231,6 +234,7 @@ public class TestBackupActivity extends OsmandActionBarActivity {
a.infoView.setText(description);
a.infoView.requestFocus();
a.prepareBackup();
a.buttonRestore.setEnabled(true);
}
}
});
@ -309,6 +313,7 @@ public class TestBackupActivity extends OsmandActionBarActivity {
private void prepareBackup() {
final WeakReference<TestBackupActivity> activityRef = new WeakReference<>(this);
buttonRefresh.setEnabled(false);
PrepareBackupTask prepareBackupTask = new PrepareBackupTask(this, new OnPrepareBackupListener() {
@Override
public void onBackupPrepared(@Nullable BackupInfo backupInfo, @Nullable String error) {
@ -329,6 +334,7 @@ public class TestBackupActivity extends OsmandActionBarActivity {
}
a.infoView.setText(description);
a.infoView.requestFocus();
a.buttonRefresh.setEnabled(true);
}
}
});