2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.authentication
;
23 import java
.util
.Locale
;
25 import com
.owncloud
.android
.MainApp
;
26 import com
.owncloud
.android
.lib
.common
.accounts
.AccountTypeUtils
;
27 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
29 import android
.accounts
.Account
;
30 import android
.accounts
.AccountManager
;
31 import android
.content
.Context
;
32 import android
.content
.SharedPreferences
;
33 import android
.preference
.PreferenceManager
;
35 public class AccountUtils
{
36 public static final String WEBDAV_PATH_4_0_AND_LATER
= "/remote.php/webdav";
37 private static final String ODAV_PATH
= "/remote.php/odav";
38 private static final String SAML_SSO_PATH
= "/remote.php/webdav";
39 public static final String STATUS_PATH
= "/status.php";
42 * Can be used to get the currently selected ownCloud {@link Account} in the
43 * application preferences.
45 * @param context The current application {@link Context}
46 * @return The ownCloud {@link Account} currently saved in preferences, or the first
47 * {@link Account} available, if valid (still registered in the system as ownCloud
48 * account). If none is available and valid, returns null.
50 public static Account
getCurrentOwnCloudAccount(Context context
) {
51 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
52 MainApp
.getAccountType());
53 Account defaultAccount
= null
;
55 SharedPreferences appPreferences
= PreferenceManager
56 .getDefaultSharedPreferences(context
);
57 String accountName
= appPreferences
58 .getString("select_oc_account", null
);
60 // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
61 if (accountName
!= null
) {
62 for (Account account
: ocAccounts
) {
63 if (account
.name
.equals(accountName
)) {
64 defaultAccount
= account
;
70 if (defaultAccount
== null
&& ocAccounts
.length
!= 0) {
71 // take first account as fallback
72 defaultAccount
= ocAccounts
[0];
75 return defaultAccount
;
79 public static boolean exists(Account account
, Context context
) {
80 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
81 MainApp
.getAccountType());
83 if (account
!= null
&& account
.name
!= null
) {
84 int lastAtPos
= account
.name
.lastIndexOf("@");
85 String hostAndPort
= account
.name
.substring(lastAtPos
+ 1);
86 String username
= account
.name
.substring(0, lastAtPos
);
87 String otherHostAndPort
, otherUsername
;
88 Locale currentLocale
= context
.getResources().getConfiguration().locale
;
89 for (Account otherAccount
: ocAccounts
) {
90 lastAtPos
= otherAccount
.name
.lastIndexOf("@");
91 otherHostAndPort
= otherAccount
.name
.substring(lastAtPos
+ 1);
92 otherUsername
= otherAccount
.name
.substring(0, lastAtPos
);
93 if (otherHostAndPort
.equals(hostAndPort
) &&
94 otherUsername
.toLowerCase(currentLocale
).
95 equals(username
.toLowerCase(currentLocale
))) {
104 public static boolean setCurrentOwnCloudAccount(Context context
, String accountName
) {
105 boolean result
= false
;
106 if (accountName
!= null
) {
107 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
108 MainApp
.getAccountType());
109 boolean found
= false
;
110 for (Account account
: ocAccounts
) {
111 found
= (account
.name
.equals(accountName
));
113 SharedPreferences
.Editor appPrefs
= PreferenceManager
114 .getDefaultSharedPreferences(context
).edit();
115 appPrefs
.putString("select_oc_account", accountName
);
127 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
128 * according to its version and the authorization method used.
130 * @param version Version of ownCloud server.
131 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in
132 * {@link AccountAuthenticator}.
133 * @return WebDAV path for given OC version and authorization method, null if OC version
134 * is unknown; versions prior to ownCloud 4 are not supported anymore
136 public static String
getWebdavPath(OwnCloudVersion version
, String authTokenType
) {
137 if (version
!= null
) {
138 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).equals(authTokenType
)) {
141 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).equals(authTokenType
)) {
142 return SAML_SSO_PATH
;
144 return WEBDAV_PATH_4_0_AND_LATER
;