new stuff for account authenticator
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / AccountUtils.java
diff --git a/src/eu/alefzero/owncloud/AccountUtils.java b/src/eu/alefzero/owncloud/AccountUtils.java
new file mode 100644 (file)
index 0000000..4077775
--- /dev/null
@@ -0,0 +1,61 @@
+/* ownCloud Android client application\r
+ *   Copyright (C) 2011  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 eu.alefzero.owncloud;\r
+\r
+import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\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 CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";\r
+  \r
+  /**\r
+   * Can be used to get the currently selected ownCloud account in the preferences\r
+   * \r
+   * @param context The current appContext\r
+   * @return The current account or null, if there is none yet.\r
+   */\r
+  public static Account getCurrentOwnCloudAccount(Context context){\r
+         Account[] ocAccounts = AccountManager.get(context).getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);\r
+         Account defaultAccount = null;\r
+         \r
+         SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);\r
+         String accountName = appPreferences.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