Filter only '/' character in user input when version of server is 8.1 or later
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AccountUtils.java
index bd3a8e7..1e67aa6 100644 (file)
@@ -24,24 +24,28 @@ import java.util.Locale;
 \r
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
 \r
 import com.owncloud.android.MainApp;\r
 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
+import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
+import com.owncloud.android.lib.common.utils.Log_OC;\r
 import com.owncloud.android.lib.resources.status.OwnCloudVersion;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import android.content.Context;\r
 import android.content.SharedPreferences;\r
 import com.owncloud.android.lib.resources.status.OwnCloudVersion;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
 import android.content.Context;\r
 import android.content.SharedPreferences;\r
+import android.net.Uri;\r
 import android.preference.PreferenceManager;\r
 \r
 public class AccountUtils {\r
 import android.preference.PreferenceManager;\r
 \r
 public class AccountUtils {\r
-    public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";\r
-    public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";\r
-    public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";\r
+\r
+    private static final String TAG = AccountUtils.class.getSimpleName();\r
+\r
+    public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/webdav";\r
     private static final String ODAV_PATH = "/remote.php/odav";\r
     private static final String SAML_SSO_PATH = "/remote.php/webdav";\r
     private static final String ODAV_PATH = "/remote.php/odav";\r
     private static final String SAML_SSO_PATH = "/remote.php/webdav";\r
-    public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";\r
-    public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";\r
     public static final String STATUS_PATH = "/status.php";\r
 \r
     public static final String STATUS_PATH = "/status.php";\r
 \r
+    public static final int ACCOUNT_VERSION = 1;\r
+\r
     /**\r
      * Can be used to get the currently selected ownCloud {@link Account} in the\r
      * application preferences.\r
     /**\r
      * Can be used to get the currently selected ownCloud {@link Account} in the\r
      * application preferences.\r
@@ -105,19 +109,6 @@ public class AccountUtils {
     }\r
     \r
 \r
     }\r
     \r
 \r
-    /**\r
-     * Checks, whether or not there are any ownCloud accounts setup.\r
-     * \r
-     * @return true, if there is at least one account.\r
-     */\r
-    public static boolean accountsAreSetup(Context context) {\r
-        AccountManager accMan = AccountManager.get(context);\r
-        Account[] accounts = accMan\r
-                .getAccountsByType(MainApp.getAccountType());\r
-        return accounts.length > 0;\r
-    }\r
-    \r
-    \r
     public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {\r
         boolean result = false;\r
         if (accountName != null) {\r
     public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {\r
         boolean result = false;\r
         if (accountName != null) {\r
@@ -145,8 +136,10 @@ public class AccountUtils {
      * according to its version and the authorization method used.\r
      * \r
      * @param   version         Version of ownCloud server.\r
      * according to its version and the authorization method used.\r
      * \r
      * @param   version         Version of ownCloud server.\r
-     * @param   authTokenType   Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}. \r
-     * @return                  WebDAV path for given OC version and authorization method, null if OC version is unknown.\r
+     * @param   authTokenType   Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in\r
+     *                          {@link AccountAuthenticator}.\r
+     * @return                  WebDAV path for given OC version and authorization method, null if OC version\r
+     *                          is unknown; versions prior to ownCloud 4 are not supported anymore\r
      */\r
     public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {\r
         if (version != null) {\r
      */\r
     public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {\r
         if (version != null) {\r
@@ -156,15 +149,105 @@ public class AccountUtils {
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) {\r
                 return SAML_SSO_PATH;\r
             }\r
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) {\r
                 return SAML_SSO_PATH;\r
             }\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)\r
-                return WEBDAV_PATH_4_0;\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0\r
-                    || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)\r
-                return WEBDAV_PATH_2_0;\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)\r
-                return WEBDAV_PATH_1_2;\r
+            return WEBDAV_PATH_4_0_AND_LATER;\r
         }\r
         return null;\r
     }\r
         }\r
         return null;\r
     }\r
-    \r
+\r
+\r
+    /**\r
+     * Update the accounts in AccountManager to meet the current version of accounts expected by the app, if needed.\r
+     *\r
+     * Introduced to handle a change in the structure of stored account names needed to allow different OC servers\r
+     * in the same domain, but not in the same path.\r
+     *\r
+     * @param   context     Used to access the AccountManager.\r
+     */\r
+    public static void updateAccountVersion(Context context) {\r
+        Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(context);\r
+        AccountManager accountMgr = AccountManager.get(context);\r
+\r
+        if ( currentAccount != null ) {\r
+            String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION);\r
+\r
+            if (currentAccountVersion == null) {\r
+                Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION);\r
+                Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType());\r
+                String serverUrl, username, newAccountName, password;\r
+                Account newAccount;\r
+                for (Account account : ocAccounts) {\r
+                    // build new account name\r
+                    serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);\r
+                    username = account.name.substring(0, account.name.lastIndexOf('@'));\r
+                    newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils.\r
+                            buildAccountName(Uri.parse(serverUrl), username);\r
+\r
+                    // migrate to a new account, if needed\r
+                    if (!newAccountName.equals(account.name)) {\r
+                        Log_OC.d(TAG, "Upgrading " + account.name + " to " + newAccountName);\r
+\r
+                        // create the new account\r
+                        newAccount = new Account(newAccountName, MainApp.getAccountType());\r
+                        password = accountMgr.getPassword(account);\r
+                        accountMgr.addAccountExplicitly(newAccount, (password != null) ? password : "", null);\r
+\r
+                        // copy base URL\r
+                        accountMgr.setUserData(newAccount, Constants.KEY_OC_BASE_URL, serverUrl);\r
+\r
+                        // copy server version\r
+                        accountMgr.setUserData(\r
+                                newAccount,\r
+                                Constants.KEY_OC_VERSION,\r
+                                accountMgr.getUserData(account, Constants.KEY_OC_VERSION)\r
+                        );\r
+\r
+                        // copy cookies\r
+                        accountMgr.setUserData(\r
+                                newAccount,\r
+                                Constants.KEY_COOKIES,\r
+                                accountMgr.getUserData(account, Constants.KEY_COOKIES)\r
+                        );\r
+\r
+                        // copy type of authentication\r
+                        String isSamlStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO);\r
+                        boolean isSaml = "TRUE".equals(isSamlStr);\r
+                        if (isSaml) {\r
+                            accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");\r
+                        }\r
+\r
+                        String isOauthStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2);\r
+                        boolean isOAuth = "TRUE".equals(isOauthStr);\r
+                        if (isOAuth) {\r
+                            accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");\r
+                        }\r
+                    /* TODO - study if it's possible to run this method in a background thread to copy the authToken\r
+                    if (isOAuth || isSaml) {\r
+                        accountMgr.setAuthToken(newAccount, mAuthTokenType, mAuthToken);\r
+                    }\r
+                    */\r
+\r
+                        // don't forget the account saved in preferences as the current one\r
+                        if (currentAccount != null && currentAccount.name.equals(account.name)) {\r
+                            AccountUtils.setCurrentOwnCloudAccount(context, newAccountName);\r
+                        }\r
+\r
+                        // remove the old account\r
+                        accountMgr.removeAccount(account, null, null);  // will assume it succeeds, not a big deal otherwise\r
+\r
+                    } else {\r
+                        // servers which base URL is in the root of their domain need no change\r
+                        Log_OC.d(TAG, account.name + " needs no upgrade ");\r
+                        newAccount = account;\r
+                    }\r
+\r
+                    // at least, upgrade account version\r
+                    Log_OC.d(TAG, "Setting version " + ACCOUNT_VERSION + " to " + newAccountName);\r
+                    accountMgr.setUserData(newAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION));\r
+\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+\r
 }\r
 }\r