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
;
21 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
22 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountManager
;
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 public static final String CARDDAV_PATH_2_0
= "/apps/contacts/carddav.php";
35 public static final String CARDDAV_PATH_4_0
= "/remote/carddav.php";
36 public static final String STATUS_PATH
= "/status.php";
39 * Can be used to get the currently selected ownCloud account in the
42 * @param context The current appContext
43 * @return The current account or first available, if none is available,
46 public static Account
getCurrentOwnCloudAccount(Context context
) {
47 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
48 AccountAuthenticator
.ACCOUNT_TYPE
);
49 Account defaultAccount
= null
;
51 SharedPreferences appPreferences
= PreferenceManager
52 .getDefaultSharedPreferences(context
);
53 String accountName
= appPreferences
54 .getString("select_oc_account", null
);
56 if (accountName
!= null
) {
57 for (Account account
: ocAccounts
) {
58 if (account
.name
.equals(accountName
)) {
59 defaultAccount
= account
;
65 if (defaultAccount
== null
&& ocAccounts
.length
!= 0) {
66 // we at least need to take first account as fallback
67 defaultAccount
= ocAccounts
[0];
70 return defaultAccount
;
76 * Checks, whether or not there are any ownCloud accounts setup.
78 * @return true, if there is at least one account.
80 public static boolean accountsAreSetup(Context context
) {
81 AccountManager accMan
= AccountManager
.get(context
);
82 Account
[] accounts
= accMan
83 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
84 return accounts
.length
> 0;
88 public static boolean setCurrentOwnCloudAccount(Context context
, String accountName
) {
89 boolean result
= false
;
90 if (accountName
!= null
) {
91 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
92 AccountAuthenticator
.ACCOUNT_TYPE
);
93 boolean found
= false
;
94 for (Account account
: ocAccounts
) {
95 found
= (account
.name
.equals(accountName
));
97 SharedPreferences
.Editor appPrefs
= PreferenceManager
98 .getDefaultSharedPreferences(context
).edit();
99 appPrefs
.putString("select_oc_account", accountName
);
112 * @param version version of owncloud
113 * @return webdav path for given OC version, null if OC version unknown
115 public static String
getWebdavPath(OwnCloudVersion version
) {
116 if (version
!= null
) {
117 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
118 return WEBDAV_PATH_4_0
;
119 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
120 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
121 return WEBDAV_PATH_2_0
;
122 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
123 return WEBDAV_PATH_1_2
;
129 * Constructs full url to host and webdav resource basing on host version
132 * @return url or null on failure
134 public static String
constructFullURLForAccount(Context context
, Account account
) {
136 AccountManager ama
= AccountManager
.get(context
);
137 String baseurl
= ama
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
);
138 String strver
= ama
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
);
139 OwnCloudVersion ver
= new OwnCloudVersion(strver
);
140 String webdavpath
= getWebdavPath(ver
);
142 if (webdavpath
== null
) return null
;
143 return baseurl
+ webdavpath
;
144 } catch (Exception e
) {