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
.MainApp
;
22 import com
.owncloud
.android
.lib
.accounts
.AccountTypeUtils
;
23 import com
.owncloud
.android
.lib
.utils
.OwnCloudVersion
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AccountManager
;
27 import android
.content
.Context
;
28 import android
.content
.SharedPreferences
;
29 import android
.preference
.PreferenceManager
;
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";
42 * Can be used to get the currently selected ownCloud {@link Account} in the
43 * application preferences.
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.
50 public static Account
getCurrentOwnCloudAccount(Context context
) {
51 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
52 MainApp
.getAccountType());
53 Account defaultAccount
= null
;
55 SharedPreferences appPreferences
= PreferenceManager
56 .getDefaultSharedPreferences(context
);
57 String accountName
= appPreferences
58 .getString("select_oc_account", null
);
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
;
70 if (defaultAccount
== null
&& ocAccounts
.length
!= 0) {
71 // take first account as fallback
72 defaultAccount
= ocAccounts
[0];
75 return defaultAccount
;
79 public static boolean exists(Account account
, Context context
) {
80 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
81 MainApp
.getAccountType());
83 if (account
!= null
&& account
.name
!= null
) {
84 for (Account ac
: ocAccounts
) {
85 if (ac
.name
.equals(account
.name
)) {
95 * Checks, whether or not there are any ownCloud accounts setup.
97 * @return true, if there is at least one account.
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;
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
));
116 SharedPreferences
.Editor appPrefs
= PreferenceManager
117 .getDefaultSharedPreferences(context
).edit();
118 appPrefs
.putString("select_oc_account", accountName
);
130 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
131 * according to its version and the authorization method used.
133 * @param version Version of ownCloud server.
134 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
135 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
137 public static String
getWebdavPath(OwnCloudVersion version
, String authTokenType
) {
138 if (version
!= null
) {
139 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).equals(authTokenType
)) {
142 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).equals(authTokenType
)) {
143 return SAML_SSO_PATH
;
145 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
146 return WEBDAV_PATH_4_0
;
147 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
148 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
149 return WEBDAV_PATH_2_0
;
150 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
151 return WEBDAV_PATH_1_2
;