Merge branch 'develop' into contact_settings
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AccountUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.authentication;
20
21 import com.owncloud.android.utils.OwnCloudVersion;
22
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;
29
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";
39
40 /**
41 * Can be used to get the currently selected ownCloud {@link Account} in the
42 * application preferences.
43 *
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.
48 */
49 public static Account getCurrentOwnCloudAccount(Context context) {
50 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
51 AccountAuthenticator.ACCOUNT_TYPE);
52 Account defaultAccount = null;
53
54 SharedPreferences appPreferences = PreferenceManager
55 .getDefaultSharedPreferences(context);
56 String accountName = appPreferences
57 .getString("select_oc_account", null);
58
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;
64 break;
65 }
66 }
67 }
68
69 if (defaultAccount == null && ocAccounts.length != 0) {
70 // take first account as fallback
71 defaultAccount = ocAccounts[0];
72 }
73
74 return defaultAccount;
75 }
76
77
78 public static boolean exists(Account account, Context context) {
79 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
80 AccountAuthenticator.ACCOUNT_TYPE);
81
82 if (account != null && account.name != null) {
83 for (Account ac : ocAccounts) {
84 if (ac.name.equals(account.name)) {
85 return true;
86 }
87 }
88 }
89 return false;
90 }
91
92
93 /**
94 * Checks, whether or not there are any ownCloud accounts setup.
95 *
96 * @return true, if there is at least one account.
97 */
98 public static boolean accountsAreSetup(Context context) {
99 AccountManager accMan = AccountManager.get(context);
100 Account[] accounts = accMan
101 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
102 return accounts.length > 0;
103 }
104
105
106 public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
107 boolean result = false;
108 if (accountName != null) {
109 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
110 AccountAuthenticator.ACCOUNT_TYPE);
111 boolean found = false;
112 for (Account account : ocAccounts) {
113 found = (account.name.equals(accountName));
114 if (found) {
115 SharedPreferences.Editor appPrefs = PreferenceManager
116 .getDefaultSharedPreferences(context).edit();
117 appPrefs.putString("select_oc_account", accountName);
118
119 appPrefs.commit();
120 result = true;
121 break;
122 }
123 }
124 }
125 return result;
126 }
127
128 /**
129 *
130 * @param version version of owncloud
131 * @return webdav path for given OC version, null if OC version unknown
132 */
133 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {
134 if (version != null) {
135 if (supportsOAuth) {
136 return ODAV_PATH;
137 }
138 if (supportsSamlSso) {
139 return SAML_SSO_PATH;
140 }
141 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
142 return WEBDAV_PATH_4_0;
143 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
144 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
145 return WEBDAV_PATH_2_0;
146 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
147 return WEBDAV_PATH_1_2;
148 }
149 return null;
150 }
151
152 /**
153 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
154 * according to its version and the authorization method used.
155 *
156 * @param version Version of ownCloud server.
157 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
158 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
159 */
160 public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
161 if (version != null) {
162 if (AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(authTokenType)) {
163 return ODAV_PATH;
164 }
165 if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(authTokenType)) {
166 return SAML_SSO_PATH;
167 }
168 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
169 return WEBDAV_PATH_4_0;
170 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
171 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
172 return WEBDAV_PATH_2_0;
173 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
174 return WEBDAV_PATH_1_2;
175 }
176 return null;
177 }
178
179 /**
180 * Constructs full url to host and webdav resource basing on host version
181 * @param context
182 * @param account
183 * @return url or null on failure
184 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
185 */
186 public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
187 AccountManager ama = AccountManager.get(context);
188 String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
189 String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);
190 boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);
191 boolean supportsSamlSso = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null);
192 OwnCloudVersion ver = new OwnCloudVersion(strver);
193 String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso);
194
195 if (baseurl == null || webdavpath == null)
196 throw new AccountNotFoundException(account, "Account not found", null);
197
198 return baseurl + webdavpath;
199 }
200
201
202 public static class AccountNotFoundException extends AccountsException {
203
204 /** Generated - should be refreshed every time the class changes!! */
205 private static final long serialVersionUID = -9013287181793186830L;
206
207 private Account mFailedAccount;
208
209 public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {
210 super(message, cause);
211 mFailedAccount = failedAccount;
212 }
213
214 public Account getFailedAccount() {
215 return mFailedAccount;
216 }
217 }
218
219 }