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
.oc_framework
.authentication
;
22 import com
.owncloud
.android
.oc_framework
.MainApp
;
23 import com
.owncloud
.android
.oc_framework
.OwnCloudVersion
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AccountManager
;
27 import android
.accounts
.AccountsException
;
28 import android
.content
.Context
;
29 import android
.content
.SharedPreferences
;
30 import android
.preference
.PreferenceManager
;
32 public class AccountUtils
{
33 public static final String WEBDAV_PATH_1_2
= "/webdav/owncloud.php";
34 public static final String WEBDAV_PATH_2_0
= "/files/webdav.php";
35 public static final String WEBDAV_PATH_4_0
= "/remote.php/webdav";
36 private static final String ODAV_PATH
= "/remote.php/odav";
37 private static final String SAML_SSO_PATH
= "/remote.php/webdav";
38 public static final String CARDDAV_PATH_2_0
= "/apps/contacts/carddav.php";
39 public static final String CARDDAV_PATH_4_0
= "/remote/carddav.php";
40 public static final String STATUS_PATH
= "/status.php";
43 * Can be used to get the currently selected ownCloud {@link Account} in the
44 * application preferences.
46 * @param context The current application {@link Context}
47 * @return The ownCloud {@link Account} currently saved in preferences, or the first
48 * {@link Account} available, if valid (still registered in the system as ownCloud
49 * account). If none is available and valid, returns null.
51 public static Account
getCurrentOwnCloudAccount(Context context
) {
52 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
53 MainApp
.getAccountType());
54 Account defaultAccount
= null
;
56 SharedPreferences appPreferences
= PreferenceManager
57 .getDefaultSharedPreferences(context
);
58 String accountName
= appPreferences
59 .getString("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
;
71 if (defaultAccount
== null
&& ocAccounts
.length
!= 0) {
72 // take first account as fallback
73 defaultAccount
= ocAccounts
[0];
76 return defaultAccount
;
80 public static boolean exists(Account account
, Context context
) {
81 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
82 MainApp
.getAccountType());
84 if (account
!= null
&& account
.name
!= null
) {
85 for (Account ac
: ocAccounts
) {
86 if (ac
.name
.equals(account
.name
)) {
96 * Checks, whether or not there are any ownCloud accounts setup.
98 * @return true, if there is at least one account.
100 public static boolean accountsAreSetup(Context context
) {
101 AccountManager accMan
= AccountManager
.get(context
);
102 Account
[] accounts
= accMan
103 .getAccountsByType(MainApp
.getAccountType());
104 return accounts
.length
> 0;
108 public static boolean setCurrentOwnCloudAccount(Context context
, String accountName
) {
109 boolean result
= false
;
110 if (accountName
!= null
) {
111 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
112 MainApp
.getAccountType());
113 boolean found
= false
;
114 for (Account account
: ocAccounts
) {
115 found
= (account
.name
.equals(accountName
));
117 SharedPreferences
.Editor appPrefs
= PreferenceManager
118 .getDefaultSharedPreferences(context
).edit();
119 appPrefs
.putString("select_oc_account", accountName
);
132 * @param version version of owncloud
133 * @return webdav path for given OC version, null if OC version unknown
135 public static String
getWebdavPath(OwnCloudVersion version
, boolean supportsOAuth
, boolean supportsSamlSso
) {
136 if (version
!= null
) {
140 if (supportsSamlSso
) {
141 return SAML_SSO_PATH
;
143 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
144 return WEBDAV_PATH_4_0
;
145 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
146 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
147 return WEBDAV_PATH_2_0
;
148 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
149 return WEBDAV_PATH_1_2
;
155 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
156 * according to its version and the authorization method used.
158 * @param version Version of ownCloud server.
159 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
160 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
162 public static String
getWebdavPath(OwnCloudVersion version
, String authTokenType
) {
163 if (version
!= null
) {
164 if (MainApp
.getAuthTokenTypeAccessToken().equals(authTokenType
)) {
167 if (MainApp
.getAuthTokenTypeSamlSessionCookie().equals(authTokenType
)) {
168 return SAML_SSO_PATH
;
170 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
171 return WEBDAV_PATH_4_0
;
172 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
173 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
174 return WEBDAV_PATH_2_0
;
175 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
176 return WEBDAV_PATH_1_2
;
182 * Constructs full url to host and webdav resource basing on host version
185 * @return url or null on failure
186 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
188 public static String
constructFullURLForAccount(Context context
, Account account
) throws AccountNotFoundException
{
189 AccountManager ama
= AccountManager
.get(context
);
190 String baseurl
= ama
.getUserData(account
, AccountAuthenticatorConstants
.KEY_OC_BASE_URL
);
191 String strver
= ama
.getUserData(account
, AccountAuthenticatorConstants
.KEY_OC_VERSION
);
192 boolean supportsOAuth
= (ama
.getUserData(account
, AccountAuthenticatorConstants
.KEY_SUPPORTS_OAUTH2
) != null
);
193 boolean supportsSamlSso
= (ama
.getUserData(account
, AccountAuthenticatorConstants
.KEY_SUPPORTS_SAML_WEB_SSO
) != null
);
194 OwnCloudVersion ver
= new OwnCloudVersion(strver
);
195 String webdavpath
= getWebdavPath(ver
, supportsOAuth
, supportsSamlSso
);
197 if (baseurl
== null
|| webdavpath
== null
)
198 throw new AccountNotFoundException(account
, "Account not found", null
);
200 return baseurl
+ webdavpath
;
204 public static class AccountNotFoundException
extends AccountsException
{
206 /** Generated - should be refreshed every time the class changes!! */
207 private static final long serialVersionUID
= -9013287181793186830L;
209 private Account mFailedAccount
;
211 public AccountNotFoundException(Account failedAccount
, String message
, Throwable cause
) {
212 super(message
, cause
);
213 mFailedAccount
= failedAccount
;
216 public Account
getFailedAccount() {
217 return mFailedAccount
;