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 com
.owncloud
.android
.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 com
.owncloud
.android
.AccountUtils
;
33 import com
.owncloud
.android
.authenticator
.OnConnectCheckListener
.ResultType
;
34 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
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
;
43 public class ConnectionCheckerRunnable
implements Runnable
{
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;
48 private static final String TAG
= "ConnectionCheckerRunnable";
49 private OnConnectCheckListener mListener
;
51 private Handler mHandler
;
52 private ResultType mLatestResult
;
53 private Context mContext
;
54 private OwnCloudVersion mOCVersion
;
56 public void setListener(OnConnectCheckListener listener
, Handler handler
) {
61 public ConnectionCheckerRunnable(String url
, Context context
) {
73 postResult(ResultType
.NO_NETWORK_CONNECTION
);
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
);
81 Uri uri
= Uri
.parse("https://" + mUrl
+ AccountUtils
.STATUS_PATH
);
82 if (tryConnection(uri
)) {
83 postResult(ResultType
.OK_SSL
);
87 "establishing secure connection failed, trying non secure connection");
88 uri
= Uri
.parse("http://" + mUrl
+ AccountUtils
.STATUS_PATH
);
90 if (tryConnection(uri
)) {
91 postResult(ResultType
.OK_NO_SSL
);
94 postResult(mLatestResult
);
98 public OwnCloudVersion
getDiscoveredVersion() {
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
;
108 int status
= wc
.executeMethod(get
, TRY_CONNECTION_TIMEOUT
);
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
;
117 mOCVersion
= new OwnCloudVersion(json
.getString("version"));
118 if (!mOCVersion
.isVersionValid())
123 case HttpStatus
.SC_NOT_FOUND
:
124 mLatestResult
= ResultType
.FILE_NOT_FOUND
;
126 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
127 mLatestResult
= ResultType
.INSTANCE_NOT_CONFIGURED
;
130 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
131 Log
.e(TAG
, "Not handled status received from server: " + status
);
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
;
144 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
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();
159 private void postResult(final ResultType result
) {
160 if (mHandler
!= null
&& mListener
!= null
) {
161 mHandler
.post(new Runnable() {
164 mListener
.onConnectionCheckResult(result
);