moving from eu.alefzero.eu to com.owncloud.android
[pub/Android/ownCloud.git] / src / com / owncloud / android / AccountUtils.java
diff --git a/src/com/owncloud/android/AccountUtils.java b/src/com/owncloud/android/AccountUtils.java
new file mode 100644 (file)
index 0000000..9c48793
--- /dev/null
@@ -0,0 +1,109 @@
+/* ownCloud Android client application\r
+ *   Copyright (C) 2012  Bartek Przybylski\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
+ *   (at your option) any later version.\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
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ *   GNU General Public License for more details.\r
+ *\r
+ *   You should have received a copy of the GNU General Public License\r
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ */\r
+\r
+package com.owncloud.android;\r
+\r
+import com.owncloud.android.authenticator.AccountAuthenticator;\r
+import com.owncloud.android.utils.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.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
+    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
+    /**\r
+     * Can be used to get the currently selected ownCloud account in the\r
+     * preferences\r
+     * \r
+     * @param context The current appContext\r
+     * @return The current account or first available, if none is available,\r
+     *         then null.\r
+     */\r
+    public static Account getCurrentOwnCloudAccount(Context context) {\r
+        Account[] ocAccounts = AccountManager.get(context).getAccountsByType(\r
+                AccountAuthenticator.ACCOUNT_TYPE);\r
+        Account defaultAccount = null;\r
+\r
+        SharedPreferences appPreferences = PreferenceManager\r
+                .getDefaultSharedPreferences(context);\r
+        String accountName = appPreferences\r
+                .getString("select_oc_account", null);\r
+\r
+        if (accountName != null) {\r
+            for (Account account : ocAccounts) {\r
+                if (account.name.equals(accountName)) {\r
+                    defaultAccount = account;\r
+                    break;\r
+                }\r
+            }\r
+        } else if (ocAccounts.length != 0) {\r
+            // we at least need to take first account as fallback\r
+            defaultAccount = ocAccounts[0];\r
+        }\r
+\r
+        return defaultAccount;\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(AccountAuthenticator.ACCOUNT_TYPE);\r
+        return accounts.length > 0;\r
+    }\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
+    }\r
+\r
+    /**\r
+     * \r
+     * @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
+        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 null;\r
+    }\r
+\r
+}\r