@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Intent i = new Intent(this, NoteEdit.class); AppNote note = listAdapter.getItem((int) id); i.putExtra(AppNote._ID, note.getId()); startActivityForResult(i, EDITOR_ACTIVITY); }
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // Context menu for deleting a note AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; AppNote note = listAdapter.getItem(info.position); menu.setHeaderTitle(note.toString()); menu.add(0, DELETE_MENU_ITEM, 0, R.string.menu_delete) .setIcon(android.R.drawable.ic_menu_delete); }
private boolean updateNotes() { ArrayList<AppNote> changed = mDbHelper.fetchNotes(NotesDbAdapter.CHANGED_NOTES_SELECTION); boolean allUpdated = true; for (AppNote appNote : changed) { try { link.update(appNote.getNote()); mDbHelper.updateNote(appNote.getId(), appNote.getNote(), true); } catch (LinkItemNotFound e) { e.printStackTrace(); try { link.add(appNote.getNote()); mDbHelper.updateNote(appNote.getId(), appNote.getNote(), true); } catch (LibOperaLinkException e1) { e1.printStackTrace(); allUpdated = false; } } catch (LinkAccessDeniedException e) { e.printStackTrace(); isConnected = false; allUpdated = false; } catch (LibOperaLinkException e) { e.printStackTrace(); allUpdated = false; } } return allUpdated; }
/** * Change notes at the server * * @return true if succeeded */ private boolean uploadNewNotes() { ArrayList<AppNote> newNotes = mDbHelper.fetchNotes(NotesDbAdapter.NEW_NOTES_SELECTION); boolean allUpdated = true; try { for (AppNote appNote : newNotes) { link.add(appNote.getNote()); mDbHelper.updateNote(appNote.getId(), appNote.getNote(), true); } } catch (LinkAccessDeniedException e) { e.printStackTrace(); allUpdated = false; isConnected = false; } catch (LibOperaLinkException e) { e.printStackTrace(); allUpdated = false; } return allUpdated; }
@Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case DELETE_MENU_ITEM: // Mark the selected note to be deleted and send changes to the // server AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); AppNote note = listAdapter.getItem((int) info.id); mDbHelper.markToDelete(note.getId()); if (note.getOpera_id() != null) { notesDeleted = true; if (automaticSync) { sendChanges(); } } fillData(); return true; } return super.onContextItemSelected(item); }
/** * Delete notes which were marked as those to be deleted, delete them also from the Opera Link * server * * @return true if deleting notes at the server succeeded */ private boolean deleteNotes() { ArrayList<AppNote> toDelete = mDbHelper.fetchNotes(NotesDbAdapter.TO_DELETE_SELECTION); boolean allDeleted = true; for (AppNote appNote : toDelete) { try { if (appNote.getOpera_id() != null) { link.delete(appNote.getNote()); } mDbHelper.deleteNote(appNote.getId()); } catch (LinkItemNotFound e) { e.printStackTrace(); mDbHelper.deleteNote(appNote.getId()); } catch (LinkAccessDeniedException e) { e.printStackTrace(); isConnected = false; allDeleted = false; } catch (LibOperaLinkException e) { e.printStackTrace(); allDeleted = false; } } return allDeleted; }
/** * Get new notes and updates from Opera Link server and insert them into database * * @return true if operation succeeded */ private boolean getServerNotes() { ArrayList<NoteFolderEntry> notes = null; try { notes = link.getRootNotes(true); } catch (LinkAccessDeniedException e) { e.printStackTrace(); isConnected = false; return false; } catch (LibOperaLinkException e) { e.printStackTrace(); return false; } int newNotes = 0; ArrayList<Note> allNotes = new ArrayList<Note>(); flattenNoteFolders(notes, allNotes); for (Note note : allNotes) { // try to get note from the database AppNote appNote = mDbHelper.fetchOperaNote(note.getId()); if (appNote == null) { // no such note in database, add a new one ++newNotes; mDbHelper.importAndAddNote(note); } else if (!appNote.getNote().content.equals(note.content)) { // update already added note if (appNote.isSynced()) { mDbHelper.updateNote(appNote.getId(), note, true); } else { // note content conflicted, add new note leaving the // old one mDbHelper.createNote(note); // set that the old one is synchronised mDbHelper.updateNote(appNote.getId(), appNote.getNote(), true); } } } showToast(String.valueOf(newNotes) + " " + getString(R.string.got_notes)); return true; }