Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorStefan Niedermann <info@niedermann.it>2021-04-25 14:14:47 +0300
committerStefan Niedermann <info@niedermann.it>2021-04-26 11:47:11 +0300
commit94a9628427afe8e575e8476decba275a6b905bf3 (patch)
tree7b63259054d26c7fc6547f1a41bd326f2dc3e875 /app
parent8208a3019ba902123b70b02d60a35ef6f486e8d0 (diff)
Use Executor in repository to submit asynchronous tasks
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java41
-rw-r--r--app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java7
2 files changed, 26 insertions, 22 deletions
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java
index 7a8be584..500593fe 100644
--- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java
+++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java
@@ -81,7 +81,7 @@ public class NotesRepository {
private static NotesRepository instance;
- private final ExecutorService executor = Executors.newSingleThreadExecutor();
+ private final ExecutorService executor;
private final Context context;
private final NotesDatabase db;
private final String defaultNonEmptyTitle;
@@ -112,13 +112,13 @@ public class NotesRepository {
public void onReceive(Context context, Intent intent) {
updateNetworkStatus();
if (isSyncPossible() && SSOUtil.isConfigured(context)) {
- new Thread(() -> {
+ executor.submit(() -> {
try {
scheduleSync(getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(context).name), false);
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
Log.v(TAG, "Can not select current SingleSignOn account after network changed, do not sync.");
}
- }).start();
+ });
}
}
};
@@ -134,14 +134,15 @@ public class NotesRepository {
public static synchronized NotesRepository getInstance(@NonNull Context context) {
if (instance == null) {
- instance = new NotesRepository(context, NotesDatabase.getInstance(context.getApplicationContext()));
+ instance = new NotesRepository(context, NotesDatabase.getInstance(context.getApplicationContext()), Executors.newCachedThreadPool());
}
return instance;
}
- private NotesRepository(@NonNull final Context context, @NonNull final NotesDatabase db) {
+ private NotesRepository(@NonNull final Context context, @NonNull final NotesDatabase db, @NonNull final ExecutorService executor) {
this.context = context.getApplicationContext();
this.db = db;
+ this.executor = executor;
this.defaultNonEmptyTitle = NoteUtil.generateNonEmptyNoteTitle("", this.context);
this.syncOnlyOnWifiKey = context.getApplicationContext().getResources().getString(R.string.pref_key_wifi_only);
@@ -364,7 +365,7 @@ public class NotesRepository {
public LiveData<Note> addNoteAndSync(Account account, Note note) {
final Note entity = new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, account.getId(), generateNoteExcerpt(note.getContent(), note.getTitle()), 0);
final MutableLiveData<Note> ret = new MutableLiveData<>();
- new Thread(() -> ret.postValue(addNote(account.getId(), entity))).start();
+ executor.submit(() -> ret.postValue(addNote(account.getId(), entity)));
return map(ret, newNote -> {
notifyWidgets();
scheduleSync(account, true);
@@ -413,10 +414,10 @@ public class NotesRepository {
@AnyThread
public void toggleFavoriteAndSync(Account account, long noteId) {
- new Thread(() -> {
+ executor.submit(() -> {
db.getNoteDao().toggleFavorite(noteId);
scheduleSync(account, true);
- }).start();
+ });
}
/**
@@ -430,11 +431,11 @@ public class NotesRepository {
*/
@AnyThread
public void setCategory(@NonNull Account account, long noteId, @NonNull String category) {
- new Thread(() -> {
+ executor.submit(() -> {
db.getNoteDao().updateStatus(noteId, DBStatus.LOCAL_EDITED);
db.getNoteDao().updateCategory(noteId, category);
scheduleSync(account, true);
- }).start();
+ });
}
/**
@@ -491,7 +492,7 @@ public class NotesRepository {
*/
@AnyThread
public void deleteNoteAndSync(Account account, long id) {
- new Thread(() -> {
+ executor.submit(() -> {
db.getNoteDao().updateStatus(id, DBStatus.LOCAL_DELETED);
notifyWidgets();
scheduleSync(account, true);
@@ -510,23 +511,23 @@ public class NotesRepository {
Log.e(TAG, ShortcutManager.class.getSimpleName() + "is null.");
}
}
- }).start();
+ });
}
/**
* Notify about changed notes.
*/
@AnyThread
- protected void notifyWidgets() {
- new Thread(() -> {
+ private void notifyWidgets() {
+ executor.submit(() -> {
updateSingleNoteWidgets(context);
updateNoteListWidgets(context);
- }).start();
+ });
}
@AnyThread
private void updateDynamicShortcuts(long accountId) {
- new Thread(() -> {
+ executor.submit(() -> {
if (SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = context.getApplicationContext().getSystemService(ShortcutManager.class);
if (shortcutManager != null) {
@@ -555,7 +556,7 @@ public class NotesRepository {
}
}
}
- }).start();
+ });
}
/**
@@ -605,7 +606,7 @@ public class NotesRepository {
*/
@AnyThread
public void modifyCategoryOrder(long accountId, @NonNull NavigationCategory selectedCategory, @NonNull CategorySortingMethod sortingMethod) {
- new Thread(() -> {
+ executor.submit(() -> {
final Context ctx = context.getApplicationContext();
final SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
int orderIndex = sortingMethod.getId();
@@ -642,7 +643,7 @@ public class NotesRepository {
}
}
sp.apply();
- }).start();
+ });
}
/**
@@ -728,7 +729,7 @@ public class NotesRepository {
*
* @param callback Implementation of ISyncCallback, contains one method that shall be executed.
*/
- public void addCallbackPush(Account account, ISyncCallback callback) {
+ private void addCallbackPush(Account account, ISyncCallback callback) {
if (account == null) {
Log.i(TAG, "ssoAccount is null. Is this a local account?");
callback.onScheduled();
diff --git a/app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java b/app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java
index 812c6811..d4253ea2 100644
--- a/app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java
+++ b/app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java
@@ -7,6 +7,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
+import com.google.common.util.concurrent.MoreExecutors;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;
import org.junit.After;
@@ -23,6 +24,8 @@ import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.persistence.entity.Note;
@@ -56,9 +59,9 @@ public class NotesRepositoryTest {
.allowMainThreadQueries()
.build();
- final Constructor<NotesRepository> constructor = NotesRepository.class.getDeclaredConstructor(Context.class, NotesDatabase.class);
+ final Constructor<NotesRepository> constructor = NotesRepository.class.getDeclaredConstructor(Context.class, NotesDatabase.class, ExecutorService.class);
constructor.setAccessible(true);
- repo = constructor.newInstance(context, db);
+ repo = constructor.newInstance(context, db, MoreExecutors.newDirectExecutorService());
repo.addAccount("https://äöüß.example.com", "彼得", "彼得@äöüß.example.com", new Capabilities("{\"ocs\":{\"meta\":{\"status\":\"ok\",\"statuscode\":200,\"message\":\"OK\"},\"data\":{\"version\":{\"major\":18,\"minor\":0,\"micro\":4,\"string\":\"18.0.4\",\"edition\":\"\",\"extendedSupport\":false},\"capabilities\":{\"core\":{\"pollinterval\":60,\"webdav-root\":\"remote.php\\/webdav\"},\"bruteforce\":{\"delay\":0},\"files\":{\"bigfilechunking\":true,\"blacklisted_files\":[\".htaccess\"],\"directEditing\":{\"url\":\"https:\\/\\/efss.qloud.my\\/ocs\\/v2.php\\/apps\\/files\\/api\\/v1\\/directEditing\",\"etag\":\"ed2b141af2a39b0e42666952ba60988d\"},\"versioning\":true,\"undelete\":true},\"activity\":{\"apiv2\":[\"filters\",\"filters-api\",\"previews\",\"rich-strings\"]},\"ocm\":{\"enabled\":true,\"apiVersion\":\"1.0-proposal1\",\"endPoint\":\"https:\\/\\/efss.qloud.my\\/index.php\\/ocm\",\"resourceTypes\":[{\"name\":\"file\",\"shareTypes\":[\"user\",\"group\"],\"protocols\":{\"webdav\":\"\\/public.php\\/webdav\\/\"}}]},\"deck\":{\"version\":\"0.8.2\"},\"richdocuments\":{\"mimetypes\":[\"application\\/vnd.oasis.opendocument.text\",\"application\\/vnd.oasis.opendocument.spreadsheet\",\"application\\/vnd.oasis.opendocument.graphics\",\"application\\/vnd.oasis.opendocument.presentation\",\"application\\/vnd.lotus-wordpro\",\"application\\/vnd.visio\",\"application\\/vnd.wordperfect\",\"application\\/msonenote\",\"application\\/msword\",\"application\\/rtf\",\"text\\/rtf\",\"application\\/vnd.openxmlformats-officedocument.wordprocessingml.document\",\"application\\/vnd.openxmlformats-officedocument.wordprocessingml.template\",\"application\\/vnd.ms-word.document.macroEnabled.12\",\"application\\/vnd.ms-word.template.macroEnabled.12\",\"application\\/vnd.ms-excel\",\"application\\/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\"application\\/vnd.openxmlformats-officedocument.spreadsheetml.template\",\"application\\/vnd.ms-excel.sheet.macroEnabled.12\",\"application\\/vnd.ms-excel.template.macroEnabled.12\",\"application\\/vnd.ms-excel.addin.macroEnabled.12\",\"application\\/vnd.ms-excel.sheet.binary.macroEnabled.12\",\"application\\/vnd.ms-powerpoint\",\"application\\/vnd.openxmlformats-officedocument.presentationml.presentation\",\"application\\/vnd.openxmlformats-officedocument.presentationml.template\",\"application\\/vnd.openxmlformats-officedocument.presentationml.slideshow\",\"application\\/vnd.ms-powerpoint.addin.macroEnabled.12\",\"application\\/vnd.ms-powerpoint.presentation.macroEnabled.12\",\"application\\/vnd.ms-powerpoint.template.macroEnabled.12\",\"application\\/vnd.ms-powerpoint.slideshow.macroEnabled.12\",\"text\\/csv\"],\"mimetypesNoDefaultOpen\":[\"image\\/svg+xml\",\"application\\/pdf\",\"text\\/plain\",\"text\\/spreadsheet\"],\"collabora\":[],\"direct_editing\":false,\"templates\":false,\"productName\":\"\\u5728\\u7ebf\\u534f\\u4f5c\"},\"dav\":{\"chunking\":\"1.0\"},\"files_sharing\":{\"api_enabled\":true,\"public\":{\"enabled\":true,\"password\":{\"enforced\":true,\"askForOptionalPassword\":false},\"expire_date\":{\"enabled\":true,\"days\":\"7\",\"enforced\":false},\"multiple_links\":true,\"expire_date_internal\":{\"enabled\":false},\"send_mail\":false,\"upload\":true,\"upload_files_drop\":true},\"resharing\":true,\"user\":{\"send_mail\":false,\"expire_date\":{\"enabled\":true}},\"group_sharing\":true,\"group\":{\"enabled\":true,\"expire_date\":{\"enabled\":true}},\"default_permissions\":31,\"federation\":{\"outgoing\":false,\"incoming\":false,\"expire_date\":{\"enabled\":true}},\"sharee\":{\"query_lookup_default\":false},\"sharebymail\":{\"enabled\":true,\"upload_files_drop\":{\"enabled\":true},\"password\":{\"enabled\":true},\"expire_date\":{\"enabled\":true}}},\"external\":{\"v1\":[\"sites\",\"device\",\"groups\",\"redirect\"]},\"notifications\":{\"ocs-endpoints\":[\"list\",\"get\",\"delete\",\"delete-all\",\"icons\",\"rich-strings\",\"action-web\"],\"push\":[\"devices\",\"object-data\",\"delete\"],\"admin-notifications\":[\"ocs\",\"cli\"]},\"password_policy\":{\"minLength\":8,\"enforceNonCommonPassword\":true,\"enforceNumericCharacters\":false,\"enforceSpecialCharacters\":false,\"enforceUpperLowerCase\":false,\"api\":{\"generate\":\"https:\\/\\/efss.qloud.my\\/ocs\\/v2.php\\/apps\\/password_policy\\/api\\/v1\\/generate\",\"validate\":\"https:\\/\\/efss.qloud.my\\/ocs\\/v2.php\\/apps\\/password_policy\\/api\\/v1\\/validate\"}},\"theming\":{\"name\":\"QloudData\",\"url\":\"https:\\/\\/www.qloud.my\\/qloud-data\\/\",\"slogan\":\"Powered by NextCloud\",\"color\":\"#1E4164\",\"color-text\":\"#ffffff\",\"color-element\":\"#1E4164\",\"logo\":\"https:\\/\\/efss.qloud.my\\/index.php\\/apps\\/theming\\/image\\/logo?useSvg=1&v=47\",\"background\":\"https:\\/\\/efss.qloud.my\\/core\\/img\\/background.png?v=47\",\"background-plain\":false,\"background-default\":true,\"logoheader\":\"https:\\/\\/efss.qloud.my\\/index.php\\/apps\\/theming\\/image\\/logo?useSvg=1&v=47\",\"favicon\":\"https:\\/\\/efss.qloud.my\\/index.php\\/apps\\/theming\\/image\\/logo?useSvg=1&v=47\"},\"registration\":{\"enabled\":true,\"apiRoot\":\"\\/ocs\\/v2.php\\/apps\\/registration\\/api\\/v1\\/\",\"apiLevel\":\"v1\"}}}}}", null));
account = repo.getAccountByName("彼得@äöüß.example.com");