Building Beyond The Obvious: Integrating Salesforce & Evernote on Android

Screen Shot 2015-02-11 at 4.03.11 pmOver the years, I’ve been fortunate to have got an opportunity to build some very interesting mobile apps  – use cases that do not sound very obvious and app ideas which look too ridiculous at first. And that’s one of the reasons why I’ve always loved competing in hackathons and online code competitions as they allow a developer an unparalleled expression of freedom.

One of the most interesting apps I’ve built (along with my colleague Sameer Sharma) using the Salesforce1 Mobile SDK is Noteprise – probably the first ever mobile integration between Evernote and Salesforce. Although the app was built in 2012, the memories are still afresh so let’s just deep dive into it.

Like the Salesforce Mobile SDK for Android, Evernote too has a reasonably well written Cloud API SDK for Android. We started off with by creating a native Android template using the Force.com Mobile SDK.  This gives you built-in oAuth dance capabilities which will launch the Salesforce oAuth webview as shown below.

Screen Shot 2015-02-11 at 4.15.39 pm

Once you enter the credentials, Salesforce then prompts you for the the connected app permissions.

Screen Shot 2015-02-11 at 4.34.38 pmAt this point, Mobile SDK has established your session with Salesforce and hands control back to your main activity class. The next step is to launch the Evernote oAuth flow.

Screen Shot 2015-02-11 at 4.35.43 pm

Similar to Mobile SDK, the Evernote API prompts you to grant permission to the Connected app as shown below.

Screen Shot 2015-02-11 at 4.36.46 pm

We used the built-in EvernoteSession class for this purpose as follows


public void setupEvernoteSession()
{
       evernoteAppInfo = new ApplicationInfo(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET           , Constants.EVERNOTE_HOST
           , Constants.APP_NAME, Constants.APP_VERSION);
    if (noteprisePreferences.isSignedInToEvernote())
       {
                 AuthenticationResult authResult = new AuthenticationResult(noteprisePreferences.getEvetnoteAuthToken(), noteprisePreferences.getEvetnoteNoteStoreUrl(),
                 noteprisePreferences.getEvernoteWebApiPrefix(), noteprisePreferences.getEverNoteUserId());
                 evernoteSession = new EvernoteSession(evernoteAppInfo, authResult, Utilities.getTempStorageDirectory());
       }
else
       {
              evernoteSession = new EvernoteSession(evernoteAppInfo, Utilities.getTempStorageDirectory());
       }
}

public Boolean isEvernoteAuthenticationComplete()
{
if (evernoteSession != null)
  {
           if (evernoteSession.completeAuthentication() || evernoteSession.isLoggedIn())
    {
           return true;
    }
  }
return false;
}

Once we’ve established sessions with both Evernote and Salesforce, it’s all about firing the relevant CRUD operations on each side and associating the note information with appropriate Salesforce objects.

Evernote exposes the Notebook object which allows you to retrieve all notebooks for any user as follows.

try 
			{
				
				authToken = evernoteSession.getAuthToken();
	        	client = evernoteSession.createNoteStore();
	        	notebookList = client.listNotebooks(authToken);
	        	spinnerItems = new ArrayList<CommonListItems>();
	        	for (int i = 0; i < notebookList.size(); i++)
	        	{
	        		Notebook notebook = notebookList.get(i);
	        		CommonListItems item = new CommonListItems();
	        		item.setLabel(notebook.getName());
	        		item.setId(notebook.getGuid());
	        		spinnerItems.add(item);
	        	}        	
			} 
			catch (TTransportException e) 
			{
				e.printStackTrace();
			} 
			catch (EDAMUserException e) 
			{
				e.printStackTrace();
			} 
			catch (EDAMSystemException e) 
			{
				e.printStackTrace();
			} 
			catch (TException e) 
			{
				e.printStackTrace();
			}

Similarly, to create a new note you can use the createNoteStore() and Note() methods as follows

try 
			{
				authToken = evernoteSession.getAuthToken();
	        	client = evernoteSession.createNoteStore();
	        	createdNote = new Note();	        	
	        	createdNote.setNotebookGuid(selectedNotebookGuid);
	        	createdNote.setTitle(noteTitleEditText.getText().toString().trim());
				createdNote.setContent(Constants.NOTE_PREFIX + Html.toHtml(noteContenteditText.getText()) + Constants.NOTE_SUFFIX);
	        	createdNote.setContent(createdNote.getContent().replace("<br>", "
"));
	        	createdNote.setContent(createdNote.getContent().replace(" ", ""));	        	
	        	savedNote = client.createNote(authToken, createdNote);
			} 
			catch (TTransportException e) 
			{
				e.printStackTrace();
			} 
			catch (TException e) 
			{
				e.printStackTrace();
			} 
			catch (EDAMUserException e) 
			{
				e.printStackTrace();
			} 
			catch (EDAMSystemException e) 
			{
				e.printStackTrace();
			} 
			catch (EDAMNotFoundException e) 
			{
				e.printStackTrace();
			}

Of course, there’s a ton of other nuances related both Mobile SDK and Evernote that went into building Noteprise and I hope to do some follow up blog posts to focus on those. A demo video of the app is available on YouTube.

The source code for this app is available on Github.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s