1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.authentication
;
21 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
23 import android
.accounts
.Account
;
24 import android
.accounts
.AccountManager
;
25 import android
.accounts
.AccountsException
;
26 import android
.content
.Context
;
27 import android
.content
.SharedPreferences
;
28 import android
.preference
.PreferenceManager
;
30 public class AccountUtils
{
31 public static final String WEBDAV_PATH_1_2
= "/webdav/owncloud.php";
32 public static final String WEBDAV_PATH_2_0
= "/files/webdav.php";
33 public static final String WEBDAV_PATH_4_0
= "/remote.php/webdav";
34 private static final String ODAV_PATH
= "/remote.php/odav";
35 private static final String SAML_SSO_PATH
= "/remote.php/webdav";
36 public static final String CARDDAV_PATH_2_0
= "/apps/contacts/carddav.php";
37 public static final String CARDDAV_PATH_4_0
= "/remote/carddav.php";
38 public static final String STATUS_PATH
= "/status.php";
41 * Can be used to get the currently selected ownCloud {@link Account} in the
42 * application preferences.
44 * @param context The current application {@link Context}
45 * @return The ownCloud {@link Account} currently saved in preferences, or the first
46 * {@link Account} available, if valid (still registered in the system as ownCloud
47 * account). If none is available and valid, returns null.
49 public static Account
getCurrentOwnCloudAccount(Context context
) {
50 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
51 AccountAuthenticator
.ACCOUNT_TYPE
);
52 Account defaultAccount
= null
;
54 SharedPreferences appPreferences
= PreferenceManager
55 .getDefaultSharedPreferences(context
);
56 String accountName
= appPreferences
57 .getString("select_oc_account", null
);
59 // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
60 if (accountName
!= null
) {
61 for (Account account
: ocAccounts
) {
62 if (account
.name
.equals(accountName
)) {
63 defaultAccount
= account
;
69 if (defaultAccount
== null
&& ocAccounts
.length
!= 0) {
70 // take first account as fallback
71 defaultAccount
= ocAccounts
[0];
74 return defaultAccount
;
80 * Checks, whether or not there are any ownCloud accounts setup.
82 * @return true, if there is at least one account.
84 public static boolean accountsAreSetup(Context context
) {
85 AccountManager accMan
= AccountManager
.get(context
);
86 Account
[] accounts
= accMan
87 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
88 return accounts
.length
> 0;
92 public static boolean setCurrentOwnCloudAccount(Context context
, String accountName
) {
93 boolean result
= false
;
94 if (accountName
!= null
) {
95 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
96 AccountAuthenticator
.ACCOUNT_TYPE
);
97 boolean found
= false
;
98 for (Account account
: ocAccounts
) {
99 found
= (account
.name
.equals(accountName
));
101 SharedPreferences
.Editor appPrefs
= PreferenceManager
102 .getDefaultSharedPreferences(context
).edit();
103 appPrefs
.putString("select_oc_account", accountName
);
116 * @param version version of owncloud
117 * @return webdav path for given OC version, null if OC version unknown
119 public static String
getWebdavPath(OwnCloudVersion version
, boolean supportsOAuth
, boolean supportsSamlSso
) {
120 if (version
!= null
) {
124 if (supportsSamlSso
) {
125 return SAML_SSO_PATH
;
127 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
128 return WEBDAV_PATH_4_0
;
129 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
130 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
131 return WEBDAV_PATH_2_0
;
132 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
133 return WEBDAV_PATH_1_2
;
139 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
140 * according to its version and the authorization method used.
142 * @param version Version of ownCloud server.
143 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
144 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
146 public static String
getWebdavPath(OwnCloudVersion version
, String authTokenType
) {
147 if (version
!= null
) {
148 if (AccountAuthenticator
.AUTH_TOKEN_TYPE_ACCESS_TOKEN
.equals(authTokenType
)) {
151 if (AccountAuthenticator
.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE
.equals(authTokenType
)) {
152 return SAML_SSO_PATH
;
154 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
155 return WEBDAV_PATH_4_0
;
156 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
157 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
158 return WEBDAV_PATH_2_0
;
159 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
160 return WEBDAV_PATH_1_2
;
166 * Constructs full url to host and webdav resource basing on host version
169 * @return url or null on failure
170 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
172 public static String
constructFullURLForAccount(Context context
, Account account
) throws AccountNotFoundException
{
173 AccountManager ama
= AccountManager
.get(context
);
174 String baseurl
= ama
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
);
175 String strver
= ama
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
);
176 boolean supportsOAuth
= (ama
.getUserData(account
, AccountAuthenticator
.KEY_SUPPORTS_OAUTH2
) != null
);
177 boolean supportsSamlSso
= (ama
.getUserData(account
, AccountAuthenticator
.KEY_SUPPORTS_SAML_WEB_SSO
) != null
);
178 OwnCloudVersion ver
= new OwnCloudVersion(strver
);
179 String webdavpath
= getWebdavPath(ver
, supportsOAuth
, supportsSamlSso
);
181 if (baseurl
== null
|| webdavpath
== null
)
182 throw new AccountNotFoundException(account
, "Account not found", null
);
184 return baseurl
+ webdavpath
;
188 public static class AccountNotFoundException
extends AccountsException
{
190 /** Generated - should be refreshed every time the class changes!! */
191 private static final long serialVersionUID
= -9013287181793186830L;
193 private Account mFailedAccount
;
195 public AccountNotFoundException(Account failedAccount
, String message
, Throwable cause
) {
196 super(message
, cause
);
197 mFailedAccount
= failedAccount
;
200 public Account
getFailedAccount() {
201 return mFailedAccount
;