Merge pull request #360 from owncloud/oc_framework_cleanup_and_documentation
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AccountUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.authentication;
20
21 import com.owncloud.android.MainApp;
22 import com.owncloud.android.lib.accounts.AccountTypeUtils;
23 import com.owncloud.android.lib.utils.OwnCloudVersion;
24
25 import android.accounts.Account;
26 import android.accounts.AccountManager;
27 import android.content.Context;
28 import android.content.SharedPreferences;
29 import android.preference.PreferenceManager;
30
31 public class AccountUtils {
32 public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
33 public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
34 public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
35 private static final String ODAV_PATH = "/remote.php/odav";
36 private static final String SAML_SSO_PATH = "/remote.php/webdav";
37 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
38 public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
39 public static final String STATUS_PATH = "/status.php";
40
41 /**
42 * Can be used to get the currently selected ownCloud {@link Account} in the
43 * application preferences.
44 *
45 * @param context The current application {@link Context}
46 * @return The ownCloud {@link Account} currently saved in preferences, or the first
47 * {@link Account} available, if valid (still registered in the system as ownCloud
48 * account). If none is available and valid, returns null.
49 */
50 public static Account getCurrentOwnCloudAccount(Context context) {
51 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
52 MainApp.getAccountType());
53 Account defaultAccount = null;
54
55 SharedPreferences appPreferences = PreferenceManager
56 .getDefaultSharedPreferences(context);
57 String accountName = appPreferences
58 .getString("select_oc_account", null);
59
60 // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
61 if (accountName != null) {
62 for (Account account : ocAccounts) {
63 if (account.name.equals(accountName)) {
64 defaultAccount = account;
65 break;
66 }
67 }
68 }
69
70 if (defaultAccount == null && ocAccounts.length != 0) {
71 // take first account as fallback
72 defaultAccount = ocAccounts[0];
73 }
74
75 return defaultAccount;
76 }
77
78
79 public static boolean exists(Account account, Context context) {
80 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
81 MainApp.getAccountType());
82
83 if (account != null && account.name != null) {
84 for (Account ac : ocAccounts) {
85 if (ac.name.equals(account.name)) {
86 return true;
87 }
88 }
89 }
90 return false;
91 }
92
93
94 /**
95 * Checks, whether or not there are any ownCloud accounts setup.
96 *
97 * @return true, if there is at least one account.
98 */
99 public static boolean accountsAreSetup(Context context) {
100 AccountManager accMan = AccountManager.get(context);
101 Account[] accounts = accMan
102 .getAccountsByType(MainApp.getAccountType());
103 return accounts.length > 0;
104 }
105
106
107 public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
108 boolean result = false;
109 if (accountName != null) {
110 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
111 MainApp.getAccountType());
112 boolean found = false;
113 for (Account account : ocAccounts) {
114 found = (account.name.equals(accountName));
115 if (found) {
116 SharedPreferences.Editor appPrefs = PreferenceManager
117 .getDefaultSharedPreferences(context).edit();
118 appPrefs.putString("select_oc_account", accountName);
119
120 appPrefs.commit();
121 result = true;
122 break;
123 }
124 }
125 }
126 return result;
127 }
128
129 /**
130 * Returns the proper URL path to access the WebDAV interface of an ownCloud server,
131 * according to its version and the authorization method used.
132 *
133 * @param version Version of ownCloud server.
134 * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}.
135 * @return WebDAV path for given OC version and authorization method, null if OC version is unknown.
136 */
137 public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {
138 if (version != null) {
139 if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(authTokenType)) {
140 return ODAV_PATH;
141 }
142 if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) {
143 return SAML_SSO_PATH;
144 }
145 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
146 return WEBDAV_PATH_4_0;
147 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
148 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
149 return WEBDAV_PATH_2_0;
150 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
151 return WEBDAV_PATH_1_2;
152 }
153 return null;
154 }
155
156 }