Added Google OAuth as a login option in astrid.com login

pull/14/head
Tim Su 14 years ago
parent 4569ca6bee
commit 2143ddb461

@ -406,8 +406,8 @@
</receiver> </receiver>
<!-- actfm --> <!-- actfm -->
<activity android:name="com.todoroo.astrid.actfm.ActFmLoginActivity"> <activity android:name="com.todoroo.astrid.actfm.ActFmLoginActivity" />
</activity> <activity android:name="com.todoroo.astrid.actfm.OAuthLoginActivity" />
<activity android:name="com.todoroo.astrid.actfm.ActFmPreferences" <activity android:name="com.todoroo.astrid.actfm.ActFmPreferences"
android:theme="@android:style/Theme" android:theme="@android:style/Theme"
android:label="@string/actfm_APr_header"> android:label="@string/actfm_APr_header">

@ -21,7 +21,9 @@ package com.todoroo.astrid.actfm;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.security.NoSuchAlgorithmException;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -35,7 +37,6 @@ import android.graphics.PixelFormat;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.text.method.PasswordTransformationMethod; import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.WindowManager; import android.view.WindowManager;
@ -57,6 +58,7 @@ import com.timsu.astrid.R;
import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.service.ExceptionService;
import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.ActFmInvoker; import com.todoroo.astrid.actfm.sync.ActFmInvoker;
@ -76,6 +78,7 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
public static final String APP_ID = "183862944961271"; //$NON-NLS-1$ public static final String APP_ID = "183862944961271"; //$NON-NLS-1$
@Autowired ExceptionService exceptionService;
@Autowired TaskService taskService; @Autowired TaskService taskService;
@Autowired ActFmPreferenceService actFmPreferenceService; @Autowired ActFmPreferenceService actFmPreferenceService;
private final ActFmInvoker actFmInvoker = new ActFmInvoker(); private final ActFmInvoker actFmInvoker = new ActFmInvoker();
@ -87,7 +90,8 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
// --- ui initialization // --- ui initialization
private static final int REQUEST_CODE_GOOGLE = 1; private static final int REQUEST_CODE_GOOGLE_ACCOUNTS = 1;
private static final int REQUEST_CODE_OAUTH = 2;
static { static {
AstridDependencyInjector.initialize(); AstridDependencyInjector.initialize();
@ -122,6 +126,7 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
"publish_stream" "publish_stream"
}); });
findViewById(R.id.gg_login).setOnClickListener(googleListener);
findViewById(R.id.pw_signup).setOnClickListener(signUpListener); findViewById(R.id.pw_signup).setOnClickListener(signUpListener);
findViewById(R.id.pw_login).setOnClickListener(loginListener); findViewById(R.id.pw_login).setOnClickListener(loginListener);
@ -135,6 +140,23 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
// --- event handler // --- event handler
private final OnClickListener googleListener = new OnClickListener() {
@Override
@SuppressWarnings("nls")
public void onClick(View arg0) {
Intent intent = new Intent(ActFmLoginActivity.this, OAuthLoginActivity.class);
try {
String url = actFmInvoker.createFetchUrl("user_oauth", "provider", "google");
intent.putExtra(OAuthLoginActivity.URL_TOKEN, url);
startActivityForResult(intent, REQUEST_CODE_OAUTH);
} catch (UnsupportedEncodingException e) {
handleError(e);
} catch (NoSuchAlgorithmException e) {
handleError(e);
}
}
};
private final OnClickListener signUpListener = new OnClickListener() { private final OnClickListener signUpListener = new OnClickListener() {
@Override @Override
public void onClick(View arg0) { public void onClick(View arg0) {
@ -292,7 +314,6 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
// --- utilities // --- utilities
@SuppressWarnings("nls")
public void authenticate(String email, String name, String provider, String secret) { public void authenticate(String email, String name, String provider, String secret) {
if(progressDialog == null) if(progressDialog == null)
progressDialog = DialogUtilities.progressDialog(this, getString(R.string.DLG_please_wait)); progressDialog = DialogUtilities.progressDialog(this, getString(R.string.DLG_please_wait));
@ -300,6 +321,16 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
try { try {
JSONObject result = actFmInvoker.authenticate(email, name, provider, secret); JSONObject result = actFmInvoker.authenticate(email, name, provider, secret);
String token = actFmInvoker.getToken(); String token = actFmInvoker.getToken();
postAuthenticate(result, token);
} catch (IOException e) {
handleError(e);
} finally {
DialogUtilities.dismissDialog(this, progressDialog);
}
}
@SuppressWarnings("nls")
private void postAuthenticate(JSONObject result, String token) {
actFmPreferenceService.setToken(token); actFmPreferenceService.setToken(token);
if(Preferences.getStringValue(R.string.actfm_APr_interval_key) == null) if(Preferences.getStringValue(R.string.actfm_APr_interval_key) == null)
@ -311,23 +342,23 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
Preferences.setString(ActFmPreferenceService.PREF_EMAIL, result.optString("email")); Preferences.setString(ActFmPreferenceService.PREF_EMAIL, result.optString("email"));
Preferences.setString(ActFmPreferenceService.PREF_PICTURE, result.optString("picture")); Preferences.setString(ActFmPreferenceService.PREF_PICTURE, result.optString("picture"));
C2DMReceiver.register();
setResult(RESULT_OK); setResult(RESULT_OK);
finish(); finish();
if(!noSync) if(!noSync)
synchronize(); synchronize();
} catch (IOException e) {
handleError(e); try {
} finally { C2DMReceiver.register();
DialogUtilities.dismissDialog(this, progressDialog); } catch (Exception e) {
// phone may not support c2dm
exceptionService.reportError("error-c2dm-register", e);
} }
} }
private void handleError(final Throwable e) { private void handleError(final Throwable e) {
DialogUtilities.dismissDialog(this, progressDialog); DialogUtilities.dismissDialog(this, progressDialog);
Log.e("astrid-sharing", "error-doing-sla", e); //$NON-NLS-1$ //$NON-NLS-2$ exceptionService.reportError("astrid-sharing-login", e); //$NON-NLS-1$
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
@ -349,7 +380,11 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, protected void onActivityResult(int requestCode, int resultCode,
Intent data) { Intent data) {
if(requestCode == REQUEST_CODE_GOOGLE){
if(resultCode == RESULT_CANCELED)
return;
if(requestCode == REQUEST_CODE_GOOGLE_ACCOUNTS){
String accounts[] = data.getExtras().getStringArray(GoogleLoginServiceConstants.ACCOUNTS_KEY); String accounts[] = data.getExtras().getStringArray(GoogleLoginServiceConstants.ACCOUNTS_KEY);
credentialsListener.getCredentials(accounts); credentialsListener.getCredentials(accounts);
} else if(requestCode == LoginButton.REQUEST_CODE_FACEBOOK) { } else if(requestCode == LoginButton.REQUEST_CODE_FACEBOOK) {
@ -370,6 +405,14 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
onFBAuthSucceed(); onFBAuthSucceed();
} }
errors.setVisibility(View.GONE); errors.setVisibility(View.GONE);
} else if(requestCode == REQUEST_CODE_OAUTH) {
String result = data.getStringExtra(OAuthLoginActivity.DATA_RESPONSE);
try {
JSONObject json = new JSONObject(result);
postAuthenticate(json, json.getString("token"));
} catch (JSONException e) {
handleError(e);
}
} }
} }
public interface OnGetCredentials { public interface OnGetCredentials {
@ -383,7 +426,7 @@ public class ActFmLoginActivity extends Activity implements AuthListener {
if(Integer.parseInt(Build.VERSION.SDK) >= 7) if(Integer.parseInt(Build.VERSION.SDK) >= 7)
credentialsListener.getCredentials(ModernAuthManager.getAccounts(this)); credentialsListener.getCredentials(ModernAuthManager.getAccounts(this));
else else
GoogleLoginServiceHelper.getAccount(this, REQUEST_CODE_GOOGLE, false); GoogleLoginServiceHelper.getAccount(this, REQUEST_CODE_GOOGLE_ACCOUNTS, false);
} }
} }

@ -0,0 +1,105 @@
/*
* ASTRID: Android's Simple Task Recording Dashboard
*
* Copyright (c) 2009 Tim Su
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.todoroo.astrid.actfm;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.timsu.astrid.R;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.service.RestClient;
/**
* This activity displays a <code>WebView</code> that allows users to log in to the
* synchronization provider requested. A callback method determines whether
* their login was successful and therefore whether to dismiss the dialog.
*
* @author timsu
*
*/
public class OAuthLoginActivity extends Activity {
/**
* URL to display
*/
public static final String URL_TOKEN = "u"; //$NON-NLS-1$
/**
* Resultant URL data
*/
public static final String DATA_RESPONSE = "response"; //$NON-NLS-1$
@Autowired RestClient restClient;
// --- ui initialization
@SuppressWarnings("nls")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DependencyInjectionService.getInstance().inject(this);
setContentView(R.layout.oauth_login_activity);
setTitle(R.string.actfm_ALA_login_title);
final String urlParam = getIntent().getStringExtra(URL_TOKEN);
final WebView webView = (WebView)findViewById(R.id.browser);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSavePassword(false);
webView.getSettings().setSupportZoom(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
System.err.println("page finished loading: " + url);
String data;
try {
data = restClient.get(url);
System.err.println("DATA: " + data);
if(data.startsWith("{")) { //$NON-NLS-1$
Intent intent = new Intent();
intent.putExtra(DATA_RESPONSE, data);
setResult(RESULT_OK, intent);
finish();
}
} catch (IOException e) {
Log.e("astrid", "error-load-url", e);
}
}
});
webView.loadUrl(urlParam);
}
}

@ -148,7 +148,7 @@ public class ActFmInvoker {
* @throws UnsupportedEncodingException * @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
private String createFetchUrl(String method, Object... getParameters) throws UnsupportedEncodingException, NoSuchAlgorithmException { public String createFetchUrl(String method, Object... getParameters) throws UnsupportedEncodingException, NoSuchAlgorithmException {
ArrayList<Pair<String, Object>> params = new ArrayList<Pair<String, Object>>(); ArrayList<Pair<String, Object>> params = new ArrayList<Pair<String, Object>>();
for(int i = 0; i < getParameters.length; i += 2) { for(int i = 0; i < getParameters.length; i += 2) {
if(getParameters[i+1] instanceof ArrayList) { if(getParameters[i+1] instanceof ArrayList) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 896 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

@ -79,17 +79,31 @@
android:textSize="16sp" android:textSize="16sp"
android:text="@string/actfm_ALA_pw_login" /> android:text="@string/actfm_ALA_pw_login" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/pw_login"
android:layout_alignParentLeft="true"
android:layout_marginBottom="3dip">
<Button
android:id="@+id/gg_login"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="45dip"
android:textSize="16sp"
android:drawableLeft="@drawable/google"
android:text="@string/actfm_ALA_gg_login" />
<com.facebook.android.LoginButton <com.facebook.android.LoginButton
android:id="@+id/fb_login" android:id="@+id/fb_login"
android:layout_weight="1"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="45dip" android:layout_height="45dip"
android:layout_above="@id/pw_login"
android:layout_alignParentLeft="true"
android:layout_marginBottom="3dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:textSize="16sp" android:textSize="16sp"
android:drawableLeft="@drawable/facebook" android:drawableLeft="@drawable/facebook"
android:text="@string/actfm_ALA_fb_login" /> android:text="@string/actfm_ALA_fb_login" />
</LinearLayout>
</RelativeLayout> </RelativeLayout>

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/actfm_OLA_prompt"/>
<WebView android:id="@+id/browser"
android:layout_weight="100"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

@ -133,13 +133,16 @@
share, and delegate with others. Perfect for personal use, friends, family, and coworkers!</string> share, and delegate with others. Perfect for personal use, friends, family, and coworkers!</string>
<!-- share login: Sharing Login FB Prompt --> <!-- share login: Sharing Login FB Prompt -->
<string name="actfm_ALA_fb_login">Login with Facebook</string> <string name="actfm_ALA_fb_login">Facebook</string>
<!-- share login: Sharing Login GG Prompt -->
<string name="actfm_ALA_gg_login">Google</string>
<!-- share login: Sharing Login Password Prompt --> <!-- share login: Sharing Login Password Prompt -->
<string name="actfm_ALA_pw_login">Login</string> <string name="actfm_ALA_pw_login">Login</string>
<!-- share login: Sharing Sign Up Prompt --> <!-- share login: Sharing Sign Up Prompt -->
<string name="actfm_ALA_pw_signup">Sign Up</string> <string name="actfm_ALA_pw_signup">Join Now</string>
<!-- share login: Name --> <!-- share login: Name -->
<string name="actfm_ALA_name_label">Name</string> <string name="actfm_ALA_name_label">Name</string>
@ -159,6 +162,9 @@
<!-- share login: Login Title --> <!-- share login: Login Title -->
<string name="actfm_ALA_login_title">Login to Astrid.com</string> <string name="actfm_ALA_login_title">Login to Astrid.com</string>
<!-- share login: OAUTH Login Prompt -->
<string name="actfm_OLA_prompt">Please connect to Google:</string>
<!-- share login: Sharing notice --> <!-- share login: Sharing notice -->
<string name="actfm_ALA_notice">We promise not to post messages or send <string name="actfm_ALA_notice">We promise not to post messages or send
e-mails without your permission.</string> e-mails without your permission.</string>

Loading…
Cancel
Save