OSDN Git Service

T27939
authorYuji Konishi <yuji.k64613@gmail.com>
Sat, 31 Mar 2012 06:06:12 +0000 (15:06 +0900)
committerYuji Konishi <yuji.k64613@gmail.com>
Sat, 31 Mar 2012 06:06:12 +0000 (15:06 +0900)
T27940
T27981

workspace/EverMemo/gen/com/yuji/em/R.java
workspace/EverMemo/res/values-ja/strings.xml
workspace/EverMemo/res/values/strings.xml
workspace/EverMemo/src/com/yuji/em/NoteListActivity.java
workspace/EverMemo/src/com/yuji/em/task/NoteListTask.java
workspace/EverMemo/src/com/yuji/em/task/UpdateNoteTask.java
workspace/EverMemo/src/com/yuji/em/utility/BaseActivity.java
workspace/EverMemo/src/com/yuji/em/utility/EvernoteUtil.java

index 98b973e..f460a93 100644 (file)
@@ -75,22 +75,22 @@ public final class R {
         public static final int cancelButton=0x7f050006;
         public static final int closeButton=0x7f050007;
         public static final int deleteButton=0x7f050005;
-        public static final int dialogErrorTitle=0x7f050021;
-        public static final int dialogSystemError=0x7f050022;
+        public static final int dialogErrorTitle=0x7f050025;
+        public static final int dialogSystemError=0x7f050026;
         public static final int headerEditTitle=0x7f050019;
         public static final int headerExplainText1=0x7f05001a;
         public static final int headerExplainText2=0x7f05001b;
         public static final int headerExplainText3=0x7f05001c;
         public static final int headerExplainText4=0x7f05001d;
         public static final int mainMenuSetting=0x7f050008;
-        public static final int mainTitle=0x7f050023;
+        public static final int mainTitle=0x7f050027;
         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=0x7f050024;
+        public static final int selectTitle=0x7f050028;
         public static final int sendButton=0x7f050001;
         public static final int settingConfirmNoteMsg=0x7f050011;
         public static final int settingConfirmTitle=0x7f05000f;
@@ -101,9 +101,13 @@ public final class R {
         public static final int settingNoteTitle=0x7f050013;
         public static final int settingUserButton=0x7f050015;
         public static final int settingUserTitle=0x7f050012;
-        public static final int toastFailedToGetList=0x7f050020;
+        public static final int toastConnect=0x7f05001f;
+        public static final int toastEvernoteSystem=0x7f050023;
+        public static final int toastFailedToGetList=0x7f050021;
         public static final int toastFailedToWrite=0x7f05001e;
-        public static final int toastNoteDoesNotExist=0x7f05001f;
+        public static final int toastInvalidAuth=0x7f050022;
+        public static final int toastNoteDoesNotExist=0x7f050020;
+        public static final int toastSystem=0x7f050024;
         public static final int updateButton=0x7f050004;
         public static final int userPasswordTitle=0x7f05000e;
         public static final int userUserTitle=0x7f05000d;
index 6a93373..c425fc5 100644 (file)
     <string name="headerExplainText4">・例) &lt;hr /&gt;&lt;h3&gt;{DATE} {TIME}&lt;/h3&gt;&lt;hr /&gt;</string>
 
     <string name="toastFailedToWrite">書き込みに失敗しました</string>
+    <string name="toastConnect">Evernoteとの接続に失敗しました</string>
     <string name="toastNoteDoesNotExist">ノートがありません</string>
     <string name="toastFailedToGetList">リストの取得に失敗しました</string>
+    <string name="toastInvalidAuth">認証に失敗しました</string>
+    <string name="toastEvernoteSystem">Evernoteとの連係に失敗しました</string>
+    <string name="toastSystem">システムエラーが発生しました</string>
 
     <string name="dialogErrorTitle">エラー</string>
     <string name="dialogSystemError">システムエラーが発生しました</string>
index 758d6c6..51697e2 100644 (file)
     <string name="headerExplainText4">・Example: &lt;hr /&gt;&lt;h3&gt;{DATE} {TIME}&lt;/h3&gt;&lt;hr /&gt;</string>
 
     <string name="toastFailedToWrite">Failed to write.</string>
+    <string name="toastConnect">Failed to connect Evernote.</string>
     <string name="toastNoteDoesNotExist">Note does not exist.</string>
     <string name="toastFailedToGetList">Failed to get list.</string>
+    <string name="toastInvalidAuth">Invalid Auth.</string>
+    <string name="toastEvernoteSystem">Failed to cooperate with Evernote.</string>
+    <string name="toastSystem">System error has occurred.</string>
 
     <string name="dialogErrorTitle">Error</string>
     <string name="dialogSystemError">System error has occurred.</string>
index 7c7a115..84324a4 100644 (file)
@@ -12,6 +12,7 @@ import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.Button;
 import android.widget.ListView;
+import android.widget.Toast;
 
 import com.evernote.edam.notestore.NoteList;
 import com.yuji.em.common.Constant;
@@ -20,6 +21,8 @@ import com.yuji.em.data.NoteDao;
 import com.yuji.em.task.NoteListTask;
 import com.yuji.em.utility.AsyncTaskCommand;
 import com.yuji.em.utility.BaseActivity;
+import com.yuji.em.utility.EvernoteUtil;
+import com.yuji.em.utility.EvernoteUtil.Error;
 
 public class NoteListActivity extends BaseActivity {
        private static List<NoteListItem> noteList = null;
@@ -99,7 +102,13 @@ public class NoteListActivity extends BaseActivity {
                finish();
        }
 
-       public void done(NoteList nList) {
+       public void done(NoteList nList, Error errorCode) {
+               if (errorCode != null && !errorCode.equals(Error.NONE)) {
+                               EvernoteUtil util = EvernoteUtil.getInstance();
+                               Toast.makeText(this, util.getErrorMessage(this, errorCode),
+                                               Toast.LENGTH_LONG).show();
+               }
+
                if (nList == null) {
                        // \83L\83\83\83\93\83Z\83\8b\83{\83^\83\93\89\9f\89º\8e\9e
                        closeButtonOnClick();
@@ -149,8 +158,8 @@ public class NoteListActivity extends BaseActivity {
                }
                return list;
        }
-       
-       public static void init(){
+
+       public static void init() {
                noteList = null;
        }
 }
index 0923b52..679235c 100644 (file)
@@ -11,14 +11,20 @@ public class NoteListTask implements AsyncTaskIF {
        private EvernoteUtil util = EvernoteUtil.getInstance();
        private NoteList noteList;
        private WeakReference<NoteListActivity> activityRef;
-
+       private EvernoteUtil.Error errorCode = null;
+       
        public NoteListTask(NoteListActivity activity) {
                activityRef = new WeakReference<NoteListActivity>(activity);
        }
 
        @Override
        public void doExecute() {
-               noteList = util.getNoteList();
+               try {
+                       noteList = util.getNoteList();
+               }
+               finally {
+                       errorCode = util.getErrorCode();
+               }
        }
 
        @Override
@@ -30,7 +36,7 @@ public class NoteListTask implements AsyncTaskIF {
        public void done(boolean isCancel) {
                NoteListActivity activity = activityRef.get();
                if (activity != null) {
-                       activity.done((!isCancel ? noteList : null));
+                       activity.done((!isCancel ? noteList : null), errorCode);
                        activityRef = null;
                }
        }
@@ -43,4 +49,8 @@ public class NoteListTask implements AsyncTaskIF {
        public NoteList getNoteList() {
                return noteList;
        }
+       
+       public EvernoteUtil.Error getErrorCode(){
+               return errorCode;
+       }
 }
index 0902305..23db15f 100644 (file)
@@ -10,12 +10,9 @@ import com.yuji.em.data.NoteDao;
 import com.yuji.em.utility.AsyncTaskIF;
 import com.yuji.em.utility.Debug;
 import com.yuji.em.utility.EvernoteUtil;
+import com.yuji.em.utility.EvernoteUtil.Error;
 
 public class UpdateNoteTask implements AsyncTaskIF {
-       public static final int ERROR_NONE = 0;
-       public static final int ERROR_NOT_FOUND = 1;
-       public static final int ERROR_SYSTEM = 9;
-
        private Context context = null;
        private String guid;
        private String noteName;
@@ -23,8 +20,8 @@ public class UpdateNoteTask implements AsyncTaskIF {
        private String text;
 
        private EvernoteUtil util = EvernoteUtil.getInstance();
-       private int status = -1;
-
+       private EvernoteUtil.Error errorCode = null;
+       
        public UpdateNoteTask(Context context, String guid, String noteName,
                        String title, String text) {
                this.context = context;
@@ -36,25 +33,16 @@ public class UpdateNoteTask implements AsyncTaskIF {
 
        @Override
        public void doExecute() {
-               try {
-                       synchronized (context) {
-                               Note note = util.updateNoteContext(guid, title, text);
-                               if (note != null) {
-                                       NoteDao dao = NoteDao.getInstance();
-                                       com.yuji.em.data.Note n = new com.yuji.em.data.Note(
-                                                       note.getGuid(), note.getTitle());
-                                       dao.update(context, n);
-                                       status = ERROR_NONE;
-                               } else {
-                                       Toast.makeText(context,
-                                                       context.getString(R.string.toastFailedToWrite)
-                                                                       + ":" + text, Toast.LENGTH_LONG);
-                                       Debug.d(this, "doExecute");
-                                       status = ERROR_SYSTEM;
-                               }
+               synchronized (context) {
+                       Note note = util.updateNoteContext(guid, title, text);
+                       if (note != null) {
+                               NoteDao dao = NoteDao.getInstance();
+                               com.yuji.em.data.Note n = new com.yuji.em.data.Note(
+                                               note.getGuid(), note.getTitle());
+                               dao.update(context, n);
+                       } else {
+                               errorCode = util.getErrorCode();
                        }
-               } catch (EDAMNotFoundException e) {
-                       status = ERROR_NOT_FOUND;
                }
        }
 
@@ -65,19 +53,17 @@ public class UpdateNoteTask implements AsyncTaskIF {
 
        @Override
        public void done(boolean isCancel) {
-               switch (status) {
-               case ERROR_NOT_FOUND:
-                       Toast.makeText(
-                                       context,
-                                       context.getString(R.string.toastNoteDoesNotExist) + ":"
-                                                       + noteName, Toast.LENGTH_LONG).show();
-                       break;
+               if (errorCode != null && !errorCode.equals(Error.NONE)){
+                       Toast.makeText(context, util.getErrorMessage(context, errorCode),
+                                       Toast.LENGTH_LONG).show();                      
                }
-
        }
 
        @Override
        public int getStatus() {
-               return status;
+               if (errorCode != null && !errorCode.equals(Error.NONE)){
+                       return -1;
+               }
+               return 0;
        }
 }
index 24d5f5c..f638cfe 100755 (executable)
@@ -9,29 +9,31 @@ import android.os.Bundle;
 import android.view.KeyEvent;\r
 import android.view.Window;\r
 import android.view.WindowManager;\r
+import android.widget.Toast;\r
 \r
 import com.yuji.em.Initialize;\r
 import com.yuji.em.R;\r
 \r
 public class BaseActivity extends Activity {\r
        @Override\r
-    public void onCreate(Bundle savedInstanceState) {\r
-        super.onCreate(savedInstanceState);\r
+       public void onCreate(Bundle savedInstanceState) {\r
+               super.onCreate(savedInstanceState);\r
                Debug.d(this, Debug.ON_CREATE);\r
 \r
                Context context = getApplicationContext();\r
-               //Thread.setDefaultUncaughtExceptionHandler(new TrainDelayedUncaughtExceptionHandler(\r
-               //              context));\r
-               //DatabaseHelper.init(context);\r
+               // Thread.setDefaultUncaughtExceptionHandler(new\r
+               // TrainDelayedUncaughtExceptionHandler(\r
+               // context));\r
+               // DatabaseHelper.init(context);\r
                Initialize.initialize(context);\r
-    }\r
-    \r
-    @Override\r
+       }\r
+\r
+       @Override\r
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {\r
-       super.onActivityResult(requestCode, resultCode, data);\r
+               super.onActivityResult(requestCode, resultCode, data);\r
        }\r
-    \r
-    @Override\r
+\r
+       @Override\r
        protected void onRestart() {\r
                super.onRestart();\r
                Debug.d(this, Debug.ON_RESTART);\r
@@ -61,30 +63,30 @@ public class BaseActivity extends Activity {
                Debug.d(this, Debug.ON_STOP);\r
        }\r
 \r
-    @Override\r
+       @Override\r
        protected void onDestroy() {\r
                super.onDestroy();\r
                Debug.d(this, Debug.ON_DESTROY);\r
        }\r
 \r
-//     @Override\r
-//     public boolean onCreateOptionsMenu(Menu menu) {\r
-//             boolean ret = super.onCreateOptionsMenu(menu);\r
-//             menu.add(0, Menu.FIRST, Menu.NONE, R.string.help);\r
-//             return ret;\r
-//     }\r
-//    \r
-//    @Override\r
-//    public boolean onOptionsItemSelected(MenuItem item) {\r
-//     try {\r
-//             Intent intent = new Intent(this, (Class<?>)Help.class);\r
-//                     startActivity(intent);\r
-//     }\r
-//     catch (Exception e){\r
-//                     terminate(e);\r
-//     }\r
-//             return super.onOptionsItemSelected(item);\r
-//     }\r
+       // @Override\r
+       // public boolean onCreateOptionsMenu(Menu menu) {\r
+       // boolean ret = super.onCreateOptionsMenu(menu);\r
+       // menu.add(0, Menu.FIRST, Menu.NONE, R.string.help);\r
+       // return ret;\r
+       // }\r
+       //\r
+       // @Override\r
+       // public boolean onOptionsItemSelected(MenuItem item) {\r
+       // try {\r
+       // Intent intent = new Intent(this, (Class<?>)Help.class);\r
+       // startActivity(intent);\r
+       // }\r
+       // catch (Exception e){\r
+       // terminate(e);\r
+       // }\r
+       // return super.onOptionsItemSelected(item);\r
+       // }\r
 \r
        @Override\r
        public boolean dispatchKeyEvent(KeyEvent event) {\r
@@ -96,22 +98,26 @@ public class BaseActivity extends Activity {
                }\r
                return super.dispatchKeyEvent(event);\r
        }\r
-    \r
+\r
        protected void setFullscreen() {\r
-       requestWindowFeature(Window.FEATURE_NO_TITLE);\r
-       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,\r
-       WindowManager.LayoutParams.FLAG_FULLSCREEN);\r
-    }\r
-       \r
+               requestWindowFeature(Window.FEATURE_NO_TITLE);\r
+               getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,\r
+                               WindowManager.LayoutParams.FLAG_FULLSCREEN);\r
+       }\r
+\r
        protected void showDialog(String title, String msg, final boolean isFinish) {\r
-               new AlertDialog.Builder(this).setTitle(title).setMessage(msg)\r
-                               .setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {\r
-                                       public void onClick(DialogInterface dialog, int whichButton) {\r
-                                               if (isFinish){\r
-                                                       finish();\r
-                                               }\r
-                                       }\r
-                               }).show();\r
+               new AlertDialog.Builder(this)\r
+                               .setTitle(title)\r
+                               .setMessage(msg)\r
+                               .setNeutralButton(android.R.string.ok,\r
+                                               new DialogInterface.OnClickListener() {\r
+                                                       public void onClick(DialogInterface dialog,\r
+                                                                       int whichButton) {\r
+                                                               if (isFinish) {\r
+                                                                       finish();\r
+                                                               }\r
+                                                       }\r
+                                               }).show();\r
        }\r
 \r
        protected void showDialog(String title, String msg) {\r
@@ -119,17 +125,22 @@ public class BaseActivity extends Activity {
        }\r
 \r
        protected void showDialog(Object target, Throwable t) {\r
-       if (t != null){\r
-               Debug.d(target, null, t);\r
-       }\r
+               if (t != null) {\r
+                       Debug.d(target, null, t);\r
+               }\r
                showDialog(getString(R.string.dialogErrorTitle),\r
                                getString(R.string.dialogSystemError));\r
        }\r
 \r
-    public void terminate(Throwable t){\r
-       if (t != null){\r
-               Debug.d(this, null, t);\r
-       }\r
-       moveTaskToBack(true);\r
-    }\r
+       public void terminate(Throwable t) {\r
+               if (t != null) {\r
+                       Debug.d(this, null, t);\r
+\r
+                       Toast.makeText(\r
+                                       this,\r
+                                       getString(R.string.toastSystem) + ":"\r
+                                                       + t.getLocalizedMessage(), Toast.LENGTH_LONG);\r
+               }\r
+               moveTaskToBack(true);\r
+       }\r
 }\r
index c84845a..bcb2d9c 100644 (file)
@@ -5,6 +5,9 @@ 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.edam.error.EDAMNotFoundException;
 import com.evernote.edam.error.EDAMSystemException;
 import com.evernote.edam.error.EDAMUserException;
@@ -16,11 +19,20 @@ 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 String username;
        private String password;
-
+       public enum Error {
+               NONE,
+               CONNECT,
+               INVALID_AUTH,
+               NOT_FOUND,
+               OTHER
+       };
+       private Error errorCode;
+       
        public static EvernoteUtil getInstance() {
                if (instance == null) {
                        instance = new EvernoteUtil();
@@ -32,7 +44,7 @@ public class EvernoteUtil {
        private static EvernoteUtil instance = null;
 
        private EvernoteUtil() {
-
+               errorCode = Error.NONE;
        }
 
        private String authenticationToken = null;
@@ -55,11 +67,16 @@ public class EvernoteUtil {
                boolean withResourcesRecognition = false;
                boolean withResourcesAlternateData = false;
                Note note = null;
+               errorCode = Error.OTHER;
                try {
                        String token = getAuthenticationToken();
+                       if (token == null){
+                               return null;
+                       }
                        note = noteStore.getNote(token, guid, withContent,
                                        withResourcesData, withResourcesRecognition,
                                        withResourcesAlternateData);
+                       errorCode = Error.NONE;
                } catch (EDAMUserException e) {
                        Debug.d(this, null, e);
                } catch (EDAMSystemException e) {
@@ -74,6 +91,8 @@ public class EvernoteUtil {
        }
 
        public NoteList getNoteList() {
+               errorCode = Error.OTHER;
+
                // \8c\9f\8dõ\8fð\8c\8f\82Æ\82µ\82Ä\81A\8c\9f\8dõ\8cê\82È\82µ\81A\8dX\90V\93ú\8f\87\83\\81[\83g\82ð\8ew\92è
                NoteFilter filter = new NoteFilter();
                filter.setOrder(NoteSortOrder.UPDATED.getValue());
@@ -82,7 +101,11 @@ public class EvernoteUtil {
                NoteList noteList = null;
                try {
                        String token = getAuthenticationToken();
+                       if (token == null){
+                               return null;
+                       }
                        noteList = noteStore.findNotes(token, filter, 0, 100);
+                       errorCode = Error.NONE;
                } catch (EDAMUserException e) {
                        Debug.d(this, null, e);
                } catch (EDAMSystemException e) {
@@ -95,9 +118,15 @@ public class EvernoteUtil {
                return noteList;
        }
 
-       public Note updateNoteContext(String guid, String title, String text)
-                       throws EDAMNotFoundException {
-               Note note = getNote(guid);
+       public Note updateNoteContext(String guid, String title, String text){
+               errorCode = Error.OTHER;
+
+               Note note = null;
+               try {
+                       note = getNote(guid);
+               } catch (EDAMNotFoundException e) {
+                       errorCode = Error.NOT_FOUND;
+               }
                if (note == null) {
                        return null;
                }
@@ -123,14 +152,21 @@ public class EvernoteUtil {
                note.setContent(content);
 
                Note n = updateNote(note);
+
+               errorCode = Error.NONE;
                return n;
        }
 
        public Note updateNote(Note note) {
+               errorCode = Error.OTHER;
                Note n = null;
                try {
                        String token = getAuthenticationToken();
+                       if (token == null){
+                               return null;
+                       }
                        n = noteStore.updateNote(token, note);
+                       errorCode = Error.NONE;
                } catch (EDAMUserException e) {
                        Debug.d(this, null, e);
                } catch (EDAMSystemException e) {
@@ -173,12 +209,18 @@ public class EvernoteUtil {
                                noteStore = new NoteStore.Client(noteStoreProt);
                        }
                } catch (TTransportException e) {
-                       // TODO
                        authenticationToken = null;
-                       noteStore = null; // clear()?
+                       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) {
@@ -187,4 +229,21 @@ public class EvernoteUtil {
 
                return authenticationToken;
        }
+       
+       public Error getErrorCode(){
+               return errorCode;
+       }
+       
+       public String getErrorMessage(Context context, Error code){
+               if (code.equals(Error.NONE)){
+                       return "";
+               }
+               if (code.equals(Error.CONNECT)){
+                       return context.getString(R.string.toastConnect);
+               }
+               if (code.equals(Error.INVALID_AUTH)){
+                       return context.getString(R.string.toastInvalidAuth);
+               }
+               return context.getString(R.string.toastEvernoteSystem);
+       }
 }