-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainActivity.java
168 lines (127 loc) · 4.51 KB
/
MainActivity.java
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
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.mobile.sample.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.liferay.mobile.android.callback.typed.JSONArrayCallback;
import com.liferay.mobile.android.oauth.OAuth;
import com.liferay.mobile.android.oauth.OAuthConfig;
import com.liferay.mobile.android.oauth.activity.OAuthActivity;
import com.liferay.mobile.android.service.Session;
import com.liferay.mobile.android.service.SessionImpl;
import com.liferay.mobile.android.util.Validator;
import com.liferay.mobile.android.v62.group.GroupService;
import com.liferay.mobile.sample.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* @author Bruno Farache
*/
public class MainActivity extends Activity {
public static final int AUTHENTICATE_REQUEST_CODE = 1;
@Override
public void onActivityResult(int request, int result, Intent intent) {
if (request != AUTHENTICATE_REQUEST_CODE) {
return;
}
if (result == RESULT_OK) {
OAuthConfig config = (OAuthConfig)intent.getSerializableExtra(
OAuthActivity.EXTRA_OAUTH_CONFIG);
String consumerKey = config.getConsumerKey();
String consumerSecret = config.getConsumerSecret();
String token = config.getToken();
String tokenSecret = config.getTokenSecret();
Log.d(_TAG, "Consumer key: " + consumerKey);
Log.d(_TAG, "Consumer secret: " + consumerSecret);
Log.d(_TAG, "Token: " + token);
Log.d(_TAG, "Token secret: " + tokenSecret);
String server = getString(R.string.oauth_server);
OAuth auth = new OAuth(config);
JSONArrayCallback callback = _getPrintSitesCallback();
Session session = new SessionImpl(server, auth, callback);
GroupService service = new GroupService(session);
try {
service.getUserSites();
}
catch (Exception e) {
Log.e(_TAG, "Error during service call", e);
}
}
else if (result == RESULT_CANCELED) {
Exception exception = (Exception)intent.getSerializableExtra(
OAuthActivity.EXTRA_EXCEPTION);
Toast.makeText(
this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
String server = getString(R.string.oauth_server);
String consumerKey = getString(R.string.oauth_consumer_key);
String consumerSecret = getString(R.string.oauth_consumer_secret);
if (Validator.isNull(server) || Validator.isNull(consumerKey) ||
Validator.isNull(consumerSecret)) {
Toast.makeText(
this, "oauth.xml is not properly configured.",
Toast.LENGTH_LONG).show();
return;
}
OAuthConfig config = new OAuthConfig(
server, consumerKey, consumerSecret);
final Intent intent = new Intent(this, OAuthActivity.class);
intent.putExtra(OAuthActivity.EXTRA_OAUTH_CONFIG, config);
Button login = (Button)findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(intent, AUTHENTICATE_REQUEST_CODE);
}
});
}
private JSONArrayCallback _getPrintSitesCallback() {
final TextView result = (TextView)findViewById(R.id.result);
return new JSONArrayCallback() {
@Override
public void onSuccess(JSONArray sites) {
try {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sites.length(); i++) {
JSONObject site = sites.getJSONObject(i);
sb.append(site.getString("name"));
sb.append("\n");
}
result.setText(sb.toString());
}
catch (JSONException je) {
Log.e(_TAG, "Could not parse JSON", je);
}
}
@Override
public void onFailure(Exception exception) {
result.setText(exception.getMessage());
}
};
}
private static final String _TAG = MainActivity.class.getSimpleName();
}