Adding cancellation to uploads (WIP)
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / ConnectionCheckOperation.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 com.owncloud.android.authenticator;
20
21 import org.apache.commons.httpclient.HttpStatus;
22 import org.apache.commons.httpclient.methods.GetMethod;
23 import org.json.JSONException;
24 import org.json.JSONObject;
25
26 import com.owncloud.android.AccountUtils;
27 import com.owncloud.android.operations.RemoteOperation;
28 import com.owncloud.android.operations.RemoteOperationResult;
29 import com.owncloud.android.utils.OwnCloudVersion;
30
31 import eu.alefzero.webdav.WebdavClient;
32 import android.content.Context;
33 import android.net.ConnectivityManager;
34 import android.net.Uri;
35 import android.util.Log;
36
37 public class ConnectionCheckOperation extends RemoteOperation {
38
39 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
40 public static final int TRY_CONNECTION_TIMEOUT = 5000;
41
42 private static final String TAG = ConnectionCheckOperation.class.getCanonicalName();
43
44 private String mUrl;
45 private RemoteOperationResult mLatestResult;
46 private Context mContext;
47 private OwnCloudVersion mOCVersion;
48
49 public ConnectionCheckOperation(String url, Context context) {
50 mUrl = url;
51 mContext = context;
52 mOCVersion = null;
53 }
54
55 public OwnCloudVersion getDiscoveredVersion() {
56 return mOCVersion;
57 }
58
59 private boolean tryConnection(WebdavClient wc, String urlSt) {
60 boolean retval = false;
61 GetMethod get = null;
62 try {
63 get = new GetMethod(urlSt);
64 int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
65 String response = get.getResponseBodyAsString();
66 if (status == HttpStatus.SC_OK) {
67 JSONObject json = new JSONObject(response);
68 if (!json.getBoolean("installed")) {
69 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
70 } else {
71 mOCVersion = new OwnCloudVersion(json.getString("version"));
72 if (!mOCVersion.isVersionValid()) {
73 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
74
75 } else {
76 mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ?
77 RemoteOperationResult.ResultCode.OK_SSL :
78 RemoteOperationResult.ResultCode.OK_NO_SSL
79 );
80
81 retval = true;
82 }
83 }
84
85 } else {
86 mLatestResult = new RemoteOperationResult(false, status);
87 }
88
89 } catch (JSONException e) {
90 mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
91
92 } catch (Exception e) {
93 mLatestResult = new RemoteOperationResult(e);
94
95 } finally {
96 if (get != null)
97 get.releaseConnection();
98 }
99
100 if (mLatestResult.isSuccess()) {
101 Log.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
102
103 } else if (mLatestResult.getException() != null) {
104 Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException());
105
106 } else {
107 Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
108 }
109
110 return retval;
111 }
112
113 private boolean isOnline() {
114 ConnectivityManager cm = (ConnectivityManager) mContext
115 .getSystemService(Context.CONNECTIVITY_SERVICE);
116 return cm != null && cm.getActiveNetworkInfo() != null
117 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
118 }
119
120 @Override
121 protected RemoteOperationResult run(WebdavClient client) {
122 if (!isOnline()) {
123 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
124 }
125 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
126 tryConnection(client, mUrl + AccountUtils.STATUS_PATH);
127
128 } else {
129 client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH));
130 boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
131 if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
132 Log.d(TAG, "establishing secure connection failed, trying non secure connection");
133 client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH));
134 tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
135 }
136 }
137 return mLatestResult;
138 }
139
140 }