OAuthAuthenticationActivity.java revision 874d25ff7073731a08c09b4528add58b720c4f6a
1package com.android.email.activity.setup; 2 3import android.app.Activity; 4import android.content.Intent; 5import android.graphics.Bitmap; 6import android.net.Uri; 7import android.os.Bundle; 8import android.os.Handler; 9import android.text.TextUtils; 10import android.webkit.CookieManager; 11import android.webkit.CookieSyncManager; 12import android.webkit.WebResourceResponse; 13import android.webkit.WebView; 14import android.webkit.WebViewClient; 15import android.widget.Toast; 16 17import com.android.email.R; 18import com.android.emailcommon.Logging; 19import com.android.emailcommon.VendorPolicyLoader.OAuthProvider; 20import com.android.mail.utils.LogUtils; 21 22import java.net.URI; 23import java.net.URISyntaxException; 24import java.net.URL; 25 26 27/** 28 * Activity to display a webview to perform oauth authentication. This activity 29 * should obtain an authorization code, which can be used to obtain access and 30 * refresh tokens. 31 */ 32public class OAuthAuthenticationActivity extends Activity { 33 private final static String TAG = Logging.LOG_TAG; 34 35 public static final String EXTRA_EMAIL_ADDRESS = "email_address"; 36 public static final String EXTRA_PROVIDER = "provider"; 37 38 WebView mWv; 39 OAuthProvider mProvider; 40 41 private class MyWebViewClient extends WebViewClient { 42 43 @Override 44 public boolean shouldOverrideUrlLoading(WebView wv, String url) { 45 // TODO: This method works for Google's redirect url to https://localhost. 46 // Does it work for the general case? I don't know what redirect url other 47 // providers use, or how the authentication code is returned. 48 LogUtils.d(TAG, "shouldOverrideUrlLoading %s", url); 49 final String deparameterizedUrl; 50 int i = url.lastIndexOf('?'); 51 if (i == -1) { 52 deparameterizedUrl = url; 53 } else { 54 deparameterizedUrl = url.substring(0,i); 55 } 56 57 if (TextUtils.equals(deparameterizedUrl, mProvider.redirectUri)) { 58 final Uri uri = Uri.parse(url); 59 // Check the params of this uri, they contain success/failure info, 60 // along with the authentication token. 61 final String error = uri.getQueryParameter("error"); 62 if (error != null) { 63 // TODO display failure screen 64 LogUtils.d(TAG, "error code %s", error); 65 Toast.makeText(OAuthAuthenticationActivity.this, 66 "Couldn't authenticate", Toast.LENGTH_LONG).show(); 67 } else { 68 // TODO use this token to request the access and refresh tokens 69 final String code = uri.getQueryParameter("code"); 70 LogUtils.d(TAG, "authorization code %s", code); 71 Toast.makeText(OAuthAuthenticationActivity.this, 72 "OAuth not implemented", Toast.LENGTH_LONG).show(); 73 } 74 finish(); 75 return true; 76 } else { 77 return false; 78 } 79 } 80 } 81 82 @Override 83 public void onCreate(Bundle bundle) { 84 super.onCreate(bundle); 85 CookieSyncManager.createInstance(this); 86 CookieManager cm = CookieManager.getInstance(); 87 cm.removeAllCookie(); 88 89 mWv = new WebView(this); 90 mWv.setWebViewClient(new MyWebViewClient()); 91 mWv.getSettings().setJavaScriptEnabled(true); 92 setContentView(mWv); 93 94 final Intent i = getIntent(); 95 final String email = i.getStringExtra(EXTRA_EMAIL_ADDRESS); 96 final String providerName = i.getStringExtra(EXTRA_PROVIDER); 97 mProvider = AccountSettingsUtils.findOAuthProvider(this, providerName); 98 final Uri uri = AccountSettingsUtils.createOAuthRegistrationRequest(this, mProvider, email); 99 LogUtils.d(Logging.LOG_TAG, "launching '%s'", uri); 100 mWv.loadUrl(uri.toString()); 101 } 102} 103