AccountUtils.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. public class AccountUtils {
  38. private static final String TAG = AccountUtils.class.getSimpleName();
  39. private static final String PREF_SELECT_OC_ACCOUNT = "select_oc_account";
  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. private AccountUtils() {
  44. // Required empty constructor
  45. }
  46. /**
  47. * Can be used to get the currently selected ownCloud {@link Account} in the
  48. * application preferences.
  49. *
  50. * @param context The current application {@link Context}
  51. * @return The ownCloud {@link Account} currently saved in preferences, or the first
  52. * {@link Account} available, if valid (still registered in the system as ownCloud
  53. * account). If none is available and valid, returns null.
  54. */
  55. public static @Nullable Account getCurrentOwnCloudAccount(Context context) {
  56. Account[] ocAccounts = getAccounts(context);
  57. Account defaultAccount = null;
  58. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
  59. SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
  60. String accountName = appPreferences.getString(PREF_SELECT_OC_ACCOUNT, null);
  61. // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
  62. if (accountName != null) {
  63. for (Account account : ocAccounts) {
  64. if (account.name.equals(accountName)) {
  65. defaultAccount = account;
  66. break;
  67. }
  68. }
  69. }
  70. if (defaultAccount == null && ocAccounts.length > 0) {
  71. // take first which is not pending for removal account as fallback
  72. for (Account account: ocAccounts) {
  73. boolean pendingForRemoval = arbitraryDataProvider.getBooleanValue(account,
  74. ManageAccountsActivity.PENDING_FOR_REMOVAL);
  75. if (!pendingForRemoval) {
  76. defaultAccount = account;
  77. break;
  78. }
  79. }
  80. }
  81. return defaultAccount;
  82. }
  83. public static Account[] getAccounts(Context context) {
  84. AccountManager accountManager = AccountManager.get(context);
  85. return accountManager.getAccountsByType(MainApp.getAccountType(context));
  86. }
  87. public static boolean exists(Account account, Context context) {
  88. Account[] ocAccounts = getAccounts(context);
  89. if (account != null && account.name != null) {
  90. int lastAtPos = account.name.lastIndexOf('@');
  91. String hostAndPort = account.name.substring(lastAtPos + 1);
  92. String username = account.name.substring(0, lastAtPos);
  93. String otherHostAndPort;
  94. String otherUsername;
  95. for (Account otherAccount : ocAccounts) {
  96. lastAtPos = otherAccount.name.lastIndexOf('@');
  97. otherHostAndPort = otherAccount.name.substring(lastAtPos + 1);
  98. otherUsername = otherAccount.name.substring(0, lastAtPos);
  99. if (otherHostAndPort.equals(hostAndPort) &&
  100. otherUsername.equalsIgnoreCase(username)) {
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. /**
  108. * returns the user's name based on the account name.
  109. *
  110. * @param accountName the account name
  111. * @return the user's name
  112. */
  113. public static String getAccountUsername(String accountName) {
  114. if (accountName != null) {
  115. return accountName.substring(0, accountName.lastIndexOf('@'));
  116. } else {
  117. return null;
  118. }
  119. }
  120. /**
  121. * Returns owncloud account identified by accountName or null if it does not exist.
  122. * @param context the context
  123. * @param accountName name of account to be returned
  124. * @return owncloud account named accountName
  125. */
  126. public static Account getOwnCloudAccountByName(Context context, String accountName) {
  127. Account[] ocAccounts = AccountManager.get(context).getAccountsByType(MainApp.getAccountType(context));
  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(PREF_SELECT_OC_ACCOUNT, accountName);
  144. // update credentials
  145. Thread t = new Thread(() -> {
  146. FileDataStorageManager storageManager = new FileDataStorageManager(account,
  147. context.getContentResolver());
  148. GetCapabilitiesOperarion getCapabilities = new GetCapabilitiesOperarion();
  149. RemoteOperationResult updateResult = getCapabilities.execute(storageManager, context);
  150. Log_OC.w(TAG, "Update Capabilities: " + updateResult.isSuccess());
  151. });
  152. t.start();
  153. appPrefs.apply();
  154. result = true;
  155. break;
  156. }
  157. }
  158. }
  159. return result;
  160. }
  161. public static void resetOwnCloudAccount(Context context) {
  162. SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
  163. appPrefs.putString(PREF_SELECT_OC_ACCOUNT, null);
  164. appPrefs.apply();
  165. }
  166. /**
  167. * Access the version of the OC server corresponding to an account SAVED IN THE ACCOUNTMANAGER
  168. *
  169. * @param account ownCloud account
  170. * @return Version of the OC server corresponding to account, according to the data saved
  171. * in the system AccountManager
  172. */
  173. public static @NonNull
  174. OwnCloudVersion getServerVersion(Account account) {
  175. OwnCloudVersion serverVersion = OwnCloudVersion.nextcloud_10;
  176. if (account != null) {
  177. AccountManager accountMgr = AccountManager.get(MainApp.getAppContext());
  178. String serverVersionStr = accountMgr.getUserData(account, Constants.KEY_OC_VERSION);
  179. if (serverVersionStr != null) {
  180. serverVersion = new OwnCloudVersion(serverVersionStr);
  181. }
  182. }
  183. return serverVersion;
  184. }
  185. public static boolean hasSearchSupport(Account account) {
  186. return getServerVersion(account).isSearchSupported();
  187. }
  188. }