\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 android.net.Uri;\r
import android.preference.PreferenceManager;\r
\r
public class AccountUtils {\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
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
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
+ 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