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
); 
  81                     "establishing secure connection failed, trying non secure connection"); 
  82             uri 
= Uri
.parse("http://" + mUrl 
+ AccountUtils
.STATUS_PATH
); 
  84             if (tryConnection(uri
)) { 
  85                 postResult(ResultType
.OK_NO_SSL
); 
  88             postResult(mLatestResult
); 
  92     public OwnCloudVersion 
getDiscoveredVersion() { 
  96     private boolean tryConnection(Uri uri
) { 
  97         WebdavClient wc 
= new WebdavClient(uri
); 
  98         wc
.allowUnsignedCertificates(); 
  99         GetMethod get 
= new GetMethod(uri
.toString()); 
 100         boolean retval 
= false
; 
 102             int status 
= wc
.executeMethod(get
); 
 104             case HttpStatus
.SC_OK
: { 
 105                 String response 
= get
.getResponseBodyAsString(); 
 106                 JSONObject json 
= new JSONObject(response
); 
 107                 if (!json
.getBoolean("installed")) { 
 108                     mLatestResult 
= ResultType
.INSTANCE_NOT_CONFIGURED
; 
 111                 mOCVersion 
= new OwnCloudVersion(json
.getString("version")); 
 112                 if (!mOCVersion
.isVersionValid()) 
 117             case HttpStatus
.SC_NOT_FOUND
: 
 118                 mLatestResult 
= ResultType
.FILE_NOT_FOUND
; 
 120             case HttpStatus
.SC_INTERNAL_SERVER_ERROR
: 
 121                 mLatestResult 
= ResultType
.INSTANCE_NOT_CONFIGURED
; 
 124                 mLatestResult 
= ResultType
.UNKNOWN_ERROR
; 
 125                 Log
.e(TAG
, "Not handled status received from server: " + status
); 
 128         } catch (Exception e
) { 
 129             if (e 
instanceof UnknownHostException
 
 130                     || e 
instanceof ConnectException
) { 
 131                 mLatestResult 
= ResultType
.HOST_NOT_AVAILABLE
; 
 132             } else if (e 
instanceof JSONException
) { 
 133                 mLatestResult 
= ResultType
.INSTANCE_NOT_CONFIGURED
; 
 134             } else if (e 
instanceof SSLHandshakeException
) { 
 135                 mLatestResult 
= ResultType
.SSL_INIT_ERROR
; 
 137                 mLatestResult 
= ResultType
.UNKNOWN_ERROR
; 
 145     private boolean isOnline() { 
 146         ConnectivityManager cm 
= (ConnectivityManager
) mContext
 
 147                 .getSystemService(Context
.CONNECTIVITY_SERVICE
); 
 148         return cm 
!= null 
&& cm
.getActiveNetworkInfo() != null
 
 149                 && cm
.getActiveNetworkInfo().isConnectedOrConnecting(); 
 152     private void postResult(final ResultType result
) { 
 153         if (mHandler 
!= null 
&& mListener 
!= null
) { 
 154             mHandler
.post(new Runnable() { 
 157                     mListener
.onConnectionCheckResult(result
);