root / src / com / rackspace / cloud / android / PithosLoginActivity.java @ 378fe36a
History | View | Annotate | Download (10.4 kB)
1 |
package com.rackspace.cloud.android; |
---|---|
2 |
|
3 |
import java.net.URLDecoder; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Arrays; |
6 |
import java.util.HashMap; |
7 |
import java.util.Map; |
8 |
import java.util.StringTokenizer; |
9 |
|
10 |
import org.apache.http.impl.cookie.BasicClientCookie; |
11 |
|
12 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
13 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
14 |
|
15 |
import android.app.AlertDialog; |
16 |
import android.content.DialogInterface; |
17 |
import android.content.Intent; |
18 |
import android.net.http.SslError; |
19 |
import android.os.AsyncTask; |
20 |
import android.os.Build; |
21 |
import android.os.Bundle; |
22 |
import android.text.method.PasswordTransformationMethod; |
23 |
import android.util.Log; |
24 |
import android.view.MotionEvent; |
25 |
import android.view.View; |
26 |
import android.webkit.CookieManager; |
27 |
import android.webkit.CookieSyncManager; |
28 |
import android.webkit.HttpAuthHandler; |
29 |
import android.webkit.SslErrorHandler; |
30 |
import android.webkit.WebSettings.LayoutAlgorithm; |
31 |
import android.webkit.WebView; |
32 |
import android.webkit.WebViewClient; |
33 |
import android.widget.EditText; |
34 |
import android.widget.LinearLayout; |
35 |
import android.widget.LinearLayout.LayoutParams; |
36 |
import android.widget.TextView; |
37 |
|
38 |
public class PithosLoginActivity extends CloudActivity { |
39 |
String loginUrl = null; |
40 |
String auth = null; |
41 |
|
42 |
public void onCreate(Bundle savedInstanceState) { |
43 |
super.onCreate(savedInstanceState);
|
44 |
trackPageView(GoogleAnalytics.PAGE_PROVIDERS); |
45 |
Log.i("LOGIN", "START"); |
46 |
|
47 |
setContentView(R.layout.login); |
48 |
CookieSyncManager.createInstance(this);
|
49 |
CookieSyncManager.getInstance().startSync(); |
50 |
CookieSyncManager.getInstance().sync(); |
51 |
// String loginUrl
|
52 |
// ="https://plus.pithos.grnet.gr/im/login/";//invitation?code=3219662435709009446");
|
53 |
/*
|
54 |
* CookieManager.getInstance().setCookie(loginUrl,
|
55 |
* "_pithos2_a='';expires=Mon, 17 Oct 2010 10:47:11 UTC;");
|
56 |
* CookieManager.getInstance().removeExpiredCookie();
|
57 |
* CookieManager.getInstance().removeSessionCookie();
|
58 |
*/
|
59 |
CookieManager.getInstance().removeAllCookie(); |
60 |
final WebView webview = (WebView) findViewById(R.id.browser);// new |
61 |
// WebView(this);
|
62 |
|
63 |
loginUrl = getIntent().getExtras().getString("login");
|
64 |
auth = getIntent().getExtras().getString("auth");
|
65 |
webview.requestFocus(View.FOCUS_DOWN);
|
66 |
|
67 |
webview.setOnTouchListener(new View.OnTouchListener() { |
68 |
@Override
|
69 |
public boolean onTouch(View v, MotionEvent event) { |
70 |
switch (event.getAction()) {
|
71 |
case MotionEvent.ACTION_DOWN:
|
72 |
case MotionEvent.ACTION_UP:
|
73 |
if (!v.hasFocus()) {
|
74 |
v.requestFocus(); |
75 |
} |
76 |
break;
|
77 |
} |
78 |
return false; |
79 |
} |
80 |
}); |
81 |
doLogin(webview); |
82 |
|
83 |
} |
84 |
|
85 |
/*
|
86 |
* protected void onSaveInstanceState(Bundle outState) {
|
87 |
* super.onSaveInstanceState(outState); //outState.putBoolean("isHidden",
|
88 |
* isHidden); }
|
89 |
*/
|
90 |
|
91 |
private void doLogin(final WebView webview) { |
92 |
|
93 |
webview.getSettings().setJavaScriptEnabled(true);
|
94 |
// webview.setHttpAuthUsernamePassword("vho.grnet.gr", "VHO login",
|
95 |
// "ebs-koutsoub", "mwJBdgbtv");
|
96 |
|
97 |
webview.clearCache(true);
|
98 |
webview.clearFormData(); |
99 |
webview.clearHistory(); |
100 |
|
101 |
// webview.getSettings().setBuiltInZoomControls(true);
|
102 |
webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL); |
103 |
// if(Build.VERSION.SDK_INT!=7)
|
104 |
// webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
|
105 |
|
106 |
webview.setWebViewClient(new WebViewClient() {
|
107 |
|
108 |
public void onReceivedSslError(WebView view, |
109 |
SslErrorHandler handler, SslError error) { |
110 |
handler.proceed(); |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public boolean shouldOverrideUrlLoading(WebView view, String url) { |
115 |
view.loadUrl(url); |
116 |
return true; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public void onPageFinished(WebView view, String url) { |
121 |
super.onPageFinished(view, url);
|
122 |
|
123 |
CookieSyncManager.getInstance().sync(); |
124 |
|
125 |
String cookie = CookieManager.getInstance().getCookie(url);
|
126 |
if (cookie != null) |
127 |
Log.i("LOGIN COOKIE",
|
128 |
url + " c: " + cookie + " " |
129 |
+ cookie.contains("_pithos2_a"));
|
130 |
/*
|
131 |
* if(url.indexOf('?')!=-1){ String q =
|
132 |
* url.substring(url.indexOf('?'),url.length());
|
133 |
* Log.i("LOGIN COOKIE",url+" c: "+q); Map<String,String> params
|
134 |
* = getQueryMap(q); String token = params.get("token"); String
|
135 |
* user = params.get("user"); if(token!=null&&user!=null)
|
136 |
* exit(user,token);
|
137 |
*
|
138 |
* }
|
139 |
*/
|
140 |
if (null != cookie && cookie.contains("_pithos2_a")) { |
141 |
StringTokenizer str1 = new StringTokenizer(cookie, ";"); |
142 |
while (str1.hasMoreTokens()) {
|
143 |
String cc = str1.nextToken();
|
144 |
Log.i("LOGIN", "token:" + cc); |
145 |
if (cc.trim().startsWith("_pithos2_a")) { |
146 |
Log.i("LOGIN COOKIE INSIDE", url + " c: " + cc); |
147 |
try {
|
148 |
String value = URLDecoder |
149 |
.decode(parseRawCookie(cc).getValue()); |
150 |
StringTokenizer str = new StringTokenizer( |
151 |
value, "|");
|
152 |
if (str.countTokens() == 2) { |
153 |
String u = str.nextToken();
|
154 |
String t = str.nextToken();
|
155 |
if (u.startsWith("\"")) |
156 |
u = u.substring(1, u.length());
|
157 |
if (t.endsWith("\"")) |
158 |
t = t.substring(0, t.length() - 1); |
159 |
Log.i("value", value);
|
160 |
Log.i("s1", u);
|
161 |
Log.i("s2", t);
|
162 |
exit(u, t); |
163 |
return;
|
164 |
} |
165 |
|
166 |
// exit(s[0], s[1]);
|
167 |
} catch (Exception e) { |
168 |
// TODO Auto-generated catch block
|
169 |
e.printStackTrace(); |
170 |
} |
171 |
} |
172 |
} |
173 |
|
174 |
} |
175 |
} |
176 |
|
177 |
@Override
|
178 |
public void onReceivedHttpAuthRequest(WebView view, |
179 |
final HttpAuthHandler handler, String host, String realm) { |
180 |
String[] up = view.getHttpAuthUsernamePassword(host, realm); |
181 |
if (up != null && up.length == 2) { |
182 |
handler.proceed(up[0], up[1]); |
183 |
|
184 |
} else {
|
185 |
final AlertDialog.Builder alert = new AlertDialog.Builder( |
186 |
PithosLoginActivity.this); |
187 |
alert.setTitle("Pithos Login");
|
188 |
// View lv = findViewById(R.layout.dialoglogin);
|
189 |
// alert.setView(lv);
|
190 |
final EditText input = new EditText( |
191 |
PithosLoginActivity.this);// (EditText)
|
192 |
// lv.findViewById(R.id.username);
|
193 |
final EditText pass = new EditText(PithosLoginActivity.this);// (EditText) |
194 |
// lv.findViewById(R.id.password);
|
195 |
pass.setTransformationMethod(new PasswordTransformationMethod());
|
196 |
LinearLayout layout = new LinearLayout(
|
197 |
PithosLoginActivity.this); |
198 |
layout.setOrientation(LinearLayout.VERTICAL); |
199 |
LayoutParams params = new LayoutParams(
|
200 |
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); |
201 |
layout.setLayoutParams(params); |
202 |
input.setLayoutParams(params); |
203 |
pass.setLayoutParams(params); |
204 |
TextView username = new TextView(PithosLoginActivity.this);
|
205 |
username.setText("Username:");
|
206 |
|
207 |
TextView password = new TextView(PithosLoginActivity.this);
|
208 |
password.setText("Password:");
|
209 |
layout.addView(username); |
210 |
layout.addView(input); |
211 |
layout.addView(password); |
212 |
layout.addView(pass); |
213 |
alert.setView(layout); |
214 |
|
215 |
alert.setPositiveButton("Login",
|
216 |
new DialogInterface.OnClickListener() {
|
217 |
public void onClick(DialogInterface dialog, |
218 |
int whichButton) {
|
219 |
String uvalue = input.getText().toString()
|
220 |
.trim(); |
221 |
String pvalue = pass.getText().toString()
|
222 |
.trim(); |
223 |
handler.proceed(uvalue, pvalue); |
224 |
} |
225 |
}); |
226 |
|
227 |
alert.setNegativeButton("Cancel",
|
228 |
new DialogInterface.OnClickListener() {
|
229 |
public void onClick(DialogInterface dialog, |
230 |
int whichButton) {
|
231 |
dialog.cancel(); |
232 |
} |
233 |
}); |
234 |
alert.show(); |
235 |
} |
236 |
} |
237 |
|
238 |
}); |
239 |
Log.i("LOGIN", "LOADING:" + loginUrl); |
240 |
// setContentView(webview);
|
241 |
webview.clearSslPreferences(); |
242 |
// webview.loadUrl("https://pithos.dev.grnet.gr/im/login/invitation?code=6879429392467020041");
|
243 |
|
244 |
webview.loadUrl(loginUrl);// invitation?code=3219662435709009446");
|
245 |
|
246 |
// webview.loadUrl("http://www.google.gr");
|
247 |
} |
248 |
|
249 |
public static Map<String, String> getQueryMap(String query) { |
250 |
String[] params = query.split("&"); |
251 |
Map<String, String> map = new HashMap<String, String>(); |
252 |
for (String param : params) { |
253 |
String name = param.split("=")[0]; |
254 |
String value = param.split("=")[1]; |
255 |
if (param.endsWith("==")) |
256 |
value = value + "==";
|
257 |
else if (param.endsWith("=")) |
258 |
value = value + "=";
|
259 |
|
260 |
Log.d("DD", name + " " + value); |
261 |
map.put(name, value); |
262 |
} |
263 |
return map;
|
264 |
} |
265 |
|
266 |
void exit(final String username, final String token) { |
267 |
Log.d("LOGINA", username + " " + token); |
268 |
Intent result = new Intent();
|
269 |
Bundle b = new Bundle();
|
270 |
b.putString("username", username);
|
271 |
b.putString("apiKey", token);
|
272 |
b.putString("server", auth);
|
273 |
result.putExtra("accountInfo", b);
|
274 |
setResult(RESULT_OK, result); |
275 |
PithosLoginActivity.this.finish(); |
276 |
} |
277 |
|
278 |
public void returnToCaller(Bundle b) { |
279 |
Intent result = new Intent();
|
280 |
result.putExtra("accountInfo", b);
|
281 |
setResult(RESULT_OK, result); |
282 |
finish(); |
283 |
} |
284 |
|
285 |
BasicClientCookie parseRawCookie(String rawCookie) throws Exception { |
286 |
String[] rawCookieParams = rawCookie.split(";"); |
287 |
|
288 |
String[] rawCookieNameAndValue = rawCookieParams[0].split("="); |
289 |
if (rawCookieNameAndValue.length != 2) { |
290 |
throw new Exception("Invalid cookie: missing name and value."); |
291 |
} |
292 |
|
293 |
String cookieName = rawCookieNameAndValue[0].trim(); |
294 |
String cookieValue = rawCookieNameAndValue[1].trim(); |
295 |
BasicClientCookie cookie = new BasicClientCookie(cookieName,
|
296 |
cookieValue); |
297 |
for (int i = 1; i < rawCookieParams.length; i++) { |
298 |
String rawCookieParamNameAndValue[] = rawCookieParams[i].trim() |
299 |
.split("=");
|
300 |
|
301 |
String paramName = rawCookieParamNameAndValue[0].trim(); |
302 |
|
303 |
if (paramName.equalsIgnoreCase("secure")) { |
304 |
cookie.setSecure(true);
|
305 |
} else {
|
306 |
if (rawCookieParamNameAndValue.length != 2) { |
307 |
throw new Exception( |
308 |
"Invalid cookie: attribute not a flag or missing value.");
|
309 |
} |
310 |
|
311 |
String paramValue = rawCookieParamNameAndValue[1].trim(); |
312 |
|
313 |
} |
314 |
} |
315 |
|
316 |
return cookie;
|
317 |
} |
318 |
|
319 |
private String lastHtml; |
320 |
|
321 |
public void setLastHtml(String lastHtml) { |
322 |
this.lastHtml = lastHtml;
|
323 |
} |
324 |
|
325 |
public String getLastHtml() { |
326 |
return lastHtml;
|
327 |
} |
328 |
|
329 |
class MyJavaScriptInterface { |
330 |
|
331 |
public void showHTML(String html) { |
332 |
setLastHtml(html); |
333 |
|
334 |
} |
335 |
} |
336 |
|
337 |
private String getHtmlValue(String substring, String html) { |
338 |
String result = null; |
339 |
boolean found = false; |
340 |
found = html.contains(substring); |
341 |
if (found) {
|
342 |
int indx = html.indexOf(substring);
|
343 |
StringBuffer res = new StringBuffer(); |
344 |
for (int i = indx + substring.length(); i < html.length(); i++) { |
345 |
char c = html.charAt(i);
|
346 |
if (c == '<') |
347 |
break;
|
348 |
res.append(c); |
349 |
} |
350 |
|
351 |
result = res.toString(); |
352 |
result = result.trim(); |
353 |
|
354 |
} |
355 |
return result;
|
356 |
} |
357 |
|
358 |
} |