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
.UnknownHostException
;
24 import javax
.net
.ssl
.SSLHandshakeException
;
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
;
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
;
41 public class ConnectionCheckerRunnable
implements Runnable
{
42 private static final String TAG
= "ConnectionCheckerRunnable";
43 private OnConnectCheckListener mListener
;
45 private Handler mHandler
;
46 private ResultType mLatestResult
;
47 private Context mContext
;
48 private OwnCloudVersion mOCVersion
;
50 public void setListener(OnConnectCheckListener listener
, Handler handler
) {
55 public ConnectionCheckerRunnable(String url
, Context context
) {
67 postResult(ResultType
.NO_NETWORK_CONNECTION
);
70 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
71 mLatestResult
= ResultType
.OK
;
72 tryConnection(Uri
.parse(mUrl
+ AccountUtils
.STATUS_PATH
));
73 postResult(mLatestResult
);
75 Uri uri
= Uri
.parse("https://" + mUrl
+ AccountUtils
.STATUS_PATH
);
76 if (tryConnection(uri
)) {
77 postResult(ResultType
.OK
);
80 Log
.d(TAG
, "establishing secure connection failed, trying non secure connection");
81 uri
= Uri
.parse("http://" + mUrl
+ AccountUtils
.STATUS_PATH
);
83 if (tryConnection(uri
)) {
84 postResult(ResultType
.OK_NO_SSL
);
87 postResult(mLatestResult
);
91 public OwnCloudVersion
getDiscoveredVersion() {
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
;
101 int status
= wc
.executeMethod(get
);
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
;
110 mOCVersion
= new OwnCloudVersion(json
.getString("version"));
111 if (!mOCVersion
.isVersionValid())
116 case HttpStatus
.SC_NOT_FOUND
:
117 mLatestResult
= ResultType
.FILE_NOT_FOUND
;
119 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
120 mLatestResult
= ResultType
.INSTANCE_NOT_CONFIGURED
;
123 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
124 Log
.e(TAG
,"Not handled status received from server: " + status
);
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
;
135 mLatestResult
= ResultType
.UNKNOWN_ERROR
;
143 private boolean isOnline() {
144 ConnectivityManager cm
=
145 (ConnectivityManager
) mContext
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
147 && cm
.getActiveNetworkInfo() != null
148 && cm
.getActiveNetworkInfo().isConnectedOrConnecting();
151 private void postResult(final ResultType result
) {
152 if (mHandler
!= null
&& mListener
!= null
) {
153 mHandler
.post(new Runnable() {
156 mListener
.onConnectionCheckResult(result
);