OSDN Git Service

FOO
[evermemo/source.git] / workspace / EverMemo / src / com / yuji / em / OAuthActivity.java
1 package com.yuji.em;
2
3 import android.os.Bundle;
4 import android.view.View;
5 import android.widget.Toast;
6
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;
11
12 /**
13  * This simple Android app demonstrates how to integrate with the Evernote Cloud
14  * API (aka EDAM) to create a note.
15  * 
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.
19  */
20 public class OAuthActivity extends BaseActivity {
21
22         /***************************************************************************
23          * You MUST change the following values to run this sample application. *
24          ***************************************************************************/
25
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";
30
31         /***************************************************************************
32          * Change these values as needed to use this code in your own application. *
33          ***************************************************************************/
34
35         // Name of this application, for logging
36         // private static final String TAG = "HelloEDAM";
37
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/";
43
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";
48
49         // private static final String APP_NAME = "FolderErver";
50         // private static final String APP_VERSION = "1.0.0";
51
52         /***************************************************************************
53          * The following values are simply part of the demo application. *
54          ***************************************************************************/
55
56         // Activity result request codes
57         // private static final int SELECT_IMAGE = 1;
58
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\">" +
64         // "<en-note>";
65
66         // The ENML postamble to every Evernote note
67         // private static final String NOTE_SUFFIX = "</en-note>";
68
69         // Used to interact with the Evernote web service
70         // private EvernoteSession session;
71
72         // UI elements that we update
73         // private Button btnAuth;
74         // private Button btnSave;
75         // private Button btnSelect;
76         // private TextView msgArea;
77
78         // The path to and MIME type of the currently selected image from the
79         // gallery
80         // private String filePath;
81         // private String mimeType;
82         // private String fileName;
83
84         // ADD
85         private OAuthUtil util = OAuthUtil.getInstance();
86         private boolean isInit = true;
87
88         /** Called when the activity is first created. */
89         @Override
90         public void onCreate(Bundle savedInstanceState) {
91                 super.onCreate(savedInstanceState);
92                 // setContentView(R.layout.main);
93                 setContentView(R.layout.oauth);
94
95                 // DELETE
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);
100
101                 setupSession();
102         }
103
104         @Override
105         public void onResume() {
106                 super.onResume();
107
108                 // Complete the Evernote authentication process if necessary
109                 // EDIT
110                 // if (!session.completeAuthentication(getPreferencesForAuthData())) {
111                 // // We only want to do this when we're resuming after
112                 // authentication...
113                 // Toast.makeText(this, "Evernote login failed",
114                 // Toast.LENGTH_LONG).show();
115                 // }
116                 boolean isCompleteAuth = util.isCompleteAuth(this);
117                 if (!isInit && !isCompleteAuth) {
118                         // We only want to do this when we're resuming after
119                         // authentication...
120                         Toast.makeText(this, getString(R.string.ErrorLogin),
121                                         Toast.LENGTH_LONG).show();
122                 }
123
124                 updateUi();
125
126                 // ADD
127                 if (isInit) {
128                         isInit = false;
129                 } else {
130                         if (isCompleteAuth) {
131                                 EvernoteUtil.getInstance().setSession(util.getSession());
132                                 util.setLoginTime(this);
133                         }
134                         finish();
135                 }
136         }
137
138         /**
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.
143          */
144         // private SharedPreferences getPreferencesForAuthData() {
145         // return getPreferences(MODE_PRIVATE);
146         // }
147
148         /**
149          * Setup the EvernoteSession used to access the Evernote API.
150          */
151         private void setupSession() {
152                 // EDIT
153                 // ApplicationInfo info =
154                 // new ApplicationInfo(CONSUMER_KEY, CONSUMER_SECRET, EVERNOTE_HOST,
155                 // APP_NAME, APP_VERSION);
156                 //
157                 // // Retrieve persisted authentication information
158                 // session = new EvernoteSession(info, getPreferencesForAuthData(),
159                 // getTempDir());
160                 util.setupSession(this);
161                 updateUi();
162
163                 // ADD
164                 startAuth(null);
165         }
166
167         /**
168          * Update the UI based on Evernote authentication state.
169          */
170         private void updateUi() {
171                 // DELETE
172                 // if (session.isLoggedIn()) {
173                 // btnAuth.setText(R.string.label_log_out);
174                 // btnSave.setEnabled(true);
175                 // btnSelect.setEnabled(true);
176                 // } else {
177                 // btnAuth.setText(R.string.label_log_in);
178                 // btnSave.setEnabled(false);
179                 // btnSelect.setEnabled(false);
180                 // }
181         }
182
183         /**
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.
186          */
187         public void startAuth(View view) {
188                 // ADD
189                 EvernoteSession session = util.getSession();
190
191                 // EDIT
192                 // if (session.isLoggedIn()) {
193                 // session.logOut(util.getPreferencesForAuthData(this));
194                 // } else {
195                 // session.authenticate(this);
196                 // }
197                 if (session.isLoggedIn()) {
198                         session.logOut(util.getPreferencesForAuthData(this));
199                 }
200                 session.authenticate(this);
201
202                 updateUi();
203         }
204
205         /**
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.
208          */
209         // private File getTempDir() {
210         // return new File(Environment.getExternalStorageDirectory(),
211         // APP_DATA_PATH);
212         // }
213 }