1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.authenticator
;
21 import java
.net
.ConnectException
;
22 import java
.net
.SocketTimeoutException
;
23 import java
.net
.UnknownHostException
;
25 import javax
.net
.ssl
.SSLHandshakeException
;
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
;
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
;
42 public class ConnectionCheckerRunnable
implements Runnable
{
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;
47 private static final String TAG
= "ConnectionCheckerRunnable";
48 private OnConnectCheckListener mListener
;
50 private Handler mHandler
;
51 private ResultType mLatestResult
;
52 private Context mContext
;
53 private OwnCloudVersion mOCVersion
;
55 public void setListener(OnConnectCheckListener listener
, Handler handler
) {
60 public ConnectionCheckerRunnable(String url
, Context context
) {
72 postResult(ResultType
.NO_NETWORK_CONNECTION
);
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
);
80 Uri uri
= Uri
.parse("https://" + mUrl
+ AccountUtils
.STATUS_PATH
);
81 if (tryConnection(uri
)) {
82 postResult(ResultType
.OK_SSL
);
86 "establishing secure connection failed, trying non secure connection");
87 uri
= Uri
.parse("http://" + mUrl
+ AccountUtils
.STATUS_PATH
);
89 if (tryConnection(uri
)) {
90 postResult(ResultType
.OK_NO_SSL
);
93 postResult(mLatestResult
);
97 public OwnCloudVersion
getDiscoveredVersion() {
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
;
107 int status
= wc
.executeMethod(get
, TRY_CONNECTION_TIMEOUT
);
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
;
116 mOCVersion
= new OwnCloudVersion(json
.getString("version"));
117 if (!mOCVersion
.isVersionValid())
122 case HttpStatus
.SC_NOT_FOUND
:
123 mLatestResult
= ResultType
.FILE_NOT_FOUND
;
125 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
126 mLatestResult
= ResultType
.INSTANCE_NOT_CONFIGURED
;
129 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
130 Log
.e(TAG
, "Not handled status received from server: " + status
);
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
;
143 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
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();
158 private void postResult(final ResultType result
) {
159 if (mHandler
!= null
&& mListener
!= null
) {
160 mHandler
.post(new Runnable() {
163 mListener
.onConnectionCheckResult(result
);