Skip to content

Commit

Permalink
Rewritten SharedPreferences management with external library
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Apr 12, 2021
1 parent f48452e commit 4381e06
Show file tree
Hide file tree
Showing 32 changed files with 194 additions and 288 deletions.
7 changes: 4 additions & 3 deletions omniNotes/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ dependencies {
exclude module: 'recyclerview-v7'
}

debugImplementation('com.squareup.leakcanary:leakcanary-android:2.4') {
exclude group: "com.android.support"
}
// debugImplementation('com.squareup.leakcanary:leakcanary-android:2.4') {
// exclude group: "com.android.support"
// }

compileOnly "org.projectlombok:lombok:1.18.12"
annotationProcessor 'org.projectlombok:lombok:1.18.12'
Expand Down Expand Up @@ -182,6 +182,7 @@ dependencies {
implementation 'org.bitbucket.cowwoc:diff-match-patch:1.1'
implementation 'com.github.bosphere.android-filelogger:filelogger:1.0.7'
implementation 'com.github.sevar83:indeterminate-checkbox:1.0.5@aar'
implementation 'com.pixplicity.easyprefs:library:1.9.0'

implementation('com.github.federicoiosue:simplegallery:3.0.1') {
exclude group: 'com.android.support'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.rule.GrantPermissionRule;
import com.pixplicity.easyprefs.library.Prefs;
import de.greenrobot.event.EventBus;
import it.feio.android.omninotes.async.bus.CategoriesUpdatedEvent;
import it.feio.android.omninotes.async.bus.NotesDeletedEvent;
Expand Down Expand Up @@ -78,7 +79,7 @@ public class BaseAndroidTestCase {
@BeforeClass
public static void setUpBeforeClass() {
testContext = ApplicationProvider.getApplicationContext();
prefs = testContext.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_MULTI_PROCESS);
prefs = Prefs.getPreferences();
dbHelper = DbHelper.getInstance(testContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.content.Context;
import android.content.SharedPreferences;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.pixplicity.easyprefs.library.Prefs;
import it.feio.android.omninotes.BaseAndroidTestCase;
import it.feio.android.omninotes.R;
import it.feio.android.omninotes.utils.Constants;
Expand All @@ -47,9 +48,7 @@ public void checkUtilityClassWellDefined() throws Exception {
@Test
public void changeSharedPrefrencesLanguage() {
LanguageHelper.updateLanguage(testContext, Locale.ITALY.toString());
SharedPreferences prefs = testContext.getSharedPreferences(Constants.PREFS_NAME, Context
.MODE_MULTI_PROCESS);
String language = prefs.getString(Constants.PREF_LANG, "");
String language = Prefs.getString(Constants.PREF_LANG, "");
assertEquals(Locale.ITALY.toString(), language);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public void addNewCategory() {

onView(
allOf(withId(R.id.menu_category), withContentDescription(R.string.category), isDisplayed()))
.perform(
click());
.perform(click());

// Materialdialog "Add Category"
onView(isRoot()).perform(waitId(R.id.md_buttonDefaultPositive, 5000));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package it.feio.android.omninotes;

import static it.feio.android.omninotes.utils.Constants.PREFS_NAME;
import static it.feio.android.omninotes.utils.ConstantsBase.INTENT_UPDATE_DASHCLOCK;
import static it.feio.android.omninotes.utils.ConstantsBase.PREF_NAVIGATION;

Expand All @@ -26,7 +25,6 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
Expand All @@ -38,6 +36,7 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.pixplicity.easyprefs.library.Prefs;
import it.feio.android.omninotes.helpers.LanguageHelper;
import it.feio.android.omninotes.helpers.LogDelegate;
import it.feio.android.omninotes.models.Note;
Expand All @@ -55,8 +54,6 @@ public class BaseActivity extends AppCompatActivity {
protected static final int TRANSITION_VERTICAL = 0;
protected static final int TRANSITION_HORIZONTAL = 1;

protected SharedPreferences prefs;

protected String navigation;
protected String navigationTmp; // used for widget navigation

Expand All @@ -76,7 +73,6 @@ protected void attachBaseContext(Context newBase) {

@Override
protected void onCreate(Bundle savedInstanceState) {
prefs = getSharedPreferences(PREFS_NAME, MODE_MULTI_PROCESS);
// Forces menu overflow icon
try {
ViewConfiguration config = ViewConfiguration.get(this.getApplicationContext());
Expand All @@ -96,13 +92,13 @@ protected void onCreate(Bundle savedInstanceState) {
protected void onResume() {
super.onResume();
String navNotes = getResources().getStringArray(R.array.navigation_list_codes)[0];
navigation = prefs.getString(PREF_NAVIGATION, navNotes);
LogDelegate.d(prefs.getAll().toString());
navigation = Prefs.getString(PREF_NAVIGATION, navNotes);
LogDelegate.d(Prefs.getAll().toString());
}


protected void showToast(CharSequence text, int duration) {
if (prefs.getBoolean("settings_enable_info", true)) {
if (Prefs.getBoolean("settings_enable_info", true)) {
Toast.makeText(getApplicationContext(), text, duration).show();
}
}
Expand All @@ -114,7 +110,7 @@ protected void showToast(CharSequence text, int duration) {
*/
public void requestPassword(final Activity mActivity, List<Note> notes,
final PasswordValidator mPasswordValidator) {
if (prefs.getBoolean("settings_password_access", false)) {
if (Prefs.getBoolean("settings_password_access", false)) {
mPasswordValidator.onPasswordValidated(PasswordValidator.Result.SUCCEED);
return;
}
Expand All @@ -139,7 +135,7 @@ public boolean updateNavigation(String nav) {
.equals(nav))) {
return false;
}
prefs.edit().putString(PREF_NAVIGATION, nav).apply();
Prefs.edit().putString(PREF_NAVIGATION, nav).apply();
navigation = nav;
navigationTmp = null;
return true;
Expand All @@ -148,8 +144,6 @@ public boolean updateNavigation(String nav) {

/**
* Retrieves resource by name
*
* @returnnotifyAppWidgets
*/
private String getStringResourceByName(String aString) {
String packageName = getApplicationContext().getPackageName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

package it.feio.android.omninotes;

import static it.feio.android.omninotes.utils.Constants.PREFS_NAME;
import static it.feio.android.omninotes.utils.ConstantsBase.INTENT_CATEGORY;
import static it.feio.android.omninotes.utils.ConstantsBase.PREF_NAVIGATION;
import static java.lang.Integer.parseInt;

import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.TextUtils;
Expand All @@ -31,6 +29,7 @@
import androidx.appcompat.app.AppCompatActivity;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.color.ColorChooserDialog;
import com.pixplicity.easyprefs.library.Prefs;
import de.greenrobot.event.EventBus;
import it.feio.android.omninotes.async.bus.CategoriesUpdatedEvent;
import it.feio.android.omninotes.databinding.ActivityCategoryBinding;
Expand Down Expand Up @@ -152,11 +151,10 @@ public void deleteCategory() {
.positiveColorRes(R.color.colorAccent)
.onPositive((dialog, which) -> {
// Changes navigation if actually are shown notes associated with this category
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_MULTI_PROCESS);
String navNotes = getResources().getStringArray(R.array.navigation_list_codes)[0];
String navigation = prefs.getString(PREF_NAVIGATION, navNotes);
String navigation = Prefs.getString(PREF_NAVIGATION, navNotes);
if (String.valueOf(category.getId()).equals(navigation)) {
prefs.edit().putString(PREF_NAVIGATION, navNotes).apply();
Prefs.edit().putString(PREF_NAVIGATION, navNotes).apply();
}
// Removes category and edit notes associated with it
DbHelper db = DbHelper.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.neopixl.pixlui.components.edittext.EditText;
import com.pixplicity.easyprefs.library.Prefs;
import com.pushbullet.android.extension.MessagingExtension;
import de.greenrobot.event.EventBus;
import de.keyboardsurfer.android.widget.crouton.Style;
Expand Down Expand Up @@ -228,7 +229,6 @@ public class DetailFragment extends BaseFragment implements OnReminderPickedList
private boolean showKeyboard = false;
private boolean swiping;
private int startSwipeX;
private SharedPreferences prefs;
private boolean orientationChanged;
private long audioRecordingTimeStart;
private long audioRecordingTime;
Expand Down Expand Up @@ -315,8 +315,6 @@ public void onActivityCreated(Bundle savedInstanceState) {

mainActivity = (MainActivity) getActivity();

prefs = mainActivity.prefs;

mainActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
mainActivity.getToolbar().setNavigationOnClickListener(v -> navigateUp());

Expand Down Expand Up @@ -435,8 +433,8 @@ private void init() {
private void checkNoteLock(Note note) {
// If note is locked security password will be requested
if (note.isLocked()
&& prefs.getString(PREF_PASSWORD, null) != null
&& !prefs.getBoolean("settings_password_access", false)) {
&& Prefs.getString(PREF_PASSWORD, null) != null
&& !Prefs.getBoolean("settings_password_access", false)) {
PasswordHelper.requestPassword(mainActivity, passwordConfirmed -> {
switch (passwordConfirmed) {
case SUCCEED:
Expand Down Expand Up @@ -495,7 +493,7 @@ private void handleIntents() {
// with tags to set tag
if (i.hasExtra(INTENT_WIDGET)) {
String widgetId = i.getExtras().get(INTENT_WIDGET).toString();
String sqlCondition = prefs.getString(PREF_WIDGET_PREFIX + widgetId, "");
String sqlCondition = Prefs.getString(PREF_WIDGET_PREFIX + widgetId, "");
String categoryId = TextHelper.checkIntentCategory(sqlCondition);
if (categoryId != null) {
Category category;
Expand Down Expand Up @@ -579,7 +577,6 @@ private void importAttachments(Intent i) {

@SuppressLint("NewApi")
private void initViews() {

// Sets onTouchListener to the whole activity to swipe notes
binding.detailRoot.setOnTouchListener(this);

Expand All @@ -601,15 +598,15 @@ private void initViews() {

private void initViewFooter() {
String creation = DateHelper
.getFormattedDate(noteTmp.getCreation(), prefs.getBoolean(PREF_PRETTIFIED_DATES, true));
.getFormattedDate(noteTmp.getCreation(), Prefs.getBoolean(PREF_PRETTIFIED_DATES, true));
binding.creation
.append(creation.length() > 0 ? getString(R.string.creation) + " " + creation : "");
if (binding.creation.getText().length() == 0) {
binding.creation.setVisibility(View.GONE);
}

String lastModification = DateHelper
.getFormattedDate(noteTmp.getLastModification(), prefs.getBoolean(
.getFormattedDate(noteTmp.getLastModification(), Prefs.getBoolean(
PREF_PRETTIFIED_DATES, true));
binding.lastModification
.append(lastModification.length() > 0 ? getString(R.string.last_update) + " " +
Expand Down Expand Up @@ -665,7 +662,7 @@ private void initViewLocation() {
}

// Automatic location insertion
if (prefs.getBoolean(PREF_AUTO_LOCATION, false) && noteTmp.get_id() == null) {
if (Prefs.getBoolean(PREF_AUTO_LOCATION, false) && noteTmp.get_id() == null) {
getLocation(detailFragment);
}

Expand Down Expand Up @@ -704,7 +701,7 @@ private void getLocation(OnGeoUtilResultListener onGeoUtilResultListener) {

private void initViewAttachments() {
// Attachments position based on preferences
if (prefs.getBoolean(PREF_ATTACHMENTS_ON_BOTTOM, false)) {
if (Prefs.getBoolean(PREF_ATTACHMENTS_ON_BOTTOM, false)) {
binding.detailAttachmentsBelow.inflate();
} else {
binding.detailAttachmentsAbove.inflate();
Expand Down Expand Up @@ -887,7 +884,7 @@ private void requestFocus(final EditText view) {
*/
private void setTagMarkerColor(Category tag) {

String colorsPref = prefs.getString("settings_colors_app", PREF_COLORS_APP_DEFAULT);
String colorsPref = Prefs.getString("settings_colors_app", PREF_COLORS_APP_DEFAULT);

// Checking preference
if (!"disabled".equals(colorsPref)) {
Expand Down Expand Up @@ -1151,14 +1148,14 @@ private void toggleChecklist() {
// Retrieves options checkboxes and initialize their values
final CheckBox keepChecked = layout.findViewById(R.id.checklist_keep_checked);
final CheckBox keepCheckmarks = layout.findViewById(R.id.checklist_keep_checkmarks);
keepChecked.setChecked(prefs.getBoolean(PREF_KEEP_CHECKED, true));
keepCheckmarks.setChecked(prefs.getBoolean(PREF_KEEP_CHECKMARKS, true));
keepChecked.setChecked(Prefs.getBoolean(PREF_KEEP_CHECKED, true));
keepCheckmarks.setChecked(Prefs.getBoolean(PREF_KEEP_CHECKMARKS, true));

new MaterialDialog.Builder(mainActivity)
.customView(layout, false)
.positiveText(R.string.ok)
.onPositive((dialog, which) -> {
prefs.edit()
Prefs.edit()
.putBoolean(PREF_KEEP_CHECKED, keepChecked.isChecked())
.putBoolean(PREF_KEEP_CHECKMARKS, keepCheckmarks.isChecked())
.apply();
Expand All @@ -1167,15 +1164,15 @@ private void toggleChecklist() {
}

private void toggleChecklist2() {
boolean keepChecked = prefs.getBoolean(PREF_KEEP_CHECKED, true);
boolean showChecks = prefs.getBoolean(PREF_KEEP_CHECKMARKS, true);
boolean keepChecked = Prefs.getBoolean(PREF_KEEP_CHECKED, true);
boolean showChecks = Prefs.getBoolean(PREF_KEEP_CHECKMARKS, true);
toggleChecklist2(keepChecked, showChecks);
}

private void toggleChecklist2(final boolean keepChecked, final boolean showChecks) {
mChecklistManager = mChecklistManager == null ? new ChecklistManager(mainActivity) : mChecklistManager;
int checkedItemsBehavior = Integer
.parseInt(prefs.getString("settings_checked_items_behavior", String.valueOf
.parseInt(Prefs.getString("settings_checked_items_behavior", String.valueOf
(it.feio.android.checklistview.Settings.CHECKED_HOLD)));
mChecklistManager
.showCheckMarks(showChecks)
Expand Down Expand Up @@ -1328,8 +1325,8 @@ private void takeVideo() {
attachmentUri = FileProviderHelper.getFileProvider(f);
takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, attachmentUri);
String maxVideoSizeStr = "".equals(prefs.getString("settings_max_video_size",
"")) ? "0" : prefs.getString("settings_max_video_size", "");
String maxVideoSizeStr = "".equals(Prefs.getString("settings_max_video_size",
"")) ? "0" : Prefs.getString("settings_max_video_size", "");
long maxVideoSize = parseLong(maxVideoSizeStr) * 1024L * 1024L;
takeVideoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize);
startActivityForResult(takeVideoIntent, TAKE_VIDEO);
Expand Down Expand Up @@ -1569,7 +1566,7 @@ void saveNote(OnNoteSaved mOnNoteSaved) {
* Checks if nothing is changed to avoid committing if possible (check)
*/
private boolean saveNotNeeded() {
if (noteTmp.get_id() == null && prefs.getBoolean(PREF_AUTO_LOCATION, false)) {
if (noteTmp.get_id() == null && Prefs.getBoolean(PREF_AUTO_LOCATION, false)) {
note.setLatitude(noteTmp.getLatitude());
note.setLongitude(noteTmp.getLongitude());
}
Expand Down Expand Up @@ -1660,14 +1657,14 @@ private void lockNote() {
LogDelegate.d("Locking or unlocking note " + note.get_id());

// If security password is not set yes will be set right now
if (prefs.getString(PREF_PASSWORD, null) == null) {
if (Prefs.getString(PREF_PASSWORD, null) == null) {
Intent passwordIntent = new Intent(mainActivity, PasswordActivity.class);
startActivityForResult(passwordIntent, SET_PASSWORD);
return;
}

// If password has already been inserted will not be asked again
if (noteTmp.isPasswordChecked() || prefs.getBoolean("settings_password_access", false)) {
if (noteTmp.isPasswordChecked() || Prefs.getBoolean("settings_password_access", false)) {
lockUnlock();
return;
}
Expand All @@ -1686,7 +1683,7 @@ private void lockNote() {

private void lockUnlock() {
// Empty password has been set
if (prefs.getString(PREF_PASSWORD, null) == null) {
if (Prefs.getString(PREF_PASSWORD, null) == null) {
mainActivity.showMessage(R.string.password_not_set, ONStyle.WARN);
return;
}
Expand Down
Loading

0 comments on commit 4381e06

Please sign in to comment.