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 eu
.alefzero
.owncloud
;
21 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
22 import eu
.alefzero
.owncloud
.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/webdav.php";
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 preferences
41 * @param context The current appContext
42 * @return The current account or first available, if none is available, then null.
44 public static Account
getCurrentOwnCloudAccount(Context context
) {
45 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
46 Account defaultAccount
= null
;
48 SharedPreferences appPreferences
= PreferenceManager
.getDefaultSharedPreferences(context
);
49 String accountName
= appPreferences
.getString("select_oc_account", null
);
51 if(accountName
!= null
){
52 for(Account account
: ocAccounts
){
53 if(account
.name
.equals(accountName
)){
54 defaultAccount
= account
;
58 } else if (ocAccounts
.length
!= 0) {
59 // we at least need to take first account as fallback
60 defaultAccount
= ocAccounts
[0];
63 return defaultAccount
;
66 public static void setCurrentOwnCloudAccount(Context context
, String name
) {
67 SharedPreferences
.Editor appPrefs
= PreferenceManager
.getDefaultSharedPreferences(context
).edit();
68 appPrefs
.putString("select_oc_account", name
);
74 * @param version version of owncloud
75 * @return webdav path for given OC version, null if OC version unknown
77 public static String
getWebdavPath(OwnCloudVersion version
) {
78 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
79 return WEBDAV_PATH_4_0
;
80 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0 ||
81 version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
82 return WEBDAV_PATH_2_0
;
83 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
84 return WEBDAV_PATH_1_2
;