2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.authentication
;
23 import java
.util
.Locale
;
25 import com
.owncloud
.android
.MainApp
;
26 import com
.owncloud
.android
.lib
.common
.accounts
.AccountTypeUtils
;
27 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
29 import android
.accounts
.Account
;
30 import android
.accounts
.AccountManager
;
31 import android
.content
.Context
;
32 import android
.content
.SharedPreferences
;
33 import android
.preference
.PreferenceManager
;
35 public class AccountUtils
{
36 public static final String WEBDAV_PATH_1_2
= "/webdav/owncloud.php";
37 public static final String WEBDAV_PATH_2_0
= "/files/webdav.php";
38 public static final String WEBDAV_PATH_4_0
= "/remote.php/webdav";
39 private static final String ODAV_PATH
= "/remote.php/odav";
40 private static final String SAML_SSO_PATH
= "/remote.php/webdav";
41 public static final String CARDDAV_PATH_2_0
= "/apps/contacts/carddav.php";
42 public static final String CARDDAV_PATH_4_0
= "/remote/carddav.php";
43 public static final String STATUS_PATH
= "/status.php";
46 * Can be used to get the currently selected ownCloud {@link Account} in the
47 * application preferences.
49 * @param context The current application {@link Context}
50 * @return The ownCloud {@link Account} currently saved in preferences, or the first
51 * {@link Account} available, if valid (still registered in the system as ownCloud
52 * account). If none is available and valid, returns null.
54 public static Account
getCurrentOwnCloudAccount(Context context
) {
55 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
56 MainApp
.getAccountType());
57 Account defaultAccount
= null
;
59 SharedPreferences appPreferences
= PreferenceManager
60 .getDefaultSharedPreferences(context
);
61 String accountName
= appPreferences
62 .getString("select_oc_account", null
);
64 // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
65 if (accountName
!= null
) {
66 for (Account account
: ocAccounts
) {
67 if (account
.name
.equals(accountName
)) {
68 defaultAccount
= account
;
74 if (defaultAccount
== null
&& ocAccounts
.length
!= 0) {
75 // take first account as fallback
76 defaultAccount
= ocAccounts
[0];
79 return defaultAccount
;
83 public static boolean exists(Account account
, Context context
) {
84 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
85 MainApp
.getAccountType());
87 if (account
!= null
&& account
.name
!= null
) {
88 int lastAtPos
= account
.name
.lastIndexOf("@");
89 String hostAndPort
= account
.name
.substring(lastAtPos
+ 1);
90 String username
= account
.name
.substring(0, lastAtPos
);
91 String otherHostAndPort
, otherUsername
;
92 Locale currentLocale
= context
.getResources().getConfiguration().locale
;
93 for (Account otherAccount
: ocAccounts
) {
94 lastAtPos
= otherAccount
.name
.lastIndexOf("@");
95 otherHostAndPort
= otherAccount
.name
.substring(lastAtPos
+ 1);
96 otherUsername
= otherAccount
.name
.substring(0, lastAtPos
);
97 if (otherHostAndPort
.equals(hostAndPort
) &&
98 otherUsername
.toLowerCase(currentLocale
).
99 equals(username
.toLowerCase(currentLocale
))) {
109 * Checks, whether or not there are any ownCloud accounts setup.
111 * @return true, if there is at least one account.
113 public static boolean accountsAreSetup(Context context
) {
114 AccountManager accMan
= AccountManager
.get(context
);
115 Account
[] accounts
= accMan
116 .getAccountsByType(MainApp
.getAccountType());
117 return accounts
.length
> 0;
121 public static boolean setCurrentOwnCloudAccount(Context context
, String accountName
) {
122 boolean result
= false
;
123 if (accountName
!= null
) {
124 Account
[] ocAccounts
= AccountManager
.get(context
).getAccountsByType(
125 MainApp
.getAccountType());
126 boolean found
= false
;
127 for (Account account
: ocAccounts
) {
128 found
= (account
.name
.equals(accountName
));
130 SharedPreferences
.Editor appPrefs
= PreferenceManager
131 .getDefaultSharedPreferences(context
).edit();
132 appPrefs
.putString("select_oc_account", accountName
);
144 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
145 * according to its version and the authorization method used.
147 * @param version Version of ownCloud server.
148 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
149 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
151 public static String
getWebdavPath(OwnCloudVersion version
, String authTokenType
) {
152 if (version
!= null
) {
153 if (AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType()).equals(authTokenType
)) {
156 if (AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType()).equals(authTokenType
)) {
157 return SAML_SSO_PATH
;
159 if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0)
160 return WEBDAV_PATH_4_0
;
161 if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0
162 || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0)
163 return WEBDAV_PATH_2_0
;
164 if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0)
165 return WEBDAV_PATH_1_2
;