Fixed bug when turning tablet with no file in the right fragment
[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.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 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 The current appContext
43 * @return The current account or first available, if none is available,
44 * then null.
45 */
46 public static Account getCurrentOwnCloudAccount(Context context) {
47 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
48 AccountAuthenticator.ACCOUNT_TYPE);
49 Account defaultAccount = null;
50
51 SharedPreferences appPreferences = PreferenceManager
52 .getDefaultSharedPreferences(context);
53 String accountName = appPreferences
54 .getString("select_oc_account", null);
55
56 if (accountName != null) {
57 for (Account account : ocAccounts) {
58 if (account.name.equals(accountName)) {
59 defaultAccount = account;
60 break;
61 }
62 }
63 }
64
65 if (defaultAccount == null && ocAccounts.length != 0) {
66 // we at least need to take first account as fallback
67 defaultAccount = ocAccounts[0];
68 }
69
70 return defaultAccount;
71 }
72
73
74
75 /**
76 * Checks, whether or not there are any ownCloud accounts setup.
77 *
78 * @return true, if there is at least one account.
79 */
80 public static boolean accountsAreSetup(Context context) {
81 AccountManager accMan = AccountManager.get(context);
82 Account[] accounts = accMan
83 .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
84 return accounts.length > 0;
85 }
86
87
88 public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {
89 boolean result = false;
90 if (accountName != null) {
91 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(
92 AccountAuthenticator.ACCOUNT_TYPE);
93 boolean found = false;
94 for (Account account : ocAccounts) {
95 found = (account.name.equals(accountName));
96 if (found) {
97 SharedPreferences.Editor appPrefs = PreferenceManager
98 .getDefaultSharedPreferences(context).edit();
99 appPrefs.putString("select_oc_account", accountName);
100
101 appPrefs.commit();
102 result = true;
103 break;
104 }
105 }
106 }
107 return result;
108 }
109
110 /**
111 *
112 * @param version version of owncloud
113 * @return webdav path for given OC version, null if OC version unknown
114 */
115 public static String getWebdavPath(OwnCloudVersion version) {
116 if (version != null) {
117 if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)
118 return WEBDAV_PATH_4_0;
119 if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0
120 || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)
121 return WEBDAV_PATH_2_0;
122 if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)
123 return WEBDAV_PATH_1_2;
124 }
125 return null;
126 }
127
128 /**
129 * Constructs full url to host and webdav resource basing on host version
130 * @param context
131 * @param account
132 * @return url or null on failure
133 */
134 public static String constructFullURLForAccount(Context context, Account account) {
135 try {
136 AccountManager ama = AccountManager.get(context);
137 String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
138 String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);
139 OwnCloudVersion ver = new OwnCloudVersion(strver);
140 String webdavpath = getWebdavPath(ver);
141
142 if (webdavpath == null) return null;
143 return baseurl + webdavpath;
144 } catch (Exception e) {
145 e.printStackTrace();
146 return null;
147 }
148 }
149
150 }