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