diff --git a/OsmAnd/src/net/osmand/AndroidNetworkUtils.java b/OsmAnd/src/net/osmand/AndroidNetworkUtils.java index b25fb1b2d4..f25d08565b 100644 --- a/OsmAnd/src/net/osmand/AndroidNetworkUtils.java +++ b/OsmAnd/src/net/osmand/AndroidNetworkUtils.java @@ -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 requests, @Nullable final OnSendRequestsListener listener) { + sendRequestsAsync(ctx, requests, listener, AsyncTask.THREAD_POOL_EXECUTOR); + } + + public static void sendRequestsAsync(@Nullable final OsmandApplication ctx, + @NonNull final List requests, + @Nullable final OnSendRequestsListener listener, + final Executor executor) { new AsyncTask>() { @@ -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 parameters, + final String userOperation, + final boolean toastAllowed, + final boolean post, + final OnRequestResultListener listener, + final Executor executor) { new AsyncTask() { @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 files, final @NonNull Map 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 files, + final @NonNull Map parameters, + final @Nullable OnFilesDownloadCallback callback, + final Executor executor) { new AsyncTask>() { @@ -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 parameters, final @Nullable Map 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 files, + final boolean gzip, + final @NonNull Map parameters, + final @Nullable Map headers, + final OnFilesUploadCallback callback, + final Executor executor) { new AsyncTask>() { @@ -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) { diff --git a/OsmAnd/src/net/osmand/plus/backup/BackupHelper.java b/OsmAnd/src/net/osmand/plus/backup/BackupHelper.java index 326315b7d1..87423ec582 100644 --- a/OsmAnd/src/net/osmand/plus/backup/BackupHelper.java +++ b/OsmAnd/src/net/osmand/plus/backup/BackupHelper.java @@ -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()); + 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 gpxFiles, @Nullable final OnUploadFilesListener listener) throws UserNotRegisteredException { @@ -294,7 +300,7 @@ public class BackupHelper { listener.onFilesUploadDone(errors); } } - }); + }, EXECUTOR); } public void deleteFiles(@NonNull List 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 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); } } diff --git a/OsmAnd/src/net/osmand/plus/backup/BackupTask.java b/OsmAnd/src/net/osmand/plus/backup/BackupTask.java index a2fc299463..27a698f73d 100644 --- a/OsmAnd/src/net/osmand/plus/backup/BackupTask.java +++ b/OsmAnd/src/net/osmand/plus/backup/BackupTask.java @@ -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) { diff --git a/OsmAnd/src/net/osmand/plus/development/TestBackupActivity.java b/OsmAnd/src/net/osmand/plus/development/TestBackupActivity.java index 3b38b1469b..8f78c63151 100644 --- a/OsmAnd/src/net/osmand/plus/development/TestBackupActivity.java +++ b/OsmAnd/src/net/osmand/plus/development/TestBackupActivity.java @@ -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 uploadErrors, @Nullable Map 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 uploadErrors, @Nullable Map 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 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); } } });