X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/390949b8323633b26d56b3a837e83941cbc24b4b..a032bdeebc51a6e81d1bd5c558944f96fc55eacb:/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java diff --git a/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java b/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java index a9866b47..4e178849 100644 --- a/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java +++ b/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java @@ -19,6 +19,7 @@ package eu.alefzero.owncloud.authenticator; import java.net.ConnectException; +import java.net.SocketTimeoutException; import java.net.UnknownHostException; import javax.net.ssl.SSLHandshakeException; @@ -39,124 +40,130 @@ import android.os.Handler; import android.util.Log; public class ConnectionCheckerRunnable implements Runnable { - private static final String TAG = "ConnectionCheckerRunnable"; - private OnConnectCheckListener mListener; - private String mUrl; - private Handler mHandler; - private ResultType mLatestResult; - private Context mContext; - private OwnCloudVersion mOCVersion; - - public void setListener(OnConnectCheckListener listener, Handler handler) { - mListener = listener; - mHandler = handler; - } - - public ConnectionCheckerRunnable(String url, Context context) { - mListener = null; - mHandler = null; - mUrl = url; - mContext = context; - mOCVersion = null; - } - - @Override - public void run() { - if (!isOnline()) { - postResult(ResultType.NO_NETWORK_CONNECTION); - return; + /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */ + public static final int TRY_CONNECTION_TIMEOUT = 5000; + + private static final String TAG = "ConnectionCheckerRunnable"; + private OnConnectCheckListener mListener; + private String mUrl; + private Handler mHandler; + private ResultType mLatestResult; + private Context mContext; + private OwnCloudVersion mOCVersion; + + public void setListener(OnConnectCheckListener listener, Handler handler) { + mListener = listener; + mHandler = handler; } - if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) { - mLatestResult = ResultType.OK; - tryConnection(Uri.parse(mUrl + AccountUtils.STATUS_PATH)); - postResult(mLatestResult); - } else { - Uri uri = Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH); - if (tryConnection(uri)) { - postResult(ResultType.OK); - return; - } - Log.d(TAG, "establishing secure connection failed, trying non secure connection"); - uri = Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH); - - if (tryConnection(uri)) { - postResult(ResultType.OK_NO_SSL); - return; - } - postResult(mLatestResult); + + public ConnectionCheckerRunnable(String url, Context context) { + mListener = null; + mHandler = null; + mUrl = url; + mContext = context; + mOCVersion = null; } - } - - public OwnCloudVersion getDiscoveredVersion() { - return mOCVersion; - } - - private boolean tryConnection(Uri uri) { - WebdavClient wc = new WebdavClient(uri); - wc.allowUnsignedCertificates(); - GetMethod get = new GetMethod(uri.toString()); - boolean retval = false; - try { - int status = wc.executeMethod(get); - switch (status) { - case HttpStatus.SC_OK: { - String response = get.getResponseBodyAsString(); - JSONObject json = new JSONObject(response); - if (!json.getBoolean("installed")) { - mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED; - break; - } - mOCVersion = new OwnCloudVersion(json.getString("version")); - if (!mOCVersion.isVersionValid()) - break; - retval = true; - break; + + @Override + public void run() { + + if (!isOnline()) { + postResult(ResultType.NO_NETWORK_CONNECTION); + return; } - case HttpStatus.SC_NOT_FOUND: - mLatestResult = ResultType.FILE_NOT_FOUND; - break; - case HttpStatus.SC_INTERNAL_SERVER_ERROR: - mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED; - break; - default: - mLatestResult = ResultType.UNKNOWN_ERROR; - Log.e(TAG,"Not handled status received from server: " + status); - } - - } catch (Exception e) { - if (e instanceof UnknownHostException || e instanceof ConnectException) { - mLatestResult = ResultType.HOST_NOT_AVAILABLE; - } else if (e instanceof JSONException) { - mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED; - } else if (e instanceof SSLHandshakeException) { - mLatestResult = ResultType.SSL_INIT_ERROR; - } else { - mLatestResult = ResultType.UNKNOWN_ERROR; - } - e.printStackTrace(); + if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) { + mLatestResult = ResultType.OK; + tryConnection(Uri.parse(mUrl + AccountUtils.STATUS_PATH)); + postResult(mLatestResult); + } else { + Uri uri = Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH); + if (tryConnection(uri)) { + postResult(ResultType.OK); + return; + } + Log.d(TAG, + "establishing secure connection failed, trying non secure connection"); + uri = Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH); + + if (tryConnection(uri)) { + postResult(ResultType.OK_NO_SSL); + return; + } + postResult(mLatestResult); + } + } + + public OwnCloudVersion getDiscoveredVersion() { + return mOCVersion; } - return retval; - } - - private boolean isOnline() { - ConnectivityManager cm = - (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); - return cm != null - && cm.getActiveNetworkInfo() != null - && cm.getActiveNetworkInfo().isConnectedOrConnecting(); - } - - private void postResult(final ResultType result) { - if (mHandler != null && mListener != null) { - mHandler.post(new Runnable() { - @Override - public void run() { - mListener.onConnectionCheckResult(result); + private boolean tryConnection(Uri uri) { + WebdavClient wc = new WebdavClient(); + wc.allowSelfsignedCertificates(); + GetMethod get = new GetMethod(uri.toString()); + boolean retval = false; + try { + int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT); + switch (status) { + case HttpStatus.SC_OK: { + String response = get.getResponseBodyAsString(); + JSONObject json = new JSONObject(response); + if (!json.getBoolean("installed")) { + mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED; + break; + } + mOCVersion = new OwnCloudVersion(json.getString("version")); + if (!mOCVersion.isVersionValid()) + break; + retval = true; + break; + } + case HttpStatus.SC_NOT_FOUND: + mLatestResult = ResultType.FILE_NOT_FOUND; + break; + case HttpStatus.SC_INTERNAL_SERVER_ERROR: + mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED; + break; + default: + mLatestResult = ResultType.UNKNOWN_ERROR; + Log.e(TAG, "Not handled status received from server: " + status); + } + + } catch (Exception e) { + if (e instanceof UnknownHostException + || e instanceof ConnectException + || e instanceof SocketTimeoutException) { + mLatestResult = ResultType.HOST_NOT_AVAILABLE; + } else if (e instanceof JSONException) { + mLatestResult = ResultType.INSTANCE_NOT_CONFIGURED; + } else if (e instanceof SSLHandshakeException) { + mLatestResult = ResultType.SSL_INIT_ERROR; + } else { + mLatestResult = ResultType.UNKNOWN_ERROR; + } + e.printStackTrace(); + } + + return retval; + } + + private boolean isOnline() { + ConnectivityManager cm = (ConnectivityManager) mContext + .getSystemService(Context.CONNECTIVITY_SERVICE); + return cm != null && cm.getActiveNetworkInfo() != null + && cm.getActiveNetworkInfo().isConnectedOrConnecting(); + } + + private void postResult(final ResultType result) { + if (mHandler != null && mListener != null) { + mHandler.post(new Runnable() { + @Override + public void run() { + mListener.onConnectionCheckResult(result); + } + }); } - }); } - } - + }