Merge pull request #152 from owncloud/oauth_login
[pub/Android/ownCloud.git] / src / com / owncloud / android / AccountUtils.java
index f38b2e7..a1d39ae 100644 (file)
@@ -3,9 +3,8 @@
  *   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 2 of the License, or\r
- *   (at your option) any later version.\r
+ *   it under the terms of the GNU General Public License version 2,\r
+ *   as published by the Free Software Foundation.\r
  *\r
  *   This program is distributed in the hope that it will be useful,\r
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
@@ -19,7 +18,7 @@
 \r
 package com.owncloud.android;\r
 \r
-import com.owncloud.android.authenticator.AccountAuthenticator;\r
+import com.owncloud.android.authentication.AccountAuthenticator;\r
 import com.owncloud.android.utils.OwnCloudVersion;\r
 \r
 import android.accounts.Account;\r
@@ -32,6 +31,7 @@ public class AccountUtils {
     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
+    private static final String ODAV_PATH = "/remote.php/odav";\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
@@ -61,7 +61,9 @@ public class AccountUtils {
                     break;\r
                 }\r
             }\r
-        } else if (ocAccounts.length != 0) {\r
+        }\r
+        \r
+        if (defaultAccount == null && ocAccounts.length != 0) {\r
             // we at least need to take first account as fallback\r
             defaultAccount = ocAccounts[0];\r
         }\r
@@ -84,11 +86,26 @@ public class AccountUtils {
     }\r
     \r
     \r
-    public static void setCurrentOwnCloudAccount(Context context, String name) {\r
-        SharedPreferences.Editor appPrefs = PreferenceManager\r
-                .getDefaultSharedPreferences(context).edit();\r
-        appPrefs.putString("select_oc_account", name);\r
-        appPrefs.commit();\r
+    public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {\r
+        boolean result = false;\r
+        if (accountName != null) {\r
+            Account[] ocAccounts = AccountManager.get(context).getAccountsByType(\r
+                    AccountAuthenticator.ACCOUNT_TYPE);\r
+            boolean found = false;\r
+            for (Account account : ocAccounts) {\r
+                found = (account.name.equals(accountName));\r
+                if (found) {\r
+                    SharedPreferences.Editor appPrefs = PreferenceManager\r
+                            .getDefaultSharedPreferences(context).edit();\r
+                    appPrefs.putString("select_oc_account", accountName);\r
+    \r
+                    appPrefs.commit();\r
+                    result = true;\r
+                    break;\r
+                }\r
+            }\r
+        }\r
+        return result;\r
     }\r
 \r
     /**\r
@@ -96,8 +113,11 @@ public class AccountUtils {
      * @param version version of owncloud\r
      * @return webdav path for given OC version, null if OC version unknown\r
      */\r
-    public static String getWebdavPath(OwnCloudVersion version) {\r
+    public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) {\r
         if (version != null) {\r
+            if (supportsOAuth) {\r
+                return ODAV_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
@@ -120,8 +140,9 @@ public class AccountUtils {
             AccountManager ama = AccountManager.get(context);\r
             String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
             String strver  = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);\r
+            boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
             OwnCloudVersion ver = new OwnCloudVersion(strver);\r
-            String webdavpath = getWebdavPath(ver);\r
+            String webdavpath = getWebdavPath(ver, supportsOAuth);\r
 \r
             if (webdavpath == null) return null;\r
             return baseurl + webdavpath;\r