123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- /*
- * Nextcloud Android client application
- *
- * @author Chris Narkiewicz
- * Copyright (C) 2019 Chris Narkiewicz
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or 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 AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.nextcloud.client.account;
- import android.accounts.Account;
- import android.accounts.AccountManager;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.preference.PreferenceManager;
- import android.text.TextUtils;
- import com.owncloud.android.MainApp;
- import com.owncloud.android.R;
- import com.owncloud.android.datamodel.ArbitraryDataProvider;
- import com.owncloud.android.datamodel.OCFile;
- 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.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.GetUserInfoRemoteOperation;
- import javax.inject.Inject;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- public class UserAccountManagerImpl implements UserAccountManager {
- private static final String TAG = UserAccountManagerImpl.class.getSimpleName();
- private static final String PREF_SELECT_OC_ACCOUNT = "select_oc_account";
- private Context context;
- private AccountManager accountManager;
- public static UserAccountManagerImpl fromContext(Context context) {
- AccountManager am = (AccountManager)context.getSystemService(Context.ACCOUNT_SERVICE);
- return new UserAccountManagerImpl(context, am);
- }
- @Inject
- public UserAccountManagerImpl(
- Context context,
- AccountManager accountManager
- ) {
- this.context = context;
- this.accountManager = accountManager;
- }
- @Override
- public void removeAllAccounts() {
- for (Account account : getAccounts()) {
- accountManager.removeAccount(account, null, null);
- }
- }
- @Override
- @NonNull
- public Account[] getAccounts() {
- return accountManager.getAccountsByType(getAccountType());
- }
- @Override
- public boolean exists(Account account) {
- Account[] nextcloudAccounts = getAccounts();
- if (account != null && account.name != null) {
- int lastAtPos = account.name.lastIndexOf('@');
- String hostAndPort = account.name.substring(lastAtPos + 1);
- String username = account.name.substring(0, lastAtPos);
- String otherHostAndPort;
- String otherUsername;
- for (Account otherAccount : nextcloudAccounts) {
- lastAtPos = otherAccount.name.lastIndexOf('@');
- otherHostAndPort = otherAccount.name.substring(lastAtPos + 1);
- otherUsername = otherAccount.name.substring(0, lastAtPos);
- if (otherHostAndPort.equals(hostAndPort) &&
- otherUsername.equalsIgnoreCase(username)) {
- return true;
- }
- }
- }
- return false;
- }
- @Nullable
- public Account getCurrentAccount() {
- Account[] ocAccounts = getAccounts();
- Account defaultAccount = null;
- ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
- SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
- String accountName = appPreferences.getString(PREF_SELECT_OC_ACCOUNT, null);
- // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
- if (accountName != null) {
- for (Account account : ocAccounts) {
- if (account.name.equals(accountName)) {
- defaultAccount = account;
- break;
- }
- }
- }
- if (defaultAccount == null && ocAccounts.length > 0) {
- // take first which is not pending for removal account as fallback
- for (Account account: ocAccounts) {
- boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(account,
- PENDING_FOR_REMOVAL);
- if (!pendingForRemoval) {
- defaultAccount = account;
- break;
- }
- }
- }
- return defaultAccount;
- }
- @Override
- @Nullable
- public OwnCloudAccount getCurrentOwnCloudAccount() {
- try {
- Account currentPlatformAccount = getCurrentAccount();
- return new OwnCloudAccount(currentPlatformAccount, context);
- } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException ex) {
- return null;
- }
- }
- @Override
- @Nullable
- public Account getAccountByName(String name) {
- for (Account account : getAccounts()) {
- if (account.name.equals(name)) {
- return account;
- }
- }
- return null;
- }
- @Override
- public boolean setCurrentOwnCloudAccount(String accountName) {
- boolean result = false;
- if (accountName != null) {
- for (final Account account : getAccounts()) {
- if (accountName.equals(account.name)) {
- SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
- appPrefs.putString(PREF_SELECT_OC_ACCOUNT, accountName);
- appPrefs.apply();
- result = true;
- break;
- }
- }
- }
- return result;
- }
- @Override
- public boolean setCurrentOwnCloudAccount(int hashCode) {
- boolean result = false;
- if (hashCode != 0) {
- for (final Account account : getAccounts()) {
- if (hashCode == account.hashCode()) {
- SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
- appPrefs.putString(PREF_SELECT_OC_ACCOUNT, account.name);
- appPrefs.apply();
- result = true;
- break;
- }
- }
- }
- return result;
- }
- @Override
- @NonNull
- public OwnCloudVersion getServerVersion(Account account) {
- OwnCloudVersion serverVersion = MainApp.MINIMUM_SUPPORTED_SERVER_VERSION;
- if (account != null) {
- AccountManager accountMgr = AccountManager.get(MainApp.getAppContext());
- String serverVersionStr = accountMgr.getUserData(account, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_OC_VERSION);
- if (serverVersionStr != null) {
- serverVersion = new OwnCloudVersion(serverVersionStr);
- }
- }
- return serverVersion;
- }
- @Override
- public boolean isSearchSupported(Account account) {
- return account != null && getServerVersion(account).isSearchSupported();
- }
- @Override
- public boolean isMediaStreamingSupported(Account account) {
- return account != null && getServerVersion(account).isMediaStreamingSupported();
- }
- @Override
- public void resetOwnCloudAccount() {
- SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
- appPrefs.putString(PREF_SELECT_OC_ACCOUNT, null);
- appPrefs.apply();
- }
- @Override
- public boolean accountOwnsFile(OCFile file, Account account) {
- return !TextUtils.isEmpty(file.getOwnerId()) && account.name.split("@")[0].equals(file.getOwnerId());
- }
- public boolean migrateUserId() {
- boolean success = false;
- Account[] ocAccounts = accountManager.getAccountsByType(MainApp.getAccountType(context));
- String userId;
- String displayName;
- GetUserInfoRemoteOperation remoteUserNameOperation = new GetUserInfoRemoteOperation();
- for (Account account : ocAccounts) {
- String storedUserId = accountManager.getUserData(account, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
- if (!TextUtils.isEmpty(storedUserId)) {
- continue;
- }
- // add userId
- try {
- OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
- OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton()
- .getClientFor(ocAccount, context);
- RemoteOperationResult result = remoteUserNameOperation.execute(client);
- if (result.isSuccess()) {
- UserInfo userInfo = (UserInfo) result.getData().get(0);
- userId = userInfo.id;
- displayName = userInfo.displayName;
- } 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;
- }
- accountManager.setUserData(account,
- com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_DISPLAY_NAME,
- displayName);
- accountManager.setUserData(account,
- com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID,
- userId);
- success = true;
- }
- return success;
- }
- private String getAccountType() {
- return context.getString(R.string.account_type);
- }
- }
|