de87660c07a2b96607272fec8457ebe62be6c369
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / 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 eu.alefzero.owncloud;
20
21 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
22 import eu.alefzero.owncloud.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/webdav.php";
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 account in the
40 * preferences
41 *
42 * @param context
43 * 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 public static void setCurrentOwnCloudAccount(Context context, String name) {
73 SharedPreferences.Editor appPrefs = PreferenceManager
74 .getDefaultSharedPreferences(context).edit();
75 appPrefs.putString("select_oc_account", name);
76 appPrefs.commit();
77 }
78
79 /**
80 *
81 * @param version
82 * version of owncloud
83 * @return webdav path for given OC version, null if OC version unknown
84 */
85 public static String getWebdavPath(OwnCloudVersion version) {
86 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
87 return WEBDAV_PATH_4_0;
88 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
89 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
90 return WEBDAV_PATH_2_0;
91 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
92 return WEBDAV_PATH_1_2;
93 return null;
94 }
95
96 }