Search in sources :

Example 1 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class AbstractIT method beforeAll.

@BeforeClass
public static void beforeAll() {
    try {
        // clean up
        targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        AccountManager platformAccountManager = AccountManager.get(targetContext);
        for (Account account : platformAccountManager.getAccounts()) {
            if (account.type.equalsIgnoreCase("nextcloud")) {
                platformAccountManager.removeAccountExplicitly(account);
            }
        }
        Account temp = new Account("test@https://nextcloud.localhost", MainApp.getAccountType(targetContext));
        platformAccountManager.addAccountExplicitly(temp, "password", null);
        platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_BASE_URL, "https://nextcloud.localhost");
        platformAccountManager.setUserData(temp, KEY_USER_ID, "test");
        final UserAccountManager userAccountManager = UserAccountManagerImpl.fromContext(targetContext);
        account = userAccountManager.getAccountByName("test@https://nextcloud.localhost");
        if (account == null) {
            throw new ActivityNotFoundException();
        }
        Optional<User> optionalUser = userAccountManager.getUser(account.name);
        user = optionalUser.orElseThrow(IllegalAccessError::new);
        client = OwnCloudClientFactory.createOwnCloudClient(account, targetContext);
        nextcloudClient = OwnCloudClientFactory.createNextcloudClient(account, targetContext);
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (AccountUtils.AccountNotFoundException e) {
        e.printStackTrace();
    }
    Bundle arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments();
    // color
    String colorParameter = arguments.getString("COLOR");
    if (!TextUtils.isEmpty(colorParameter)) {
        FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, targetContext.getContentResolver());
        String colorHex = null;
        COLOR = colorParameter;
        switch(colorParameter) {
            case "red":
                colorHex = "#7c0000";
                break;
            case "green":
                colorHex = "#00ff00";
                break;
            case "white":
                colorHex = "#ffffff";
                break;
            case "black":
                colorHex = "#000000";
                break;
            case "lightgreen":
                colorHex = "#aaff00";
                break;
            default:
                break;
        }
        if (colorHex != null) {
            OCCapability capability = fileDataStorageManager.getCapability(account.name);
            capability.setServerColor(colorHex);
            fileDataStorageManager.saveCapabilities(capability);
        }
    }
    // dark / light
    String darkModeParameter = arguments.getString("DARKMODE");
    if (darkModeParameter != null) {
        if (darkModeParameter.equalsIgnoreCase("dark")) {
            DARK_MODE = "dark";
            AppPreferencesImpl.fromContext(targetContext).setDarkThemeMode(DarkMode.DARK);
            MainApp.setAppTheme(DarkMode.DARK);
        } else {
            DARK_MODE = "light";
        }
    }
    if (DARK_MODE.equalsIgnoreCase("light") && COLOR.equalsIgnoreCase("blue")) {
        // use already existing names
        DARK_MODE = "";
        COLOR = "";
    }
}
Also used : Account(android.accounts.Account) User(com.nextcloud.client.account.User) OCCapability(com.owncloud.android.lib.resources.status.OCCapability) Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) AccountUtils(com.owncloud.android.lib.common.accounts.AccountUtils) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException) UserAccountManager(com.nextcloud.client.account.UserAccountManager) ActivityNotFoundException(android.content.ActivityNotFoundException) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) UserAccountManager(com.nextcloud.client.account.UserAccountManager) AccountManager(android.accounts.AccountManager) BeforeClass(org.junit.BeforeClass)

Example 2 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class AbstractOnServerIT method beforeAll.

@BeforeClass
public static void beforeAll() {
    try {
        // clean up
        targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        AccountManager platformAccountManager = AccountManager.get(targetContext);
        for (Account account : platformAccountManager.getAccounts()) {
            if (account.type.equalsIgnoreCase("nextcloud")) {
                platformAccountManager.removeAccountExplicitly(account);
            }
        }
        Bundle arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments();
        Uri baseUrl = Uri.parse(arguments.getString("TEST_SERVER_URL"));
        String loginName = arguments.getString("TEST_SERVER_USERNAME");
        String password = arguments.getString("TEST_SERVER_PASSWORD");
        Account temp = new Account(loginName + "@" + baseUrl, MainApp.getAccountType(targetContext));
        UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
        if (!accountManager.exists(temp)) {
            platformAccountManager.addAccountExplicitly(temp, password, null);
            platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(UserAccountManager.ACCOUNT_VERSION));
            platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_VERSION, "14.0.0.0");
            platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_BASE_URL, baseUrl.toString());
            // same as userId
            platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_USER_ID, loginName);
        }
        final UserAccountManager userAccountManager = UserAccountManagerImpl.fromContext(targetContext);
        account = userAccountManager.getAccountByName(loginName + "@" + baseUrl);
        if (account == null) {
            throw new ActivityNotFoundException();
        }
        Optional<User> optionalUser = userAccountManager.getUser(account.name);
        user = optionalUser.orElseThrow(IllegalAccessError::new);
        client = OwnCloudClientFactory.createOwnCloudClient(account, targetContext);
        createDummyFiles();
        waitForServer(client, baseUrl);
        // makes sure that no file/folder is in root
        deleteAllFiles();
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (AccountUtils.AccountNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : Account(android.accounts.Account) User(com.nextcloud.client.account.User) Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) AccountUtils(com.owncloud.android.lib.common.accounts.AccountUtils) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException) Uri(android.net.Uri) UserAccountManager(com.nextcloud.client.account.UserAccountManager) ActivityNotFoundException(android.content.ActivityNotFoundException) UserAccountManager(com.nextcloud.client.account.UserAccountManager) AccountManager(android.accounts.AccountManager) BeforeClass(org.junit.BeforeClass)

Example 3 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class ManageAccountsActivityIT method userInfoDetail.

@Test
@ScreenshotTest
public void userInfoDetail() {
    ManageAccountsActivity sut = activityRule.launchActivity(null);
    User user = sut.accountManager.getUser();
    UserInfo userInfo = new UserInfo("test", true, "Test User", "test@nextcloud.com", "+49 123 456", "Address 123, Berlin", "https://www.nextcloud.com", "https://twitter.com/Nextclouders", new Quota(), new ArrayList<>());
    sut.showUser(user, userInfo);
    shortSleep();
    shortSleep();
    screenshot(getCurrentActivity());
}
Also used : User(com.nextcloud.client.account.User) Quota(com.owncloud.android.lib.common.Quota) UserInfo(com.owncloud.android.lib.common.UserInfo) ScreenshotTest(com.owncloud.android.utils.ScreenshotTest) Test(org.junit.Test) ScreenshotTest(com.owncloud.android.utils.ScreenshotTest)

Example 4 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class DialogFragmentIT method testAccountChooserDialogWithStatusDisabled.

@Test
@ScreenshotTest
public void testAccountChooserDialogWithStatusDisabled() throws AccountUtils.AccountNotFoundException {
    AccountManager accountManager = AccountManager.get(targetContext);
    for (Account account : accountManager.getAccounts()) {
        accountManager.removeAccountExplicitly(account);
    }
    Account newAccount = new Account("test@https://nextcloud.localhost", MainApp.getAccountType(targetContext));
    accountManager.addAccountExplicitly(newAccount, "password", null);
    accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_OC_BASE_URL, SERVER_URL);
    accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_USER_ID, "test");
    accountManager.setAuthToken(newAccount, AccountTypeUtils.getAuthTokenTypePass(newAccount.type), "password");
    FileDisplayActivity fda = getFileDisplayActivity();
    UserAccountManager userAccountManager = fda.getUserAccountManager();
    User newUser = userAccountManager.getUser(newAccount.name).get();
    FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(newUser, targetContext.getContentResolver());
    OCCapability capability = new OCCapability();
    capability.setUserStatus(CapabilityBooleanType.FALSE);
    fileDataStorageManager.saveCapabilities(capability);
    ChooseAccountDialogFragment sut = ChooseAccountDialogFragment.newInstance(new RegisteredUser(newAccount, new OwnCloudAccount(newAccount, targetContext), new Server(URI.create(SERVER_URL), OwnCloudVersion.nextcloud_20)));
    showDialog(fda, sut);
}
Also used : UserAccountManager(com.nextcloud.client.account.UserAccountManager) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) FileDisplayActivity(com.owncloud.android.ui.activity.FileDisplayActivity) User(com.nextcloud.client.account.User) RegisteredUser(com.nextcloud.client.account.RegisteredUser) OCCapability(com.owncloud.android.lib.resources.status.OCCapability) Server(com.nextcloud.client.account.Server) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ChooseAccountDialogFragment(com.nextcloud.ui.ChooseAccountDialogFragment) UserAccountManager(com.nextcloud.client.account.UserAccountManager) AccountManager(android.accounts.AccountManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) RegisteredUser(com.nextcloud.client.account.RegisteredUser) ScreenshotTest(com.owncloud.android.utils.ScreenshotTest) Test(org.junit.Test) ScreenshotTest(com.owncloud.android.utils.ScreenshotTest)

Example 5 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class AuthenticatorActivity method createAccount.

/**
 * Creates a new account through the Account Authenticator that started this activity.
 * <p>
 * This makes the account permanent.
 * <p>
 * TODO Decide how to name the OAuth accounts
 */
@SuppressFBWarnings("DMI")
@SuppressLint("TrulyRandom")
protected boolean createAccount(RemoteOperationResult<UserInfo> authResult) {
    String accountType = MainApp.getAccountType(this);
    // create and save new ownCloud account
    String lastPermanentLocation = authResult.getLastPermanentLocation();
    if (lastPermanentLocation != null) {
        mServerInfo.mBaseUrl = AuthenticatorUrlUtils.trimWebdavSuffix(lastPermanentLocation);
    }
    Uri uri = Uri.parse(mServerInfo.mBaseUrl);
    // used for authenticate on every login/network connection, determined by first login (weblogin/old login)
    // can be anything: email, name, name with whitespaces
    String loginName = webViewUser;
    String accountName = com.owncloud.android.lib.common.accounts.AccountUtils.buildAccountName(uri, loginName);
    Account newAccount = new Account(accountName, accountType);
    if (accountManager.exists(newAccount)) {
        // fail - not a new account, but an existing one; disallow
        RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_NEW);
        updateAuthStatusIconAndText(result);
        showAuthStatus();
        Log_OC.d(TAG, result.getLogMessage());
        return false;
    } else {
        UserInfo userInfo = authResult.getResultData();
        if (userInfo == null) {
            Log_OC.e(this, "Could not read user data!");
            return false;
        }
        mAccount = newAccount;
        mAccountMgr.addAccountExplicitly(mAccount, webViewPassword, null);
        mAccountMgr.notifyAccountAuthenticated(mAccount);
        // add the new account as default in preferences, if there is none already
        User defaultAccount = accountManager.getUser();
        if (defaultAccount.isAnonymous()) {
            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
            editor.putString("select_oc_account", accountName);
            editor.apply();
        }
        // / prepare result to return to the Authenticator
        // TODO check again what the Authenticator makes with it; probably has the same
        // effect as addAccountExplicitly, but it's not well done
        final Intent intent = new Intent();
        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
        intent.putExtra(AccountManager.KEY_USERDATA, loginName);
        // / add user data to the new account; TODO probably can be done in the last parameter
        // addAccountExplicitly, or in KEY_USERDATA
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION, mServerInfo.mVersion.getVersion());
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_BASE_URL, mServerInfo.mBaseUrl);
        mAccountMgr.setUserData(mAccount, Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName());
        mAccountMgr.setUserData(mAccount, Constants.KEY_USER_ID, userInfo.getId());
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(UserAccountManager.ACCOUNT_VERSION_WITH_PROPER_ID));
        setAccountAuthenticatorResult(intent.getExtras());
        setResult(RESULT_OK, intent);
        // notify Document Provider
        DocumentsStorageProvider.notifyRootsChanged(this);
        return true;
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) User(com.nextcloud.client.account.User) SharedPreferences(android.content.SharedPreferences) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UserInfo(com.owncloud.android.lib.common.UserInfo) Intent(android.content.Intent) Uri(android.net.Uri) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) SuppressLint(android.annotation.SuppressLint)

Aggregations

User (com.nextcloud.client.account.User)84 OCFile (com.owncloud.android.datamodel.OCFile)21 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)19 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)19 Intent (android.content.Intent)14 Account (android.accounts.Account)12 ArrayList (java.util.ArrayList)12 Context (android.content.Context)9 Fragment (androidx.fragment.app.Fragment)9 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)9 File (java.io.File)9 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)7 Uri (android.net.Uri)6 Bundle (android.os.Bundle)6 OCCapability (com.owncloud.android.lib.resources.status.OCCapability)6 GalleryFragment (com.owncloud.android.ui.fragment.GalleryFragment)6 PreviewTextFileFragment (com.owncloud.android.ui.preview.PreviewTextFileFragment)6 PreviewTextFragment (com.owncloud.android.ui.preview.PreviewTextFragment)6 PreviewTextStringFragment (com.owncloud.android.ui.preview.PreviewTextStringFragment)6 View (android.view.View)5