Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 458c2062 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

6684-Automatically_pick_murena_account

issue: e/backlog#6684

- We want to pick murena account if it is setup in the device.
- Refactor accountSyncUtil code
parent 4eb2f7c4
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.6'

    // Nextcloud SSO
    implementation 'foundation.e.lib:Android-SingleSignOn:1.0.3-alpha'
    implementation 'foundation.e.lib:Android-SingleSignOn:1.0.4-alpha'
    implementation ('com.github.stefan-niedermann:android-commons:0.2.7') {
        exclude group: 'com.github.nextcloud', module: 'Android-SingleSignOn'
    }
+34 −0
Original line number Diff line number Diff line
@@ -14,7 +14,9 @@ import androidx.preference.PreferenceManager;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.AccountImportCancelledException;
import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountPermissionNotGrantedException;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotSupportedException;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;
import com.nextcloud.android.sso.exceptions.UnknownErrorException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
@@ -44,6 +46,8 @@ public class ImportAccountActivity extends AppCompatActivity {
    private final ExecutorService executor = Executors.newSingleThreadExecutor();

    private ImportAccountViewModel importAccountViewModel;
    private ImportMurenaAccountViewModel importMurenaAccountViewModel;

    private ActivityImportAccountBinding binding;

    @Override
@@ -54,6 +58,7 @@ public class ImportAccountActivity extends AppCompatActivity {

        binding = ActivityImportAccountBinding.inflate(getLayoutInflater());
        importAccountViewModel = new ViewModelProvider(this).get(ImportAccountViewModel.class);
        importMurenaAccountViewModel = new ViewModelProvider(this).get(ImportMurenaAccountViewModel.class);

        setContentView(binding.getRoot());

@@ -75,6 +80,8 @@ public class ImportAccountActivity extends AppCompatActivity {
                AccountImporter.requestAndroidAccountPermissionsAndPickAccount(this);
            }
        });

        handleAutoMurenaAccountSetup();
    }

    @Override
@@ -178,4 +185,31 @@ public class ImportAccountActivity extends AppCompatActivity {
            binding.progressText.setVisibility(View.GONE);
        });
    }

    private void handleAutoMurenaAccountSetup() {
        if (importMurenaAccountViewModel.isMurenaAccountPresent()) {
            binding.addButton.setEnabled(false);
            binding.addButton.setVisibility(View.INVISIBLE);
            binding.status.setVisibility(View.GONE);

            pickMurenaAccount();
        }
    }

    private void pickMurenaAccount() {
        try {
            AccountImporter.pickAccount(this, importMurenaAccountViewModel.getMurenaAccount());
        } catch (NextcloudFilesAppNotInstalledException |
                 NextcloudFilesAppNotSupportedException e) {
            UiExceptionManager.showDialogForException(this, e);
            Log.w(TAG, "=============================================================");
            Log.w(TAG, "Nextcloud app is not installed. Cannot choose account");
            e.printStackTrace();
        } catch (AndroidGetAccountsPermissionNotGranted |
                 NextcloudFilesAppAccountPermissionNotGrantedException e) {
            binding.addButton.setEnabled(true);
            binding.addButton.setVisibility(View.VISIBLE);
            AccountImporter.requestAndroidAccountPermissionsAndPickAccount(this);
        }
    }
}
 No newline at end of file
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package it.niedermann.owncloud.notes.importaccount;

import android.accounts.Account;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import java.util.List;

import it.niedermann.owncloud.notes.shared.util.AccountSyncUtil;

public class ImportMurenaAccountViewModel extends AndroidViewModel {

    private Account account;

    public ImportMurenaAccountViewModel(@NonNull Application application) {
        super(application);
        retrieveAccount();
    }

    private void retrieveAccount() {
        List<Account> accounts = AccountSyncUtil.getMuenaAccounts(getApplication());

        if (accounts.isEmpty()) {
            return;
        }

        if (AccountSyncUtil.isSyncEnable(accounts.get(0))) {
            account = accounts.get(0); // retrieve the first account, as we have policy that one murena account per device
        }
    }

    public boolean isMurenaAccountPresent() {
        return account != null;
    }

    public Account getMurenaAccount() {
        return account;
    }
}
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package it.niedermann.owncloud.notes.shared.constant;

public final class MurenaAccountConstants {

    public static final String MURENA_ACCOUNT_TYPE = "e.foundation.webdav.eelo";
    public static final String NOTES_CONTENT_AUTHORITY = "foundation.e.notes.android.providers.AppContentProvider";

    private MurenaAccountConstants() {
    }
}
+17 −12
Original line number Diff line number Diff line
@@ -19,33 +19,32 @@ package it.niedermann.owncloud.notes.shared.util;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import it.niedermann.owncloud.notes.shared.constant.MurenaAccountConstants;
import trikita.log.Log;

import it.niedermann.owncloud.notes.persistence.entity.Account;

public class AccountSyncUtil {
public final class AccountSyncUtil {

    private static final String TAG = AccountSyncUtil.class.getSimpleName();
    private static final String murena_account_type = "e.foundation.webdav.eelo";
    private static final String notes_content_authority = "foundation.e.notes.android.providers.AppContentProvider";

    private AccountSyncUtil() {
        throw new UnsupportedOperationException("Do not instantiate this util class.");
    }

    private static android.accounts.Account[] getMurenaAccountsOnDevice(AccountManager accountManager) {
        return accountManager.getAccountsByType(murena_account_type);
    }

    private static boolean isSyncEnable(android.accounts.Account account) {
        return ContentResolver.getMasterSyncAutomatically() && ContentResolver.getSyncAutomatically(account, notes_content_authority);
    public static boolean isSyncEnable(android.accounts.Account account) {
        return ContentResolver.getMasterSyncAutomatically() && ContentResolver.getSyncAutomatically(account, MurenaAccountConstants.NOTES_CONTENT_AUTHORITY);
    }

    public static boolean isSyncEnable(Context context, Account account) {
        AccountManager accountManager = AccountManager.get(context);

        try {
            android.accounts.Account[] murenaAccounts = getMurenaAccountsOnDevice(accountManager);
            List<android.accounts.Account> murenaAccounts = getMuenaAccounts(context);

            for (android.accounts.Account murenaAccount : murenaAccounts) {
                if (account.getAccountName().equals(murenaAccount.name)) {
@@ -59,4 +58,10 @@ public class AccountSyncUtil {
        return true;
    }

    public static List<android.accounts.Account> getMuenaAccounts(Context context) {
        AccountManager accountManager = AccountManager.get(context);
        return Arrays.stream(accountManager.getAccountsByType(MurenaAccountConstants.MURENA_ACCOUNT_TYPE))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}