a9866b47e952692bc915f0db1d7a1a3437250361
[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, "establishing secure connection failed, trying non secure connection");
81 uri = Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH);
82
83 if (tryConnection(uri)) {
84 postResult(ResultType.OK_NO_SSL);
85 return;
86 }
87 postResult(mLatestResult);
88 }
89 }
90
91 public OwnCloudVersion getDiscoveredVersion() {
92 return mOCVersion;
93 }
94
95 private boolean tryConnection(Uri uri) {
96 WebdavClient wc = new WebdavClient(uri);
97 wc.allowUnsignedCertificates();
98 GetMethod get = new GetMethod(uri.toString());
99 boolean retval = false;
100 try {
101 int status = wc.executeMethod(get);
102 switch (status) {
103 case HttpStatus.SC_OK: {
104 String response = get.getResponseBodyAsString();
105 JSONObject json = new JSONObject(response);
106 if (!json.getBoolean("installed")) {
107 mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
108 break;
109 }
110 mOCVersion = new OwnCloudVersion(json.getString("version"));
111 if (!mOCVersion.isVersionValid())
112 break;
113 retval = true;
114 break;
115 }
116 case HttpStatus.SC_NOT_FOUND:
117 mLatestResult = ResultType.FILE_NOT_FOUND;
118 break;
119 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
120 mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
121 break;
122 default:
123 mLatestResult = ResultType.UNKNOWN_ERROR;
124 Log.e(TAG,"Not handled status received from server: " + status);
125 }
126
127 } catch (Exception e) {
128 if (e instanceof UnknownHostException || e instanceof ConnectException) {
129 mLatestResult = ResultType.HOST_NOT_AVAILABLE;
130 } else if (e instanceof JSONException) {
131 mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED;
132 } else if (e instanceof SSLHandshakeException) {
133 mLatestResult = ResultType.SSL_INIT_ERROR;
134 } else {
135 mLatestResult = ResultType.UNKNOWN_ERROR;
136 }
137 e.printStackTrace();
138 }
139
140 return retval;
141 }
142
143 private boolean isOnline() {
144 ConnectivityManager cm =
145 (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
146 return cm != null
147 && cm.getActiveNetworkInfo() != null
148 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
149 }
150
151 private void postResult(final ResultType result) {
152 if (mHandler != null && mListener != null) {
153 mHandler.post(new Runnable() {
154 @Override
155 public void run() {
156 mListener.onConnectionCheckResult(result);
157 }
158 });
159 }
160 }
161
162 }