-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOauthActivity.java
More file actions
245 lines (189 loc) · 6.2 KB
/
Copy pathOauthActivity.java
File metadata and controls
245 lines (189 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.bluevia.android.samples.oauth;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.bluevia.android.commons.connector.http.oauth.OAuthToken;
import com.bluevia.android.commons.connector.http.oauth.RequestToken;
import com.bluevia.android.commons.exception.BlueviaException;
import com.bluevia.android.oauth.client.BVOauth;
import com.bluevia.android.samples.ApplicationInfo;
import com.bluevia.android.samples.BlueviaAsyncTask;
import com.bluevia.android.samples.R;
public class OauthActivity extends Activity {
public static final String TAG = "OauthActivity";
private RequestToken mRequestToken;
private OAuthToken mAccessToken;
private BVOauth mClient;
private EditText mConsumerKey;
private EditText mConsumerSecret;
private Button mGetRequestTokenButton;
private TextView mVerifier;
private Button mGetAccessTokenButton;
private EditText mAccess;
private EditText mAccessSecret;
private Button mResetButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oauth_activity);
mConsumerKey = (EditText)findViewById(R.id.key);
mConsumerSecret = (EditText)findViewById(R.id.secret);
mGetRequestTokenButton = (Button) findViewById(R.id.getRequestToken);
mVerifier = (TextView)findViewById(R.id.verifier_code);
mGetAccessTokenButton = (Button) findViewById(R.id.get_access);
mAccess = (EditText)findViewById(R.id.text_access);
mAccessSecret = (EditText)findViewById(R.id.text_access_secret);
mResetButton = (Button) findViewById(R.id.reset);
//Default values
mConsumerKey.setText(ApplicationInfo.CONSUMER_KEY);
mConsumerSecret.setText(ApplicationInfo.CONSUMER_SECRET);
mGetRequestTokenButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do not create client until consumer key and consumer secret has been typed
createClient();
if (mConsumerKey.getText().toString() == null || mConsumerKey.getText().toString().equals("") ||
mConsumerSecret.getText().toString() == null || mConsumerSecret.getText().toString().equals(""))
throw new IllegalArgumentException("Consumer Secret and Consumer Key cannot be null nor empty");
getRequestToken();
}
});
mGetAccessTokenButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getAccessToken();
}
});
mResetButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
reset();
}
});
}
private void createClient(){
closeClient();
try {
mClient = new BVOauth(ApplicationInfo.MODE,
mConsumerKey.getText().toString(),
mConsumerSecret.getText().toString());
} catch (BlueviaException e){
Log.e(TAG, e.getMessage());
}
}
private void closeClient(){
if (mClient != null)
mClient.close();
mClient = null;
}
private void getRequestToken(){
new GetRequestTokenAsyncTask(this).execute();
}
private class GetRequestTokenAsyncTask extends BlueviaAsyncTask<Void, Void, RequestToken>{
public GetRequestTokenAsyncTask(Context context) {
super(context);
}
@Override
protected RequestToken doInBackground(Void... params) {
RequestToken response = null;
try {
response = mClient.getRequestToken();
} catch (BlueviaException e){
Log.e(TAG, e.getMessage(), e);
errorMessage = e.getMessage();
}
return response;
}
@Override
protected void onPostExecute(RequestToken result) {
super.onPostExecute(result);
if (result != null) {
onRequestTokenReceived(result);
} else {
requestTokenError(errorMessage);
}
}
}
private void onRequestTokenReceived(RequestToken token){
mRequestToken = token;
if (mRequestToken.getVerificationUrl() != null){
Uri url = Uri.parse(mRequestToken.getVerificationUrl());
Intent i = new Intent(Intent.ACTION_VIEW, url);
startActivity(i);
}
}
private void requestTokenError(String error){
mAccess.setText("Exception " + error);
}
private void getAccessToken(){
if (mRequestToken == null){
Toast.makeText(this, R.string.error_request_token_null, Toast.LENGTH_LONG).show();
} else if (mVerifier.getText() == null || mVerifier.getText().toString().equals("")){
Toast.makeText(this, R.string.error_pincode_null, Toast.LENGTH_LONG).show();
} else {
String param1 = mVerifier.getText().toString();
String param2 = mRequestToken.getToken();
String param3 = mRequestToken.getSecret();
String[] params = {param1, param2, param3};
new GetAccessTokenAsyncTask(this).execute(params);
}
}
private class GetAccessTokenAsyncTask extends BlueviaAsyncTask<String, Void, OAuthToken>{
public GetAccessTokenAsyncTask(Context context) {
super(context);
}
@Override
protected OAuthToken doInBackground(String... params) {
String param1 = params[0];
String param2 = params[1];
String param3 = params[2];
OAuthToken response = null;
try {
response = mClient.getAccessToken(param1,param2, param3);
} catch (BlueviaException e){
Log.e(TAG, e.getMessage(), e);
errorMessage = e.getMessage();
}
return response;
}
@Override
protected void onPostExecute(OAuthToken result) {
super.onPostExecute(result);
if (result != null) {
onAccessTokenReceived(result);
} else {
accessTokenError(errorMessage);
}
}
}
private void onAccessTokenReceived(OAuthToken token){
mAccessToken = token;
mAccess.setText(mAccessToken.getToken());
mAccessSecret.setText(mAccessToken.getSecret());
Log.i(TAG, "Token received: " + mAccessToken.getToken() + " " + mAccessToken.getSecret());
}
private void accessTokenError(String error){
mAccess.setText("Exception " + error);
}
private void reset(){
mConsumerKey.setText("");
mConsumerSecret.setText("");
mVerifier.setText("");
mAccess.setText("");
mAccessSecret.setText("");
mRequestToken = null;
mAccessToken = null;
}
@Override
protected void onDestroy() {
super.onDestroy();
//Close client
closeClient();
}
}