10f777193981f2276029974d727f837f0dee4128
[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.content.Context;
26 import android.content.SharedPreferences;
27 import android.preference.PreferenceManager;
28
29 public class AccountUtils {
30 public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
31 public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
32 public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
33 private static final String ODAV_PATH = "/remote.php/odav";
34 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
35 public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";
36 public static final String STATUS_PATH = "/status.php";
37
38 /**
39 * Can be used to get the currently selected ownCloud {@link Account} in the
40 * application preferences.
41 *
42 * @param context The current application {@link Context}
43 * @return The ownCloud {@link Account} currently saved in preferences, or the first
44 * {@link Account} available, if valid (still registered in the system as ownCloud
45 * account). If none is available and valid, returns 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 // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager
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 // 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 }