瀏覽代碼

Fix bug: when installing the app for the first time, app crashes

masensio 10 年之前
父節點
當前提交
98f73400ca
共有 1 個文件被更改,包括 68 次插入66 次删除
  1. 68 66
      src/com/owncloud/android/authentication/AccountUtils.java

+ 68 - 66
src/com/owncloud/android/authentication/AccountUtils.java

@@ -167,82 +167,84 @@ public class AccountUtils {
         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");
-                    }
+        if ( currentAccount != null ) {
+            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);
-                    }
+                        // 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
+                        // 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;
-                }
+                    } 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));
+                    // 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));
 
+                }
             }
         }
     }