3 import android.os.Bundle;
4 import android.view.View;
5 import android.widget.Toast;
7 import com.evernote.client.oauth.android.EvernoteSession;
8 import com.yuji.em.utility.BaseActivity;
9 import com.yuji.em.utility.EvernoteUtil;
10 import com.yuji.em.utility.OAuthUtil;
13 * This simple Android app demonstrates how to integrate with the Evernote Cloud
14 * API (aka EDAM) to create a note.
16 * In this sample, the user authorizes access to their account using OAuth and
17 * chooses an image from the device's image gallery. The image is then saved
18 * directly to Evernote using the Cloud API.
20 public class OAuthActivity extends BaseActivity {
22 /***************************************************************************
23 * You MUST change the following values to run this sample application. *
24 ***************************************************************************/
26 // Your Evernote API key. See http://dev.evernote.com/documentation/cloud/
27 // Please obfuscate your code to help keep these values secret.
28 // private static final String CONSUMER_KEY = "yuji-k64613";
29 // private static final String CONSUMER_SECRET = "98b2cbf35f747d2d";
31 /***************************************************************************
32 * Change these values as needed to use this code in your own application. *
33 ***************************************************************************/
35 // Name of this application, for logging
36 // private static final String TAG = "HelloEDAM";
38 // A directory on disk where your application stores temporary data
39 // private static final String APP_DATA_PATH =
40 // "/Android/data/com.evernote.android.sample/temp/";
41 // private static final String APP_DATA_PATH =
42 // "/Android/data/com.yuji.ef/temp/";
44 // Change to "www.evernote.com" to use the Evernote production service
45 // instead of the sandbox
46 // private static final String EVERNOTE_HOST = "sandbox.evernote.com";
47 // private static final String EVERNOTE_HOST = "www.evernote.com";
49 // private static final String APP_NAME = "FolderErver";
50 // private static final String APP_VERSION = "1.0.0";
52 /***************************************************************************
53 * The following values are simply part of the demo application. *
54 ***************************************************************************/
56 // Activity result request codes
57 // private static final int SELECT_IMAGE = 1;
59 // The ENML preamble to every Evernote note.
60 // Note content goes between <en-note> and </en-note>
61 // private static final String NOTE_PREFIX =
62 // "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
63 // "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" +
66 // The ENML postamble to every Evernote note
67 // private static final String NOTE_SUFFIX = "</en-note>";
69 // Used to interact with the Evernote web service
70 // private EvernoteSession session;
72 // UI elements that we update
73 // private Button btnAuth;
74 // private Button btnSave;
75 // private Button btnSelect;
76 // private TextView msgArea;
78 // The path to and MIME type of the currently selected image from the
80 // private String filePath;
81 // private String mimeType;
82 // private String fileName;
85 private OAuthUtil util = OAuthUtil.getInstance();
86 private boolean isInit = true;
88 /** Called when the activity is first created. */
90 public void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 // setContentView(R.layout.main);
93 setContentView(R.layout.oauth);
96 // msgArea = (TextView)findViewById(R.id.message);
97 // btnAuth = (Button) findViewById(R.id.auth_button);
98 // btnSelect = (Button) findViewById(R.id.select_button);
99 // btnSave = (Button) findViewById(R.id.save_button);
105 public void onResume() {
108 // Complete the Evernote authentication process if necessary
110 // if (!session.completeAuthentication(getPreferencesForAuthData())) {
111 // // We only want to do this when we're resuming after
113 // Toast.makeText(this, "Evernote login failed",
114 // Toast.LENGTH_LONG).show();
116 boolean isCompleteAuth = util.isCompleteAuth(this);
117 if (!isInit && !isCompleteAuth) {
118 // We only want to do this when we're resuming after
120 Toast.makeText(this, getString(R.string.ErrorLogin),
121 Toast.LENGTH_LONG).show();
130 if (isCompleteAuth) {
131 EvernoteUtil.getInstance().setSession(util.getSession());
132 util.setLoginTime(this);
139 * Evernote authentication data will be stored to this SharedPreferences if
140 * we are resuming as a result of a successful OAuth authorization. You may
141 * wish to pass a different SharedPreferences so that Evernote settings are
142 * stored along with other settings persisted by your application.
144 // private SharedPreferences getPreferencesForAuthData() {
145 // return getPreferences(MODE_PRIVATE);
149 * Setup the EvernoteSession used to access the Evernote API.
151 private void setupSession() {
153 // ApplicationInfo info =
154 // new ApplicationInfo(CONSUMER_KEY, CONSUMER_SECRET, EVERNOTE_HOST,
155 // APP_NAME, APP_VERSION);
157 // // Retrieve persisted authentication information
158 // session = new EvernoteSession(info, getPreferencesForAuthData(),
160 util.setupSession(this);
168 * Update the UI based on Evernote authentication state.
170 private void updateUi() {
172 // if (session.isLoggedIn()) {
173 // btnAuth.setText(R.string.label_log_out);
174 // btnSave.setEnabled(true);
175 // btnSelect.setEnabled(true);
177 // btnAuth.setText(R.string.label_log_in);
178 // btnSave.setEnabled(false);
179 // btnSelect.setEnabled(false);
184 * Called when the user taps the "Log in to Evernote" button. Initiates the
185 * Evernote OAuth process, or logs out if the user is already logged in.
187 public void startAuth(View view) {
189 EvernoteSession session = util.getSession();
192 // if (session.isLoggedIn()) {
193 // session.logOut(util.getPreferencesForAuthData(this));
195 // session.authenticate(this);
197 if (session.isLoggedIn()) {
198 session.logOut(util.getPreferencesForAuthData(this));
200 session.authenticate(this);
206 * Get a temporary directory that can be used by this application to store
207 * potentially large files sent to and retrieved from the Evernote API.
209 // private File getTempDir() {
210 // return new File(Environment.getExternalStorageDirectory(),