Эх сурвалжийг харах

Upgraded accounts in AccountManager with new account name

David A. Velasco 10 жил өмнө
parent
commit
cc9431feeb

+ 1 - 1
owncloud-android-library

@@ -1 +1 @@
-Subproject commit 2e993f9893739ca4bf0883b881b125030a9cad26
+Subproject commit ddb8648809b7e5c91ec6d081ccdb0597dbebd520

+ 103 - 1
src/com/owncloud/android/authentication/AccountUtils.java

@@ -24,20 +24,28 @@ import java.util.Locale;
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
+import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
+import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
 
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.net.Uri;
 import android.preference.PreferenceManager;
 
 public class AccountUtils {
+
+    private static final String TAG = AccountUtils.class.getSimpleName();
+
     public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/webdav";
     private static final String ODAV_PATH = "/remote.php/odav";
     private static final String SAML_SSO_PATH = "/remote.php/webdav";
     public static final String STATUS_PATH = "/status.php";
 
+    public static final int ACCOUNT_VERSION = 1;
+
     /**
      * Can be used to get the currently selected ownCloud {@link Account} in the
      * application preferences.
@@ -145,5 +153,99 @@ public class AccountUtils {
         }
         return null;
     }
-    
+
+
+    /**
+     * Update the accounts in AccountManager to meet the current version of accounts expected by the app, if needed.
+     *
+     * Introduced to handle a change in the structure of stored account names needed to allow different OC servers
+     * in the same domain, but not in the same path.
+     *
+     * @param   context     Used to access the AccountManager.
+     */
+    public static void updateAccountVersion(Context context) {
+        Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(context);
+        AccountManager accountMgr = AccountManager.get(context);
+
+        String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION);
+
+        if (currentAccountVersion == null) {
+            Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION);
+            Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType());
+            String serverUrl, username, newAccountName, password;
+            Account newAccount;
+            for (Account account : ocAccounts) {
+                // build new account name
+                serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);
+                username = account.name.substring(0, account.name.lastIndexOf('@'));
+                newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils.
+                    buildAccountName(Uri.parse(serverUrl), username);
+
+                // migrate to a new account, if needed
+                if (!newAccountName.equals(account.name)) {
+                    Log_OC.d(TAG, "Upgrading " + account.name + " to " + newAccountName );
+
+                    // create the new account
+                    newAccount = new Account(newAccountName, MainApp.getAccountType());
+                    password = accountMgr.getPassword(account);
+                    accountMgr.addAccountExplicitly(newAccount, (password != null) ? password : "", null);
+
+                    // copy base URL
+                    accountMgr.setUserData(newAccount, Constants.KEY_OC_BASE_URL, serverUrl);
+
+                    // copy server version
+                    accountMgr.setUserData(
+                        newAccount,
+                        Constants.KEY_OC_VERSION,
+                        accountMgr.getUserData(account, Constants.KEY_OC_VERSION)
+                    );
+
+                    // copy cookies
+                    accountMgr.setUserData(
+                        newAccount,
+                        Constants.KEY_COOKIES,
+                        accountMgr.getUserData(account, Constants.KEY_COOKIES)
+                    );
+
+                    // copy type of authentication
+                    String isSamlStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO);
+                    boolean isSaml = "TRUE".equals(isSamlStr);
+                    if (isSaml) {
+                        accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");
+                    }
+
+                    String isOauthStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2);
+                    boolean isOAuth = "TRUE".equals(isOauthStr);
+                    if (isOAuth) {
+                        accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");
+                    }
+                    /* TODO - study if it's possible to run this method in a background thread to copy the authToken
+                    if (isOAuth || isSaml) {
+                        accountMgr.setAuthToken(newAccount, mAuthTokenType, mAuthToken);
+                    }
+                    */
+
+                    // don't forget the account saved in preferences as the current one
+                    if (currentAccount != null && currentAccount.name.equals(account.name)) {
+                        AccountUtils.setCurrentOwnCloudAccount(context, newAccountName);
+                    }
+
+                    // remove the old account
+                    accountMgr.removeAccount(account, null, null);  // will assume it succeeds, not a big deal otherwise
+
+                } else {
+                    // servers which base URL is in the root of their domain need no change
+                    Log_OC.d(TAG, account.name + " needs no upgrade ");
+                    newAccount = account;
+                }
+
+                // at least, upgrade account version
+                Log_OC.d(TAG, "Setting version " + ACCOUNT_VERSION + " to " + newAccountName);
+                accountMgr.setUserData(newAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION));
+
+            }
+        }
+    }
+
+
 }

+ 7 - 0
src/com/owncloud/android/authentication/AuthenticatorActivity.java

@@ -1483,6 +1483,13 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
                 );
             }
 
+            // include account version with the new account
+            mAccountMgr.setUserData(
+                mAccount,
+                Constants.KEY_OC_ACCOUNT_VERSION,
+                Integer.toString(AccountUtils.ACCOUNT_VERSION)
+            );
+
             /// add the new account as default in preferences, if there is none already
             Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
             if (defaultAccount == null) {

+ 1 - 1
src/com/owncloud/android/providers/FileContentProvider.java

@@ -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);

+ 2 - 2
src/com/owncloud/android/ui/activity/FileActivity.java

@@ -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