OSDN Git Service

T29296
authorYuji Konishi <yuji.k64613@gmail.com>
Sat, 22 Sep 2012 04:59:47 +0000 (13:59 +0900)
committerYuji Konishi <yuji.k64613@gmail.com>
Sat, 22 Sep 2012 04:59:47 +0000 (13:59 +0900)
source/workspace/EverFolder/src/com/yuji/ef/Initialize.java
source/workspace/EverFolder/src/com/yuji/ef/OAuthActivity.java
source/workspace/EverFolder/src/com/yuji/ef/common/Constant.java
source/workspace/EverFolder/src/com/yuji/ef/utility/OAuthUtil.java

index c2e4d5d..6a438b4 100644 (file)
@@ -31,7 +31,8 @@ public class Initialize {
                        pref.get(Constant.PREF_UPDATE_DATETIME, "");
                        pref.getInt(Constant.PREF_DELETE_LOCK, 0);
                        pref.getInt(Constant.PREF_UPDATE_DATA, Constant.OFF);
-                       pref.getLong(Constant.PREF_NEXT_TIME, 0);
+                       pref.getLong(Constant.PREF_NEXT_TIME, 0L);
+                       pref.getLong(Constant.PREF_LOGIN_DATE, 0L);
                        pref.update();
                        
                        DatabaseHelper.addDao(NodeCacheDao.getInstance());
index 96269d4..9785d81 100644 (file)
 package com.yuji.ef;
 
+import java.util.Calendar;
+
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Toast;
 
 import com.evernote.client.oauth.android.EvernoteSession;
+import com.yuji.ef.common.Constant;
+import com.yuji.ef.pref.EditPrefUtil;
 import com.yuji.ef.utility.BaseActivity;
 import com.yuji.ef.utility.EvernoteUtil;
 import com.yuji.ef.utility.OAuthUtil;
 
 /**
- * This simple Android app demonstrates how to integrate with the 
- * Evernote Cloud API (aka EDAM) to create a note.
+ * 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.
+ * 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();
-    
+
+       /***************************************************************************
+        * 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
-       if (isInit) {
-               isInit = false;
-       } else {
-               if (isCompleteAuth) {
-                       EvernoteUtil.getInstance().setSession(util.getSession());
+       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));
                }
-               finish();
+               session.authenticate(this);
+
+               updateUi();
        }
-  }
-
-  /**
-   * 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);
-  //}
+
+       /**
+        * 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 d1a3c2d..3bda139 100644 (file)
@@ -13,6 +13,7 @@ public class Constant {
        public static final String PREF_UPDATE_DATETIME = "UPDATE_DATETIME";
        public static final String PREF_DELETE_LOCK = "DELETE_LOCK";
        public static final String PREF_NEXT_TIME = "NEXT_TIME";
+       public static final String PREF_LOGIN_DATE = "LOGIN_DATE";
 
        public static final String LOCK_UPDATE_NOTE = "UPDATE_NOTE";
 
index 2cc7a69..e50793a 100644 (file)
@@ -1,6 +1,7 @@
 package com.yuji.ef.utility;
 
 import java.io.File;
+import java.util.Calendar;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
@@ -12,6 +13,8 @@ import android.os.Environment;
 
 import com.evernote.client.conn.ApplicationInfo;
 import com.evernote.client.oauth.android.EvernoteSession;
+import com.yuji.ef.common.Constant;
+import com.yuji.ef.pref.EditPrefUtil;
 import com.yuji.ef.pref.PrefUtil;
 import com.yuji.ef.pref.PrefUtilImpl;
 
@@ -45,6 +48,24 @@ public class OAuthUtil {
                // 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;
        }
 
@@ -72,5 +93,17 @@ public class OAuthUtil {
 
        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();
        }
 }