OAuth clean-up and refactoring
[pub/Android/ownCloud.git] / src / com / owncloud / android / 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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android;
21
22 import com.owncloud.android.authentication.AccountAuthenticator;
23 import com.owncloud.android.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 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
37 public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
38 public static final String STATUS_PATH = "/status.php";
39
40 /**
41 * Can be used to get the currently selected ownCloud account in the
42 * preferences
43 *
44 * @param context The current appContext
45 * @return The current account or first available, if none is available,
46 * then 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 if (accountName != null) {
59 for (Account account : ocAccounts) {
60 if (account.name.equals(accountName)) {
61 defaultAccount = account;
62 break;
63 }
64 }
65 }
66
67 if (defaultAccount == null && ocAccounts.length != 0) {
68 // we at least need to take first account as fallback
69 defaultAccount = ocAccounts[0];
70 }
71
72 return defaultAccount;
73 }
74
75
76
77 /**
78 * Checks, whether or not there are any ownCloud accounts setup.
79 *
80 * @return true, if there is at least one account.
81 */
82 public static boolean accountsAreSetup(Context context) {
83 AccountManager accMan = AccountManager.get(context);
84 Account[] accounts = accMan
85 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
86 return accounts.length > 0;
87 }
88
89
90 public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
91 boolean result = false;
92 if (accountName != null) {
93 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
94 AccountAuthenticator.ACCOUNT_TYPE);
95 boolean found = false;
96 for (Account account : ocAccounts) {
97 found = (account.name.equals(accountName));
98 if (found) {
99 SharedPreferences.Editor appPrefs = PreferenceManager
100 .getDefaultSharedPreferences(context).edit();
101 appPrefs.putString("select_oc_account", accountName);
102
103 appPrefs.commit();
104 result = true;
105 break;
106 }
107 }
108 }
109 return result;
110 }
111
112 /**
113 *
114 * @param version version of owncloud
115 * @return webdav path for given OC version, null if OC version unknown
116 */
117 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) {
118 if (version != null) {
119 if (supportsOAuth) {
120 return ODAV_PATH;
121 }
122 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
123 return WEBDAV_PATH_4_0;
124 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
125 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
126 return WEBDAV_PATH_2_0;
127 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
128 return WEBDAV_PATH_1_2;
129 }
130 return null;
131 }
132
133 /**
134 * Constructs full url to host and webdav resource basing on host version
135 * @param context
136 * @param account
137 * @return url or null on failure
138 */
139 public static String constructFullURLForAccount(Context context, Account account) {
140 try {
141 AccountManager ama = AccountManager.get(context);
142 String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
143 String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);
144 boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);
145 OwnCloudVersion ver = new OwnCloudVersion(strver);
146 String webdavpath = getWebdavPath(ver, supportsOAuth);
147
148 if (webdavpath == null) return null;
149 return baseurl + webdavpath;
150 } catch (Exception e) {
151 e.printStackTrace();
152 return null;
153 }
154 }
155
156 }