Search in sources :

Example 1 with UserAccountManager

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

the class AbstractIT method uploadOCUpload.

public void uploadOCUpload(OCUpload ocUpload) {
    ConnectivityService connectivityServiceMock = new ConnectivityService() {

        @Override
        public boolean isInternetWalled() {
            return false;
        }

        @Override
        public Connectivity getConnectivity() {
            return Connectivity.CONNECTED_WIFI;
        }
    };
    PowerManagementService powerManagementServiceMock = new PowerManagementService() {

        @NonNull
        @Override
        public BatteryStatus getBattery() {
            return new BatteryStatus();
        }

        @Override
        public boolean isPowerSavingEnabled() {
            return false;
        }

        @Override
        public boolean isPowerSavingExclusionAvailable() {
            return false;
        }
    };
    UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
    UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(accountManager, targetContext.getContentResolver());
    UploadFileOperation newUpload = new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, FileUploader.LOCAL_BEHAVIOUR_COPY, targetContext, false, false, getStorageManager());
    newUpload.addRenameUploadListener(() -> {
    // dummy
    });
    newUpload.setRemoteFolderToBeCreated();
    RemoteOperationResult result = newUpload.execute(client);
    assertTrue(result.getLogMessage(), result.isSuccess());
}
Also used : UserAccountManager(com.nextcloud.client.account.UserAccountManager) ConnectivityService(com.nextcloud.client.network.ConnectivityService) PowerManagementService(com.nextcloud.client.device.PowerManagementService) BatteryStatus(com.nextcloud.client.device.BatteryStatus) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation)

Example 2 with UserAccountManager

use of com.nextcloud.client.account.UserAccountManager 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 3 with UserAccountManager

use of com.nextcloud.client.account.UserAccountManager 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 4 with UserAccountManager

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

the class AbstractOnServerIT method uploadOCUpload.

public void uploadOCUpload(OCUpload ocUpload, int localBehaviour) {
    ConnectivityService connectivityServiceMock = new ConnectivityService() {

        @Override
        public boolean isInternetWalled() {
            return false;
        }

        @Override
        public Connectivity getConnectivity() {
            return Connectivity.CONNECTED_WIFI;
        }
    };
    PowerManagementService powerManagementServiceMock = new PowerManagementService() {

        @NonNull
        @Override
        public BatteryStatus getBattery() {
            return new BatteryStatus();
        }

        @Override
        public boolean isPowerSavingEnabled() {
            return false;
        }

        @Override
        public boolean isPowerSavingExclusionAvailable() {
            return false;
        }
    };
    UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
    UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(accountManager, targetContext.getContentResolver());
    UploadFileOperation newUpload = new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, localBehaviour, targetContext, false, false, getStorageManager());
    newUpload.addRenameUploadListener(() -> {
    // dummy
    });
    newUpload.setRemoteFolderToBeCreated();
    RemoteOperationResult result = newUpload.execute(client);
    assertTrue(result.getLogMessage(), result.isSuccess());
    OCFile parentFolder = getStorageManager().getFileByEncryptedRemotePath(new File(ocUpload.getRemotePath()).getParent() + "/");
    String uploadedFileName = new File(ocUpload.getRemotePath()).getName();
    OCFile uploadedFile = getStorageManager().getFileByDecryptedRemotePath(parentFolder.getDecryptedRemotePath() + uploadedFileName);
    assertNotNull(uploadedFile.getRemoteId());
    if (localBehaviour == FileUploader.LOCAL_BEHAVIOUR_COPY || localBehaviour == FileUploader.LOCAL_BEHAVIOUR_MOVE) {
        assertTrue(new File(uploadedFile.getStoragePath()).exists());
    }
}
Also used : UserAccountManager(com.nextcloud.client.account.UserAccountManager) OCFile(com.owncloud.android.datamodel.OCFile) ConnectivityService(com.nextcloud.client.network.ConnectivityService) PowerManagementService(com.nextcloud.client.device.PowerManagementService) BatteryStatus(com.nextcloud.client.device.BatteryStatus) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 5 with UserAccountManager

use of com.nextcloud.client.account.UserAccountManager 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)

Aggregations

UserAccountManager (com.nextcloud.client.account.UserAccountManager)10 Account (android.accounts.Account)7 AccountManager (android.accounts.AccountManager)6 Bundle (android.os.Bundle)4 User (com.nextcloud.client.account.User)4 Context (android.content.Context)3 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)3 OCCapability (com.owncloud.android.lib.resources.status.OCCapability)3 BeforeClass (org.junit.BeforeClass)3 Test (org.junit.Test)3 AuthenticatorException (android.accounts.AuthenticatorException)2 OperationCanceledException (android.accounts.OperationCanceledException)2 ActivityNotFoundException (android.content.ActivityNotFoundException)2 Uri (android.net.Uri)2 RegisteredUser (com.nextcloud.client.account.RegisteredUser)2 Server (com.nextcloud.client.account.Server)2 BatteryStatus (com.nextcloud.client.device.BatteryStatus)2 PowerManagementService (com.nextcloud.client.device.PowerManagementService)2 ConnectivityService (com.nextcloud.client.network.ConnectivityService)2 ChooseAccountDialogFragment (com.nextcloud.ui.ChooseAccountDialogFragment)2