OSDN Git Service

FOO
authorYuji Konishi <yuji.k64613@gmail.com>
Thu, 27 Sep 2012 14:33:59 +0000 (23:33 +0900)
committerYuji Konishi <yuji.k64613@gmail.com>
Thu, 27 Sep 2012 14:33:59 +0000 (23:33 +0900)
27 files changed:
workspace/EverMemo/.classpath
workspace/EverMemo/AndroidManifest.xml
workspace/EverMemo/default.properties [deleted file]
workspace/EverMemo/gen/com/yuji/em/BuildConfig.java [new file with mode: 0644]
workspace/EverMemo/gen/com/yuji/em/R.java
workspace/EverMemo/lib/evernote-api-1.20.jar [deleted file]
workspace/EverMemo/lib/log4j-1.2.14.jar [deleted file]
workspace/EverMemo/libs/evernote-api-1.22.jar [new file with mode: 0644]
workspace/EverMemo/libs/evernote-client-android.jar [new file with mode: 0644]
workspace/EverMemo/libs/libthrift.jar [moved from workspace/EverMemo/lib/libthrift.jar with 84% similarity]
workspace/EverMemo/libs/scribe-1.3.1.jar [new file with mode: 0644]
workspace/EverMemo/libs/slf4j-android-1.5.8.jar [new file with mode: 0644]
workspace/EverMemo/libs/slf4j.license [new file with mode: 0644]
workspace/EverMemo/project.properties [new file with mode: 0644]
workspace/EverMemo/res/layout/oauth.xml [new file with mode: 0644]
workspace/EverMemo/res/values-ja/strings.xml
workspace/EverMemo/res/values/strings.xml
workspace/EverMemo/src/com/yuji/em/Initialize.java
workspace/EverMemo/src/com/yuji/em/OAuthActivity.java [new file with mode: 0644]
workspace/EverMemo/src/com/yuji/em/SettingActivity.java
workspace/EverMemo/src/com/yuji/em/UserPasswordActivity.java
workspace/EverMemo/src/com/yuji/em/common/Constant.java
workspace/EverMemo/src/com/yuji/em/utility/EditPrefUtil.java
workspace/EverMemo/src/com/yuji/em/utility/EvernoteUtil.java
workspace/EverMemo/src/com/yuji/em/utility/OAuthUtil.java [new file with mode: 0644]
workspace/EverMemo/src/com/yuji/em/utility/PrefUtil.java
workspace/EverMemo/src/com/yuji/em/utility/PrefUtilImpl.java

index 375e374..3e94e64 100644 (file)
@@ -1,10 +1,13 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<classpath>\r
-       <classpathentry kind="src" path="src"/>\r
-       <classpathentry kind="src" path="gen"/>\r
-       <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>\r
-       <classpathentry kind="lib" path="lib/evernote-api-1.20.jar"/>\r
-       <classpathentry kind="lib" path="lib/libthrift.jar"/>\r
-       <classpathentry kind="lib" path="lib/log4j-1.2.14.jar"/>\r
-       <classpathentry kind="output" path="bin"/>\r
-</classpath>\r
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="src" path="src"/>
+       <classpathentry kind="src" path="gen"/>
+       <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+       <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
+       <classpathentry kind="lib" path="libs/evernote-api-1.22.jar"/>
+       <classpathentry kind="lib" path="libs/evernote-client-android.jar"/>
+       <classpathentry kind="lib" path="libs/libthrift.jar"/>
+       <classpathentry kind="lib" path="libs/scribe-1.3.1.jar"/>
+       <classpathentry kind="lib" path="libs/slf4j-android-1.5.8.jar"/>
+       <classpathentry kind="output" path="bin/classes"/>
+</classpath>
index 0080acf..ae7e32c 100644 (file)
@@ -1,29 +1,53 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-      package="com.yuji.em"
-      android:versionCode="2"
-      android:versionName="1.0.1">
+    package="com.yuji.em"
+    android:versionCode="2"
+    android:versionName="1.0.1" >
+
     <uses-sdk android:minSdkVersion="7" />
 
-    <application android:icon="@drawable/icon" android:label="@string/app_name"
-       android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 
-    >
-        <activity android:name=".EverMemoActivity"
-                       android:label="@string/app_name"
-                               android:windowSoftInputMode="stateVisible|adjustResize"
-                               android:launchMode="singleTop"
-            >
+    <application
+        android:icon="@drawable/icon"
+        android:label="@string/app_name"
+        android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
+        <activity
+            android:name=".EverMemoActivity"
+            android:label="@string/app_name"
+            android:launchMode="singleTop"
+            android:windowSoftInputMode="stateVisible|adjustResize" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
+
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
-       <activity android:name=".SelectNoteListActivity"></activity>
-       <activity android:name=".NoteListActivity"></activity>
-       <activity android:name=".SettingActivity"></activity>
-       <activity android:name=".HeaderActivity"></activity>
-       <activity android:name=".UserPasswordActivity"></activity>
-       
-       </application>
+        <activity android:name=".SelectNoteListActivity" >
+        </activity>
+        <activity android:name=".NoteListActivity" >
+        </activity>
+        <activity android:name=".SettingActivity" >
+        </activity>
+        <activity android:name=".HeaderActivity" >
+        </activity>
+        <activity android:name=".OAuthActivity" >
+        </activity>
+        <activity
+            android:name="com.evernote.client.oauth.android.EvernoteOAuthActivity"
+            android:configChanges="orientation|keyboard"
+            android:launchMode="singleTask" >
+            <intent-filter>
+
+                <!-- Change this to be en- followed by your consumer key -->
+                <data android:scheme="en-yuji-k64613" />
+
+                <action android:name="android.intent.action.VIEW" />
+
+                <category android:name="android.intent.category.BROWSABLE" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+    </application>
+
     <uses-permission android:name="android.permission.INTERNET" />
+
 </manifest>
\ No newline at end of file
diff --git a/workspace/EverMemo/default.properties b/workspace/EverMemo/default.properties
deleted file mode 100644 (file)
index 46769a7..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-# This file is automatically generated by Android Tools.
-# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
-#
-# This file must be checked in Version Control Systems.
-#
-# To customize properties used by the Ant build system use,
-# "build.properties", and override values to adapt the script to your
-# project structure.
-
-# Project target.
-target=android-7
diff --git a/workspace/EverMemo/gen/com/yuji/em/BuildConfig.java b/workspace/EverMemo/gen/com/yuji/em/BuildConfig.java
new file mode 100644 (file)
index 0000000..33132b5
--- /dev/null
@@ -0,0 +1,6 @@
+/** Automatically generated file. DO NOT MODIFY */
+package com.yuji.em;
+
+public final class BuildConfig {
+    public final static boolean DEBUG = true;
+}
\ No newline at end of file
index f460a93..49d9580 100644 (file)
@@ -63,14 +63,16 @@ public final class R {
         public static final int header=0x7f030000;
         public static final int main=0x7f030001;
         public static final int note_list=0x7f030002;
-        public static final int select_note_list=0x7f030003;
-        public static final int setting=0x7f030004;
-        public static final int simple_list_item_1=0x7f030005;
-        public static final int simple_list_item_2=0x7f030006;
-        public static final int small_list_item_1=0x7f030007;
-        public static final int user_password=0x7f030008;
+        public static final int oauth=0x7f030003;
+        public static final int select_note_list=0x7f030004;
+        public static final int setting=0x7f030005;
+        public static final int simple_list_item_1=0x7f030006;
+        public static final int simple_list_item_2=0x7f030007;
+        public static final int small_list_item_1=0x7f030008;
+        public static final int user_password=0x7f030009;
     }
     public static final class string {
+        public static final int ErrorLogin=0x7f050027;
         public static final int app_name=0x7f050000;
         public static final int cancelButton=0x7f050006;
         public static final int closeButton=0x7f050007;
@@ -83,14 +85,14 @@ public final class R {
         public static final int headerExplainText3=0x7f05001c;
         public static final int headerExplainText4=0x7f05001d;
         public static final int mainMenuSetting=0x7f050008;
-        public static final int mainTitle=0x7f050027;
+        public static final int mainTitle=0x7f050028;
         public static final int noButton=0x7f050003;
         public static final int noteListGetting=0x7f050018;
         public static final int selectListConfirmDeleteMsg=0x7f05000b;
         public static final int selectListConfirmNoteMsg=0x7f05000c;
         public static final int selectListConfirmTitle=0x7f05000a;
         public static final int selectListTitle=0x7f050009;
-        public static final int selectTitle=0x7f050028;
+        public static final int selectTitle=0x7f050029;
         public static final int sendButton=0x7f050001;
         public static final int settingConfirmNoteMsg=0x7f050011;
         public static final int settingConfirmTitle=0x7f05000f;
diff --git a/workspace/EverMemo/lib/evernote-api-1.20.jar b/workspace/EverMemo/lib/evernote-api-1.20.jar
deleted file mode 100644 (file)
index c5b6da7..0000000
Binary files a/workspace/EverMemo/lib/evernote-api-1.20.jar and /dev/null differ
diff --git a/workspace/EverMemo/lib/log4j-1.2.14.jar b/workspace/EverMemo/lib/log4j-1.2.14.jar
deleted file mode 100644 (file)
index 6251307..0000000
Binary files a/workspace/EverMemo/lib/log4j-1.2.14.jar and /dev/null differ
diff --git a/workspace/EverMemo/libs/evernote-api-1.22.jar b/workspace/EverMemo/libs/evernote-api-1.22.jar
new file mode 100644 (file)
index 0000000..57f1fe7
Binary files /dev/null and b/workspace/EverMemo/libs/evernote-api-1.22.jar differ
diff --git a/workspace/EverMemo/libs/evernote-client-android.jar b/workspace/EverMemo/libs/evernote-client-android.jar
new file mode 100644 (file)
index 0000000..01fbdf3
Binary files /dev/null and b/workspace/EverMemo/libs/evernote-client-android.jar differ
similarity index 84%
rename from workspace/EverMemo/lib/libthrift.jar
rename to workspace/EverMemo/libs/libthrift.jar
index 47c3cd3..fff381f 100644 (file)
Binary files a/workspace/EverMemo/lib/libthrift.jar and b/workspace/EverMemo/libs/libthrift.jar differ
diff --git a/workspace/EverMemo/libs/scribe-1.3.1.jar b/workspace/EverMemo/libs/scribe-1.3.1.jar
new file mode 100644 (file)
index 0000000..4abc551
Binary files /dev/null and b/workspace/EverMemo/libs/scribe-1.3.1.jar differ
diff --git a/workspace/EverMemo/libs/slf4j-android-1.5.8.jar b/workspace/EverMemo/libs/slf4j-android-1.5.8.jar
new file mode 100644 (file)
index 0000000..e0128bc
Binary files /dev/null and b/workspace/EverMemo/libs/slf4j-android-1.5.8.jar differ
diff --git a/workspace/EverMemo/libs/slf4j.license b/workspace/EverMemo/libs/slf4j.license
new file mode 100644 (file)
index 0000000..8ef6d25
--- /dev/null
@@ -0,0 +1,21 @@
+ Copyright (c) 2004-2008 QOS.ch
+ All rights reserved.
+
+ Permission is hereby granted, free  of charge, to any person obtaining
+ a  copy  of this  software  and  associated  documentation files  (the
+ "Software"), to  deal in  the Software without  restriction, including
+ without limitation  the rights to  use, copy, modify,  merge, publish,
+ distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ permit persons to whom the Software  is furnished to do so, subject to
+ the following conditions:
+ The  above  copyright  notice  and  this permission  notice  shall  be
+ included in all copies or substantial portions of the Software.
+ THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/workspace/EverMemo/project.properties b/workspace/EverMemo/project.properties
new file mode 100644 (file)
index 0000000..22d0dca
--- /dev/null
@@ -0,0 +1,14 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=android-7
diff --git a/workspace/EverMemo/res/layout/oauth.xml b/workspace/EverMemo/res/layout/oauth.xml
new file mode 100644 (file)
index 0000000..9e3d912
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:orientation="vertical" >
+
+    <LinearLayout
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content" >
+    </LinearLayout>
+
+</LinearLayout>
\ No newline at end of file
index 3942422..c670053 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-    <string name="app_name">EverMemo</string>
+    <string name="app_name">MemoEver</string>
 
     <string name="sendButton">送信</string>
     <string name="yesButton">はい</string>
@@ -48,4 +48,6 @@
 
     <string name="dialogErrorTitle">エラー</string>
     <string name="dialogSystemError">システムエラーが発生しました</string>
+    
+    <string name="ErrorLogin">ログインに失敗しました</string>
 </resources>
index 51697e2..f4b53c0 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-    <string name="app_name">EverMemo</string>
+    <string name="app_name">MemoEver</string>
 
     <string name="sendButton">Send</string>
     <string name="yesButton">Yes</string>
@@ -48,4 +48,6 @@
 
     <string name="dialogErrorTitle">Error</string>
     <string name="dialogSystemError">System error has occurred.</string>
+    
+    <string name="ErrorLogin">Failed to login</string>
 </resources>
index 43680b0..5be16e9 100644 (file)
@@ -2,9 +2,11 @@ package com.yuji.em;
 
 import android.content.Context;
 
-import com.yuji.em.common.CommonUtil;
+import com.evernote.client.oauth.android.EvernoteSession;
+import com.yuji.em.common.Constant;
+import com.yuji.em.utility.EditPrefUtil;
 import com.yuji.em.utility.EvernoteUtil;
-import com.yuji.em.utility.PasswordUtil;
+import com.yuji.em.utility.OAuthUtil;
 
 public class Initialize {
        private static boolean isInit = true; // falseになっていた。なぞ。
@@ -13,14 +15,29 @@ public class Initialize {
                if (!isInit) {
                        return;
                }
-               isInit = false;
+               //EvernoteUtil util = EvernoteUtil.getInstance();
+               //String username = PasswordUtil.getUsername(context);
+               //String password = PasswordUtil.getPassword(context);
+               //if (!CommonUtil.isNull(username) && !CommonUtil.isNull(password)) {
+               //      util.setConfig(username, password);
+               //}
+               
+               EditPrefUtil pref = new EditPrefUtil(context);
+               pref.getLong(Constant.PREF_LOGIN_DATE, 0L);
+               pref.update();
 
-               EvernoteUtil util = EvernoteUtil.getInstance();
-               String username = PasswordUtil.getUsername(context);
-               String password = PasswordUtil.getPassword(context);
-               if (!CommonUtil.isNull(username) && !CommonUtil.isNull(password)) {
-                       util.setConfig(username, password);
+               try {
+                       OAuthUtil authUtil = OAuthUtil.getInstance();
+                       EvernoteSession session = authUtil.setupSession(context);
+                       EvernoteUtil.getInstance().setSession(session);
+               }
+               catch (Throwable t){
+                       // TODO
+                       t.printStackTrace();
+                       return;
                }
+
+               isInit = false;
        }
 
        public static void setInitialize() {
diff --git a/workspace/EverMemo/src/com/yuji/em/OAuthActivity.java b/workspace/EverMemo/src/com/yuji/em/OAuthActivity.java
new file mode 100644 (file)
index 0000000..1113baa
--- /dev/null
@@ -0,0 +1,213 @@
+package com.yuji.em;
+
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Toast;
+
+import com.evernote.client.oauth.android.EvernoteSession;
+import com.yuji.em.utility.BaseActivity;
+import com.yuji.em.utility.EvernoteUtil;
+import com.yuji.em.utility.OAuthUtil;
+
+/**
+ * This simple Android app demonstrates how to integrate with the Evernote Cloud
+ * API (aka EDAM) to create a note.
+ * 
+ * In this sample, the user authorizes access to their account using OAuth and
+ * chooses an image from the device's image gallery. The image is then saved
+ * directly to Evernote using the Cloud API.
+ */
+public class OAuthActivity extends BaseActivity {
+
+       /***************************************************************************
+        * You MUST change the following values to run this sample application. *
+        ***************************************************************************/
+
+       // Your Evernote API key. See http://dev.evernote.com/documentation/cloud/
+       // Please obfuscate your code to help keep these values secret.
+       // private static final String CONSUMER_KEY = "yuji-k64613";
+       // private static final String CONSUMER_SECRET = "98b2cbf35f747d2d";
+
+       /***************************************************************************
+        * Change these values as needed to use this code in your own application. *
+        ***************************************************************************/
+
+       // Name of this application, for logging
+       // private static final String TAG = "HelloEDAM";
+
+       // A directory on disk where your application stores temporary data
+       // private static final String APP_DATA_PATH =
+       // "/Android/data/com.evernote.android.sample/temp/";
+       // private static final String APP_DATA_PATH =
+       // "/Android/data/com.yuji.ef/temp/";
+
+       // Change to "www.evernote.com" to use the Evernote production service
+       // instead of the sandbox
+       // private static final String EVERNOTE_HOST = "sandbox.evernote.com";
+       // private static final String EVERNOTE_HOST = "www.evernote.com";
+
+       // private static final String APP_NAME = "FolderErver";
+       // private static final String APP_VERSION = "1.0.0";
+
+       /***************************************************************************
+        * The following values are simply part of the demo application. *
+        ***************************************************************************/
+
+       // Activity result request codes
+       // private static final int SELECT_IMAGE = 1;
+
+       // The ENML preamble to every Evernote note.
+       // Note content goes between <en-note> and </en-note>
+       // private static final String NOTE_PREFIX =
+       // "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+       // "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" +
+       // "<en-note>";
+
+       // The ENML postamble to every Evernote note
+       // private static final String NOTE_SUFFIX = "</en-note>";
+
+       // Used to interact with the Evernote web service
+       // private EvernoteSession session;
+
+       // UI elements that we update
+       // private Button btnAuth;
+       // private Button btnSave;
+       // private Button btnSelect;
+       // private TextView msgArea;
+
+       // The path to and MIME type of the currently selected image from the
+       // gallery
+       // private String filePath;
+       // private String mimeType;
+       // private String fileName;
+
+       // ADD
+       private OAuthUtil util = OAuthUtil.getInstance();
+       private boolean isInit = true;
+
+       /** Called when the activity is first created. */
+       @Override
+       public void onCreate(Bundle savedInstanceState) {
+               super.onCreate(savedInstanceState);
+               // setContentView(R.layout.main);
+               setContentView(R.layout.oauth);
+
+               // DELETE
+               // msgArea = (TextView)findViewById(R.id.message);
+               // btnAuth = (Button) findViewById(R.id.auth_button);
+               // btnSelect = (Button) findViewById(R.id.select_button);
+               // btnSave = (Button) findViewById(R.id.save_button);
+
+               setupSession();
+       }
+
+       @Override
+       public void onResume() {
+               super.onResume();
+
+               // Complete the Evernote authentication process if necessary
+               // EDIT
+               // if (!session.completeAuthentication(getPreferencesForAuthData())) {
+               // // We only want to do this when we're resuming after
+               // authentication...
+               // Toast.makeText(this, "Evernote login failed",
+               // Toast.LENGTH_LONG).show();
+               // }
+               boolean isCompleteAuth = util.isCompleteAuth(this);
+               if (!isInit && !isCompleteAuth) {
+                       // We only want to do this when we're resuming after
+                       // authentication...
+                       Toast.makeText(this, getString(R.string.ErrorLogin),
+                                       Toast.LENGTH_LONG).show();
+               }
+
+               updateUi();
+
+               // ADD
+               if (isInit) {
+                       isInit = false;
+               } else {
+                       if (isCompleteAuth) {
+                               EvernoteUtil.getInstance().setSession(util.getSession());
+                               util.setLoginTime(this);
+                       }
+                       finish();
+               }
+       }
+
+       /**
+        * Evernote authentication data will be stored to this SharedPreferences if
+        * we are resuming as a result of a successful OAuth authorization. You may
+        * wish to pass a different SharedPreferences so that Evernote settings are
+        * stored along with other settings persisted by your application.
+        */
+       // private SharedPreferences getPreferencesForAuthData() {
+       // return getPreferences(MODE_PRIVATE);
+       // }
+
+       /**
+        * Setup the EvernoteSession used to access the Evernote API.
+        */
+       private void setupSession() {
+               // EDIT
+               // ApplicationInfo info =
+               // new ApplicationInfo(CONSUMER_KEY, CONSUMER_SECRET, EVERNOTE_HOST,
+               // APP_NAME, APP_VERSION);
+               //
+               // // Retrieve persisted authentication information
+               // session = new EvernoteSession(info, getPreferencesForAuthData(),
+               // getTempDir());
+               util.setupSession(this);
+               updateUi();
+
+               // ADD
+               startAuth(null);
+       }
+
+       /**
+        * Update the UI based on Evernote authentication state.
+        */
+       private void updateUi() {
+               // DELETE
+               // if (session.isLoggedIn()) {
+               // btnAuth.setText(R.string.label_log_out);
+               // btnSave.setEnabled(true);
+               // btnSelect.setEnabled(true);
+               // } else {
+               // btnAuth.setText(R.string.label_log_in);
+               // btnSave.setEnabled(false);
+               // btnSelect.setEnabled(false);
+               // }
+       }
+
+       /**
+        * Called when the user taps the "Log in to Evernote" button. Initiates the
+        * Evernote OAuth process, or logs out if the user is already logged in.
+        */
+       public void startAuth(View view) {
+               // ADD
+               EvernoteSession session = util.getSession();
+
+               // EDIT
+               // if (session.isLoggedIn()) {
+               // session.logOut(util.getPreferencesForAuthData(this));
+               // } else {
+               // session.authenticate(this);
+               // }
+               if (session.isLoggedIn()) {
+                       session.logOut(util.getPreferencesForAuthData(this));
+               }
+               session.authenticate(this);
+
+               updateUi();
+       }
+
+       /**
+        * Get a temporary directory that can be used by this application to store
+        * potentially large files sent to and retrieved from the Evernote API.
+        */
+       // private File getTempDir() {
+       // return new File(Environment.getExternalStorageDirectory(),
+       // APP_DATA_PATH);
+       // }
+}
\ No newline at end of file
index 49e2ce8..de2c37b 100644 (file)
@@ -9,10 +9,9 @@ import android.widget.Button;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
-import com.yuji.em.common.CommonUtil;
 import com.yuji.em.data.NoteDao;
 import com.yuji.em.utility.BaseActivity;
-import com.yuji.em.utility.PasswordUtil;
+import com.yuji.em.utility.EvernoteUtil;
 
 public class SettingActivity extends BaseActivity {
        private LinearLayout messageLayout = null;
@@ -75,7 +74,9 @@ public class SettingActivity extends BaseActivity {
        }
 
        private void userButtonOnClick() {
-               Intent intent = new Intent(this, (Class<?>) UserPasswordActivity.class);
+               //Intent intent = new Intent(this, (Class<?>) UserPasswordActivity.class);
+               //startActivityForResult(intent, 0);
+               Intent intent = new Intent(this, (Class<?>) OAuthActivity.class);
                startActivityForResult(intent, 0);
        }
 
@@ -102,9 +103,11 @@ public class SettingActivity extends BaseActivity {
        }
 
        private void setStatus() {
-               String passwd = PasswordUtil.getPassword(this);
+               //String passwd = PasswordUtil.getPassword(this);
 
-               if (CommonUtil.isNull(passwd)) {
+               EvernoteUtil util = EvernoteUtil.getInstance();
+               //if (CommonUtil.isNull(passwd)) {
+               if (!util.isLoggedIn()){
                        messageLayout.setVisibility(View.VISIBLE);
                        messageView.setText(getString(R.string.settingConfirmUserMsg));
                        noteButton.setEnabled(false);
index 34e98c0..e9f5636 100644 (file)
@@ -89,7 +89,7 @@ public class UserPasswordActivity extends BaseActivity {
                PasswordUtil.setUserAndPassword(this, username, password);
                
                EvernoteUtil util = EvernoteUtil.getInstance();
-               util.setConfig(username, password);
+               //util.setConfig(username, password);
                
                finish();
        }
index 1ddafc2..daf505c 100644 (file)
@@ -15,4 +15,5 @@ public class Constant {
        public static final int TOGGLE_SIZE = 5;
        
        public static final String DEFAULT_HEADER_TEXT = "<hr /><h3>{DATE} {TIME}</h3><hr />";
+       public static final String PREF_LOGIN_DATE = "LOGIN_DATE";
 }
index 98823cc..d71c689 100644 (file)
@@ -7,6 +7,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 
 import android.content.Context;
+import android.content.SharedPreferences;
 
 public class EditPrefUtil implements PrefUtil {
        private static final String DELM = "\t";
@@ -17,14 +18,27 @@ public class EditPrefUtil implements PrefUtil {
                pref = new PrefUtilImpl(context);
        }
        
+       @Override
+       public SharedPreferences getSharedPreferences() {
+               return pref.getSharedPreferences();
+       }
+
+       @Override
        public void put(String key, String value) {
                hash.put(key, value);
        }
 
+       @Override
        public void put(String key, int value) {
                hash.put(key, String.valueOf(value));
        }
 
+       @Override
+       public void put(String key, long value) {
+               hash.put(key, String.valueOf(value));
+       }
+
+       @Override
        public String get(String key) {
                String value = hash.get(key);
                if (value != null){
@@ -35,10 +49,17 @@ public class EditPrefUtil implements PrefUtil {
                return value;
        }
 
+       @Override
        public int getInt(String key) {
                return Integer.parseInt(get(key));
        }
 
+       @Override
+       public long getLong(String key) {
+               return Long.parseLong(get(key));
+       }
+
+       @Override
        public String get(String key, String initValue) {
                String value = get(key);
                if (value != null){
@@ -48,10 +69,16 @@ public class EditPrefUtil implements PrefUtil {
                return initValue;
        }
 
+       @Override
        public int getInt(String key, int initValue) {
                return Integer.valueOf(get(key, String.valueOf(initValue)));
        }
        
+       @Override
+       public long getLong(String key, long initValue) {
+               return Long.valueOf(get(key, String.valueOf(initValue)));
+       }
+
        public void update(){
                Set<Entry<String, String>> set = hash.entrySet();
                for (Iterator<Entry<String, String>> it = set.iterator(); it.hasNext(); ){
@@ -63,6 +90,13 @@ public class EditPrefUtil implements PrefUtil {
                hash.clear();
        }
 
+       @Override
+       public void copy(String srcKey, String dstKey){
+               String value = get(srcKey);
+               put(dstKey, value);
+       }
+       
+       @Override
        public void remove(String key) {
                hash.put(key, null);
        }
@@ -95,5 +129,4 @@ public class EditPrefUtil implements PrefUtil {
                        put(prefKey, prefValue);
                }
        }
-
 }
index 63fc25b..c215cd2 100644 (file)
@@ -5,13 +5,11 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.thrift.TException;
-import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.transport.THttpClient;
 import org.apache.thrift.transport.TTransportException;
 
 import android.content.Context;
 
-import com.evernote.edam.error.EDAMErrorCode;
+import com.evernote.client.oauth.android.EvernoteSession;
 import com.evernote.edam.error.EDAMNotFoundException;
 import com.evernote.edam.error.EDAMSystemException;
 import com.evernote.edam.error.EDAMUserException;
@@ -20,24 +18,20 @@ import com.evernote.edam.notestore.NoteList;
 import com.evernote.edam.notestore.NoteStore;
 import com.evernote.edam.type.Note;
 import com.evernote.edam.type.NoteSortOrder;
-import com.evernote.edam.type.User;
-import com.evernote.edam.userstore.AuthenticationResult;
-import com.evernote.edam.userstore.UserStore;
 import com.yuji.em.R;
 
 public class EvernoteUtil {
        private static final int N = 50;
-       private String username;
-       private String password;
+       private EvernoteSession session = null;
+
+       // private String username;
+       // private String password;
        public enum Error {
-               NONE,
-               CONNECT,
-               INVALID_AUTH,
-               NOT_FOUND,
-               OTHER
+               NONE, CONNECT, INVALID_AUTH, NOT_FOUND, OTHER
        };
+
        private Error errorCode;
-       
+
        public static EvernoteUtil getInstance() {
                if (instance == null) {
                        instance = new EvernoteUtil();
@@ -55,12 +49,12 @@ public class EvernoteUtil {
        private String authenticationToken = null;
        private NoteStore.Client noteStore = null;
 
-       public void setConfig(String username, String password) {
-               this.username = username;
-               this.password = password;
-               authenticationToken = null;
-               noteStore = null;
-       }
+       // public void setConfig(String username, String password) {
+       // this.username = username;
+       // this.password = password;
+       // authenticationToken = null;
+       // noteStore = null;
+       // }
 
        private void init() {
 
@@ -75,7 +69,7 @@ public class EvernoteUtil {
                errorCode = Error.OTHER;
                try {
                        String token = getAuthenticationToken();
-                       if (token == null){
+                       if (token == null) {
                                return null;
                        }
                        note = noteStore.getNote(token, guid, withContent,
@@ -91,23 +85,23 @@ public class EvernoteUtil {
                        throw e;
                } catch (TException e) {
                        Debug.d(this, null, e);
-                       
-                       if (e.getCause() instanceof ConnectException){
-                               errorCode = Error.CONNECT;                                                              
+
+                       if (e.getCause() instanceof ConnectException) {
+                               errorCode = Error.CONNECT;
                        }
                }
                return note;
        }
 
-       private NoteFilter getDefaultFilter(){
+       private NoteFilter getDefaultFilter() {
                // 検索条件として、検索語なし、更新日順ソートを指定
                NoteFilter filter = new NoteFilter();
                filter.setOrder(NoteSortOrder.UPDATED.getValue());
-               filter.setAscending(false);             
+               filter.setAscending(false);
 
                return filter;
        }
-       
+
        public List<NoteList> getNoteList() {
                NoteFilter filter = getDefaultFilter();
                return getNoteList(filter);
@@ -118,7 +112,7 @@ public class EvernoteUtil {
                filter.setNotebookGuid(notebookGuid);
                return getNoteList(filter);
        }
-       
+
        public List<NoteList> getNoteListByTag(String tagGuid) {
                NoteFilter filter = getDefaultFilter();
                List<String> tagGuids = new ArrayList<String>();
@@ -126,7 +120,7 @@ public class EvernoteUtil {
                filter.setTagGuids(tagGuids);
                return getNoteList(filter);
        }
-       
+
        public List<NoteList> getNoteList(NoteFilter filter) {
                errorCode = Error.OTHER;
 
@@ -137,20 +131,21 @@ public class EvernoteUtil {
                                return null;
                        }
                        int index = 0;
-                       while (true){
-                               NoteList noteList = noteStore.findNotes(token, filter, index, N);
+                       while (true) {
+                               NoteList noteList = noteStore
+                                               .findNotes(token, filter, index, N);
                                int size = noteList.getNotesSize();
-                               
-                               if (size <= 0){
+
+                               if (size <= 0) {
                                        break;
                                }
                                list.add(noteList);
-                               if (size < N){
+                               if (size < N) {
                                        break;
                                }
                                index += size;
                        }
-                       
+
                        errorCode = Error.NONE;
                } catch (EDAMUserException e) {
                        Debug.d(this, null, e);
@@ -172,7 +167,7 @@ public class EvernoteUtil {
                return list;
        }
 
-       public Note updateNoteContext(String guid, String title, String text){
+       public Note updateNoteContext(String guid, String title, String text) {
                errorCode = Error.OTHER;
 
                Note note = null;
@@ -216,7 +211,7 @@ public class EvernoteUtil {
                Note n = null;
                try {
                        String token = getAuthenticationToken();
-                       if (token == null){
+                       if (token == null) {
                                return null;
                        }
                        n = noteStore.updateNote(token, note);
@@ -230,87 +225,53 @@ public class EvernoteUtil {
                } catch (TException e) {
                        Debug.d(this, null, e);
 
-                       if (e.getCause() instanceof ConnectException){
-                               errorCode = Error.CONNECT;                                                              
+                       if (e.getCause() instanceof ConnectException) {
+                               errorCode = Error.CONNECT;
                        }
                }
                return n;
        }
 
        private String getAuthenticationToken() {
-               try {
-                       if (authenticationToken == null) {
-                               //String userStoreUrl = "https://sandbox.evernote.com/edam/user";
-                               String userStoreUrl = "https://www.evernote.com/edam/user";
-                               THttpClient userStoreTrans = new THttpClient(userStoreUrl);
-                               TBinaryProtocol userStoreProt = new TBinaryProtocol(
-                                               userStoreTrans);
-                               UserStore.Client userStore = new UserStore.Client(userStoreProt);
-
-                               // #27612
-                               // http://www.antun.net/tips/api/evernote.html
-                               // expiredTime=time.time()+(authResult.expiration/1000.0-authResult.currentTime/1000.0)
-                               // # expiredTime<time.time() なら認証し直して、authenticationToken
-                               // を取得し直すべし
-
-                               String consumerKey = "yuji_k64613";
-                               String consumerSecret = "d5528b4fdb3a7fee";
-                               AuthenticationResult authResult = userStore.authenticate(
-                                               username, password, consumerKey, consumerSecret);
-                               authenticationToken = authResult.getAuthenticationToken();
-
-                               //String noteStoreUrlBase = "https://sandbox.evernote.com/edam/note/";
-                               String noteStoreUrlBase = "https://www.evernote.com/edam/note/";
-                               User user = authResult.getUser();
-                               String noteStoreUrl = noteStoreUrlBase + user.getShardId();
-                               THttpClient noteStoreTrans = new THttpClient(noteStoreUrl);
-                               TBinaryProtocol noteStoreProt = new TBinaryProtocol(
-                                               noteStoreTrans);
-                               noteStore = new NoteStore.Client(noteStoreProt);
-                       }
-               } catch (TTransportException e) {
-                       authenticationToken = null;
-                       noteStore = null;
-                       Debug.d(this, null, e);
-
-                       errorCode = Error.CONNECT;                              
-               } catch (EDAMUserException e) {
-                       Debug.d(this, null, e);
-                       
-                       EDAMErrorCode code = e.getErrorCode();
-                       if (code.equals(EDAMErrorCode.INVALID_AUTH)){
-                               errorCode = Error.INVALID_AUTH;                         
-                       }
-               } catch (EDAMSystemException e) {
-                       Debug.d(this, null, e);
-               } catch (TException e) {
-                       Debug.d(this, null, e);
-
-                       if (e.getCause() instanceof ConnectException){
-                               errorCode = Error.CONNECT;                                                              
+               if (authenticationToken == null) {
+                       authenticationToken = session.getAuthToken();
+                       try {
+                               noteStore = session.createNoteStore();
+                       } catch (TTransportException e) {
+                               authenticationToken = null;
                        }
                }
-
                return authenticationToken;
        }
-       
-       public Error getErrorCode(){
+
+       public Error getErrorCode() {
                return errorCode;
        }
-       
-       public String getErrorMessage(Context context, Error code){
-               if (code.equals(Error.NONE)){
+
+       public String getErrorMessage(Context context, Error code) {
+               if (code.equals(Error.NONE)) {
                        return "";
                }
-               if (code.equals(Error.CONNECT)){
+               if (code.equals(Error.CONNECT)) {
                        return context.getString(R.string.toastConnect);
                }
-               if (code.equals(Error.INVALID_AUTH)){
+               if (code.equals(Error.INVALID_AUTH)) {
                        return context.getString(R.string.toastInvalidAuth);
                }
-               if (code.equals(Error.NOT_FOUND)){
+               if (code.equals(Error.NOT_FOUND)) {
                        return context.getString(R.string.toastNoteDoesNotExist);
                }
                return context.getString(R.string.toastEvernoteSystem);
        }
+
+       public void setSession(EvernoteSession session) {
+               this.session = session;
+       }
+
+       public boolean isLoggedIn() {
+               if (session == null) {
+                       return false;
+               }
+               return session.isLoggedIn();
+       }
 }
diff --git a/workspace/EverMemo/src/com/yuji/em/utility/OAuthUtil.java b/workspace/EverMemo/src/com/yuji/em/utility/OAuthUtil.java
new file mode 100644 (file)
index 0000000..8a0e014
--- /dev/null
@@ -0,0 +1,102 @@
+package com.yuji.em.utility;
+
+import java.io.File;
+import java.util.Calendar;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.os.Environment;
+
+import com.evernote.client.conn.ApplicationInfo;
+import com.evernote.client.oauth.android.EvernoteSession;
+import com.yuji.em.common.Constant;
+
+public class OAuthUtil {
+       private static final String CONSUMER_KEY = "yuji-k64613";
+       private static final String CONSUMER_SECRET = "98b2cbf35f747d2d";
+       private static final String APP_DATA_PATH = "/Android/data/com.yuji.em/temp/";
+       private static final String EVERNOTE_HOST = "www.evernote.com";
+
+       private static final String APP_NAME = "MemoErver";
+       private static final String APP_VERSION = "1.0.0";
+
+       private static OAuthUtil instance = null;
+       private EvernoteSession session;
+
+       public static OAuthUtil getInstance() {
+               if (instance == null) {
+                       instance = new OAuthUtil();
+               }
+               return instance;
+       }
+
+       private OAuthUtil() {
+
+       }
+
+       public EvernoteSession setupSession(Context context) {
+               ApplicationInfo info = new ApplicationInfo(CONSUMER_KEY,
+                               CONSUMER_SECRET, EVERNOTE_HOST, APP_NAME, APP_VERSION);
+
+               // Retrieve persisted authentication information
+               session = new EvernoteSession(info,
+                               getPreferencesForAuthData(context), getTempDir());
+
+               // セッションの期限切れをチェック
+               EditPrefUtil pref = new EditPrefUtil(context);
+               Calendar cal = Calendar.getInstance();
+               long now = cal.getTimeInMillis();
+               long loginTime = pref.getLong(Constant.PREF_LOGIN_DATE);
+               long diff = (now - loginTime) / 1000;
+
+               //if (loginTime > 0 && diff > 60){ // 1分
+               if (loginTime > 0 && diff > (365 - 1) * 24 * 60 * 60){ // 余裕を持って1日前に削除
+                       logout(context);
+                       pref.put(Constant.PREF_LOGIN_DATE, 0L);
+                       pref.update();
+
+                       session = new EvernoteSession(info,
+                                       getPreferencesForAuthData(context), getTempDir());
+               }
+               
+               return session;
+       }
+
+       public boolean isCompleteAuth(Context context) {
+               if (session == null) {
+                       return false;
+               }
+               return session
+                               .completeAuthentication(getPreferencesForAuthData(context));
+       }
+
+       public SharedPreferences getPreferencesForAuthData(Context context) {
+               PrefUtil pref = new PrefUtilImpl(context);
+               return pref.getSharedPreferences();
+       }
+
+       private File getTempDir() {
+               return new File(Environment.getExternalStorageDirectory(),
+                               APP_DATA_PATH);
+       }
+
+       public EvernoteSession getSession() {
+               return session;
+       }
+
+       public void logout(Context context){
+           session.logOut(getPreferencesForAuthData(context)); 
+
+           EditPrefUtil pref = new EditPrefUtil(context);
+               pref.put(Constant.PREF_LOGIN_DATE, 0L);
+               pref.update();
+       }
+
+       public void setLoginTime(Context context) {
+               Calendar cal = Calendar.getInstance();
+               long t = cal.getTimeInMillis();
+               EditPrefUtil pref = new EditPrefUtil(context);
+               pref.put(Constant.PREF_LOGIN_DATE, t);
+               pref.update();
+       }
+}
index 68e4c43..74c6f15 100644 (file)
@@ -1,11 +1,18 @@
 package com.yuji.em.utility;
 
+import android.content.SharedPreferences;
+
 public interface PrefUtil {
+       SharedPreferences getSharedPreferences();
        void put(String key, String value);
        void put(String key, int value);
+       void put(String key, long value);
        String get(String key);
        int getInt(String key);
+       long getLong(String key);
        String get(String key, String initValue);
        int getInt(String key, int initValue);
+       long getLong(String key, long initValue);
+       void copy(String srcKey, String dstKey);
        void remove(String key);
 }
index d2c84df..d72cb49 100644 (file)
@@ -6,7 +6,7 @@ import android.content.SharedPreferences.Editor;
 import android.content.pm.PackageManager.NameNotFoundException;
 
 public class PrefUtilImpl implements PrefUtil {
-       private static final String EVER_MEMO_ACTIVITY = "EverMemoActivity";
+       private static final String EVER_CLIP_ACTIVITY = "EverMemoActivity";
        private static final String PACKAGE_COM_YUJI_EM = "com.yuji.em";
        private Context context;
 
@@ -14,16 +14,25 @@ public class PrefUtilImpl implements PrefUtil {
                this.context = context;
        }
 
+       @Override
+       public SharedPreferences getSharedPreferences(){
+               SharedPreferences pref = null;
+               try {
+                       Context ctxt = context.createPackageContext(PACKAGE_COM_YUJI_EM,
+                                       Context.CONTEXT_RESTRICTED);
+                       pref = ctxt.getSharedPreferences(EVER_CLIP_ACTIVITY,
+                                       Context.MODE_PRIVATE);
+               } catch (NameNotFoundException e) {
+                       Debug.d(this, null, e);
+               }               
+               return pref;
+       }
+       
+       @Override
        public void put(String key, String value) {
                synchronized (this) {
-                       SharedPreferences pref = null;
-                       try {
-                               Context ctxt = context.createPackageContext(PACKAGE_COM_YUJI_EM,
-                                               Context.CONTEXT_RESTRICTED);
-                               pref = ctxt.getSharedPreferences(EVER_MEMO_ACTIVITY,
-                                               Context.MODE_PRIVATE);
-                       } catch (NameNotFoundException e) {
-                               Debug.d(this, null, e);
+                       SharedPreferences pref = getSharedPreferences();
+                       if (pref == null){
                                return;
                        }
 
@@ -33,34 +42,43 @@ public class PrefUtilImpl implements PrefUtil {
                }
        }
 
+       @Override
        public void put(String key, int value) {
                put(key, String.valueOf(value));
        }
 
+       @Override
+       public void put(String key, long value) {
+               put(key, String.valueOf(value));
+       }
+
+       @Override
        public String get(String key) {
                String val;
                
                synchronized (this) {
-                       SharedPreferences pref = null;
-                       try {
-                               Context ctxt = context.createPackageContext(PACKAGE_COM_YUJI_EM,
-                                               Context.CONTEXT_RESTRICTED);
-                               pref = ctxt.getSharedPreferences(EVER_MEMO_ACTIVITY,
-                                               Context.MODE_WORLD_READABLE);
-                       } catch (NameNotFoundException e) {
-                               Debug.d(this, null, e);
+                       SharedPreferences pref = getSharedPreferences();
+                       if (pref == null){
                                return null;
                        }
+
                        val = pref.getString(key, null);
                }
 
                return val;
        }
 
+       @Override
        public int getInt(String key) {
                return Integer.parseInt(get(key));
        }
 
+       @Override
+       public long getLong(String key) {
+               return Long.parseLong(get(key));
+       }
+
+       @Override
        public String get(String key, String initValue) {
                String value = get(key);
                if (value != null) {
@@ -70,10 +88,23 @@ public class PrefUtilImpl implements PrefUtil {
                return initValue;
        }
 
+       @Override
        public int getInt(String key, int initValue) {
                return Integer.valueOf(get(key, String.valueOf(initValue)));
        }
 
+       @Override
+       public long getLong(String key, long initValue) {
+               return Long.valueOf(get(key, String.valueOf(initValue)));
+       }
+       
+       @Override
+       public void copy(String srcKey, String dstKey){
+               String value = get(srcKey);
+               put(dstKey, value);
+       }
+       
+       @Override
        public void remove(String key) {
                put(key, null);
        }