Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / PithosLoginActivity.java @ 74c976a0

History | View | Annotate | Download (7.4 kB)

1
package com.rackspace.cloud.android;
2

    
3

    
4

    
5
import java.net.URLDecoder;
6
import java.util.Date;
7
import java.util.StringTokenizer;
8
import java.util.regex.Matcher;
9
import java.util.regex.Pattern;
10

    
11
import org.apache.http.impl.cookie.BasicClientCookie;
12

    
13
import android.app.AlertDialog;
14
import android.content.DialogInterface;
15
import android.content.Intent;
16
import android.net.http.SslError;
17
import android.os.Build;
18
import android.os.Bundle;
19
import android.text.format.DateFormat;
20
import android.text.method.PasswordTransformationMethod;
21
import android.util.Log;
22
import android.webkit.CookieManager;
23
import android.webkit.CookieSyncManager;
24
import android.webkit.HttpAuthHandler;
25
import android.webkit.SslErrorHandler;
26
import android.webkit.WebSettings.LayoutAlgorithm;
27
import android.webkit.WebView;
28
import android.webkit.WebViewClient;
29
import android.widget.EditText;
30
import android.widget.LinearLayout;
31
import android.widget.LinearLayout.LayoutParams;
32
import android.widget.TextView;
33

    
34

    
35
public class PithosLoginActivity extends CloudActivity{
36
        String loginUrl=null;
37
        String auth = null;
38
        public void onCreate(Bundle savedInstanceState) {
39
        super.onCreate(savedInstanceState);
40
        trackPageView(GoogleAnalytics.PAGE_PROVIDERS);
41
        Log.i("LOGIN","START");
42
        setContentView(R.layout.login);
43
        loginUrl = getIntent().getExtras().getString("login");
44
        auth = getIntent().getExtras().getString("auth");
45
        doLogin();
46
    
47
        
48
    } 
49
        /*
50
        protected void onSaveInstanceState(Bundle outState) {
51
                super.onSaveInstanceState(outState);
52
                //outState.putBoolean("isHidden", isHidden);
53
        }*/
54
        
55
        private void doLogin(){
56
                CookieSyncManager.createInstance(this);
57
                CookieSyncManager.getInstance().startSync();
58
                final WebView webview = (WebView) findViewById(R.id.browser);//new WebView(this);
59
                webview.getSettings().setJavaScriptEnabled(true);
60
                // webview.setHttpAuthUsernamePassword("vho.grnet.gr", "VHO login",
61
                // "ebs-koutsoub", "mwJBdgbtv");
62
                
63
                webview.clearCache(true);
64
                webview.clearFormData();
65
                webview.clearHistory();
66
                
67

    
68
                webview.getSettings().setBuiltInZoomControls(true);
69
                //webview.getSettings().setDefaultZoom(ZoomDensity.);
70
                if(Build.VERSION.SDK_INT!=7)
71
                        webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
72

    
73
                webview.setWebViewClient(new WebViewClient() {
74
                        
75
                        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
76
                          handler.proceed();
77
                     }
78

    
79
                          @Override public boolean shouldOverrideUrlLoading(WebView view,
80
                          String url) { 
81
                                  view.loadUrl(url); return true;
82
                          }
83
                         
84
                        @Override
85
                        public void onPageFinished(WebView view, String url) {
86
                                super.onPageFinished(view, url);
87
                                //webview.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
88
                                CookieSyncManager.getInstance().sync();
89
                                String cookie = CookieManager.getInstance().getCookie(url);
90
                                Log.i("LOGIN COOKIE","c: "+cookie);
91
                                
92
                                if(null!=cookie && cookie.startsWith("_pithos2_a")){
93
                                        try {
94
                                                String value = URLDecoder.decode(parseRawCookie(cookie).getValue());
95
                                                StringTokenizer str = new StringTokenizer(value,"|");
96
                                                if(str.countTokens()==2){
97
                                                        String u = str.nextToken();
98
                                                        String t = str.nextToken();
99
                                                        Log.i("value",value);
100
                                                        Log.i("s1",u);
101
                                                        Log.i("s2",t);
102
                                                        exit(u,t);
103
                                                }
104
                                                
105
                                                //exit(s[0], s[1]);
106
                                        } catch (Exception e) {
107
                                                // TODO Auto-generated catch block
108
                                                e.printStackTrace();
109
                                        }
110
                                }
111
                        }
112

    
113
                        @Override
114
                        public void onReceivedHttpAuthRequest(WebView view,
115
                                        final HttpAuthHandler handler, String host, String realm) {
116
                                String[] up = view.getHttpAuthUsernamePassword(host, realm);
117
                                if (up != null && up.length == 2) {
118
                                        handler.proceed(up[0], up[1]);
119
                                        
120
                                } else {
121
                                                final AlertDialog.Builder alert = new AlertDialog.Builder(PithosLoginActivity.this);
122
                                        alert.setTitle("Pithos Login");
123
                                        //View lv = findViewById(R.layout.dialoglogin);
124
                                        //alert.setView(lv);
125
                                            final EditText input = new EditText(PithosLoginActivity.this);//(EditText) lv.findViewById(R.id.username); 
126
                                            final EditText pass = new EditText(PithosLoginActivity.this);//(EditText) lv.findViewById(R.id.password);
127
                                            pass.setTransformationMethod(new PasswordTransformationMethod());
128
                                            LinearLayout layout = new LinearLayout(PithosLoginActivity.this);
129
                                            layout.setOrientation(LinearLayout.VERTICAL);
130
                                            LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
131
                                            layout.setLayoutParams(params);
132
                                            input.setLayoutParams(params);
133
                                            pass.setLayoutParams(params);
134
                                            TextView username = new TextView(PithosLoginActivity.this);
135
                                            username.setText("Username:");
136
                                            
137
                                            TextView password = new TextView(PithosLoginActivity.this);
138
                                            password.setText("Password:");
139
                                            layout.addView(username);
140
                                            layout.addView(input);
141
                                            layout.addView(password);
142
                                            layout.addView(pass);
143
                                            alert.setView(layout);
144
                                            
145
                                            alert.setPositiveButton("Login", new DialogInterface.OnClickListener() {
146
                                                    public void onClick(DialogInterface dialog, int whichButton) {
147
                                                            String uvalue = input.getText().toString().trim();
148
                                                            String pvalue = pass.getText().toString().trim();
149
                                                            handler.proceed(uvalue, pvalue);                                                            
150
                                                    }
151
                                            });
152

    
153
                                            alert.setNegativeButton("Cancel",
154
                                                            new DialogInterface.OnClickListener() {
155
                                                                    public void onClick(DialogInterface dialog, int whichButton) {
156
                                                                            dialog.cancel();
157
                                                                    }
158
                                                            });
159
                                            alert.show();
160
                                        }
161
                                }
162

    
163
                        
164
                });
165
                Log.i("LOGIN","LOADING:"+loginUrl);
166
                //setContentView(webview);
167
                webview.clearSslPreferences();
168
                //webview.loadUrl("https://pithos.dev.grnet.gr/im/login/invitation?code=6879429392467020041");
169
                webview.loadUrl("https://plus.pithos.grnet.gr/im/login/invitation?code=3219662435709009446");
170
                //webview.loadUrl("http://www.google.gr");
171
        }
172
        void exit(String username,String token){
173
                Log.i("LOGINA",username+" "+token);
174
                Intent result = new Intent();
175
                Bundle b = new Bundle();
176
                b.putString("username", username);
177
                b.putString("apiKey", token);
178
                b.putString("server", auth);
179
                result.putExtra("accountInfo", b);
180
                setResult(RESULT_OK, result);
181
                finish();
182
        }
183
        
184
        
185
        BasicClientCookie parseRawCookie(String rawCookie) throws Exception {
186
            String[] rawCookieParams = rawCookie.split(";");
187

    
188
            String[] rawCookieNameAndValue = rawCookieParams[0].split("=");
189
            if (rawCookieNameAndValue.length != 2) {
190
                throw new Exception("Invalid cookie: missing name and value.");
191
            }
192

    
193
            String cookieName = rawCookieNameAndValue[0].trim();
194
            String cookieValue = rawCookieNameAndValue[1].trim();
195
            BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
196
            for (int i = 1; i < rawCookieParams.length; i++) {
197
                String rawCookieParamNameAndValue[] = rawCookieParams[i].trim().split("=");
198

    
199
                String paramName = rawCookieParamNameAndValue[0].trim();
200

    
201
                if (paramName.equalsIgnoreCase("secure")) {
202
                    cookie.setSecure(true);
203
                } else {
204
                    if (rawCookieParamNameAndValue.length != 2) {
205
                        throw new Exception("Invalid cookie: attribute not a flag or missing value.");
206
                    }
207

    
208
                    String paramValue = rawCookieParamNameAndValue[1].trim();
209

    
210
                    
211
                }
212
            }
213

    
214
            return cookie;
215
        }
216
}