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; }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notes_list); // Initialise database connection and read notes mDbHelper = new NotesDbAdapter(this); mDbHelper.open(); fillData(); registerForContextMenu(getListView()); // Read preferences pref = getPreferences(MODE_PRIVATE); requestToken = pref.getString(REQUEST_TOKEN_PREF_KEY, null); accessToken = pref.getString(ACCESS_TOKEN_PREF_KEY, null); tokenSecret = pref.getString(TOKEN_SECRET_PREF_KEY, null); automaticSync = pref.getBoolean(getString(R.string.sync_option), true); // Create object for connection with Opera Link if (accessToken != null && tokenSecret != null) { isConnected = true; link = LinkClient.createFromAccessToken(consumerKey, consumerSecret, accessToken, tokenSecret); if (automaticSync) { syncItems(); } } else if (requestToken != null) { link = LinkClient.createFromRequestToken(consumerKey, consumerSecret, requestToken, tokenSecret); } else { link = new LinkClient(consumerKey, consumerSecret); } }
/** * Obtains OAuth access tokens which can be used during entire communication process with Opera * Link server and invokes first synchronisation */ public void grantAccess() { Uri uri = this.getIntent().getData(); if (uri == null) { return; } String verifier = uri.getQueryParameter(LinkClient.OAUTH_VERIFIER); if (verifier == null) { return; } try { link.grantAccess(verifier); Log.i(SYNC_TAG, "Access granted"); accessToken = link.getAccessToken(); tokenSecret = link.getTokenSecret(); Editor prefEditor = pref.edit(); prefEditor.putString(ACCESS_TOKEN_PREF_KEY, accessToken); prefEditor.putString(TOKEN_SECRET_PREF_KEY, tokenSecret); prefEditor.commit(); isConnected = true; syncItems(); } catch (LinkAccessDeniedException e) { showToast("Access forbidden for access token " + accessToken); e.printStackTrace(); } catch (LibOperaLinkException e) { e.printStackTrace(); } }
/** * Starts activity with url where user can grant permission to the user's Opera Link data * * @return true if redirection succeeded */ private boolean redirectToBrowser() { try { String authorizeUrl = link.getAuthorizationURL(CALLBACK_URL); Editor prefEditor = pref.edit(); prefEditor.putString(REQUEST_TOKEN_PREF_KEY, link.getRequestToken()); prefEditor.putString(TOKEN_SECRET_PREF_KEY, link.getTokenSecret()); prefEditor.commit(); Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(authorizeUrl)); startActivityForResult(i, REDIRECTION_ACTIVITY); } catch (LibOperaLinkException e) { e.printStackTrace(); showToast("Could not connect to the server"); return false; } return true; }
/** * 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; }
/** * 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; }
/** * 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; }