Refresh authorization token on failed downloads / uploads when notification error...
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / AccountAuthenticator.java
index 391e9e8..a42c1f2 100644 (file)
@@ -1,9 +1,10 @@
 /* ownCloud Android client application\r
  *   Copyright (C) 2012  Bartek Przybylski\r
+ *   Copyright (C) 2012-2013 ownCloud Inc.\r
  *\r
  *   This program is free software: you can redistribute it and/or modify\r
  *   it under the terms of the GNU General Public License as published by\r
- *   the Free Software Foundation, either version 3 of the License, or\r
+ *   the Free Software Foundation, either version 2 of the License, or\r
  *   (at your option) any later version.\r
  *\r
  *   This program is distributed in the hope that it will be useful,\r
@@ -32,6 +33,7 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
      * used by application and all extensions.\r
      */\r
     public static final String ACCOUNT_TYPE = "owncloud";\r
+    public static final String AUTHORITY = "org.owncloud";\r
     public static final String AUTH_TOKEN_TYPE = "org.owncloud";\r
     public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";\r
     public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";\r
@@ -93,13 +95,14 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
             return e.getFailureBundle();\r
         }\r
         final Intent intent = new Intent(mContext, AuthenticatorActivity.class);\r
-        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,\r
-                response);\r
+        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);\r
         intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);\r
         intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);\r
         intent.putExtra(KEY_LOGIN_OPTIONS, options);\r
+        intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_CREATE);\r
 \r
         setIntentFlags(intent);\r
+        \r
         final Bundle bundle = new Bundle();\r
         bundle.putParcelable(AccountManager.KEY_INTENT, intent);\r
         return bundle;\r
@@ -138,10 +141,14 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
         return null;\r
     }\r
 \r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
     @Override\r
     public Bundle getAuthToken(AccountAuthenticatorResponse response,\r
             Account account, String authTokenType, Bundle options)\r
             throws NetworkErrorException {\r
+        /// validate parameters\r
         try {\r
             validateAccountType(account.type);\r
             validateAuthTokenType(authTokenType);\r
@@ -151,34 +158,31 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
             e.printStackTrace();\r
             return e.getFailureBundle();\r
         }\r
+        \r
+        /// check if required token is stored\r
         final AccountManager am = AccountManager.get(mContext);\r
-        if (authTokenType.equals(AUTH_TOKEN_TYPE_ACCESS_TOKEN)) {\r
-            final String accessToken = am.peekAuthToken(account, authTokenType);\r
-            if (accessToken != null) {\r
-                final Bundle result = new Bundle();\r
-                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
-                result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);\r
-                result.putString(AccountManager.KEY_AUTHTOKEN, accessToken);\r
-                return result;\r
-            }\r
-            \r
-        } else if (authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD)) {\r
-            final String password = am.getPassword(account);\r
-            if (password != null) {\r
-                final Bundle result = new Bundle();\r
-                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
-                result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);\r
-                result.putString(AccountManager.KEY_AUTHTOKEN, password);\r
-                return result;\r
-            }\r
+        String accessToken;\r
+        if (authTokenType.equals(AUTH_TOKEN_TYPE_PASSWORD)) {\r
+            accessToken = am.getPassword(account);\r
+        } else {\r
+            accessToken = am.peekAuthToken(account, authTokenType);\r
         }\r
-\r
+        if (accessToken != null) {\r
+            final Bundle result = new Bundle();\r
+            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);\r
+            result.putString(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);\r
+            result.putString(AccountManager.KEY_AUTHTOKEN, accessToken);\r
+            return result;\r
+        }\r
+        \r
+        /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account\r
         final Intent intent = new Intent(mContext, AuthenticatorActivity.class);\r
-        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,\r
-                response);\r
+        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);\r
         intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);\r
         intent.putExtra(KEY_LOGIN_OPTIONS, options);\r
-        intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);    // TODO fix, this will pass the accountName, not the username\r
+        intent.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);\r
+        intent.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_TOKEN);\r
+        \r
 \r
         final Bundle bundle = new Bundle();\r
         bundle.putParcelable(AccountManager.KEY_INTENT, intent);\r
@@ -194,7 +198,7 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
     public Bundle hasFeatures(AccountAuthenticatorResponse response,\r
             Account account, String[] features) throws NetworkErrorException {\r
         final Bundle result = new Bundle();\r
-        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);\r
+        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);\r
         return result;\r
     }\r
 \r