Upgraded accounts in AccountManager with new account name
authorDavid A. Velasco <dvelasco@solidgear.es>
Wed, 15 Apr 2015 10:52:07 +0000 (12:52 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Wed, 15 Apr 2015 10:52:07 +0000 (12:52 +0200)
owncloud-android-library
src/com/owncloud/android/authentication/AccountUtils.java
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/providers/FileContentProvider.java
src/com/owncloud/android/ui/activity/FileActivity.java

index 2e993f9..ddb8648 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 2e993f9893739ca4bf0883b881b125030a9cad26
+Subproject commit ddb8648809b7e5c91ec6d081ccdb0597dbebd520
index bb2315d..a22164f 100644 (file)
@@ -24,20 +24,28 @@ import java.util.Locale;
 \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
@@ -145,5 +153,99 @@ public class AccountUtils {
         }\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
index 6514ef7..f245900 100644 (file)
@@ -1483,6 +1483,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                 );\r
             }\r
 \r
+            // include account version with the new account\r
+            mAccountMgr.setUserData(\r
+                mAccount,\r
+                Constants.KEY_OC_ACCOUNT_VERSION,\r
+                Integer.toString(AccountUtils.ACCOUNT_VERSION)\r
+            );\r
+\r
             /// add the new account as default in preferences, if there is none already\r
             Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);\r
             if (defaultAccount == null) {\r
index b50609a..b36c5b5 100644 (file)
@@ -843,7 +843,7 @@ public class FileContentProvider extends ContentProvider {
                        Account[] accounts = AccountManager.get(getContext()).getAccountsByType(MainApp.getAccountType());
             String serverUrl, username, oldAccountName;
                        for (Account account : accounts) {
-                // build new account name
+                // build old account name
                 serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
                 username = account.name.substring(0, account.name.lastIndexOf('@'));
                 oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
index a566ccd..e3af836 100644 (file)
@@ -61,7 +61,6 @@ import com.owncloud.android.operations.SynchronizeFolderOperation;
 import com.owncloud.android.operations.UnshareLinkOperation;
 import com.owncloud.android.services.OperationsService;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
-import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
 import com.owncloud.android.ui.dialog.LoadingDialog;
 import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
 import com.owncloud.android.utils.ErrorMessageAdapter;
@@ -152,6 +151,8 @@ public class FileActivity extends SherlockFragmentActivity
             mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
         }
 
+        AccountUtils.updateAccountVersion(this); // best place, before any access to AccountManager or database
+
         setAccount(account, savedInstanceState != null);
         
         mOperationsServiceConnection = new OperationsServiceConnection();
@@ -168,7 +169,6 @@ public class FileActivity extends SherlockFragmentActivity
 
     }
 
-    
     /**
      *  Since ownCloud {@link Account}s can be managed from the system setting menu, 
      *  the existence of the {@link Account} associated to the instance must be checked