AccountUtils.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2012 Bartek Przybylski
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.authentication;
  21. import android.accounts.Account;
  22. import android.accounts.AccountManager;
  23. import android.content.Context;
  24. import android.content.SharedPreferences;
  25. import android.preference.PreferenceManager;
  26. import android.support.annotation.NonNull;
  27. import android.support.annotation.Nullable;
  28. import com.owncloud.android.MainApp;
  29. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  30. import com.owncloud.android.datamodel.FileDataStorageManager;
  31. import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
  32. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  33. import com.owncloud.android.lib.common.utils.Log_OC;
  34. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  35. import com.owncloud.android.operations.GetCapabilitiesOperarion;
  36. import com.owncloud.android.ui.activity.ManageAccountsActivity;
  37. import java.util.Locale;
  38. public class AccountUtils {
  39. private static final String TAG = AccountUtils.class.getSimpleName();
  40. public static final int ACCOUNT_VERSION = 1;
  41. public static final int ACCOUNT_VERSION_WITH_PROPER_ID = 2;
  42. public static final String ACCOUNT_USES_STANDARD_PASSWORD = "ACCOUNT_USES_STANDARD_PASSWORD";
  43. /**
  44. * Can be used to get the currently selected ownCloud {@link Account} in the
  45. * application preferences.
  46. *
  47. * @param context The current application {@link Context}
  48. * @return The ownCloud {@link Account} currently saved in preferences, or the first
  49. * {@link Account} available, if valid (still registered in the system as ownCloud
  50. * account). If none is available and valid, returns null.
  51. */
  52. public static @Nullable Account getCurrentOwnCloudAccount(Context context) {
  53. Account[] ocAccounts = getAccounts(context);
  54. Account defaultAccount = null;
  55. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
  56. SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
  57. String accountName = appPreferences.getString("select_oc_account", null);
  58. // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
  59. if (accountName != null) {
  60. for (Account account : ocAccounts) {
  61. if (account.name.equals(accountName)) {
  62. defaultAccount = account;
  63. break;
  64. }
  65. }
  66. }
  67. if (defaultAccount == null && ocAccounts.length > 0) {
  68. // take first which is not pending for removal account as fallback
  69. for (Account account: ocAccounts) {
  70. boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(account,
  71. ManageAccountsActivity.PENDING_FOR_REMOVAL);
  72. if (!pendingForRemoval) {
  73. defaultAccount = account;
  74. break;
  75. }
  76. }
  77. }
  78. return defaultAccount;
  79. }
  80. public static Account[] getAccounts(Context context) {
  81. AccountManager accountManager = AccountManager.get(context);
  82. return accountManager.getAccountsByType(MainApp.getAccountType());
  83. }
  84. public static boolean exists(Account account, Context context) {
  85. Account[] ocAccounts = getAccounts(context);
  86. if (account != null && account.name != null) {
  87. int lastAtPos = account.name.lastIndexOf("@");
  88. String hostAndPort = account.name.substring(lastAtPos + 1);
  89. String username = account.name.substring(0, lastAtPos);
  90. String otherHostAndPort;
  91. String otherUsername;
  92. Locale currentLocale = context.getResources().getConfiguration().locale;
  93. for (Account otherAccount : ocAccounts) {
  94. lastAtPos = otherAccount.name.lastIndexOf("@");
  95. otherHostAndPort = otherAccount.name.substring(lastAtPos + 1);
  96. otherUsername = otherAccount.name.substring(0, lastAtPos);
  97. if (otherHostAndPort.equals(hostAndPort) &&
  98. otherUsername.toLowerCase(currentLocale).
  99. equals(username.toLowerCase(currentLocale))) {
  100. return true;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * returns the user's name based on the account name.
  108. *
  109. * @param accountName the account name
  110. * @return the user's name
  111. */
  112. public static String getAccountUsername(String accountName) {
  113. if (accountName != null) {
  114. return accountName.substring(0, accountName.lastIndexOf('@'));
  115. } else {
  116. return null;
  117. }
  118. }
  119. /**
  120. * Returns owncloud account identified by accountName or null if it does not exist.
  121. * @param context the context
  122. * @param accountName name of account to be returned
  123. * @return owncloud account named accountName
  124. */
  125. public static Account getOwnCloudAccountByName(Context context, String accountName) {
  126. Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
  127. MainApp.getAccountType());
  128. for (Account account : ocAccounts) {
  129. if(account.name.equals(accountName)) {
  130. return account;
  131. }
  132. }
  133. return null;
  134. }
  135. public static boolean setCurrentOwnCloudAccount(final Context context, String accountName) {
  136. boolean result = false;
  137. if (accountName != null) {
  138. boolean found;
  139. for (final Account account : getAccounts(context)) {
  140. found = (account.name.equals(accountName));
  141. if (found) {
  142. SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
  143. appPrefs.putString("select_oc_account", accountName);
  144. // update credentials
  145. Thread t = new Thread(new Runnable() {
  146. @Override
  147. public void run() {
  148. FileDataStorageManager storageManager = new FileDataStorageManager(account,
  149. context.getContentResolver());
  150. GetCapabilitiesOperarion getCapabilities = new GetCapabilitiesOperarion();
  151. RemoteOperationResult updateResult = getCapabilities.execute(storageManager, context);
  152. Log_OC.w(TAG, "Update Capabilities: " + updateResult.isSuccess());
  153. }
  154. });
  155. t.start();
  156. appPrefs.apply();
  157. result = true;
  158. break;
  159. }
  160. }
  161. }
  162. return result;
  163. }
  164. public static void resetOwnCloudAccount(Context context) {
  165. SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
  166. appPrefs.putString("select_oc_account", null);
  167. appPrefs.apply();
  168. }
  169. /**
  170. * Access the version of the OC server corresponding to an account SAVED IN THE ACCOUNTMANAGER
  171. *
  172. * @param account ownCloud account
  173. * @return Version of the OC server corresponding to account, according to the data saved
  174. * in the system AccountManager
  175. */
  176. public static @NonNull
  177. OwnCloudVersion getServerVersion(Account account) {
  178. OwnCloudVersion serverVersion = OwnCloudVersion.nextcloud_10;
  179. if (account != null) {
  180. AccountManager accountMgr = AccountManager.get(MainApp.getAppContext());
  181. String serverVersionStr = accountMgr.getUserData(account, Constants.KEY_OC_VERSION);
  182. if (serverVersionStr != null) {
  183. serverVersion = new OwnCloudVersion(serverVersionStr);
  184. }
  185. }
  186. return serverVersion;
  187. }
  188. public static boolean hasSearchUsersSupport(Account account) {
  189. return getServerVersion(account).isSearchUsersSupported();
  190. }
  191. public static boolean hasSearchSupport(Account account) {
  192. return getServerVersion(account).isSearchSupported();
  193. }
  194. }