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
.OwnCloudVersion
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountManager
;
26 import android
.accounts
.AccountsException
;
27 import android
.content
.Context
;
29 public class AccountUtils
{
30 public static final String WEBDAV_PATH_1_2
= "/webdav/owncloud.php";
31 public static final String WEBDAV_PATH_2_0
= "/files/webdav.php";
32 public static final String WEBDAV_PATH_4_0
= "/remote.php/webdav";
33 private static final String ODAV_PATH
= "/remote.php/odav";
34 private static final String SAML_SSO_PATH
= "/remote.php/webdav";
35 public static final String CARDDAV_PATH_2_0
= "/apps/contacts/carddav.php";
36 public static final String CARDDAV_PATH_4_0
= "/remote/carddav.php";
37 public static final String STATUS_PATH
= "/status.php";
40 // * Can be used to get the currently selected ownCloud {@link Account} in the
41 // * application preferences.
43 // * @param context The current application {@link Context}
44 // * @return The ownCloud {@link Account} currently saved in preferences, or the first
45 // * {@link Account} available, if valid (still registered in the system as ownCloud
46 // * account). If none is available and valid, returns null.
48 // public static Account getCurrentOwnCloudAccount(Context context) {
49 // Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
50 // MainApp.getAccountType());
51 // Account defaultAccount = null;
53 // SharedPreferences appPreferences = PreferenceManager
54 // .getDefaultSharedPreferences(context);
55 // String accountName = appPreferences
56 // .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;
68 // if (defaultAccount == null && ocAccounts.length != 0) {
69 // // take first account as fallback
70 // defaultAccount = ocAccounts[0];
73 // return defaultAccount;
77 // public static boolean exists(Account account, Context context) {
78 // Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
79 // MainApp.getAccountType());
81 // if (account != null && account.name != null) {
82 // for (Account ac : ocAccounts) {
83 // if (ac.name.equals(account.name)) {
93 // * Checks, whether or not there are any ownCloud accounts setup.
95 // * @return true, if there is at least one account.
97 // public static boolean accountsAreSetup(Context context) {
98 // AccountManager accMan = AccountManager.get(context);
99 // Account[] accounts = accMan
100 // .getAccountsByType(MainApp.getAccountType());
101 // return accounts.length > 0;
105 // public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
106 // boolean result = false;
107 // if (accountName != null) {
108 // Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
109 // MainApp.getAccountType());
110 // boolean found = false;
111 // for (Account account : ocAccounts) {
112 // found = (account.name.equals(accountName));
114 // SharedPreferences.Editor appPrefs = PreferenceManager
115 // .getDefaultSharedPreferences(context).edit();
116 // appPrefs.putString("select_oc_account", accountName);
118 // appPrefs.commit();
129 * @param version version of owncloud
130 * @return webdav path for given OC version, null if OC version unknown
132 public static String
getWebdavPath(OwnCloudVersion version
, boolean supportsOAuth
, boolean supportsSamlSso
) {
133 if (version
!= null
) {
137 if (supportsSamlSso
) {
138 return SAML_SSO_PATH
;
140 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
141 return WEBDAV_PATH_4_0
;
142 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
143 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
144 return WEBDAV_PATH_2_0
;
145 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
146 return WEBDAV_PATH_1_2
;
152 // * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
153 // * according to its version and the authorization method used.
155 // * @param version Version of ownCloud server.
156 // * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
157 // * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
159 // public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
160 // if (version != null) {
161 // if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {
164 // if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {
165 // return SAML_SSO_PATH;
167 // if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
168 // return WEBDAV_PATH_4_0;
169 // if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
170 // || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
171 // return WEBDAV_PATH_2_0;
172 // if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
173 // return WEBDAV_PATH_1_2;
179 * Constructs full url to host and webdav resource basing on host version
182 * @return url or null on failure
183 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
185 public static String
constructFullURLForAccount(Context context
, Account account
) throws AccountNotFoundException
{
186 AccountManager ama
= AccountManager
.get(context
);
187 String baseurl
= ama
.getUserData(account
, AccountAuthenticatorConstants
.KEY_OC_BASE_URL
);
188 String strver
= ama
.getUserData(account
, AccountAuthenticatorConstants
.KEY_OC_VERSION
);
189 boolean supportsOAuth
= (ama
.getUserData(account
, AccountAuthenticatorConstants
.KEY_SUPPORTS_OAUTH2
) != null
);
190 boolean supportsSamlSso
= (ama
.getUserData(account
, AccountAuthenticatorConstants
.KEY_SUPPORTS_SAML_WEB_SSO
) != null
);
191 OwnCloudVersion ver
= new OwnCloudVersion(strver
);
192 String webdavpath
= getWebdavPath(ver
, supportsOAuth
, supportsSamlSso
);
194 if (baseurl
== null
|| webdavpath
== null
)
195 throw new AccountNotFoundException(account
, "Account not found", null
);
197 return baseurl
+ webdavpath
;
201 public static class AccountNotFoundException
extends AccountsException
{
203 /** Generated - should be refreshed every time the class changes!! */
204 private static final long serialVersionUID
= -9013287181793186830L;
206 private Account mFailedAccount
;
208 public AccountNotFoundException(Account failedAccount
, String message
, Throwable cause
) {
209 super(message
, cause
);
210 mFailedAccount
= failedAccount
;
213 public Account
getFailedAccount() {
214 return mFailedAccount
;