Added support to OAuth2 'authorization code' grat type: get & save access token,...
[pub/Android/ownCloud.git] / src / com / owncloud / android / AccountUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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;
20
21 import com.owncloud.android.authenticator.AccountAuthenticator;
22 import com.owncloud.android.utils.OwnCloudVersion;
23
24 import android.accounts.Account;
25 import android.accounts.AccountManager;
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 account in the
41 * preferences
42 *
43 * @param context The current appContext
44 * @return The current account or first available, if none is available,
45 * then null.
46 */
47 public static Account getCurrentOwnCloudAccount(Context context) {
48 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
49 AccountAuthenticator.ACCOUNT_TYPE);
50 Account defaultAccount = null;
51
52 SharedPreferences appPreferences = PreferenceManager
53 .getDefaultSharedPreferences(context);
54 String accountName = appPreferences
55 .getString("select_oc_account", null);
56
57 if (accountName != null) {
58 for (Account account : ocAccounts) {
59 if (account.name.equals(accountName)) {
60 defaultAccount = account;
61 break;
62 }
63 }
64 } else if (ocAccounts.length != 0) {
65 // we at least need to take first account as fallback
66 defaultAccount = ocAccounts[0];
67 }
68
69 return defaultAccount;
70 }
71
72
73
74 /**
75 * Checks, whether or not there are any ownCloud accounts setup.
76 *
77 * @return true, if there is at least one account.
78 */
79 public static boolean accountsAreSetup(Context context) {
80 AccountManager accMan = AccountManager.get(context);
81 Account[] accounts = accMan
82 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
83 return accounts.length > 0;
84 }
85
86
87 public static void setCurrentOwnCloudAccount(Context context, String name) {
88 SharedPreferences.Editor appPrefs = PreferenceManager
89 .getDefaultSharedPreferences(context).edit();
90 appPrefs.putString("select_oc_account", name);
91 appPrefs.commit();
92 }
93
94 /**
95 *
96 * @param version version of owncloud
97 * @return webdav path for given OC version, null if OC version unknown
98 */
99 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) {
100 if (supportsOAuth) {
101 return ODAV_PATH;
102 }
103 if (version != null) {
104 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
105 return WEBDAV_PATH_4_0;
106 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
107 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
108 return WEBDAV_PATH_2_0;
109 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
110 return WEBDAV_PATH_1_2;
111 }
112 return null;
113 }
114
115 /**
116 * Constructs full url to host and webdav resource basing on host version
117 * @param context
118 * @param account
119 * @return url or null on failure
120 */
121 public static String constructFullURLForAccount(Context context, Account account) {
122 try {
123 AccountManager ama = AccountManager.get(context);
124 String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
125 String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);
126 boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);
127 OwnCloudVersion ver = new OwnCloudVersion(strver);
128 String webdavpath = getWebdavPath(ver, supportsOAuth);
129
130 if (webdavpath == null) return null;
131 return baseurl + webdavpath;
132 } catch (Exception e) {
133 e.printStackTrace();
134 return null;
135 }
136 }
137
138 }