Merge pull request #195 from owncloud/fixed_crash_due_to_uploads_to_accounts_not_alre...
[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.utils.OwnCloudVersion;
22
23 import android.accounts.Account;
24 import android.accounts.AccountManager;
25 import android.accounts.AccountsException;
26 import android.content.Context;
27 import android.content.SharedPreferences;
28 import android.preference.PreferenceManager;
29
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.php/webdav";
34 private static final String ODAV_PATH = "/remote.php/odav";
35 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
36 public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
37 public static final String STATUS_PATH = "/status.php";
38
39 /**
40 * Can be used to get the currently selected ownCloud {@link Account} in the
41 * application preferences.
42 *
43 * @param context The current application {@link Context}
44 * @return The ownCloud {@link Account} currently saved in preferences, or the first
45 * {@link Account} available, if valid (still registered in the system as ownCloud
46 * account). If none is available and valid, returns null.
47 */
48 public static Account getCurrentOwnCloudAccount(Context context) {
49 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
50 AccountAuthenticator.ACCOUNT_TYPE);
51 Account defaultAccount = null;
52
53 SharedPreferences appPreferences = PreferenceManager
54 .getDefaultSharedPreferences(context);
55 String accountName = appPreferences
56 .getString("select_oc_account", null);
57
58 // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
59 if (accountName != null) {
60 for (Account account : ocAccounts) {
61 if (account.name.equals(accountName)) {
62 defaultAccount = account;
63 break;
64 }
65 }
66 }
67
68 if (defaultAccount == null && ocAccounts.length != 0) {
69 // take first account as fallback
70 defaultAccount = ocAccounts[0];
71 }
72
73 return defaultAccount;
74 }
75
76
77
78 /**
79 * Checks, whether or not there are any ownCloud accounts setup.
80 *
81 * @return true, if there is at least one account.
82 */
83 public static boolean accountsAreSetup(Context context) {
84 AccountManager accMan = AccountManager.get(context);
85 Account[] accounts = accMan
86 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
87 return accounts.length > 0;
88 }
89
90
91 public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
92 boolean result = false;
93 if (accountName != null) {
94 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
95 AccountAuthenticator.ACCOUNT_TYPE);
96 boolean found = false;
97 for (Account account : ocAccounts) {
98 found = (account.name.equals(accountName));
99 if (found) {
100 SharedPreferences.Editor appPrefs = PreferenceManager
101 .getDefaultSharedPreferences(context).edit();
102 appPrefs.putString("select_oc_account", accountName);
103
104 appPrefs.commit();
105 result = true;
106 break;
107 }
108 }
109 }
110 return result;
111 }
112
113 /**
114 *
115 * @param version version of owncloud
116 * @return webdav path for given OC version, null if OC version unknown
117 */
118 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) {
119 if (version != null) {
120 if (supportsOAuth) {
121 return ODAV_PATH;
122 }
123 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
124 return WEBDAV_PATH_4_0;
125 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
126 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
127 return WEBDAV_PATH_2_0;
128 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
129 return WEBDAV_PATH_1_2;
130 }
131 return null;
132 }
133
134 /**
135 * Constructs full url to host and webdav resource basing on host version
136 * @param context
137 * @param account
138 * @return url or null on failure
139 * @throws AccountNotFoundException When 'account' is unknown for the AccountManager
140 */
141 public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {
142 AccountManager ama = AccountManager.get(context);
143 String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
144 String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);
145 boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);
146 OwnCloudVersion ver = new OwnCloudVersion(strver);
147 String webdavpath = getWebdavPath(ver, supportsOAuth);
148
149 if (baseurl == null || webdavpath == null)
150 throw new AccountNotFoundException(account, "Account not found", null);
151
152 return baseurl + webdavpath;
153 }
154
155
156 public static class AccountNotFoundException extends AccountsException {
157
158 private static final long serialVersionUID = 4276870654168776992L;
159
160 private Account mFailedAccount;
161
162 public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {
163 super(message, cause);
164 mFailedAccount = failedAccount;
165 }
166
167 public Account getFailedAccount() {
168 return mFailedAccount;
169 }
170 }
171
172 }