Merge pull request #186 from owncloud/fixed_contradicted_messages_in_login_view
[pub/Android/ownCloud.git] / src / com / owncloud / android / AccountUtils.java
index 06ab31a..7297ee1 100644 (file)
@@ -1,10 +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
- *   (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
@@ -18,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
@@ -37,12 +37,13 @@ public class AccountUtils {
     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
+     * Can be used to get the currently selected ownCloud {@link Account} in the\r
+     * application preferences.\r
      * \r
-     * @param context The current appContext\r
-     * @return The current account or first available, if none is available,\r
-     *         then null.\r
+     * @param   context     The current application {@link Context}\r
+     * @return              The ownCloud {@link Account} currently saved in preferences, or the first \r
+     *                      {@link Account} available, if valid (still registered in the system as ownCloud \r
+     *                      account). If none is available and valid, returns null.\r
      */\r
     public static Account getCurrentOwnCloudAccount(Context context) {\r
         Account[] ocAccounts = AccountManager.get(context).getAccountsByType(\r
@@ -54,6 +55,7 @@ public class AccountUtils {
         String accountName = appPreferences\r
                 .getString("select_oc_account", null);\r
 \r
+        // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager\r
         if (accountName != null) {\r
             for (Account account : ocAccounts) {\r
                 if (account.name.equals(accountName)) {\r
@@ -61,8 +63,10 @@ public class AccountUtils {
                     break;\r
                 }\r
             }\r
-        } else if (ocAccounts.length != 0) {\r
-            // we at least need to take first account as fallback\r
+        }\r
+        \r
+        if (defaultAccount == null && ocAccounts.length != 0) {\r
+            // take first account as fallback\r
             defaultAccount = ocAccounts[0];\r
         }\r
 \r
@@ -84,11 +88,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