1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
;
63 } else if (ocAccounts
.length
!= 0) {
64 // we at least need to take first account as fallback
65 defaultAccount
= ocAccounts
[0];
68 return defaultAccount
;
74 * Checks, whether or not there are any ownCloud accounts setup.
76 * @return true, if there is at least one account.
78 public static boolean accountsAreSetup(Context context
) {
79 AccountManager accMan
= AccountManager
.get(context
);
80 Account
[] accounts
= accMan
81 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
82 return accounts
.length
> 0;
86 public static void setCurrentOwnCloudAccount(Context context
, String name
) {
87 SharedPreferences
.Editor appPrefs
= PreferenceManager
88 .getDefaultSharedPreferences(context
).edit();
89 appPrefs
.putString("select_oc_account", name
);
95 * @param version version of owncloud
96 * @return webdav path for given OC version, null if OC version unknown
98 public static String
getWebdavPath(OwnCloudVersion version
) {
99 if (version
!= null
) {
100 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
101 return WEBDAV_PATH_4_0
;
102 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
103 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
104 return WEBDAV_PATH_2_0
;
105 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
106 return WEBDAV_PATH_1_2
;