2dacc7d0dcf9a03d34abbb5d87189029cbbd56db
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AccountUtils.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 package com.owncloud.android.authentication;
22
23 import java.util.Locale;
24
25 import com.owncloud.android.MainApp;
26 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
27 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
28
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;
34
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";
44
45 /**
46 * Can be used to get the currently selected ownCloud {@link Account} in the
47 * application preferences.
48 *
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.
53 */
54 public static Account getCurrentOwnCloudAccount(Context context) {
55 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
56 MainApp.getAccountType());
57 Account defaultAccount = null;
58
59 SharedPreferences appPreferences = PreferenceManager
60 .getDefaultSharedPreferences(context);
61 String accountName = appPreferences
62 .getString("select_oc_account", null);
63
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;
69 break;
70 }
71 }
72 }
73
74 if (defaultAccount == null && ocAccounts.length != 0) {
75 // take first account as fallback
76 defaultAccount = ocAccounts[0];
77 }
78
79 return defaultAccount;
80 }
81
82
83 public static boolean exists(Account account, Context context) {
84 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
85 MainApp.getAccountType());
86
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))) {
100 return true;
101 }
102 }
103 }
104 return false;
105 }
106
107
108 public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
109 boolean result = false;
110 if (accountName != null) {
111 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
112 MainApp.getAccountType());
113 boolean found = false;
114 for (Account account : ocAccounts) {
115 found = (account.name.equals(accountName));
116 if (found) {
117 SharedPreferences.Editor appPrefs = PreferenceManager
118 .getDefaultSharedPreferences(context).edit();
119 appPrefs.putString("select_oc_account", accountName);
120
121 appPrefs.commit();
122 result = true;
123 break;
124 }
125 }
126 }
127 return result;
128 }
129
130 /**
131 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
132 * according to its version and the authorization method used.
133 *
134 * @param version Version of ownCloud server.
135 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
136 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
137 */
138 public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
139 if (version != null) {
140 if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(authTokenType)) {
141 return ODAV_PATH;
142 }
143 if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) {
144 return SAML_SSO_PATH;
145 }
146 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
147 return WEBDAV_PATH_4_0;
148 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
149 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
150 return WEBDAV_PATH_2_0;
151 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
152 return WEBDAV_PATH_1_2;
153 }
154 return null;
155 }
156
157 }