Login page updated to get OAuth2 access token
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / oauth2 / OAuth2GetCodeRunnable.java
1 package com.owncloud.android.authenticator.oauth2;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.ArrayList;
5 import java.util.List;
6
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;
12
13 import android.content.Context;
14 import android.net.ConnectivityManager;
15 import android.os.Handler;
16 import android.util.Log;
17
18 import com.owncloud.android.authenticator.oauth2.OnOAuth2GetCodeResultListener.ResultOAuthType;
19 import com.owncloud.android.authenticator.oauth2.connection.ConnectorOAuth2;
20
21 /**
22 * Implements the communication with oAuth2 server to get User Code and other useful values.
23 *
24 * @author SolidGear S.L.
25 *
26 */
27 public class OAuth2GetCodeRunnable implements Runnable {
28
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";
36
37 private static final String TAG = "OAuth2GetCodeRunnable";
38 private OnOAuth2GetCodeResultListener mListener;
39 private String mUrl;
40 private Handler mHandler;
41 private Context mContext;
42
43 public void setListener(OnOAuth2GetCodeResultListener listener, Handler handler) {
44 mListener = listener;
45 mHandler = handler;
46 }
47
48 public OAuth2GetCodeRunnable(String url, Context context) {
49 mListener = null;
50 mHandler = null;
51 mUrl = url;
52 mContext = context;
53 }
54
55 @Override
56 public void run() {
57 ResultOAuthType mLatestResult;
58 String targetURI = null;
59 JSONObject codeResponseJson = null;
60
61 if (!isOnline()) {
62 postResult(ResultOAuthType.NO_NETWORK_CONNECTION,null);
63 return;
64 }
65
66 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
67 mLatestResult = (mUrl.startsWith("https://"))? ResultOAuthType.OK_SSL : ResultOAuthType.OK_NO_SSL;
68 } else {
69 mUrl = "https://" + mUrl;
70 mLatestResult = ResultOAuthType.OK_SSL;
71 }
72 targetURI = mUrl + OAuth2Context.OAUTH2_DEVICE_GETCODE_URL;
73
74 ConnectorOAuth2 connectorOAuth2 = new ConnectorOAuth2(targetURI);
75
76 try {
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());
88 }
89
90 if (codeResponseJson == null) {
91 mLatestResult = ResultOAuthType.HOST_NOT_AVAILABLE;
92 }
93
94 postResult(mLatestResult, codeResponseJson);
95 }
96
97 private boolean isOnline() {
98 ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
99 return cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
100 }
101
102 private void postResult(final ResultOAuthType result,final JSONObject codeResponseJson) {
103 if (mHandler != null && mListener != null) {
104 mHandler.post(new Runnable() {
105 @Override
106 public void run() {
107 mListener.onOAuth2GetCodeResult(result, codeResponseJson);
108 }
109 });
110 }
111 }
112
113 }