Add temporary code to do oauth authentication

This just adds an oauth button to the accountSetupBasics
screen, which will launch a webview and go to the google
authentication page.

Change-Id: I09d5182fa6081fb94b40e7910b71afbbee70387e
This commit is contained in:
Martin Hibdon 2013-12-04 14:25:59 -08:00
parent 5632d4449b
commit 874d25ff70
8 changed files with 178 additions and 5 deletions

View File

@ -138,6 +138,11 @@
<activity-alias android:name="com.android.mail.compose.ComposeActivity"
android:targetActivity="com.android.email.activity.ComposeActivityEmail"/>
<activity
android:name=".activity.setup.OAuthAuthenticationActivity"
android:label="@string/oauth_authentication_title">
</activity>
<activity
android:name=".activity.EventViewer"
android:label="@string/app_name"

View File

@ -55,12 +55,20 @@
style="@style/accountSetupButton"
android:text="@string/account_setup_basics_manual_setup_action" />
<Button
android:id="@+id/next"
android:id="@+id/oauth_setup"
android:layout_below="@+id/manual_setup"
android:layout_alignParentRight="true"
android:layout_marginTop="@dimen/setup_buttons_vertical_spacing"
android:layout_marginRight="@dimen/setup_buttons_padding_right"
style="@style/accountSetupButton"
android:text="@string/account_setup_basics_oauth_setup_action" />
<Button
android:id="@+id/next"
android:layout_below="@+id/oauth_setup"
android:layout_alignParentRight="true"
android:layout_marginTop="@dimen/setup_buttons_vertical_spacing"
android:layout_marginRight="@dimen/setup_buttons_padding_right"
style="@style/accountSetupButton"
android:text="@string/next_action" />
<!-- Frame on the left containing the (common) setup info -->

View File

@ -83,6 +83,13 @@
android:layout_marginLeft="@dimen/setup_buttons_padding_left"
style="@style/accountSetupButton"
android:text="@string/account_setup_basics_manual_setup_action" />
<Button
android:id="@+id/oauth_setup"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/setup_buttons_padding_left"
style="@style/accountSetupButton"
android:text="@string/account_setup_basics_oauth_setup_action" />
<Button
android:id="@+id/next"
android:layout_alignParentTop="true"

View File

@ -61,6 +61,12 @@
android:layout_width="0dip"
android:layout_weight="1"
android:text="@string/account_setup_basics_manual_setup_action" />
<Button
android:id="@+id/oauth_setup"
style="@style/accountSetupButton"
android:layout_width="0dip"
android:layout_weight="1"
android:text="@string/account_setup_basics_oauth_setup_action" />
<Button
android:id="@+id/next"
style="@style/accountSetupButton"

View File

@ -190,6 +190,8 @@
<!-- Title of screen when setting up new email account [CHAR LIMIT=45] -->
<string name="account_setup_basics_title">Account setup</string>
<!-- Do Not Translate. Title of screen when trying to get oauth authentication -->
<string name="oauth_authentication_title" translatable="false">OAuth authentication</string>
<!-- Headline of screen when setting up new email account (large text over divider)
[CHAR LIMIT=none] -->
<string name="account_setup_basics_headline">Email account</string>
@ -202,6 +204,10 @@
<string name="account_setup_basics_password_label">Password</string>
<!-- Button name on "Set up email" screen [CHAR LIMIT=20] -->
<string name="account_setup_basics_manual_setup_action">Manual setup</string>
<!-- Do not translate. Button name on "Set up email" screen [CHAR LIMIT=20] TODO: This is a temporary workaround
to allow initiating the OAuth setup flow before we have the real design working. This string
should be removed. -->
<string name="account_setup_basics_oauth_setup_action" translatable="false">OAuth</string>
<!-- Toast when we can't build a URI from the given email & password -->
<!-- Note, the error message in the toast is purposefully vague, because I *don't* know
exactly what's wrong. -->

View File

@ -114,6 +114,9 @@ public class AccountSetupBasics extends AccountSetupActivity
private final EmailAddressValidator mEmailValidator = new EmailAddressValidator();
private Provider mProvider;
private Button mManualButton;
// TODO: This is a temporary hack to allow us to start testing OAuth flow. It should be
// removed once we have the new setup UI working.
private Button mOAuthButton;
private Button mNextButton;
private boolean mNextButtonInhibit;
private boolean mPaused;
@ -237,12 +240,15 @@ public class AccountSetupBasics extends AccountSetupActivity
// Configure buttons
mManualButton = UiUtilities.getView(this, R.id.manual_setup);
mOAuthButton = UiUtilities.getView(this, R.id.oauth_setup);
mNextButton = UiUtilities.getView(this, R.id.next);
mManualButton.setVisibility(View.VISIBLE);
mManualButton.setOnClickListener(this);
mOAuthButton.setOnClickListener(this);
mNextButton.setOnClickListener(this);
// Force disabled until validator notifies otherwise
onEnableProceedButtons(false);
mOAuthButton.setEnabled(false);
// Lightweight debounce while Async tasks underway
mNextButtonInhibit = false;
@ -379,6 +385,22 @@ public class AccountSetupBasics extends AccountSetupActivity
case R.id.manual_setup:
onManualSetup(false);
break;
case R.id.oauth_setup:
// TODO: This is a temporary hack to allow oauth flow to be shown. It should be
// removed when the real account setup flow is implemented.
// TODO: Also note that this check reads and parses the xml file each time. This
// should probably get cached somewhere.
final String email = mEmailView.getText().toString().trim();
final String[] emailParts = email.split("@");
final String domain = emailParts[1].trim();
Provider provider = AccountSettingsUtils.findProviderForDomain(this, domain);
if (provider != null && provider.oauth != null) {
final Intent i = new Intent(this, OAuthAuthenticationActivity.class);
i.putExtra(OAuthAuthenticationActivity.EXTRA_EMAIL_ADDRESS, email);
i.putExtra(OAuthAuthenticationActivity.EXTRA_PROVIDER, provider.oauth);
startActivity(i);
}
break;
}
}
@ -410,6 +432,23 @@ public class AccountSetupBasics extends AccountSetupActivity
&& mEmailValidator.isValid(mEmailView.getText().toString().trim());
onEnableProceedButtons(valid);
// TODO: This is a temporary hack to allow oauth flow to be started. It should be
// removed when the real account setup flow is implemented.
boolean allowOauth = false;
if (!TextUtils.isEmpty(mEmailView.getText())
&& mEmailValidator.isValid(mEmailView.getText().toString().trim())) {
final String email = mEmailView.getText().toString().trim();
final String[] emailParts = email.split("@");
final String domain = emailParts[1].trim();
// TODO: Note that this check reads and parses the xml file each time. This
// should probably get cached somewhere.
Provider provider = AccountSettingsUtils.findProviderForDomain(this, domain);
if (provider != null && provider.oauth != null) {
allowOauth = true;
}
}
mOAuthButton.setEnabled(allowOauth);
// Warn (but don't prevent) if password has leading/trailing spaces
AccountSettingsUtils.checkPasswordSpaces(this, mPasswordView);
}

View File

@ -0,0 +1,102 @@
package com.android.email.activity.setup;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.android.email.R;
import com.android.emailcommon.Logging;
import com.android.emailcommon.VendorPolicyLoader.OAuthProvider;
import com.android.mail.utils.LogUtils;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* Activity to display a webview to perform oauth authentication. This activity
* should obtain an authorization code, which can be used to obtain access and
* refresh tokens.
*/
public class OAuthAuthenticationActivity extends Activity {
private final static String TAG = Logging.LOG_TAG;
public static final String EXTRA_EMAIL_ADDRESS = "email_address";
public static final String EXTRA_PROVIDER = "provider";
WebView mWv;
OAuthProvider mProvider;
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
// TODO: This method works for Google's redirect url to https://localhost.
// Does it work for the general case? I don't know what redirect url other
// providers use, or how the authentication code is returned.
LogUtils.d(TAG, "shouldOverrideUrlLoading %s", url);
final String deparameterizedUrl;
int i = url.lastIndexOf('?');
if (i == -1) {
deparameterizedUrl = url;
} else {
deparameterizedUrl = url.substring(0,i);
}
if (TextUtils.equals(deparameterizedUrl, mProvider.redirectUri)) {
final Uri uri = Uri.parse(url);
// Check the params of this uri, they contain success/failure info,
// along with the authentication token.
final String error = uri.getQueryParameter("error");
if (error != null) {
// TODO display failure screen
LogUtils.d(TAG, "error code %s", error);
Toast.makeText(OAuthAuthenticationActivity.this,
"Couldn't authenticate", Toast.LENGTH_LONG).show();
} else {
// TODO use this token to request the access and refresh tokens
final String code = uri.getQueryParameter("code");
LogUtils.d(TAG, "authorization code %s", code);
Toast.makeText(OAuthAuthenticationActivity.this,
"OAuth not implemented", Toast.LENGTH_LONG).show();
}
finish();
return true;
} else {
return false;
}
}
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
CookieSyncManager.createInstance(this);
CookieManager cm = CookieManager.getInstance();
cm.removeAllCookie();
mWv = new WebView(this);
mWv.setWebViewClient(new MyWebViewClient());
mWv.getSettings().setJavaScriptEnabled(true);
setContentView(mWv);
final Intent i = getIntent();
final String email = i.getStringExtra(EXTRA_EMAIL_ADDRESS);
final String providerName = i.getStringExtra(EXTRA_PROVIDER);
mProvider = AccountSettingsUtils.findOAuthProvider(this, providerName);
final Uri uri = AccountSettingsUtils.createOAuthRegistrationRequest(this, mProvider, email);
LogUtils.d(Logging.LOG_TAG, "launching '%s'", uri);
mWv.loadUrl(uri.toString());
}
}

View File

@ -497,10 +497,10 @@ public class EmailProvider extends ContentProvider {
if (DatabaseUtils.longForQuery(mainDatabase,
"SELECT EXISTS (SELECT ? FROM " + Account.TABLE_NAME + " )",
EmailContent.ID_PROJECTION) > 0) {
if (MailActivityEmail.DEBUG) {
LogUtils.w(TAG, "restoreIfNeeded: Account exists.");
}
return;
if (MailActivityEmail.DEBUG) {
LogUtils.w(TAG, "restoreIfNeeded: Account exists.");
}
return;
}
restoreAccounts(context, mainDatabase);