diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevLoginActivity.java b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevLoginActivity.java index c4c1efacf..e7a6c7d77 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevLoginActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/ProducteevLoginActivity.java @@ -20,6 +20,7 @@ package com.todoroo.astrid.producteev; import android.app.Activity; +import android.app.ProgressDialog; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -32,8 +33,12 @@ import android.widget.TextView; import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.utility.DialogUtilities; +import com.todoroo.astrid.producteev.api.ProducteevInvoker; +import com.todoroo.astrid.producteev.sync.ProducteevSyncProvider; +import com.todoroo.astrid.utility.Preferences; /** * This activity allows users to sign in or log in to Producteev @@ -56,6 +61,8 @@ public class ProducteevLoginActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + ContextManager.setContext(this); + setContentView(R.layout.producteev_login_activity); setTitle(R.string.producteev_PLA_title); @@ -85,9 +92,13 @@ public class ProducteevLoginActivity extends Activity { if(email.length() == 0 || password.length() == 0) { errors.setVisibility(View.VISIBLE); errors.setText(R.string.producteev_PLA_errorEmpty); + return; } + + performLogin(email.toString(), password.toString()); } } + }); Button createNew = (Button) findViewById(R.id.createNew); @@ -99,14 +110,108 @@ public class ProducteevLoginActivity extends Activity { else { Editable email = emailEditText.getText(); Editable password = passwordEditText.getText(); - if(email.length() == 0 || password.length() == 0) { + Editable confirmPassword = ((EditText)findViewById(R.id.confirmPassword)).getText(); + Editable firstName = ((EditText)findViewById(R.id.firstName)).getText(); + Editable lastName = ((EditText)findViewById(R.id.lastName)).getText(); + if(email.length() == 0 || password.length() == 0 || + confirmPassword.length() == 0 || firstName.length() == 0 || + lastName.length() == 0) { errors.setVisibility(View.VISIBLE); errors.setText(R.string.producteev_PLA_errorEmpty); + return; } - // + if(!confirmPassword.toString().equals(password.toString())) { + errors.setVisibility(View.VISIBLE); + errors.setText(R.string.producteev_PLA_errorMatch); + return; + } + performSignup(email.toString(), password.toString(), + firstName.toString(), lastName.toString()); } } }); } + + + private void performLogin(final String email, final String password) { + final ProgressDialog dialog = dialogUtilities.progressDialog(this, + getString(R.string.DLG_wait)); + final TextView errors = (TextView) findViewById(R.id.error); + dialog.show(); + new Thread() { + @Override + public void run() { + ProducteevInvoker invoker = ProducteevSyncProvider.getInvoker(); + final StringBuilder errorMessage = new StringBuilder(); + try { + invoker.authenticate(email, password); + + Preferences.setString(R.string.producteev_PPr_email, email); + Preferences.setString(R.string.producteev_PPr_password, password); + ProducteevUtilities.INSTANCE.setToken(invoker.getToken()); + + synchronize(); + } catch (Exception e) { + errorMessage.append(e.getMessage()); + } finally { + runOnUiThread(new Runnable() { + public void run() { + dialog.dismiss(); + if(errorMessage.length() > 0) { + errors.setVisibility(View.VISIBLE); + errors.setText(errorMessage); + } + } + }); + } + } + }.start(); + } + + private void performSignup(final String email, final String password, + final String firstName, final String lastName) { + final ProgressDialog dialog = dialogUtilities.progressDialog(this, + getString(R.string.DLG_wait)); + final TextView errors = (TextView) findViewById(R.id.error); + dialog.show(); + new Thread() { + @Override + public void run() { + ProducteevInvoker invoker = ProducteevSyncProvider.getInvoker(); + final StringBuilder errorMessage = new StringBuilder(); + try { + invoker.usersSignUp(email, firstName, lastName, password, null); + invoker.authenticate(email, password); + + Preferences.setString(R.string.producteev_PPr_email, email); + Preferences.setString(R.string.producteev_PPr_password, password); + ProducteevUtilities.INSTANCE.setToken(invoker.getToken()); + + synchronize(); + } catch (Exception e) { + errorMessage.append(e.getMessage()); + } finally { + runOnUiThread(new Runnable() { + public void run() { + dialog.dismiss(); + if(errorMessage.length() > 0) { + errors.setVisibility(View.VISIBLE); + errors.setText(errorMessage); + } + } + }); + } + } + }.start(); + } + + /** + * Perform synchronization + */ + protected void synchronize() { + startService(new Intent(ProducteevBackgroundService.SYNC_ACTION, null, + this, ProducteevBackgroundService.class)); + finish(); + } } \ No newline at end of file diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java b/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java index 584d018c4..01d621d42 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevInvoker.java @@ -81,6 +81,21 @@ public class ProducteevInvoker { } + // --- users + + /** + * Sign up as the given user + */ + public JSONObject usersSignUp(String email, String firstName, String lastName, String + password, Long fbUid) throws IOException, ApiServiceException { + return invokeGet("users/signup.json", + "email", email, + "firstname", firstName, + "lastname", lastName, + "password", password, + "fbuid", fbUid); + } + // --- tasks /** diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevRestClient.java b/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevRestClient.java index 3c59ae65b..a8e4d589e 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevRestClient.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/api/ProducteevRestClient.java @@ -87,7 +87,7 @@ public class ProducteevRestClient implements RestClient { } int statusCode = response.getStatusLine().getStatusCode(); - if(statusCode != HTTP_OK) { + if(statusCode != HTTP_OK || (body != null && body.startsWith("{\"error\":"))) { //$NON-NLS-1$ ApiServiceException error; try { JSONObject errorObject = new JSONObject(body).getJSONObject("error"); //$NON-NLS-1$ diff --git a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java index 4489d636b..7b909229a 100644 --- a/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java +++ b/astrid/plugin-src/com/todoroo/astrid/producteev/sync/ProducteevSyncProvider.java @@ -145,10 +145,7 @@ public class ProducteevSyncProvider extends SyncProvider