فهرست منبع

Fix for stuff

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 سال پیش
والد
کامیت
d014d6b3cd

+ 1 - 1
build.gradle

@@ -184,7 +184,7 @@ dependencies {
     /// dependencies for app building
     implementation name: 'touch-image-view'
     implementation 'com.android.support:multidex:1.0.2'
-    implementation 'com.github.nextcloud:android-library:1.0.28'
+    compile 'com.github.nextcloud:android-library:login-improvements-SNAPSHOT'
     implementation "com.android.support:support-v4:${supportLibraryVersion}"
     implementation "com.android.support:design:${supportLibraryVersion}"
     implementation 'com.jakewharton:disklrucache:2.0.2'

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

@@ -24,22 +24,16 @@ import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.content.Context;
 import android.content.SharedPreferences;
-import android.net.Uri;
 import android.preference.PreferenceManager;
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.lib.common.OwnCloudAccount;
-import com.owncloud.android.lib.common.OwnCloudClient;
-import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
-import com.owncloud.android.lib.common.UserInfo;
 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
-import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
 import com.owncloud.android.operations.GetCapabilitiesOperarion;
 import com.owncloud.android.ui.activity.ManageAccountsActivity;
 
@@ -228,126 +222,7 @@ 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);
-
-        if ( currentAccount != null ) {
-            String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION);
-
-            if (!String.valueOf(ACCOUNT_VERSION_WITH_PROPER_ID).equalsIgnoreCase(currentAccountVersion)) {
-                Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION_WITH_PROPER_ID);
-                Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType());
-                String serverUrl;
-                String username;
-                String newAccountName;
-                String password;
-                Account newAccount;
-                for (Account account : ocAccounts) {
-                    // build new account name
-                    serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);
-
-                    // update user name
-                    try {
-                        OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
-                        OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton()
-                                .getClientFor(ocAccount, context);
-
-                        GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
-                        RemoteOperationResult result = remoteUserNameOperation.execute(client);
-
-                        if (result.isSuccess()) {
-                            UserInfo userInfo = (UserInfo) result.getData().get(0);
-                            username = userInfo.id;
-                        } else {
-                            // skip account, try it next time
-                            Log_OC.e(TAG, "Error while getting username for account: " + account.name);
-                            continue;
-                        }
-                    } catch (Exception e) {
-                        Log_OC.e(TAG, "Error while getting username: " + e.getMessage());
-                        continue;
-                    }
-
-                    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
-                        final String isSamlStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO);
-                        if (Boolean.parseBoolean(isSamlStr)) {
-                            accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");
-                        }
-
-                        final String isOauthStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2);
-                        if (Boolean.parseBoolean(isOauthStr)) {
-                            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.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_WITH_PROPER_ID + " to " + newAccountName);
-                    accountMgr.setUserData(newAccount,
-                            Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION_WITH_PROPER_ID));
-                }
-            }
-        }
-    }
-
-
+    
     public static String trimWebdavSuffix(String url) {
         while(url.endsWith("/")) {
             url = url.substring(0, url.length() - 1);

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

@@ -1914,6 +1914,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         if (isOAuth) {
             username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();
         }
+
         String accountName = com.owncloud.android.lib.common.accounts.AccountUtils.
                 buildAccountName(uri, username);
         Account newAccount = new Account(accountName, MainApp.getAccountType());
@@ -1979,12 +1980,17 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             mAccountMgr.setUserData(
                     mAccount, Constants.KEY_OC_BASE_URL, mServerInfo.mBaseUrl
             );
+
             if (authResult.getData() != null) {
                 try {
                     UserInfo userInfo = (UserInfo) authResult.getData().get(0);
                     mAccountMgr.setUserData(
                             mAccount, Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName()
                     );
+
+                    mAccountMgr.setUserData(
+                            mAccount, Constants.KEY_USER_ID, userInfo.getId()
+                    );
                 } catch (ClassCastException c) {
                     Log_OC.w(TAG, "Couldn't get display name for " + username);
                 }

+ 6 - 0
src/main/java/com/owncloud/android/operations/GetUserProfileOperation.java

@@ -65,6 +65,12 @@ public class GetUserProfileOperation extends SyncOperation {
                 AccountUtils.Constants.KEY_DISPLAY_NAME,
                 userInfo.getDisplayName()
             );
+
+            accountManager.setUserData(
+                    storedAccount,
+                    AccountUtils.Constants.KEY_USER_ID,
+                    userInfo.getId()
+            );
         }
         return result;
     }

+ 21 - 1
src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -39,6 +39,7 @@ import android.support.v4.view.GravityCompat;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.text.Html;
+import android.text.TextUtils;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
@@ -932,8 +933,18 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
         // set user space information
         Thread t = new Thread(new Runnable() {
             public void run() {
+                AccountManager mAccountMgr = AccountManager.get(MainApp.getAppContext());
+
+                String userId = mAccountMgr.getUserData(AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this),
+                        com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
+
+                RemoteOperation getQuotaInfoOperation;
+                if (TextUtils.isEmpty(userId)) {
+                    getQuotaInfoOperation = new GetRemoteUserInfoOperation();
+                } else {
+                    getQuotaInfoOperation = new GetRemoteUserInfoOperation(userId);
+                }
 
-                RemoteOperation getQuotaInfoOperation = new GetRemoteUserInfoOperation();
                 RemoteOperationResult result = getQuotaInfoOperation.execute(
                         AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this), DrawerActivity.this);
 
@@ -941,6 +952,15 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
                     final UserInfo userInfo = (UserInfo) result.getData().get(0);
                     final Quota quota = userInfo.getQuota();
 
+                    // Since we always call this method, might as well put it here
+                    if (userInfo.getId() != null) {
+                        mAccountMgr.setUserData(
+                                AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this),
+                                com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID,
+                                userInfo.getId()
+                        );
+                    }
+
                     if (quota != null) {
                         final long used = quota.getUsed();
                         final long total = quota.getTotal();

+ 0 - 10
src/main/java/com/owncloud/android/ui/activity/FileActivity.java

@@ -38,7 +38,6 @@ import android.widget.Toast;
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
-import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileDownloader;
@@ -156,15 +155,6 @@ public abstract class FileActivity extends DrawerActivity
                     false);
         }
 
-        Thread t = new Thread(new Runnable() {
-            @Override
-            public void run() {
-                // best place, before any access to AccountManager or database
-                AccountUtils.updateAccountVersion(getApplicationContext());
-            }
-        });
-        t.start();
-
         setAccount(account, savedInstanceState != null);
 
         mOperationsServiceConnection = new OperationsServiceConnection();

+ 12 - 1
src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -22,6 +22,7 @@
 package com.owncloud.android.ui.fragment;
 
 import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.accounts.AuthenticatorException;
 import android.accounts.OperationCanceledException;
 import android.app.Activity;
@@ -40,6 +41,7 @@ import android.support.design.widget.BottomNavigationView;
 import android.support.design.widget.Snackbar;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v4.widget.SwipeRefreshLayout;
+import android.text.TextUtils;
 import android.util.SparseBooleanArray;
 import android.view.ActionMode;
 import android.view.LayoutInflater;
@@ -1415,6 +1417,8 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
         Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
 
         OwnCloudAccount ocAccount = null;
+        AccountManager mAccountMgr = AccountManager.get(getActivity());
+
         try {
             ocAccount = new OwnCloudAccount(
                     currentAccount,
@@ -1424,8 +1428,15 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
             OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                     getClientFor(ocAccount, MainApp.getAppContext());
 
+            String userId = mAccountMgr.getUserData(currentAccount,
+                    com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
+
+            if (TextUtils.isEmpty(userId)) {
+                userId = mClient.getCredentials().getUsername();
+            }
+
             ToggleFavoriteOperation toggleFavoriteOperation = new ToggleFavoriteOperation(event.shouldFavorite,
-                    event.remotePath);
+                    event.remotePath, userId);
             RemoteOperationResult remoteOperationResult = toggleFavoriteOperation.execute(mClient);
 
             if (remoteOperationResult.isSuccess()) {