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