From 20ccb863551d29605ce4fca34acf0dccae03e413 Mon Sep 17 00:00:00 2001 From: Federico Iosue Date: Fri, 23 Aug 2024 14:35:38 +0200 Subject: [PATCH] MIN_SDK 24, moved from rxJava to native streams --- gradle.properties | 2 +- .../async/upgrade/UpgradeProcessorTest.kt | 6 +- .../omninotes/helpers/BackupHelperTest.java | 13 ++-- .../omninotes/testutils/RetryableAssert.java | 6 +- .../omninotes/ui/FabCameraNoteTest.java | 5 +- .../android/omninotes/DetailFragment.java | 6 +- .../feio/android/omninotes/ListFragment.java | 8 +-- .../android/omninotes/PasswordActivity.java | 66 ++++++++----------- .../android/omninotes/SettingsFragment.java | 7 +- .../async/DataBackupIntentService.java | 48 +++++++------- .../async/upgrade/UpgradeProcessor.java | 27 ++++---- .../feio/android/omninotes/db/DbHelper.java | 21 +++--- .../omninotes/helpers/BackupHelper.java | 42 ++++++------ .../helpers/count/DefaultWordCounter.java | 7 +- .../helpers/count/IdeogramsWordCounter.java | 11 ++-- .../omninotes/utils/PasswordHelper.java | 33 ++++------ .../android/omninotes/utils/TagsHelper.java | 9 +-- 17 files changed, 147 insertions(+), 170 deletions(-) diff --git a/gradle.properties b/gradle.properties index 0dae6c8f2..a6efa9547 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ VERSION_NAME=6.4.0 VERSION_CODE=333 PACKAGE=it.feio.android.omninotes -MIN_SDK=21 +MIN_SDK=24 TARGET_SDK=33 android.enableJetifier=true diff --git a/omniNotes/src/androidTest/java/it/feio/android/omninotes/async/upgrade/UpgradeProcessorTest.kt b/omniNotes/src/androidTest/java/it/feio/android/omninotes/async/upgrade/UpgradeProcessorTest.kt index 922cf77e3..01571324c 100644 --- a/omniNotes/src/androidTest/java/it/feio/android/omninotes/async/upgrade/UpgradeProcessorTest.kt +++ b/omniNotes/src/androidTest/java/it/feio/android/omninotes/async/upgrade/UpgradeProcessorTest.kt @@ -23,7 +23,7 @@ import it.feio.android.omninotes.utils.FileProviderHelper.getShareableUri import it.feio.android.omninotes.utils.StorageHelper.createAttachmentFromUri import org.junit.Assert.* import org.junit.Test -import rx.Observable.from +import java.util.stream.Stream class UpgradeProcessorTest : BaseAndroidTestCase() { @@ -37,11 +37,11 @@ class UpgradeProcessorTest : BaseAndroidTestCase() { note.attachmentsList[0] = attachment dbHelper.updateNote(note, false) - assertFalse(from(dbHelper.allAttachments).all { a -> a.uri.scheme != "content" }.toBlocking().single()) + assertFalse(Stream.of(dbHelper.allAttachments).allMatch { a -> a.single().uri.scheme != "content" }); UpgradeProcessor.process(624, 625) - assertTrue(from(dbHelper.allAttachments).all { a -> a.uri.scheme != "content" }.toBlocking().single()) + assertTrue(Stream.of(dbHelper.allAttachments).allMatch { a -> a.single().uri.scheme != "content" }); } } diff --git a/omniNotes/src/androidTest/java/it/feio/android/omninotes/helpers/BackupHelperTest.java b/omniNotes/src/androidTest/java/it/feio/android/omninotes/helpers/BackupHelperTest.java index 977e30dc2..f4ad30dd0 100644 --- a/omniNotes/src/androidTest/java/it/feio/android/omninotes/helpers/BackupHelperTest.java +++ b/omniNotes/src/androidTest/java/it/feio/android/omninotes/helpers/BackupHelperTest.java @@ -19,8 +19,8 @@ import static it.feio.android.omninotes.utils.ConstantsBase.PREF_BACKUP_FOLDER_URI; import static it.feio.android.omninotes.utils.ConstantsBase.PREF_PASSWORD; +import static java.util.stream.IntStream.range; import static org.junit.Assert.*; -import static rx.Observable.from; import android.net.Uri; import androidx.test.ext.junit.runners.AndroidJUnit4; @@ -37,6 +37,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; @@ -44,7 +45,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import rx.Observable; @RunWith(AndroidJUnit4.class) public class BackupHelperTest extends BaseAndroidTestCase { @@ -95,7 +95,7 @@ public void exportNotes_nothingToExport() throws IOException { @Test public void exportNotes() throws IOException { - Observable.range(1, 4).forEach(i -> createTestNote("Note" + i, "content" + i, 1)); + range(1, 4).forEach(i -> createTestNote("Note" + i, "content" + i, 1)); var backupDir = DocumentFileCompat.Companion.fromFile(testContext, Files.createTempDirectory("testBackupFolder").toFile()); @@ -112,10 +112,11 @@ public void exportNote() { BackupHelper.exportNote(backupDir, note); - var noteFiles = from(backupDir.listFiles()) - .filter(f -> f.getName().matches("\\d{13}.json")).toList().toBlocking().single(); + var noteFiles = backupDir.listFiles().stream() + .filter(f -> f.getName().matches("\\d{13}.json")) + .collect(Collectors.toList()); assertEquals(1, noteFiles.size()); - Note retrievedNote = from(noteFiles).map(BackupHelper::importNote).toBlocking().first(); + var retrievedNote = noteFiles.stream().map(BackupHelper::importNote).collect(Collectors.toList()); assertEquals(note, retrievedNote); } diff --git a/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/RetryableAssert.java b/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/RetryableAssert.java index b5c6183ca..3faa3f023 100644 --- a/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/RetryableAssert.java +++ b/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/RetryableAssert.java @@ -17,10 +17,9 @@ package it.feio.android.omninotes.testutils; -import static rx.Observable.from; - import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.stream.Stream; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.junit.Assert; @@ -58,8 +57,7 @@ public static void assertTrue(long timeoutInMillisec, @Nonnull Object o, } private static Method getMethod(@Nonnull Object o, @Nonnull String methodName) { - return from(o.getClass().getMethods()).filter(m -> methodName.equals(m.getName())) - .toBlocking().first(); + return Stream.of(o.getClass().getMethods()).filter(m -> methodName.equals(m.getName())).findFirst().orElseThrow(); } } diff --git a/omniNotes/src/androidTest/java/it/feio/android/omninotes/ui/FabCameraNoteTest.java b/omniNotes/src/androidTest/java/it/feio/android/omninotes/ui/FabCameraNoteTest.java index 432d9e80d..8db3036db 100644 --- a/omniNotes/src/androidTest/java/it/feio/android/omninotes/ui/FabCameraNoteTest.java +++ b/omniNotes/src/androidTest/java/it/feio/android/omninotes/ui/FabCameraNoteTest.java @@ -42,12 +42,9 @@ import de.greenrobot.event.EventBus; import it.feio.android.omninotes.R; import it.feio.android.omninotes.async.bus.NotesUpdatedEvent; -import it.feio.android.omninotes.models.Note; import it.feio.android.omninotes.utils.Constants; import org.junit.Test; import org.junit.runner.RunWith; -import rx.Observable; - @LargeTest @RunWith(AndroidJUnit4.class) @@ -91,7 +88,7 @@ public void fabCameraNoteTest() { } public void onEvent(NotesUpdatedEvent notesUpdatedEvent) { - Note updatedNote = Observable.from(notesUpdatedEvent.getNotes()).toBlocking().first(); + var updatedNote = notesUpdatedEvent.getNotes().get(0); assertEquals(0, updatedNote.getAttachmentsList().size()); assertEquals(Constants.MIME_TYPE_IMAGE, updatedNote.getAttachmentsList().get(0).getMime_type()); diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java index 018207bd0..7705cebb1 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java @@ -195,8 +195,8 @@ import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; -import rx.Observable; public class DetailFragment extends BaseFragment implements OnReminderPickedListener, @@ -1236,7 +1236,7 @@ private void moveCheckedItemsToBottom() { private void categorizeNote() { var currentCategory = noteTmp.getCategory() != null ? String.valueOf(noteTmp.getCategory().getId()) : null; var originalCategory = noteOriginal.getCategory() != null ? String.valueOf(noteOriginal.getCategory().getId()) : null; - final var categories = Observable.from(DbHelper.getInstance().getCategories()) + final var categories = DbHelper.getInstance().getCategories().stream() .map(category -> { if (String.valueOf(category.getId()).equals(currentCategory) && currentCategory != originalCategory) { category.setCount(category.getCount() + 1); @@ -1245,7 +1245,7 @@ private void categorizeNote() { category.setCount(category.getCount() - 1); } return category; - }).toList().toBlocking().single(); + }).collect(Collectors.toList()); var dialogBuilder = new MaterialDialog.Builder(mainActivity) .title(R.string.categorize_as) diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java b/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java index cbc12167e..b66fc48bd 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java @@ -127,13 +127,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.SortedMap; import java.util.TreeMap; +import java.util.stream.IntStream; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; -import rx.Observable; -import rx.functions.Action1; public class ListFragment extends BaseFragment implements OnViewTouchedListener, @@ -1189,8 +1187,8 @@ public void onEvent(NotesLoadedEvent notesLoadedEvent) { } public void onEvent(NotesUpdatedEvent notesUpdatedEvent) { - Observable.from(notesUpdatedEvent.getNotes()).forEach(updatedNote -> - Observable.range(0, listAdapter.getNotes().size() - 1) + notesUpdatedEvent.getNotes().stream().forEach(updatedNote -> + IntStream.range(0, listAdapter.getNotes().size() - 1) .filter(i -> listAdapter.getItem(i).get_id().equals(updatedNote.get_id())) .forEach(i -> { listAdapter.getNotes().set(i, updatedNote); diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/PasswordActivity.java b/omniNotes/src/main/java/it/feio/android/omninotes/PasswordActivity.java index d84c0d302..7d42225e3 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/PasswordActivity.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/PasswordActivity.java @@ -37,9 +37,6 @@ import it.feio.android.omninotes.models.PasswordValidator; import it.feio.android.omninotes.utils.PasswordHelper; import it.feio.android.omninotes.utils.Security; -import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; public class PasswordActivity extends BaseActivity { @@ -163,38 +160,31 @@ private void updatePassword(String passwordText, String questionText, String ans .content(R.string.agree_unlocking_all_notes) .positiveText(R.string.ok) .onPositive((dialog, which) -> PasswordHelper.removePassword()).build().show(); - } else if (passwordText.length() == 0) { + } else if (passwordText.isEmpty()) { Crouton.makeText(mActivity, R.string.empty_password, ONStyle.WARN, croutonHandle).show(); } else { - Observable - .from(DbHelper.getInstance().getNotesWithLock(true)) - .subscribeOn(Schedulers.newThread()) - .observeOn(AndroidSchedulers.mainThread()) - .doOnSubscribe(() -> Prefs.edit() - .putString(PREF_PASSWORD, Security.md5(passwordText)) - .putString(PREF_PASSWORD_QUESTION, questionText) - .putString(PREF_PASSWORD_ANSWER, Security.md5(answerText)) - .apply()) - .doOnNext(note -> DbHelper.getInstance().updateNote(note, false)) - .doOnCompleted(() -> { - Crouton crouton = Crouton - .makeText(mActivity, R.string.password_successfully_changed, ONStyle - .CONFIRM, croutonHandle); - crouton.setLifecycleCallback(new LifecycleCallback() { - @Override - public void onDisplayed() { - // Does nothing! - } - - - @Override - public void onRemoved() { - onBackPressed(); - } - }); - crouton.show(); - }) - .subscribe(); + Prefs.edit() + .putString(PREF_PASSWORD, Security.md5(passwordText)) + .putString(PREF_PASSWORD_QUESTION, questionText) + .putString(PREF_PASSWORD_ANSWER, Security.md5(answerText)) + .apply(); + DbHelper.getInstance().getNotesWithLock(true) + .forEach(note -> DbHelper.getInstance().updateNote(note, false)); + var crouton = Crouton.makeText(mActivity, R.string.password_successfully_changed, + ONStyle.CONFIRM, croutonHandle); + crouton.setLifecycleCallback(new LifecycleCallback() { + @Override + public void onDisplayed() { + // Does nothing! + } + + + @Override + public void onRemoved() { + onBackPressed(); + } + }); + crouton.show(); } } @@ -210,15 +200,15 @@ private boolean checkData() { return true; } - boolean passwordOk = password.getText().toString().length() > 0; + boolean passwordOk = !password.getText().toString().isEmpty(); boolean passwordCheckOk = - passwordCheck.getText().toString().length() > 0 && password.getText().toString() + !passwordCheck.getText().toString().isEmpty() && password.getText().toString() .equals( passwordCheck.getText().toString()); - boolean questionOk = question.getText().toString().length() > 0; - boolean answerOk = answer.getText().toString().length() > 0; + boolean questionOk = !question.getText().toString().isEmpty(); + boolean answerOk = !answer.getText().toString().isEmpty(); boolean answerCheckOk = - answerCheck.getText().toString().length() > 0 && answer.getText().toString().equals + !answerCheck.getText().toString().isEmpty() && answer.getText().toString().equals (answerCheck.getText().toString()); if (!passwordOk || !passwordCheckOk || !questionOk || !answerOk || !answerCheckOk) { diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/SettingsFragment.java b/omniNotes/src/main/java/it/feio/android/omninotes/SettingsFragment.java index fcc7586cc..da984492a 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/SettingsFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/SettingsFragment.java @@ -30,8 +30,8 @@ import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.reverse; +import static java.util.stream.Collectors.toList; -import android.Manifest; import android.Manifest.permission; import android.annotation.TargetApi; import android.app.Activity; @@ -85,7 +85,6 @@ import java.util.List; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; -import rx.Observable; public class SettingsFragment extends PreferenceFragmentCompat { @@ -578,8 +577,8 @@ private void importNotes() { private void importNotes(DocumentFileCompat documentFile) { String[] backupsArray; if (documentFile != null) { - backupsArray = Observable.from(documentFile.listFiles()).map(DocumentFileCompat::getName).toList() - .toBlocking().single().toArray(new String[0]); + backupsArray = documentFile.listFiles().stream().map(DocumentFileCompat::getName) + .collect(toList()).toArray(new String[0]); } else { backupsArray = StorageHelper.getOrCreateExternalStoragePublicDir().list(); } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/async/DataBackupIntentService.java b/omniNotes/src/main/java/it/feio/android/omninotes/async/DataBackupIntentService.java index e74f5ae23..49b78db5d 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/async/DataBackupIntentService.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/async/DataBackupIntentService.java @@ -35,6 +35,7 @@ import it.feio.android.omninotes.OmniNotes; import it.feio.android.omninotes.R; import it.feio.android.omninotes.db.DbHelper; +import it.feio.android.omninotes.exceptions.BackupException; import it.feio.android.omninotes.helpers.BackupHelper; import it.feio.android.omninotes.helpers.DocumentFileHelper; import it.feio.android.omninotes.helpers.LogDelegate; @@ -45,10 +46,7 @@ import it.feio.android.omninotes.models.Note; import it.feio.android.omninotes.models.listeners.OnAttachingFileListener; import it.feio.android.omninotes.utils.ReminderHelper; -import it.feio.android.omninotes.utils.StorageHelper; -import java.io.File; import java.io.IOException; -import rx.Observable; public class DataBackupIntentService extends IntentService implements OnAttachingFileListener { @@ -109,13 +107,16 @@ private void exportData(Intent intent) { @TargetApi(VERSION_CODES.O) private synchronized void importData(Intent intent) { - var backupDir = Observable.from(DocumentFileCompat.Companion.fromTreeUri(getBaseContext(), - Uri.parse(Prefs.getString(PREF_BACKUP_FOLDER_URI, null))).listFiles()) - .filter(f -> f.getName().equals(intent.getStringExtra(INTENT_BACKUP_NAME))).toBlocking() - .single(); + var backupDir = DocumentFileCompat.Companion.fromTreeUri(getBaseContext(), + Uri.parse(Prefs.getString(PREF_BACKUP_FOLDER_URI, null))).listFiles().stream() + .filter(f -> f.getName().equals(intent.getStringExtra(INTENT_BACKUP_NAME))).findFirst(); + + if (!backupDir.isPresent()) { + throw new BackupException("Backup folder not found", new RuntimeException()); + } - BackupHelper.importNotes(backupDir); - BackupHelper.importAttachments(backupDir, mNotificationsHelper); + BackupHelper.importNotes(backupDir.get()); + BackupHelper.importAttachments(backupDir.get(), mNotificationsHelper); resetReminders(); mNotificationsHelper.cancel(); @@ -133,21 +134,24 @@ private synchronized void importData(Intent intent) { private synchronized void deleteData(Intent intent) { String backupName = intent.getStringExtra(INTENT_BACKUP_NAME); - var backupDir = Observable.from(DocumentFileCompat.Companion.fromTreeUri(getBaseContext(), - Uri.parse(Prefs.getString(PREF_BACKUP_FOLDER_URI, null))).listFiles()) - .filter(f -> f.getName().equals(intent.getStringExtra(INTENT_BACKUP_NAME))).toBlocking() - .single(); - try { - if (DocumentFileHelper.delete(backupDir)) { - mNotificationsHelper.finish(getString(R.string.data_deletion_completed), - backupName + " " + getString(R.string.deleted)); - } else { - LogDelegate.e("Can't delete backup " + backupName); + var backupDir = DocumentFileCompat.Companion.fromTreeUri(getBaseContext(), + Uri.parse(Prefs.getString(PREF_BACKUP_FOLDER_URI, null))).listFiles().stream() + .filter(f -> f.getName().equals(intent.getStringExtra(INTENT_BACKUP_NAME))) + .findFirst(); + + if (backupDir.isPresent()) { + try { + if (DocumentFileHelper.delete(backupDir.get())) { + mNotificationsHelper.finish(getString(R.string.data_deletion_completed), + backupName + " " + getString(R.string.deleted)); + } else { + LogDelegate.e("Can't delete backup " + backupName); + mNotificationsHelper.finish(getString(R.string.data_deletion_error), backupName); + } + } catch (IOException e) { + LogDelegate.e("Can't delete backup " + backupName, e); mNotificationsHelper.finish(getString(R.string.data_deletion_error), backupName); } - } catch (IOException e) { - LogDelegate.e("Can't delete backup " + backupName, e); - mNotificationsHelper.finish(getString(R.string.data_deletion_error), backupName); } } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/async/upgrade/UpgradeProcessor.java b/omniNotes/src/main/java/it/feio/android/omninotes/async/upgrade/UpgradeProcessor.java index 825653ddf..633a9f4a0 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/async/upgrade/UpgradeProcessor.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/async/upgrade/UpgradeProcessor.java @@ -23,7 +23,7 @@ import static it.feio.android.omninotes.utils.ConstantsBase.MIME_TYPE_IMAGE; import static it.feio.android.omninotes.utils.ConstantsBase.MIME_TYPE_VIDEO; import static java.lang.Integer.parseInt; -import static rx.Observable.from; +import static java.util.stream.Collectors.toList; import android.content.ContentValues; import android.net.Uri; @@ -39,13 +39,15 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.commons.io.FilenameUtils; /** * Processor used to perform asynchronous tasks on database upgrade. It's not intended to be used to - * perform actions strictly related to DB (for this {@link it.feio.android.omninotes.db.DbHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, + * perform actions strictly related to DB (for this + * {@link it.feio.android.omninotes.db.DbHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, * int, int)} DbHelper.onUpgrade()} is used */ public class UpgradeProcessor { @@ -81,12 +83,12 @@ public static void process(int dbOldVersion, int dbNewVersion) } private List getMethodsToLaunch(int dbOldVersion, int dbNewVersion) { - return from(getInstance().getClass().getDeclaredMethods()) + return Arrays.stream(getInstance().getClass().getDeclaredMethods()) .filter(method -> method.getName().matches(METHODS_PREFIX + "\\d+")) .filter(method -> { int methodVersionPostfix = parseInt(method.getName().replace(METHODS_PREFIX, "")); return dbOldVersion <= methodVersionPostfix && methodVersionPostfix <= dbNewVersion; - }).toList().toBlocking().single(); + }).collect(toList()); } /** @@ -184,16 +186,17 @@ private void onUpgradeTo501() { private void onUpgradeTo625() { var attachmentsDir = StorageHelper.getAttachmentDir(); var dbHelper = DbHelper.getInstance(); - from(dbHelper.getAllAttachments()) + dbHelper.getAllAttachments().stream() .filter(attachment -> "content".equals(attachment.getUri().getScheme())) .forEach(attachment -> { - var fileName = attachment.getUri().getPathSegments().get(attachment.getUri().getPathSegments().size() - 1); - var file = new File(attachmentsDir + "/" + fileName); - if (file.exists()) { - attachment.setUri(Uri.fromFile(file)); - dbHelper.updateAttachment(attachment); - } - }); + var fileName = attachment.getUri().getPathSegments() + .get(attachment.getUri().getPathSegments().size() - 1); + var file = new File(attachmentsDir + "/" + fileName); + if (file.exists()) { + attachment.setUri(Uri.fromFile(file)); + dbHelper.updateAttachment(attachment); + } + }); } } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/db/DbHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/db/DbHelper.java index 3f0c30c17..c0905a6a9 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/db/DbHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/db/DbHelper.java @@ -30,6 +30,8 @@ import static it.feio.android.omninotes.utils.ConstantsBase.PREF_SORTING_COLUMN; import static it.feio.android.omninotes.utils.ConstantsBase.TIMESTAMP_UNIX_EPOCH; import static it.feio.android.omninotes.utils.Navigation.checkNavigation; +import static java.util.regex.Pattern.MULTILINE; +import static java.util.stream.Collectors.toList; import android.content.ContentValues; import android.content.Context; @@ -56,6 +58,7 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.HashMap; @@ -752,19 +755,17 @@ public List getNotesByTag(String[] tags) { whereCondition.append(" AND " + KEY_TRASHED + " IS ") .append(checkNavigation(Navigation.TRASH) ? "" : "NOT ").append("1"); - return rx.Observable.from(getNotes(whereCondition.toString(), true)) + return getNotes(whereCondition.toString(), true).stream() .map(note -> { - boolean matches = rx.Observable.from(tags) - .all(tag -> { - Pattern p = Pattern.compile(".*(\\s|^)" + tag + "(\\s|$).*", - Pattern.MULTILINE); - return p.matcher( - (note.getTitle() + " " + note.getContent())).find(); - }).toBlocking().single(); + boolean matches = Arrays.stream(tags) + .allMatch(tag -> { + var p = Pattern.compile(".*(\\s|^)" + tag + "(\\s|$).*", MULTILINE); + return p.matcher((note.getTitle() + " " + note.getContent())).find(); + }); return matches ? note : null; }) - .filter(o -> o != null) - .toList().toBlocking().single(); + .filter(Objects::nonNull) + .collect(toList()); } /** diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/BackupHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/BackupHelper.java index 47a96158c..a87478d36 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/BackupHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/BackupHelper.java @@ -24,11 +24,10 @@ import static it.feio.android.omninotes.utils.ConstantsBase.DATABASE_NAME; import static it.feio.android.omninotes.utils.ConstantsBase.PREF_BACKUP_FOLDER_URI; import static it.feio.android.omninotes.utils.ConstantsBase.PREF_PASSWORD; +import static java.util.stream.Collectors.toList; import android.content.Context; import android.content.Intent; -import android.os.Build.VERSION; -import android.os.Build.VERSION_CODES; import android.text.TextUtils; import android.webkit.MimeTypeMap; import androidx.annotation.NonNull; @@ -50,17 +49,16 @@ import java.io.IOException; import java.net.URI; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import lombok.experimental.UtilityClass; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch; -import rx.Observable; @UtilityClass public final class BackupHelper { @@ -121,7 +119,7 @@ public static boolean exportAttachments(NotificationsHelper notificationsHelper, notifyAttachmentBackup(notificationsHelper, list, exported, failedString); } - Observable.from(listOld) + listOld.stream() .filter(attachment -> !list.contains(attachment)) .forEach(attachment -> destinationattachmentsDir.findFile( attachment.getUri().getLastPathSegment()).delete()); @@ -153,11 +151,11 @@ private static void exportAttachment(DocumentFileCompat attachmentsDestination, } public static List importNotes(DocumentFileCompat backupDir) { - return Observable.from(backupDir.listFiles()) + return backupDir.listFiles().stream() .filter(f -> f.getName().matches("\\d{13}.json")) .map(BackupHelper::importNote) - .filter(n -> n != null) - .toList().toBlocking().single(); + .filter(Objects::nonNull) + .collect(toList()); } @Nullable @@ -206,18 +204,18 @@ public static boolean importAttachments(DocumentFileCompat backupDir, Notificati AtomicInteger imported = new AtomicInteger(); ArrayList attachments = DbHelper.getInstance().getAllAttachments(); var BackupedAttachments = backupAttachmentsDir.listFiles(); - rx.Observable.from(attachments) - .forEach(attachment -> { - try { - importAttachment(BackupedAttachments, attachmentsDir, attachment); - if (notificationsHelper != null) { - notificationsHelper.updateMessage(TextHelper.capitalize(getAppContext().getString(R.string.attachment)) + " " - + imported.incrementAndGet() + "/" + attachments.size()); - } - } catch (BackupAttachmentException e) { - result.set(false); - } - }); + attachments.forEach(attachment -> { + try { + importAttachment(BackupedAttachments, attachmentsDir, attachment); + if (notificationsHelper != null) { + notificationsHelper.updateMessage( + TextHelper.capitalize(getAppContext().getString(R.string.attachment)) + " " + + imported.incrementAndGet() + "/" + attachments.size()); + } + } catch (BackupAttachmentException e) { + result.set(false); + } + }); return result.get(); } @@ -226,8 +224,8 @@ static void importAttachment(List backupedAttachments, File String attachmentName = attachment.getUri().getLastPathSegment(); try { File destinationAttachment = new File(attachmentsDir, attachmentName); - var backupedAttachment = Observable.from(backupedAttachments) - .filter(ba -> attachmentName.equals(ba.getName())).toBlocking().single(); + var backupedAttachment = backupedAttachments.stream().filter(ba -> attachmentName.equals(ba.getName())) + .findFirst().get(); DocumentFileHelper.copyFileTo(getAppContext(), backupedAttachment, destinationAttachment); } catch (Exception e) { LogDelegate.e("Error importing the attachment " + attachment.getUri().getPath(), e); diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/count/DefaultWordCounter.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/count/DefaultWordCounter.java index b972c7e95..c4131a21b 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/count/DefaultWordCounter.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/count/DefaultWordCounter.java @@ -18,7 +18,7 @@ package it.feio.android.omninotes.helpers.count; import it.feio.android.omninotes.models.Note; -import rx.Observable; +import java.util.stream.Stream; public class DefaultWordCounter implements WordCounter { @@ -50,11 +50,10 @@ public int countWords(Note note) { @Override public int countChars(Note note) { String titleAndContent = note.getTitle() + "\n" + note.getContent(); - return Observable - .from(sanitizeTextForWordsAndCharsCount(note, titleAndContent).split("")) + return (int) Stream.of(sanitizeTextForWordsAndCharsCount(note, titleAndContent).split("")) .map(String::trim) .filter(s -> !s.isEmpty()) - .count().toBlocking().single(); + .count(); } } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/count/IdeogramsWordCounter.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/count/IdeogramsWordCounter.java index 1cbe3361d..40a4edbba 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/count/IdeogramsWordCounter.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/count/IdeogramsWordCounter.java @@ -18,7 +18,7 @@ package it.feio.android.omninotes.helpers.count; import it.feio.android.omninotes.models.Note; -import rx.Observable; +import java.util.Arrays; public class IdeogramsWordCounter implements WordCounter { @@ -30,9 +30,10 @@ public int countWords(Note note) { @Override public int countChars(Note note) { String titleAndContent = note.getTitle() + "\n" + note.getContent(); - return Observable - .from(sanitizeTextForWordsAndCharsCount(note, titleAndContent).split("")) - .filter(s -> !s.matches("\\s")) - .count().toBlocking().single(); + return Math.toIntExact( + Arrays.stream(sanitizeTextForWordsAndCharsCount(note, titleAndContent).split("")) + .filter(s -> !s.matches("\\s")) + .count()); } + } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/PasswordHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/PasswordHelper.java index 0e82fe60a..f7a2d84f1 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/PasswordHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/PasswordHelper.java @@ -36,9 +36,6 @@ import it.feio.android.omninotes.db.DbHelper; import it.feio.android.omninotes.models.PasswordValidator; import lombok.experimental.UtilityClass; -import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; @UtilityClass @@ -139,23 +136,17 @@ public static void resetPassword(final Activity mActivity) { public static void removePassword() { - Observable - .from(DbHelper.getInstance().getNotesWithLock(true)) - .subscribeOn(Schedulers.newThread()) - .observeOn(AndroidSchedulers.mainThread()) - .doOnNext(note -> { - note.setLocked(false); - DbHelper.getInstance().updateNote(note, false); - }) - .doOnCompleted(() -> { - Prefs.edit() - .remove(PREF_PASSWORD) - .remove(PREF_PASSWORD_QUESTION) - .remove(PREF_PASSWORD_ANSWER) - .remove("settings_password_access") - .apply(); - EventBus.getDefault().post(new PasswordRemovedEvent()); - }) - .subscribe(); + DbHelper.getInstance().getNotesWithLock(true).forEach(note -> { + note.setLocked(false); + DbHelper.getInstance().updateNote(note, false); + }); + Prefs.edit() + .remove(PREF_PASSWORD) + .remove(PREF_PASSWORD_QUESTION) + .remove(PREF_PASSWORD_ANSWER) + .remove("settings_password_access") + .apply(); + EventBus.getDefault().post(new PasswordRemovedEvent()); } + } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java index 5b28ce0a1..306342dea 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java @@ -17,8 +17,6 @@ package it.feio.android.omninotes.utils; -import static rx.Observable.from; - import androidx.core.util.Pair; import it.feio.android.omninotes.db.DbHelper; import it.feio.android.omninotes.models.Note; @@ -96,7 +94,7 @@ public static String removeTags(String text, List tagsToRemove) { return text; } var textCopy = new AtomicReference<>(text); - from(tagsToRemove).forEach(tagToRemove -> textCopy.set(removeTag(textCopy.get(), tagToRemove))); + tagsToRemove.forEach(tagToRemove -> textCopy.set(removeTag(textCopy.get(), tagToRemove))); return textCopy.get(); } @@ -106,11 +104,10 @@ private static String removeTag(String textCopy, Tag tagToRemove) { } private static String tokenizeAndRemoveTag(String text, String separator, Tag tagToRemove) { - return from(text.split(separator)) + return Arrays.stream(text.split(separator)) .map(word -> removeTagFromWord(word, tagToRemove)) .reduce((s, s2) -> s + separator + s2) - .toBlocking() - .singleOrDefault("") + .orElse("") .trim(); }