Android Open Source - evernote-sdk-android Simple Note






From Project

Back to project page evernote-sdk-android.

License

The source code is released under:

Apache License

If you think the Android project evernote-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Evernote Corporation//from   www.  j  a  v  a 2 s  .  com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.evernote.android.sample;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.evernote.client.android.EvernoteUtil;
import com.evernote.client.android.OnClientCallback;
import com.evernote.edam.type.Note;
import com.evernote.edam.type.Notebook;
import com.evernote.thrift.transport.TTransportException;

import java.util.List;
/**
 * This sample shows how to list Evernote notebooks and create a note in the specific notebook.
 * <p/>
 * class created by @tylersmithnet
 */
public class SimpleNote extends ParentActivity {

  /**
   * *************************************************************************
   * The following values and code are simply part of the demo application.  *
   * *************************************************************************
   */

  private static final String LOGTAG = "SimpleNote";

  private EditText mEditTextTitle;
  private EditText mEditTextContent;
  private Button mBtnSave;
  private Button mBtnSelect;

  private String mSelectedNotebookGuid;

  // Callback used as a result of creating a note in a normal notebook or a linked notebook
  private OnClientCallback<Note> mNoteCreateCallback = new OnClientCallback<Note>() {
    @Override
    public void onSuccess(Note note) {
      Toast.makeText(getApplicationContext(), R.string.note_saved, Toast.LENGTH_LONG).show();
      removeDialog(DIALOG_PROGRESS);
    }

    @Override
    public void onException(Exception exception) {
      Log.e(LOGTAG, "Error saving note", exception);
      Toast.makeText(getApplicationContext(), R.string.error_saving_note, Toast.LENGTH_LONG).show();
      removeDialog(DIALOG_PROGRESS);
    }
  };

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_note);

    mEditTextTitle = (EditText) findViewById(R.id.text_title);
    mEditTextContent = (EditText) findViewById(R.id.text_content);
    mBtnSelect = (Button) findViewById(R.id.select_button);
    mBtnSave = (Button) findViewById(R.id.save_button);
  }

  /**
   * Saves text field content as note to selected notebook, or default notebook if no notebook select
   */
  public void saveNote(View view) {
    String title = mEditTextTitle.getText().toString();
    String content = mEditTextContent.getText().toString();
    if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
      Toast.makeText(getApplicationContext(), R.string.empty_content_error, Toast.LENGTH_LONG).show();
      return;
    }

    Note note = new Note();
    note.setTitle(title);

    //TODO: line breaks need to be converted to render in ENML
    note.setContent(EvernoteUtil.NOTE_PREFIX + content + EvernoteUtil.NOTE_SUFFIX);

    if(!mEvernoteSession.getAuthenticationResult().isAppLinkedNotebook()) {
      //If User has selected a notebook guid, assign it now
      if (!TextUtils.isEmpty(mSelectedNotebookGuid)) {
        note.setNotebookGuid(mSelectedNotebookGuid);
      }
      showDialog(DIALOG_PROGRESS);
      try {
        mEvernoteSession.getClientFactory().createNoteStoreClient().createNote(note, mNoteCreateCallback);
      } catch (TTransportException exception) {
        Log.e(LOGTAG, "Error creating notestore", exception);
        Toast.makeText(getApplicationContext(), R.string.error_creating_notestore, Toast.LENGTH_LONG).show();
        removeDialog(DIALOG_PROGRESS);
      }
    } else {
      super.createNoteInAppLinkedNotebook(note, mNoteCreateCallback);
    }
  }


  /**
   * Select notebook, create AlertDialog to pick notebook guid
   */
  public void selectNotebook(View view) {
    if(mEvernoteSession.isAppLinkedNotebook()) {
      Toast.makeText(getApplicationContext(), getString(R.string.CANT_LIST_APP_LNB), Toast.LENGTH_LONG).show();
      return;
    }

    try {
      mEvernoteSession.getClientFactory().createNoteStoreClient().listNotebooks(new OnClientCallback<List<Notebook>>() {
        int mSelectedPos = -1;

        @Override
        public void onSuccess(final List<Notebook> notebooks) {
          CharSequence[] names = new CharSequence[notebooks.size()];
          int selected = -1;
          Notebook notebook = null;
          for (int index = 0; index < notebooks.size(); index++) {
            notebook = notebooks.get(index);
            names[index] = notebook.getName();
            if (notebook.getGuid().equals(mSelectedNotebookGuid)) {
              selected = index;
            }
          }

          AlertDialog.Builder builder = new AlertDialog.Builder(SimpleNote.this);

          builder
              .setSingleChoiceItems(names, selected, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  mSelectedPos = which;
                }
              })
              .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  if (mSelectedPos > -1) {
                    mSelectedNotebookGuid = notebooks.get(mSelectedPos).getGuid();
                  }
                  dialog.dismiss();
                }
              })
              .create()
              .show();
        }

        @Override
        public void onException(Exception exception) {
          Log.e(LOGTAG, "Error listing notebooks", exception);
          Toast.makeText(getApplicationContext(), R.string.error_listing_notebooks, Toast.LENGTH_LONG).show();
          removeDialog(DIALOG_PROGRESS);
        }
      });
    } catch (TTransportException exception) {
      Log.e(LOGTAG, "Error creating notestore", exception);
      Toast.makeText(getApplicationContext(), R.string.error_creating_notestore, Toast.LENGTH_LONG).show();
      removeDialog(DIALOG_PROGRESS);
    }
  }
}




Java Source Code List

com.evernote.android.sample.HelloEDAM.java
com.evernote.android.sample.ImagePicker.java
com.evernote.android.sample.ParentActivity.java
com.evernote.android.sample.SearchNotes.java
com.evernote.android.sample.SimpleNote.java
com.evernote.client.android.AsyncBusinessNoteStoreClient.java
com.evernote.client.android.AsyncLinkedNoteStoreClient.java
com.evernote.client.android.AsyncNoteStoreClient.java
com.evernote.client.android.AsyncReflector.java
com.evernote.client.android.AsyncUserStoreClient.java
com.evernote.client.android.AuthenticationResult.java
com.evernote.client.android.BootstrapManager.java
com.evernote.client.android.ClientFactory.java
com.evernote.client.android.EvernoteOAuthActivity.java
com.evernote.client.android.EvernoteSession.java
com.evernote.client.android.EvernoteUtil.java
com.evernote.client.android.InvalidAuthenticationException.java
com.evernote.client.android.OnClientCallback.java
com.evernote.client.android.SessionPreferences.java
com.evernote.client.conn.mobile.DiskBackedByteStore.java
com.evernote.client.conn.mobile.FileDataException.java
com.evernote.client.conn.mobile.FileData.java
com.evernote.client.conn.mobile.TEvernoteHttpClient.java
com.evernote.client.oauth.EvernoteAuthToken.java
com.evernote.client.oauth.YinxiangApi.java