1 package com
.owncloud
.android
.authenticator
.oauth2
;
3 import java
.io
.UnsupportedEncodingException
;
4 import java
.util
.ArrayList
;
7 import org
.apache
.http
.NameValuePair
;
8 import org
.apache
.http
.client
.entity
.UrlEncodedFormEntity
;
9 import org
.apache
.http
.message
.BasicNameValuePair
;
10 import org
.json
.JSONException
;
11 import org
.json
.JSONObject
;
13 import android
.content
.Context
;
14 import android
.net
.ConnectivityManager
;
15 import android
.os
.Handler
;
16 import android
.util
.Log
;
18 import com
.owncloud
.android
.authenticator
.oauth2
.OnOAuth2GetCodeResultListener
.ResultOAuthType
;
19 import com
.owncloud
.android
.authenticator
.oauth2
.connection
.ConnectorOAuth2
;
22 * Implements the communication with oAuth2 server to get User Code and other useful values.
24 * @author SolidGear S.L.
27 public class OAuth2GetCodeRunnable
implements Runnable
{
29 public static final String CODE_USER_CODE
= "user_code";
30 public static final String CODE_CLIENT_ID
= "client_id";
31 public static final String CODE_CLIENT_SCOPE
= "scope";
32 public static final String CODE_VERIFICATION_URL
= "verification_url";
33 public static final String CODE_EXPIRES_IN
= "expires_in";
34 public static final String CODE_DEVICE_CODE
= "device_code";
35 public static final String CODE_INTERVAL
= "interval";
37 private static final String TAG
= "OAuth2GetCodeRunnable";
38 private OnOAuth2GetCodeResultListener mListener
;
40 private Handler mHandler
;
41 private Context mContext
;
43 public void setListener(OnOAuth2GetCodeResultListener listener
, Handler handler
) {
48 public OAuth2GetCodeRunnable(String url
, Context context
) {
57 ResultOAuthType mLatestResult
;
58 String targetURI
= null
;
59 JSONObject codeResponseJson
= null
;
62 postResult(ResultOAuthType
.NO_NETWORK_CONNECTION
,null
);
66 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
67 mLatestResult
= (mUrl
.startsWith("https://"))? ResultOAuthType
.OK_SSL
: ResultOAuthType
.OK_NO_SSL
;
69 mUrl
= "https://" + mUrl
;
70 mLatestResult
= ResultOAuthType
.OK_SSL
;
72 targetURI
= mUrl
+ OAuth2Context
.OAUTH2_DEVICE_GETCODE_URL
;
74 ConnectorOAuth2 connectorOAuth2
= new ConnectorOAuth2(targetURI
);
77 List
<NameValuePair
> nameValuePairs
= new ArrayList
<NameValuePair
>(2);
78 nameValuePairs
.add(new BasicNameValuePair(CODE_CLIENT_ID
, OAuth2Context
.OAUTH2_DEVICE_CLIENT_ID
));
79 nameValuePairs
.add(new BasicNameValuePair(CODE_CLIENT_SCOPE
,OAuth2Context
.OAUTH2_DEVICE_GETCODE_SCOPES
));
80 UrlEncodedFormEntity params
= new UrlEncodedFormEntity(nameValuePairs
);
81 codeResponseJson
= new JSONObject(connectorOAuth2
.connPost(params
));
82 } catch (JSONException e
) {
83 Log
.e(TAG
, "JSONException converting to Json: " + e
.toString());
84 } catch (UnsupportedEncodingException e
) {
85 Log
.e(TAG
, "UnsupportedEncodingException encoding URL values: " + e
.toString());
86 } catch (Exception e
) {
87 Log
.e(TAG
, "Exception : " + e
.toString());
90 if (codeResponseJson
== null
) {
91 mLatestResult
= ResultOAuthType
.HOST_NOT_AVAILABLE
;
94 postResult(mLatestResult
, codeResponseJson
);
97 private boolean isOnline() {
98 ConnectivityManager cm
= (ConnectivityManager
) mContext
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
99 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnectedOrConnecting();
102 private void postResult(final ResultOAuthType result
,final JSONObject codeResponseJson
) {
103 if (mHandler
!= null
&& mListener
!= null
) {
104 mHandler
.post(new Runnable() {
107 mListener
.onOAuth2GetCodeResult(result
, codeResponseJson
);