diff --git a/README.md b/README.md index ff9f206484..09cfa30f6d 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ They're all listed into the [build.gradle](https://github.com/federicoiosue/Omni * Federico Iosue - [Website](https://federico.iosue.it) * [Other contributors](https://github.com/federicoiosue/Omni-Notes/blob/master/https://github.com/federicoiosue/Omni-Notes/blob/master/CONTRIBUTORS.md) +## auto backup ## License 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 73a2e3fffa..69dd823381 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java @@ -117,11 +117,11 @@ public class ListFragment extends BaseFragment implements OnViewTouchedListener, @BindView(R.id.fab) View fabView; @BindView(R.id.undobar) View undoBarView; @BindView(R.id.progress_wheel) ProgressWheel progress_wheel; - @BindView(R.id.snackbar_placeholder) View snackBarPlaceholder; + @BindView(R.id.snackbar_placeholder) View snackBarPlaceholder; - NoteViewHolder noteViewHolder; + NoteViewHolder noteViewHolder; - private List selectedNotes = new ArrayList<>(); + public static List selectedNotes = new ArrayList<>(); private SearchView searchView; private MenuItem searchMenuItem; private Menu menu; @@ -170,7 +170,7 @@ public void onCreate(Bundle savedInstanceState) { @Override public void onStart() { - super.onStart(); + super.onStart(); EventBus.getDefault().register(this, 1); } @@ -207,22 +207,22 @@ public void onActivityCreated(Bundle savedInstanceState) { if (savedInstanceState != null) { mainActivity.navigationTmp = savedInstanceState.getString("navigationTmp"); } - init(); + init(); } - private void init() { - initEasterEgg(); - initListView(); - ubc = new UndoBarController(undoBarView, this); + private void init() { + initEasterEgg(); + initListView(); + ubc = new UndoBarController(undoBarView, this); - initNotesList(mainActivity.getIntent()); - initFab(); - initTitle(); + initNotesList(mainActivity.getIntent()); + initFab(); + initTitle(); - // Restores again DefaultSharedPreferences too reload in case of data erased from Settings - prefs = mainActivity.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_MULTI_PROCESS); - } + // Restores again DefaultSharedPreferences too reload in case of data erased from Settings + prefs = mainActivity.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_MULTI_PROCESS); + } private void initFab() { @@ -596,7 +596,7 @@ private void prepareActionModeMenu() { } menu.findItem(R.id.menu_add_reminder).setVisible(true); menu.findItem(R.id.menu_category).setVisible(true); - menu.findItem(R.id.menu_uncomplete_checklists).setVisible(false); + menu.findItem(R.id.menu_uncomplete_checklists).setVisible(false); menu.findItem(R.id.menu_tags).setVisible(true); menu.findItem(R.id.menu_trash).setVisible(true); } @@ -643,70 +643,70 @@ private void initSearchView(final Menu menu) { MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() { - boolean searchPerformed = false; + boolean searchPerformed = false; - @Override - public boolean onMenuItemActionCollapse(MenuItem item) { - // Reinitialize notes list to all notes when search is collapsed - searchQuery = null; - if (searchLayout.getVisibility() == View.VISIBLE) { - toggleSearchLabel(false); - } - mainActivity.getIntent().setAction(Intent.ACTION_MAIN); - initNotesList(mainActivity.getIntent()); - mainActivity.supportInvalidateOptionsMenu(); - return true; - } - + @Override + public boolean onMenuItemActionCollapse(MenuItem item) { + // Reinitialize notes list to all notes when search is collapsed + searchQuery = null; + if (searchLayout.getVisibility() == View.VISIBLE) { + toggleSearchLabel(false); + } + mainActivity.getIntent().setAction(Intent.ACTION_MAIN); + initNotesList(mainActivity.getIntent()); + mainActivity.supportInvalidateOptionsMenu(); + return true; + } - @Override - public boolean onMenuItemActionExpand(MenuItem item) { - searchView.setOnQueryTextListener(new OnQueryTextListener() { - @Override - public boolean onQueryTextSubmit(String arg0) { + @Override + public boolean onMenuItemActionExpand(MenuItem item) { - return prefs.getBoolean("settings_instant_search", false); - } + searchView.setOnQueryTextListener(new OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String arg0) { + return prefs.getBoolean("settings_instant_search", false); + } - @Override - public boolean onQueryTextChange(String pattern) { - if (prefs.getBoolean("settings_instant_search", false) && searchLayout != null && - searchPerformed && mFragment.isAdded()) { - searchTags = null; - searchQuery = pattern; - NoteLoaderTask.getInstance().execute("getNotesByPattern", pattern); - return true; - } else { - searchPerformed = true; - return false; - } - } - }); - return true; - } - }); + @Override + public boolean onQueryTextChange(String pattern) { + + if (prefs.getBoolean("settings_instant_search", false) && searchLayout != null && + searchPerformed && mFragment.isAdded()) { + searchTags = null; + searchQuery = pattern; + NoteLoaderTask.getInstance().execute("getNotesByPattern", pattern); + return true; + } else { + searchPerformed = true; + return false; + } + } + }); + return true; + } + }); } private void setActionItemsVisibility(Menu menu, boolean searchViewHasFocus) { - boolean drawerOpen = mainActivity.getDrawerLayout() != null && mainActivity.getDrawerLayout().isDrawerOpen - (GravityCompat.START); - boolean expandedView = prefs.getBoolean(Constants.PREF_EXPANDED_VIEW, true); + boolean drawerOpen = mainActivity.getDrawerLayout() != null && mainActivity.getDrawerLayout().isDrawerOpen + (GravityCompat.START); + boolean expandedView = prefs.getBoolean(Constants.PREF_EXPANDED_VIEW, true); - int navigation = Navigation.getNavigation(); - boolean navigationReminders = navigation == Navigation.REMINDERS; - boolean navigationArchive = navigation == Navigation.ARCHIVE; - boolean navigationTrash = navigation == Navigation.TRASH; - boolean navigationCategory = navigation == Navigation.CATEGORY; + int navigation = Navigation.getNavigation(); + boolean navigationReminders = navigation == Navigation.REMINDERS; + boolean navigationArchive = navigation == Navigation.ARCHIVE; + boolean navigationTrash = navigation == Navigation.TRASH; + boolean navigationCategory = navigation == Navigation.CATEGORY; - boolean filterPastReminders = prefs.getBoolean(Constants.PREF_FILTER_PAST_REMINDERS, true); - boolean filterArchivedInCategory = navigationCategory && prefs.getBoolean(Constants - .PREF_FILTER_ARCHIVED_IN_CATEGORIES + Navigation.getCategory(), false); + boolean filterPastReminders = prefs.getBoolean(Constants.PREF_FILTER_PAST_REMINDERS, true); + boolean filterArchivedInCategory = navigationCategory && prefs.getBoolean(Constants + .PREF_FILTER_ARCHIVED_IN_CATEGORIES + Navigation.getCategory(), false); if (isFabAllowed()) { fab.setAllowed(true); @@ -716,19 +716,19 @@ private void setActionItemsVisibility(Menu menu, boolean searchViewHasFocus) { fab.hideFab(); } menu.findItem(R.id.menu_search).setVisible(!drawerOpen); - menu.findItem(R.id.menu_filter).setVisible(!drawerOpen && !filterPastReminders && navigationReminders && - !searchViewHasFocus); - menu.findItem(R.id.menu_filter_remove).setVisible(!drawerOpen && filterPastReminders && navigationReminders - && !searchViewHasFocus); - menu.findItem(R.id.menu_filter_category).setVisible(!drawerOpen && !filterArchivedInCategory && - navigationCategory && !searchViewHasFocus); - menu.findItem(R.id.menu_filter_category_remove).setVisible(!drawerOpen && filterArchivedInCategory && - navigationCategory && !searchViewHasFocus); - menu.findItem(R.id.menu_sort).setVisible(!drawerOpen && !navigationReminders && !searchViewHasFocus); + menu.findItem(R.id.menu_filter).setVisible(!drawerOpen && !filterPastReminders && navigationReminders && + !searchViewHasFocus); + menu.findItem(R.id.menu_filter_remove).setVisible(!drawerOpen && filterPastReminders && navigationReminders + && !searchViewHasFocus); + menu.findItem(R.id.menu_filter_category).setVisible(!drawerOpen && !filterArchivedInCategory && + navigationCategory && !searchViewHasFocus); + menu.findItem(R.id.menu_filter_category_remove).setVisible(!drawerOpen && filterArchivedInCategory && + navigationCategory && !searchViewHasFocus); + menu.findItem(R.id.menu_sort).setVisible(!drawerOpen && !navigationReminders && !searchViewHasFocus); menu.findItem(R.id.menu_expanded_view).setVisible(!drawerOpen && !expandedView && !searchViewHasFocus); menu.findItem(R.id.menu_contracted_view).setVisible(!drawerOpen && expandedView && !searchViewHasFocus); menu.findItem(R.id.menu_empty_trash).setVisible(!drawerOpen && navigationTrash); - menu.findItem(R.id.menu_uncomplete_checklists).setVisible(searchViewHasFocus); + menu.findItem(R.id.menu_uncomplete_checklists).setVisible(searchViewHasFocus); menu.findItem(R.id.menu_tags).setVisible(searchViewHasFocus); } @@ -762,20 +762,20 @@ public boolean performAction(MenuItem item, ActionMode actionMode) { mainActivity.getDrawerLayout().openDrawer(GravityCompat.START); } break; - case R.id.menu_filter: - filterReminders(true); - break; - case R.id.menu_filter_remove: - filterReminders(false); - break; - case R.id.menu_filter_category: - filterCategoryArchived(true); - break; - case R.id.menu_filter_category_remove: - filterCategoryArchived(false); - case R.id.menu_uncomplete_checklists: - filterByUncompleteChecklists(); - break; + case R.id.menu_filter: + filterReminders(true); + break; + case R.id.menu_filter_remove: + filterReminders(false); + break; + case R.id.menu_filter_category: + filterCategoryArchived(true); + break; + case R.id.menu_filter_category_remove: + filterCategoryArchived(false); + case R.id.menu_uncomplete_checklists: + filterByUncompleteChecklists(); + break; case R.id.menu_tags: filterByTags(); break; @@ -791,8 +791,8 @@ public boolean performAction(MenuItem item, ActionMode actionMode) { case R.id.menu_empty_trash: emptyTrash(); break; - default: - Log.e(Constants.TAG, "Wrong element choosen: " + item.getItemId()); + default: + Log.e(Constants.TAG, "Wrong element choosen: " + item.getItemId()); } } else { switch (item.getItemId()) { @@ -864,27 +864,27 @@ void editNote(final Note note, final View view) { if (note.isLocked() && !prefs.getBoolean("settings_password_access", false)) { PasswordHelper.requestPassword(mainActivity, passwordConfirmed -> { if (passwordConfirmed.equals(PasswordValidator.Result.SUCCEED)) { - note.setPasswordChecked(true); - AnimationsHelper.zoomListItem(mainActivity, view, getZoomListItemView(view, note), - listRoot, buildAnimatorListenerAdapter(note)); - } - }); + note.setPasswordChecked(true); + AnimationsHelper.zoomListItem(mainActivity, view, getZoomListItemView(view, note), + listRoot, buildAnimatorListenerAdapter(note)); + } + }); } else { AnimationsHelper.zoomListItem(mainActivity, view, getZoomListItemView(view, note), - listRoot, buildAnimatorListenerAdapter(note)); + listRoot, buildAnimatorListenerAdapter(note)); } } - void editNote2(Note note) { + void editNote2(Note note) { if (note.get_id() == null) { Log.d(Constants.TAG, "Adding new note"); // if navigation is a category it will be set into note try { if (Navigation.checkNavigation(Navigation.CATEGORY) || !TextUtils.isEmpty(mainActivity.navigationTmp)) { - String categoryId = (String) ObjectUtils.defaultIfNull(mainActivity.navigationTmp, - Navigation.getCategory().toString()); - note.setCategory(DbHelper.getInstance().getCategory(Long.parseLong(categoryId))); + String categoryId = (String) ObjectUtils.defaultIfNull(mainActivity.navigationTmp, + Navigation.getCategory().toString()); + note.setCategory(DbHelper.getInstance().getCategory(Long.parseLong(categoryId))); } } catch (NumberFormatException e) { Log.v(Constants.TAG, "Maybe was not a category!"); @@ -953,13 +953,13 @@ private void checkSortActionPerformed(MenuItem item) { listViewPositionOffset = 16; listViewPosition = 0; restoreListScrollPosition(); - toggleSearchLabel(false); + toggleSearchLabel(false); // Updates app widgets mainActivity.updateWidgets(); } else { - ((OmniNotes) getActivity().getApplication()).getAnalyticsHelper().trackActionFromResourceId(getActivity(), - item.getItemId()); - } + ((OmniNotes) getActivity().getApplication()).getAnalyticsHelper().trackActionFromResourceId(getActivity(), + item.getItemId()); + } } @@ -971,32 +971,32 @@ private void emptyTrash() { .content(R.string.empty_trash_confirmation) .positiveText(R.string.ok) .callback(new MaterialDialog.ButtonCallback() { - @Override - public void onPositive(MaterialDialog materialDialog) { - - boolean mustDeleteLockedNotes = false; - for (int i = 0; i < listAdapter.getCount(); i++) { - selectedNotes.add(listAdapter.getItem(i)); - mustDeleteLockedNotes = mustDeleteLockedNotes || listAdapter.getItem(i).isLocked(); - } - if (mustDeleteLockedNotes) { - mainActivity.requestPassword(mainActivity, getSelectedNotes(), + @Override + public void onPositive(MaterialDialog materialDialog) { + + boolean mustDeleteLockedNotes = false; + for (int i = 0; i < listAdapter.getCount(); i++) { + selectedNotes.add(listAdapter.getItem(i)); + mustDeleteLockedNotes = mustDeleteLockedNotes || listAdapter.getItem(i).isLocked(); + } + if (mustDeleteLockedNotes) { + mainActivity.requestPassword(mainActivity, getSelectedNotes(), passwordConfirmed -> { if (passwordConfirmed.equals(PasswordValidator.Result.SUCCEED) ) { deleteNotesExecute(); - } - }); - } else { - deleteNotesExecute(); - } - } - }).build().show(); + } + }); + } else { + deleteNotesExecute(); + } + } + }).build().show(); } /** * Notes list adapter initialization and association to view - * @FIXME: This method is a divine opprobrium and MUST be refactored. I'm ashamed by myself. + * @FIXME: This method is a divine opprobrium and MUST be refactored. I'm ashamed by myself. */ void initNotesList(Intent intent) { Log.d(Constants.TAG, "initNotesList intent: " + intent.getAction()); @@ -1013,14 +1013,14 @@ void initNotesList(Intent intent) { } if (Constants.ACTION_SHORTCUT_WIDGET.equals(intent.getAction())) { - return; - } + return; + } // Searching searchQuery = searchQueryInstant; searchQueryInstant = null; if (searchTags != null || searchQuery != null || searchUncompleteChecklists - || IntentChecker.checkAction(intent, Intent.ACTION_SEARCH, Constants.ACTION_SEARCH_UNCOMPLETE_CHECKLISTS)) { + || IntentChecker.checkAction(intent, Intent.ACTION_SEARCH, Constants.ACTION_SEARCH_UNCOMPLETE_CHECKLISTS)) { // Using tags if (searchTags != null && intent.getStringExtra(SearchManager.QUERY) == null) { @@ -1028,9 +1028,9 @@ void initNotesList(Intent intent) { NoteLoaderTask.getInstance().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "getNotesByTag", searchQuery); } else if (searchUncompleteChecklists || Constants.ACTION_SEARCH_UNCOMPLETE_CHECKLISTS.equals(intent.getAction())) { - searchQuery = getContext().getResources().getString(R.string.uncompleted_checklists); - searchUncompleteChecklists = true; - NoteLoaderTask.getInstance().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "getNotesByUncompleteChecklist"); + searchQuery = getContext().getResources().getString(R.string.uncompleted_checklists); + searchUncompleteChecklists = true; + NoteLoaderTask.getInstance().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "getNotesByUncompleteChecklist"); } else { // Get the intent, verify the action and get the query if (intent.getStringExtra(SearchManager.QUERY) != null) { @@ -1056,13 +1056,13 @@ void initNotesList(Intent intent) { mainActivity.navigationTmp = !TextUtils.isEmpty(categoryId) ? categoryId : null; } intent.removeExtra(Constants.INTENT_WIDGET); - if (mainActivity.navigationTmp != null) { + if (mainActivity.navigationTmp != null) { Long categoryId = Long.parseLong(mainActivity.navigationTmp); NoteLoaderTask.getInstance().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "getNotesByCategory", categoryId); - } else { + } else { NoteLoaderTask.getInstance().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "getAllNotes", true); - } + } } else { NoteLoaderTask.getInstance().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "getAllNotes", true); @@ -1083,7 +1083,7 @@ public void toggleSearchLabel(boolean activate) { AnimationsHelper.expandOrCollapse(searchLayout, false); searchTags = null; searchQuery = null; - searchUncompleteChecklists = false; + searchUncompleteChecklists = false; if (!goBackOnToggleSearchLabel) { mainActivity.getIntent().setAction(Intent.ACTION_MAIN); if (searchView != null) { @@ -1123,9 +1123,9 @@ public void onEvent(NotesLoadedEvent notesLoadedEvent) { View noteLayout = LayoutInflater.from(mainActivity).inflate(layoutSelected, null, false); noteViewHolder = new NoteViewHolder(noteLayout); - if (Navigation.getNavigation() != Navigation.UNCATEGORIZED && prefs.getBoolean(Constants.PREF_ENABLE_SWIPE, - true)) { - list.enableSwipeToDismiss((viewGroup, reverseSortedPositions) -> { + if (Navigation.getNavigation() != Navigation.UNCATEGORIZED && prefs.getBoolean(Constants.PREF_ENABLE_SWIPE, + true)) { + list.enableSwipeToDismiss((viewGroup, reverseSortedPositions) -> { // Avoids conflicts with action mode finishActionMode(); @@ -1139,13 +1139,13 @@ public void onEvent(NotesLoadedEvent notesLoadedEvent) { continue; } - if (note != null && note.isLocked()) { - PasswordHelper.requestPassword(mainActivity, passwordConfirmed -> { + if (note != null && note.isLocked()) { + PasswordHelper.requestPassword(mainActivity, passwordConfirmed -> { if (!passwordConfirmed.equals(PasswordValidator.Result.SUCCEED)) { - onUndo(null); - } - }); - } + onUndo(null); + } + }); + } getSelectedNotes().add(note); @@ -1185,27 +1185,27 @@ else if (Navigation.checkNavigation(Navigation.CATEGORY)) { restoreListScrollPosition(); } - animateListView(); + animateListView(); - closeFab(); + closeFab(); } - public void onEvent(PasswordRemovedEvent passwordRemovedEvent) { - initNotesList(mainActivity.getIntent()); - } + public void onEvent(PasswordRemovedEvent passwordRemovedEvent) { + initNotesList(mainActivity.getIntent()); + } - private void animateListView() { - if (!OmniNotes.isDebugBuild()) { - animate(progress_wheel).setDuration(getResources().getInteger(R.integer.list_view_fade_anim)).alpha(0); - animate(list).setDuration(getResources().getInteger(R.integer.list_view_fade_anim)).alpha(1); - } else { - progress_wheel.setVisibility(View.INVISIBLE); - list.setAlpha(1); - } - } + private void animateListView() { + if (!OmniNotes.isDebugBuild()) { + animate(progress_wheel).setDuration(getResources().getInteger(R.integer.list_view_fade_anim)).alpha(0); + animate(list).setDuration(getResources().getInteger(R.integer.list_view_fade_anim)).alpha(1); + } else { + progress_wheel.setVisibility(View.INVISIBLE); + list.setAlpha(1); + } + } - private void restoreListScrollPosition() { + private void restoreListScrollPosition() { if (list.getCount() > listViewPosition) { list.setSelectionFromTop(listViewPosition, listViewPositionOffset); new Handler().postDelayed(fab::showFab, 150); @@ -1282,7 +1282,7 @@ protected void trashNote(List notes, boolean trash) { private void selectAllNotes() { for (int i = 0; i < list.getChildCount(); i++) { LinearLayout v = (LinearLayout) list.getChildAt(i).findViewById(R.id.card_layout); - v.setBackgroundColor(getResources().getColor(R.color.list_bg_selected)); + v.setBackgroundColor(getResources().getColor(R.color.list_bg_selected)); } selectedNotes.clear(); for (int i = 0; i < listAdapter.getCount(); i++) { @@ -1297,19 +1297,19 @@ private void selectAllNotes() { /** * Batch note permanent deletion */ - private void deleteNotes() { - new MaterialDialog.Builder(mainActivity) - .content(R.string.delete_note_confirmation) - .positiveText(R.string.ok) - .onPositive((dialog, which) -> mainActivity.requestPassword(mainActivity, getSelectedNotes(), + private void deleteNotes() { + new MaterialDialog.Builder(mainActivity) + .content(R.string.delete_note_confirmation) + .positiveText(R.string.ok) + .onPositive((dialog, which) -> mainActivity.requestPassword(mainActivity, getSelectedNotes(), passwordConfirmed -> { - if (passwordConfirmed.equals(PasswordValidator.Result.SUCCEED) ) { - deleteNotesExecute(); - } - })) - .build() - .show(); - } + if (passwordConfirmed.equals(PasswordValidator.Result.SUCCEED) ) { + deleteNotesExecute(); + } + })) + .build() + .show(); + } /** @@ -1351,10 +1351,10 @@ public void archiveNotes(boolean archive) { // If actual navigation is not "Notes" the item will not be removed but replaced to fit the new state if (Navigation.checkNavigation(Navigation.NOTES) - || (Navigation.checkNavigation(Navigation.ARCHIVE) && !archive) - || (Navigation.checkNavigation(Navigation.CATEGORY) && prefs.getBoolean(Constants - .PREF_FILTER_ARCHIVED_IN_CATEGORIES + Navigation.getCategory(), false))) { - listAdapter.remove(note); + || (Navigation.checkNavigation(Navigation.ARCHIVE) && !archive) + || (Navigation.checkNavigation(Navigation.CATEGORY) && prefs.getBoolean(Constants + .PREF_FILTER_ARCHIVED_IN_CATEGORIES + Navigation.getCategory(), false))) { + listAdapter.remove(note); } else { note.setArchived(archive); listAdapter.replace(note, listAdapter.getPosition(note)); @@ -1533,7 +1533,7 @@ private void tagNotes() { .itemsCallbackMultiChoice(preSelectedTags, (dialog, which, text) -> { dialog.dismiss(); tagNotesExecute(tags, which, preSelectedTags); - return false; + return false; }).build().show(); } @@ -1702,8 +1702,8 @@ public void onPositive(MaterialDialog dialog) { @Override public void onNegative(MaterialDialog dialog) { - EventBus.getDefault().post(new NotesMergeEvent(true)); - } + EventBus.getDefault().post(new NotesMergeEvent(true)); + } }).build().show(); } @@ -1713,8 +1713,8 @@ public void onNegative(MaterialDialog dialog) { */ public void onEventAsync(NotesMergeEvent notesMergeEvent) { - final Note finalMergedNote = NotesHelper.mergeNotes(getSelectedNotes(), notesMergeEvent.keepMergedNotes); - new Handler(Looper.getMainLooper()).post(() -> { + final Note finalMergedNote = NotesHelper.mergeNotes(getSelectedNotes(), notesMergeEvent.keepMergedNotes); + new Handler(Looper.getMainLooper()).post(() -> { if (!notesMergeEvent.keepMergedNotes) { ArrayList notesIds = new ArrayList<>(); @@ -1735,7 +1735,7 @@ public void onEventAsync(NotesMergeEvent notesMergeEvent) { } - /** + /** * Excludes past reminders */ private void filterReminders(boolean filter) { @@ -1747,15 +1747,15 @@ private void filterReminders(boolean filter) { } - /** + /** * Excludes archived notes in categories navigation */ private void filterCategoryArchived(boolean filter) { - if (filter) { - prefs.edit().putBoolean(Constants.PREF_FILTER_ARCHIVED_IN_CATEGORIES + Navigation.getCategory(), true).apply(); - } else { - prefs.edit().remove(Constants.PREF_FILTER_ARCHIVED_IN_CATEGORIES + Navigation.getCategory()).apply(); - } + if (filter) { + prefs.edit().putBoolean(Constants.PREF_FILTER_ARCHIVED_IN_CATEGORIES + Navigation.getCategory(), true).apply(); + } else { + prefs.edit().remove(Constants.PREF_FILTER_ARCHIVED_IN_CATEGORIES + Navigation.getCategory()).apply(); + } // Change list view initNotesList(mainActivity.getIntent()); // Called to switch menu voices @@ -1763,9 +1763,9 @@ private void filterCategoryArchived(boolean filter) { } - private void filterByUncompleteChecklists() { - initNotesList(new Intent(Constants.ACTION_SEARCH_UNCOMPLETE_CHECKLISTS)); - } + private void filterByUncompleteChecklists() { + initNotesList(new Intent(Constants.ACTION_SEARCH_UNCOMPLETE_CHECKLISTS)); + } private void filterByTags() { @@ -1801,7 +1801,7 @@ private void filterByTags() { intent.removeExtra(SearchManager.QUERY); initNotesList(intent); - return false; + return false; }).build().show(); } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java b/omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java index 712ab4eb3d..bf186c7265 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java @@ -82,16 +82,18 @@ public class MainActivity extends BaseActivity implements OnDateSetListener, OnT @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setTheme(R.style.OmniNotesTheme_ApiSpec); + setTheme(R.style.OmniNotesTheme_ApiSpec); setContentView(R.layout.activity_main); ButterKnife.bind(this); - EventBus.getDefault().register(this); + EventBus.getDefault().register(this); initUI(); - if (IntroActivity.mustRun()) { - startActivity(new Intent(this.getApplicationContext(), IntroActivity.class)); - } + if (IntroActivity.mustRun()) { + startActivity(new Intent(this.getApplicationContext(), IntroActivity.class)); + }else{ + loadFromPreferenes(); + } new UpdaterTask(this).execute(); } @@ -539,5 +541,215 @@ public void onDateSet(DatePicker view, int year, int monthOfYear, if (f != null && f.isAdded() && f.onDateSetListener != null) { f.onDateSetListener.onDateSet(view, year, monthOfYear, dayOfMonth); } + } + + private void importNotes() { + final CharSequence[] backups = StorageHelper.getExternalStoragePublicDir().list(); + + if (backups != null && backups.length == 0) { + (MainActivity.this).showMessage(R.string.no_backups_available, ONStyle.WARN); + } else { + + MaterialDialog importDialog = new MaterialDialog.Builder(MainActivity.this) + .title(R.string.data_import_message) + .items(backups) + .positiveText(R.string.confirm) + .callback(new MaterialDialog.ButtonCallback() { + @Override + public void onPositive(MaterialDialog materialDialog) { + + } + }).build(); + + // OnShow is overridden to allow long-click on item so user can remove them + importDialog.setOnShowListener(dialog -> { + + ListView lv = importDialog.getListView(); + assert lv != null; + lv.setOnItemClickListener((parent, view, position, id) -> { + + // Retrieves backup size + File backupDir = StorageHelper.getBackupDir(backups[position].toString()); + long size = StorageHelper.getSize(backupDir) / 1024; + String sizeString = size > 1024 ? size / 1024 + "Mb" : size + "Kb"; + + // Check preference presence + String prefName = StorageHelper.getSharedPreferencesFile(MainActivity.this).getName(); + boolean hasPreferences = (new File(backupDir, prefName)).exists(); + + String message = backups[position] + + " (" + sizeString + + (hasPreferences ? " " + getString(R.string.settings_included) : "") + + ")"; + + new MaterialDialog.Builder(MainActivity.this) + .title(R.string.confirm_restoring_backup) + .content(message) + .positiveText(R.string.confirm) + .callback(new MaterialDialog.ButtonCallback() { + @Override + public void onPositive(MaterialDialog materialDialog) { + + ((OmniNotes)MainActivity.this.getApplication()).getAnalyticsHelper().trackEvent(AnalyticsHelper.CATEGORIES.SETTING, + "settings_import_data"); + + importDialog.dismiss(); + + // An IntentService will be launched to accomplish the import task + Intent service = new Intent(MainActivity.this, + DataBackupIntentService.class); + service.setAction(DataBackupIntentService.ACTION_DATA_IMPORT); + service.putExtra(DataBackupIntentService.INTENT_BACKUP_NAME, + backups[position]); + MainActivity.this.startService(service); + } + }).build().show(); + }); + + // Creation of backup removal dialog + lv.setOnItemLongClickListener((parent, view, position, id) -> { + + // Retrieves backup size + File backupDir = StorageHelper.getBackupDir(backups[position].toString()); + long size = StorageHelper.getSize(backupDir) / 1024; + String sizeString = size > 1024 ? size / 1024 + "Mb" : size + "Kb"; + + new MaterialDialog.Builder(MainActivity.this) + .title(R.string.confirm_removing_backup) + .content(backups[position] + "" + " (" + sizeString + ")") + .positiveText(R.string.confirm) + .callback(new MaterialDialog.ButtonCallback() { + @Override + public void onPositive(MaterialDialog materialDialog) { + importDialog.dismiss(); + // An IntentService will be launched to accomplish the deletion task + Intent service = new Intent(MainActivity.this, + DataBackupIntentService.class); + service.setAction(DataBackupIntentService.ACTION_DATA_DELETE); + service.putExtra(DataBackupIntentService.INTENT_BACKUP_NAME, + backups[position]); + MainActivity.this.startService(service); + } + }).build().show(); + + return true; + }); + }); + + importDialog.show(); + } + } + public void loadFromPreferenes(){ + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage("Do you want to Load your saved data!?"); + // add a button + builder.setPositiveButton("yes",new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Preference importData = null; + Context context = MainActivity.this; + SharedPreferences sharedPref = context.getSharedPreferences( + getString(R.string.preference_file_key), Context.MODE_PRIVATE); + if (sharedPref != null) { + + PermissionsHelper.requestPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, R + .string.permission_external_storage, MainActivity.this.findViewById(R.id.crouton_handle), () -> importNotes()); + + SharedPreferences sharedPreferences = getSharedPreferences("mypreferences", Context.MODE_PRIVATE); + + byte[] bytes = sharedPreferences.getString("selectedNotesList", "{}").getBytes(); + if (bytes.length == 0) { + Toast.makeText(MainActivity.this, "No Data stored to be loaded", Toast.LENGTH_LONG).show(); + return; + } + ByteArrayInputStream byteArray = new ByteArrayInputStream(bytes); + Base64InputStream base64InputStream = new Base64InputStream(byteArray, Base64.DEFAULT); + ObjectInputStream objectInputStream; + try { + objectInputStream = new ObjectInputStream(base64InputStream); + ListFragment.selectedNotes = (List) objectInputStream.readObject(); + Toast.makeText(MainActivity.this, "Load Successful" + ListFragment.selectedNotes.size(), Toast.LENGTH_LONG).show(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + } + } + }); + + builder.setNegativeButton("no", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + dialogInterface.cancel(); + } + }); + // create and show the alert dialog + AlertDialog dialog = builder.create(); + dialog.show(); + } + + public void saveToPreferences(){ +// AlertDialog.Builder builder = new AlertDialog.Builder(this); +// builder.setMessage("Do you want to upload data!?"); +// // add a button +// builder.setPositiveButton("yes",new DialogInterface.OnClickListener() { +// @Override +// public void onClick(DialogInterface dialog, int which) { +// Preference importData = null; +// Context context = MainActivity.this; +// SharedPreferences sharedPref = context.getSharedPreferences( +// getString(R.string.preference_file_key), Context.MODE_PRIVATE); +// if (sharedPref != null) { +// +// PermissionsHelper.requestPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, R +// .string.permission_external_storage, MainActivity.this.findViewById(R.id.crouton_handle), () -> importNotes()); +// +// +// } +// } +// }); +// +// builder.setNegativeButton("no", new DialogInterface.OnClickListener() { +// @Override +// public void onClick(DialogInterface dialogInterface, int i) { +// dialogInterface.cancel(); +// } +// }); +// // create and show the alert dialog +// AlertDialog dialog = builder.create(); +// dialog.show(); + + SharedPreferences sharedPreferences = getSharedPreferences("mypreferences", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + + ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); + + ObjectOutputStream objectOutput; + + try { + objectOutput = new ObjectOutputStream(arrayOutputStream); + objectOutput.writeObject(ListFragment.selectedNotes); + byte[] data = arrayOutputStream.toByteArray(); + objectOutput.close(); + arrayOutputStream.close(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Base64OutputStream b64 = new Base64OutputStream(out, Base64.DEFAULT); + b64.write(data); + b64.close(); + out.close(); + + editor.putString("selectedNotesList", new String(out.toByteArray())); + + editor.commit(); + Toast.makeText(MainActivity.this, "List Saved" + ListFragment.selectedNotes.size(), Toast.LENGTH_LONG).show(); + + } catch (IOException e) { + e.printStackTrace(); + } + + } }