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 org
.apache
.commons
.httpclient
.HttpStatus
;
22 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
23 import org
.json
.JSONException
;
24 import org
.json
.JSONObject
;
26 import com
.owncloud
.android
.AccountUtils
;
27 import com
.owncloud
.android
.operations
.RemoteOperation
;
28 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
29 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
31 import eu
.alefzero
.webdav
.WebdavClient
;
32 import android
.content
.Context
;
33 import android
.net
.ConnectivityManager
;
34 import android
.net
.Uri
;
35 import android
.util
.Log
;
37 public class ConnectionCheckOperation
extends RemoteOperation
{
39 /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
40 public static final int TRY_CONNECTION_TIMEOUT
= 5000;
42 private static final String TAG
= ConnectionCheckerRunnable
.class.getCanonicalName();
45 private RemoteOperationResult mLatestResult
;
46 private Context mContext
;
47 private OwnCloudVersion mOCVersion
;
49 public ConnectionCheckOperation(String url
, Context context
) {
55 public OwnCloudVersion
getDiscoveredVersion() {
59 private boolean tryConnection(WebdavClient wc
, String urlSt
) {
60 boolean retval
= false
;
63 get
= new GetMethod(urlSt
);
64 int status
= wc
.executeMethod(get
, TRY_CONNECTION_TIMEOUT
, TRY_CONNECTION_TIMEOUT
);
65 String response
= get
.getResponseBodyAsString();
66 if (status
== HttpStatus
.SC_OK
) {
67 JSONObject json
= new JSONObject(response
);
68 if (!json
.getBoolean("installed")) {
69 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
71 mOCVersion
= new OwnCloudVersion(json
.getString("version"));
72 if (!mOCVersion
.isVersionValid()) {
73 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.BAD_OC_VERSION
);
81 mLatestResult
= new RemoteOperationResult(false
, status
);
84 } catch (JSONException e
) {
85 mLatestResult
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.INSTANCE_NOT_CONFIGURED
);
86 //Log.e(TAG, "JSON exception while trying connection (instance not configured) ", e);
88 } catch (Exception e
) {
89 mLatestResult
= new RemoteOperationResult(e
);
90 //Log.e(TAG, "Unexpected exception while trying connection", e);
94 get
.releaseConnection();
100 private boolean isOnline() {
101 ConnectivityManager cm
= (ConnectivityManager
) mContext
102 .getSystemService(Context
.CONNECTIVITY_SERVICE
);
103 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
104 && cm
.getActiveNetworkInfo().isConnectedOrConnecting();
108 protected RemoteOperationResult
run(WebdavClient client
) {
110 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.NO_NETWORK_CONNECTION
);
112 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
113 mLatestResult
= new RemoteOperationResult(
114 mUrl
.startsWith("https://") ? RemoteOperationResult
.ResultCode
.OK_SSL
: RemoteOperationResult
.ResultCode
.OK_NO_SSL
116 tryConnection(client
, mUrl
+ AccountUtils
.STATUS_PATH
);
117 return mLatestResult
;
120 client
.setBaseUri(Uri
.parse("https://" + mUrl
+ AccountUtils
.STATUS_PATH
));
121 if (tryConnection(client
, "https://" + mUrl
+ AccountUtils
.STATUS_PATH
)) {
122 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.OK_SSL
);
124 } else if (!mLatestResult
.isSslRecoverableException()) {
126 Log
.d(TAG
, "establishing secure connection failed, trying non secure connection");
127 client
.setBaseUri(Uri
.parse("http://" + mUrl
+ AccountUtils
.STATUS_PATH
));
128 if (tryConnection(client
, "http://" + mUrl
+ AccountUtils
.STATUS_PATH
)) {
129 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.OK_NO_SSL
);
132 return mLatestResult
;