OC-208: Changes from comments
[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 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;
20
21 import com.owncloud.android.authentication.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 }
65
66 if (defaultAccount == null && ocAccounts.length != 0) {
67 // we at least need to take first account as fallback
68 defaultAccount = ocAccounts[0];
69 }
70
71 return defaultAccount;
72 }
73
74
75
76 /**
77 * Checks, whether or not there are any ownCloud accounts setup.
78 *
79 * @return true, if there is at least one account.
80 */
81 public static boolean accountsAreSetup(Context context) {
82 AccountManager accMan = AccountManager.get(context);
83 Account[] accounts = accMan
84 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
85 return accounts.length > 0;
86 }
87
88
89 public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
90 boolean result = false;
91 if (accountName != null) {
92 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
93 AccountAuthenticator.ACCOUNT_TYPE);
94 boolean found = false;
95 for (Account account : ocAccounts) {
96 found = (account.name.equals(accountName));
97 if (found) {
98 SharedPreferences.Editor appPrefs = PreferenceManager
99 .getDefaultSharedPreferences(context).edit();
100 appPrefs.putString("select_oc_account", accountName);
101
102 appPrefs.commit();
103 result = true;
104 break;
105 }
106 }
107 }
108 return result;
109 }
110
111 /**
112 *
113 * @param version version of owncloud
114 * @return webdav path for given OC version, null if OC version unknown
115 */
116 public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) {
117 if (version != null) {
118 if (supportsOAuth) {
119 return ODAV_PATH;
120 }
121 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
122 return WEBDAV_PATH_4_0;
123 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
124 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
125 return WEBDAV_PATH_2_0;
126 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
127 return WEBDAV_PATH_1_2;
128 }
129 return null;
130 }
131
132 /**
133 * Constructs full url to host and webdav resource basing on host version
134 * @param context
135 * @param account
136 * @return url or null on failure
137 */
138 public static String constructFullURLForAccount(Context context, Account account) {
139 try {
140 AccountManager ama = AccountManager.get(context);
141 String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
142 String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);
143 boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);
144 OwnCloudVersion ver = new OwnCloudVersion(strver);
145 String webdavpath = getWebdavPath(ver, supportsOAuth);
146
147 if (webdavpath == null) return null;
148 return baseurl + webdavpath;
149 } catch (Exception e) {
150 e.printStackTrace();
151 return null;
152 }
153 }
154
155 }