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 java
.util
.Locale
;
23 import com
.owncloud
.android
.MainApp
;
24 import com
.owncloud
.android
.lib
.common
.accounts
.AccountTypeUtils
;
25 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
27 import android
.accounts
.Account
;
28 import android
.accounts
.AccountManager
;
29 import android
.content
.Context
;
30 import android
.content
.SharedPreferences
;
31 import android
.preference
.PreferenceManager
;
33 public class AccountUtils
{
34 public static final String WEBDAV_PATH_1_2
= "/webdav/owncloud.php";
35 public static final String WEBDAV_PATH_2_0
= "/files/webdav.php";
36 public static final String WEBDAV_PATH_4_0
= "/remote.php/webdav";
37 private static final String ODAV_PATH
= "/remote.php/odav";
38 private static final String SAML_SSO_PATH
= "/remote.php/webdav";
39 public static final String CARDDAV_PATH_2_0
= "/apps/contacts/carddav.php";
40 public static final String CARDDAV_PATH_4_0
= "/remote/carddav.php";
41 public static final String STATUS_PATH
= "/status.php";
44 * Can be used to get the currently selected ownCloud {@link Account} in the
45 * application preferences.
47 * @param context The current application {@link Context}
48 * @return The ownCloud {@link Account} currently saved in preferences, or the first
49 * {@link Account} available, if valid (still registered in the system as ownCloud
50 * account). If none is available and valid, returns null.
52 public static Account
getCurrentOwnCloudAccount(Context context
) {
53 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
54 MainApp
.getAccountType());
55 Account defaultAccount
= null
;
57 SharedPreferences appPreferences
= PreferenceManager
58 .getDefaultSharedPreferences(context
);
59 String accountName
= appPreferences
60 .getString("select_oc_account", null
);
62 // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
63 if (accountName
!= null
) {
64 for (Account account
: ocAccounts
) {
65 if (account
.name
.equals(accountName
)) {
66 defaultAccount
= account
;
72 if (defaultAccount
== null
&& ocAccounts
.length
!= 0) {
73 // take first account as fallback
74 defaultAccount
= ocAccounts
[0];
77 return defaultAccount
;
81 public static boolean exists(Account account
, Context context
) {
82 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
83 MainApp
.getAccountType());
85 if (account
!= null
&& account
.name
!= null
) {
86 int lastAtPos
= account
.name
.lastIndexOf("@");
87 String hostAndPort
= account
.name
.substring(lastAtPos
+ 1);
88 String username
= account
.name
.substring(0, lastAtPos
);
89 String otherHostAndPort
, otherUsername
;
90 Locale currentLocale
= context
.getResources().getConfiguration().locale
;
91 for (Account otherAccount
: ocAccounts
) {
92 lastAtPos
= otherAccount
.name
.lastIndexOf("@");
93 otherHostAndPort
= otherAccount
.name
.substring(lastAtPos
+ 1);
94 otherUsername
= otherAccount
.name
.substring(0, lastAtPos
);
95 if (otherHostAndPort
.equals(hostAndPort
) &&
96 otherUsername
.toLowerCase(currentLocale
).
97 equals(username
.toLowerCase(currentLocale
))) {
107 * Checks, whether or not there are any ownCloud accounts setup.
109 * @return true, if there is at least one account.
111 public static boolean accountsAreSetup(Context context
) {
112 AccountManager accMan
= AccountManager
.get(context
);
113 Account
[] accounts
= accMan
114 .getAccountsByType(MainApp
.getAccountType());
115 return accounts
.length
> 0;
119 public static boolean setCurrentOwnCloudAccount(Context context
, String accountName
) {
120 boolean result
= false
;
121 if (accountName
!= null
) {
122 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
123 MainApp
.getAccountType());
124 boolean found
= false
;
125 for (Account account
: ocAccounts
) {
126 found
= (account
.name
.equals(accountName
));
128 SharedPreferences
.Editor appPrefs
= PreferenceManager
129 .getDefaultSharedPreferences(context
).edit();
130 appPrefs
.putString("select_oc_account", accountName
);
142 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
143 * according to its version and the authorization method used.
145 * @param version Version of ownCloud server.
146 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
147 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
149 public static String
getWebdavPath(OwnCloudVersion version
, String authTokenType
) {
150 if (version
!= null
) {
151 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).equals(authTokenType
)) {
154 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).equals(authTokenType
)) {
155 return SAML_SSO_PATH
;
157 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
158 return WEBDAV_PATH_4_0
;
159 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
160 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
161 return WEBDAV_PATH_2_0
;
162 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
163 return WEBDAV_PATH_1_2
;