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