Skip to content

Commit

Permalink
MIN_SDK 24, moved from rxJava to native streams
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Aug 23, 2024
1 parent 3a118e4 commit 20ccb86
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 170 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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" });
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,14 +37,14 @@
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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import rx.Observable;

@RunWith(AndroidJUnit4.class)
public class BackupHelperTest extends BaseAndroidTestCase {
Expand Down Expand Up @@ -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());

Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down
Loading

0 comments on commit 20ccb86

Please sign in to comment.