More robust instant photo uploads
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / OwnCloudClientUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 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 package com.owncloud.android.utils;
19
20 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
21 import org.apache.commons.httpclient.protocol.Protocol;
22
23 import com.owncloud.android.AccountUtils;
24 import com.owncloud.android.authenticator.AccountAuthenticator;
25 import com.owncloud.android.authenticator.EasySSLSocketFactory;
26 import com.owncloud.android.utils.OwnCloudVersion;
27
28 import eu.alefzero.webdav.WebdavClient;
29
30 import android.accounts.Account;
31 import android.accounts.AccountManager;
32 import android.content.Context;
33 import android.net.Uri;
34 import android.util.Log;
35
36 public class OwnCloudClientUtils {
37
38 final private static String TAG = "OwnCloudClientFactory";
39
40 /** Default timeout for waiting data from the server */
41 public static final int DEFAULT_DATA_TIMEOUT = 20000;
42
43 /** Default timeout for establishing a connection */
44 public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
45
46 /** Connection manager for all the WebdavClients */
47 static private MultiThreadedHttpConnectionManager mConnManager = null;
48
49
50 /**
51 * Creates a WebdavClient setup for an ownCloud account
52 *
53 * @param account The ownCloud account
54 * @param context The application context
55 * @return A WebdavClient object ready to be used
56 */
57 public static WebdavClient createOwnCloudClient (Account account, Context context) {
58 Log.d(TAG, "Creating WebdavClient associated to " + account.name);
59
60 String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
61 OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_VERSION));
62 String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion);
63
64 WebdavClient client = createOwnCloudClient(Uri.parse(baseUrl + webDavPath));
65
66 String username = account.name.substring(0, account.name.lastIndexOf('@'));
67 String password = AccountManager.get(context).getPassword(account);
68 //String password = am.blockingGetAuthToken(mAccount, AccountAuthenticator.AUTH_TOKEN_TYPE, true);
69
70 client.setCredentials(username, password);
71
72 return client;
73 }
74
75
76 /**
77 * Creates a WebdavClient to try a new account before saving it
78 *
79 * @param uri URL to the ownCloud server
80 * @param username User name
81 * @param password User password
82 * @return A WebdavClient object ready to be used
83 */
84 public static WebdavClient createOwnCloudClient(Uri uri, String username, String password) {
85 Log.d(TAG, "Creating WebdavClient for " + username + "@" + uri);
86
87 WebdavClient client = createOwnCloudClient(uri);
88
89 client.setCredentials(username, password);
90
91 return client;
92 }
93
94
95 /**
96 * Creates a WebdavClient to access a URL and sets the desired parameters for ownCloud client connections.
97 *
98 * @param uri URL to the ownCloud server
99 * @return A WebdavClient object ready to be used
100 */
101 public static WebdavClient createOwnCloudClient(Uri uri) {
102 Log.d(TAG, "Creating WebdavClient for " + uri);
103
104 allowSelfsignedCertificates(true);
105
106 WebdavClient client = new WebdavClient(getMultiThreadedConnManager());
107
108 allowSelfsignedCertificates(true);
109 client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
110 client.setBaseUri(uri);
111
112 return client;
113 }
114
115
116 /**
117 * Allows or disallows self-signed certificates in ownCloud servers to reach
118 *
119 * @param allow 'True' to allow, 'false' to disallow
120 */
121 public static void allowSelfsignedCertificates(boolean allow) {
122 Protocol pr = null;
123 try {
124 pr = Protocol.getProtocol("https");
125 } catch (IllegalStateException e) {
126 // nothing to do here; really
127 }
128 boolean isAllowed = (pr != null && pr.getSocketFactory() instanceof EasySSLSocketFactory);
129 if (allow && !isAllowed) {
130 Protocol.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443));
131 } else if (!allow && isAllowed) {
132 // TODO - a more strict SocketFactory object should be provided here
133 }
134 }
135
136
137
138 static private MultiThreadedHttpConnectionManager getMultiThreadedConnManager() {
139 if (mConnManager == null) {
140 mConnManager = new MultiThreadedHttpConnectionManager();
141 mConnManager.getParams().setDefaultMaxConnectionsPerHost(5);
142 mConnManager.getParams().setMaxTotalConnections(5);
143 }
144 return mConnManager;
145 }
146
147 }