moving from eu.alefzero.eu to com.owncloud.android
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / ConnectionCheckerRunnable.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 java.net.ConnectException;
22 import java.net.SocketTimeoutException;
23 import java.net.UnknownHostException;
24
25 import javax.net.ssl.SSLHandshakeException;
26
27 import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.commons.httpclient.methods.GetMethod;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31
32 import com.owncloud.android.AccountUtils;
33 import com.owncloud.android.authenticator.OnConnectCheckListener.ResultType;
34 import com.owncloud.android.utils.OwnCloudVersion;
35
36 import eu.alefzero.webdav.WebdavClient;
37 import android.content.Context;
38 import android.net.ConnectivityManager;
39 import android.net.Uri;
40 import android.os.Handler;
41 import android.util.Log;
42
43 public class ConnectionCheckerRunnable implements Runnable {
44
45 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
46 public static final int TRY_CONNECTION_TIMEOUT = 5000;
47
48 private static final String TAG = "ConnectionCheckerRunnable";
49 private OnConnectCheckListener mListener;
50 private String mUrl;
51 private Handler mHandler;
52 private ResultType mLatestResult;
53 private Context mContext;
54 private OwnCloudVersion mOCVersion;
55
56 public void setListener(OnConnectCheckListener listener, Handler handler) {
57 mListener = listener;
58 mHandler = handler;
59 }
60
61 public ConnectionCheckerRunnable(String url, Context context) {
62 mListener = null;
63 mHandler = null;
64 mUrl = url;
65 mContext = context;
66 mOCVersion = null;
67 }
68
69 @Override
70 public void run() {
71
72 if (!isOnline()) {
73 postResult(ResultType.NO_NETWORK_CONNECTION);
74 return;
75 }
76 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
77 mLatestResult = (mUrl.startsWith("https://"))? ResultType.OK_SSL : ResultType.OK_NO_SSL;
78 tryConnection(Uri.parse(mUrl + AccountUtils.STATUS_PATH));
79 postResult(mLatestResult);
80 } else {
81 Uri uri = Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH);
82 if (tryConnection(uri)) {
83 postResult(ResultType.OK_SSL);
84 return;
85 }
86 Log.d(TAG,
87 "establishing secure connection failed, trying non secure connection");
88 uri = Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH);
89
90 if (tryConnection(uri)) {
91 postResult(ResultType.OK_NO_SSL);
92 return;
93 }
94 postResult(mLatestResult);
95 }
96 }
97
98 public OwnCloudVersion getDiscoveredVersion() {
99 return mOCVersion;
100 }
101
102 private boolean tryConnection(Uri uri) {
103 WebdavClient wc = new WebdavClient();
104 wc.allowSelfsignedCertificates();
105 GetMethod get = new GetMethod(uri.toString());
106 boolean retval = false;
107 try {
108 int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT);
109 switch (status) {
110 case HttpStatus.SC_OK: {
111 String response = get.getResponseBodyAsString();
112 JSONObject json = new JSONObject(response);
113 if (!json.getBoolean("installed")) {
114 mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
115 break;
116 }
117 mOCVersion = new OwnCloudVersion(json.getString("version"));
118 if (!mOCVersion.isVersionValid())
119 break;
120 retval = true;
121 break;
122 }
123 case HttpStatus.SC_NOT_FOUND:
124 mLatestResult = ResultType.FILE_NOT_FOUND;
125 break;
126 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
127 mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
128 break;
129 default:
130 mLatestResult = ResultType.UNKNOWN_ERROR;
131 Log.e(TAG, "Not handled status received from server: " + status);
132 }
133
134 } catch (Exception e) {
135 if (e instanceof UnknownHostException
136 || e instanceof ConnectException
137 || e instanceof SocketTimeoutException) {
138 mLatestResult = ResultType.HOST_NOT_AVAILABLE;
139 } else if (e instanceof JSONException) {
140 mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
141 } else if (e instanceof SSLHandshakeException) {
142 mLatestResult = ResultType.SSL_INIT_ERROR;
143 } else {
144 mLatestResult = ResultType.UNKNOWN_ERROR;
145 }
146 e.printStackTrace();
147 }
148
149 return retval;
150 }
151
152 private boolean isOnline() {
153 ConnectivityManager cm = (ConnectivityManager) mContext
154 .getSystemService(Context.CONNECTIVITY_SERVICE);
155 return cm != null && cm.getActiveNetworkInfo() != null
156 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
157 }
158
159 private void postResult(final ResultType result) {
160 if (mHandler != null && mListener != null) {
161 mHandler.post(new Runnable() {
162 @Override
163 public void run() {
164 mListener.onConnectionCheckResult(result);
165 }
166 });
167 }
168 }
169
170 }