make configurable calendar selection
[WhereAmI.git] / src / org / average / whereami / Authorize.java
1 // http://blog.doityourselfandroid.com/2011/08/06/oauth-2-0-flow-android/
2 // http://code.google.com/p/google-api-java-client/wiki/OAuth2Draft10
3
4 package org.average.whereami;
5
6 import java.io.IOException;
7
8 import org.average.whereami.ClientCredentials;
9 import org.average.whereami.PersistentStore;
10
11 import android.app.Activity;
12 import android.os.Bundle;
13 import android.graphics.Bitmap;
14 import android.content.Context;
15 import android.content.Intent;
16 import android.content.SharedPreferences;
17 import android.preference.PreferenceManager;
18 import android.util.Log;
19 import android.view.View;
20 import android.webkit.WebView;
21 import android.webkit.WebViewClient;
22
23 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
24 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
25 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
26 import com.google.api.client.http.javanet.NetHttpTransport;
27 import com.google.api.client.json.jackson.JacksonFactory;
28
29 public class Authorize extends Activity {
30
31     final String TAG = getClass().getName();
32     SharedPreferences prefs;
33
34     @Override
35     public void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         Log.w(TAG, "Starting task to retrieve request token.");
38         prefs = PreferenceManager.getDefaultSharedPreferences(this);
39     }
40
41     @Override
42     protected void onResume() {
43         super.onResume();
44         WebView webview = new WebView(this);
45         webview.getSettings().setJavaScriptEnabled(true);
46         webview.setVisibility(View.VISIBLE);
47         setContentView(webview);
48         String authorizationUrl = new GoogleAuthorizationRequestUrl(
49                                         ClientCredentials.CLIENT_ID,
50                                         ClientCredentials.REDIRECT_URI,
51                                         ClientCredentials.SCOPE).build();
52         webview.setWebViewClient(new WebViewClient() {
53             @Override
54             public void onPageStarted(WebView view, String url, Bitmap bitmap) {
55                 Log.w(TAG, "onPageStarted : " + url);
56             }
57             @Override
58             public void onPageFinished(WebView view, String url) {
59                 if (url.startsWith(ClientCredentials.REDIRECT_URI)) {
60                     try {
61                         if (url.indexOf("code=")!=-1) {
62                             String code = extractCodeFromUrl(url);
63                             AccessTokenResponse accessTokenResponse =
64                               new GoogleAuthorizationCodeGrant(
65                                 new NetHttpTransport(),
66                                 new JacksonFactory(),
67                                 ClientCredentials.CLIENT_ID,
68                                 ClientCredentials.CLIENT_SECRET,
69                                 code,
70                                 ClientCredentials.REDIRECT_URI).execute();
71                             PersistentStore credentialStore =
72                                                     new PersistentStore(prefs);
73                             credentialStore.write(accessTokenResponse);
74                             finish();
75                         } else if (url.indexOf("error=")!=-1) {
76                             new PersistentStore(prefs).clear();
77                             finish();
78                         }
79                     } catch (IOException e) {
80                         e.printStackTrace();
81                     }
82                 }
83                 Log.w(TAG, "onPageFinished : " + url);
84             }
85
86             private String extractCodeFromUrl(String url) {
87                 return url.substring(
88                             ClientCredentials.REDIRECT_URI.length()+7,
89                             url.length());
90             }
91         });
92
93         webview.loadUrl(authorizationUrl);
94     }
95 }