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
.utils
.OwnCloudVersion
;
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
;
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 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 AccountAuthenticator
.ACCOUNT_TYPE
);
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
;
79 * Checks, whether or not there are any ownCloud accounts setup.
81 * @return true, if there is at least one account.
83 public static boolean accountsAreSetup(Context context
) {
84 AccountManager accMan
= AccountManager
.get(context
);
85 Account
[] accounts
= accMan
86 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
87 return accounts
.length
> 0;
91 public static boolean setCurrentOwnCloudAccount(Context context
, String accountName
) {
92 boolean result
= false
;
93 if (accountName
!= null
) {
94 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
95 AccountAuthenticator
.ACCOUNT_TYPE
);
96 boolean found
= false
;
97 for (Account account
: ocAccounts
) {
98 found
= (account
.name
.equals(accountName
));
100 SharedPreferences
.Editor appPrefs
= PreferenceManager
101 .getDefaultSharedPreferences(context
).edit();
102 appPrefs
.putString("select_oc_account", accountName
);
115 * @param version version of owncloud
116 * @return webdav path for given OC version, null if OC version unknown
118 public static String
getWebdavPath(OwnCloudVersion version
, boolean supportsOAuth
) {
119 if (version
!= null
) {
123 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
124 return WEBDAV_PATH_4_0
;
125 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
126 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
127 return WEBDAV_PATH_2_0
;
128 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
129 return WEBDAV_PATH_1_2
;
135 * Constructs full url to host and webdav resource basing on host version
138 * @return url or null on failure
139 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
141 public static String
constructFullURLForAccount(Context context
, Account account
) throws AccountNotFoundException
{
142 AccountManager ama
= AccountManager
.get(context
);
143 String baseurl
= ama
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
);
144 String strver
= ama
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
);
145 boolean supportsOAuth
= (ama
.getUserData(account
, AccountAuthenticator
.KEY_SUPPORTS_OAUTH2
) != null
);
146 OwnCloudVersion ver
= new OwnCloudVersion(strver
);
147 String webdavpath
= getWebdavPath(ver
, supportsOAuth
);
149 if (baseurl
== null
|| webdavpath
== null
)
150 throw new AccountNotFoundException(account
, "Account not found", null
);
152 return baseurl
+ webdavpath
;
156 public static class AccountNotFoundException
extends AccountsException
{
158 private static final long serialVersionUID
= 4276870654168776992L;
160 private Account mFailedAccount
;
162 public AccountNotFoundException(Account failedAccount
, String message
, Throwable cause
) {
163 super(message
, cause
);
164 mFailedAccount
= failedAccount
;
167 public Account
getFailedAccount() {
168 return mFailedAccount
;