Skip to content

Commit

Permalink
Issue photo#459 (Card 7) (Multi-site login)
Browse files Browse the repository at this point in the history
app:
- AccountLogin.LogInFragment, AccountSignup.NewUserFragment: now it
implements LoginActionHandler
- AccountLogin.LogInFragment, AccountSignup.NewUserFragment: added
static fields sCurrentInstance and sCurrentInstanceAccessor
- AccountLogin.LogInFragment, AccountSignup.NewUserFragment: added new
fields mDelayedLoginProcess, mLastResponse
- AccountLogin.LogInFragment, AccountSignup.NewUserFragment: added
implementation of onCreate and onDestroy to init sCurrentInstance static
field
- AccountLogin.LogInFragment, AccountSignup.NewUserFragment,
GoogleLoginFragment: added new methods processLoginCredentials,
processLoginResponse, onViewCreated
- AccountLogin.LogInFragment.LogInUserTask,
AccountSignup.NewUserFragment.NewUserTask,
GoogleLoginFragment.LogInUserTask: reworked
onSuccessPostExecuteAdditional method to call processLoginResonse or
schedule delayed login processing depend on fragment-activity attached
state
- GoogleLoginFragment: renamed currentInstance field to
sCurrentInstance, requestCode to mRequestCode, delayedLoggedIn to
mDelayedLoginProcessing
- GoogleLoginFragment: added new static field sCurrentInstanceAccessor
- GoogleLoginFragment: added new field mLastResponse
- GoogleLoginFragment: removed method onLoggedIn and reworked
onViewCreated to call processLoginResonse
- SetupActivity: replaced getActivity with getSupportActivity call for
OAuthUtils.askOAuth call
- Credentials: added
- AccountTroveboxResponse: removed different credentials field and added
mCredentials field
- AccountTroveboxResponse: reworked constructor to support array of
credentials in the json result
- OAuthUtils: replaced Activity reference in import with HE one
- LoginUtils: added new methods: onLoggedIn,
processSuccessfulLoginResult, performLogin
- LoginUtils.SelectAccountSelectedActionHandler: added
- LoginUtils.LoginActionHandler: added
- LoginUtils.SelectAccountDialogFragment: added
- res/values/strings.xml, res/values-ru/strings.xml: added
select_trovebox_account constant

test:
- CredentialsTest: added
- AccountTroveboxApiTest: modified to use credentials reference from the
modified AccountTroveboxResponse
- AccountTroveboxResponseTest: added
- res/raw/json_credentials.txt: added
- res/raw/json_login_multiple.txt: added
- res/raw/json_login_simple.txt: added
  • Loading branch information
httpdispatch committed Oct 2, 2013
1 parent 390a434 commit 11447ec
Show file tree
Hide file tree
Showing 16 changed files with 680 additions and 167 deletions.
1 change: 1 addition & 0 deletions app/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
<string name="password_recovery_instructions">Введите email чтобы восстановить пароль</string>
<string name="instant_sign_in_instructions"><![CDATA[<b>Зарегистрируйтесь</b> или <b>войдите</b> в одно касание]]></string>
<string name="select_google_account">Пожалуйста выберите аккаунт google для входа</string>
<string name="select_trovebox_account">Пожалуйста выберите аккаунт trovebox для входа</string>
<!-- Hints -->
<string name="hint_email">email</string>
<string name="hint_password">пароль</string>
Expand Down
1 change: 1 addition & 0 deletions app/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
<string name="password_recovery_instructions">Enter your email to recover your password</string>
<string name="instant_sign_in_instructions"><![CDATA[<b>Sign up</b> or <b>sign in</b> with a single tap]]></string>
<string name="select_google_account">Please select google account to login with</string>
<string name="select_trovebox_account">Please select trovebox account to login with</string>
<!-- Hints -->
<string name="hint_email">email</string>
<string name="hint_password">password</string>
Expand Down
76 changes: 67 additions & 9 deletions app/src/com/trovebox/android/app/AccountLogin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

package com.trovebox.android.app;

import java.lang.ref.WeakReference;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.widget.TextView;

Expand All @@ -20,6 +22,8 @@
import com.trovebox.android.app.util.CommonUtils;
import com.trovebox.android.app.util.GuiUtils;
import com.trovebox.android.app.util.LoginUtils;
import com.trovebox.android.app.util.LoginUtils.LoginActionHandler;
import com.trovebox.android.app.util.ObjectAccessor;
import com.trovebox.android.app.util.TrackerUtils;

public class AccountLogin extends CommonActivity {
Expand Down Expand Up @@ -122,9 +126,44 @@ public RecoverPasswordFragment getRecoverPasswordFragment() {
* The log in fragment with the retained instance across configuration
* change
*/
public static class LogInFragment extends CommonRetainedFragmentWithTaskAndProgress {
public static class LogInFragment extends CommonRetainedFragmentWithTaskAndProgress implements
LoginActionHandler {
private static final String TAG = LogInFragment.class.getSimpleName();

static WeakReference<LogInFragment> sCurrentInstance;
static ObjectAccessor<LogInFragment> sCurrentInstanceAccessor = new ObjectAccessor<LogInFragment>() {
private static final long serialVersionUID = 1L;

@Override
public LogInFragment run() {
return sCurrentInstance == null ? null : sCurrentInstance.get();
}
};

boolean mDelayedLoginProcessing = false;
AccountTroveboxResponse mLastResponse;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sCurrentInstance = new WeakReference<LogInFragment>(this);
}

@Override
public void onDestroy() {
if (sCurrentInstance != null) {
if (sCurrentInstance.get() == LogInFragment.this
|| sCurrentInstance.get() == null) {
CommonUtils.debug(TAG, "Nullify current instance");
sCurrentInstance = null;
} else {
CommonUtils.debug(TAG,
"Skipped nullify of current instance, such as it is not the same");
}
}
super.onDestroy();
}

@Override
public String getLoadingMessage() {
return CommonUtils.getStringResource(R.string.logging_in_message);
Expand All @@ -134,6 +173,26 @@ public void doLogin(String user, String pwd) {
startRetainedTask(new LogInUserTask(new Credentials(user, pwd)));
}

@Override
public void processLoginCredentials(com.trovebox.android.app.model.Credentials credentials) {
Activity activity = getSupportActivity();
credentials.saveCredentials(activity);
LoginUtils.onLoggedIn(activity, true);
}

void processLoginResonse(Activity activity) {
LoginUtils.processSuccessfulLoginResult(mLastResponse, sCurrentInstanceAccessor,
activity);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (mDelayedLoginProcessing) {
mDelayedLoginProcessing = false;
processLoginResonse(getSupportActivity());
}
}
class LogInUserTask extends RetainedTask {
private Credentials credentials;
AccountTroveboxResponse result;
Expand Down Expand Up @@ -161,15 +220,14 @@ protected Boolean doInBackground(Void... params) {
protected void onSuccessPostExecuteAdditional() {
try
{
mLastResponse = result;
Activity activity = getSupportActivity();
// save credentials.
result.saveCredentials(activity);

// start new activity
startActivity(new Intent(activity,
MainActivity.class));
LoginUtils.sendLoggedInBroadcast(activity);
activity.finish();
if (activity != null) {
processLoginResonse(activity);
} else {
TrackerUtils.trackErrorEvent("activity_null", TAG);
mDelayedLoginProcessing = true;
}
} catch (Exception e)
{
GuiUtils.error(TAG, e);
Expand Down
76 changes: 66 additions & 10 deletions app/src/com/trovebox/android/app/AccountSignup.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

package com.trovebox.android.app;

import java.lang.ref.WeakReference;

import org.holoeverywhere.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
Expand All @@ -17,6 +18,8 @@
import com.trovebox.android.app.util.CommonUtils;
import com.trovebox.android.app.util.GuiUtils;
import com.trovebox.android.app.util.LoginUtils;
import com.trovebox.android.app.util.LoginUtils.LoginActionHandler;
import com.trovebox.android.app.util.ObjectAccessor;
import com.trovebox.android.app.util.TrackerUtils;

/**
Expand Down Expand Up @@ -83,9 +86,43 @@ NewUserFragment getNewUserFragment() {
* The create new user fragment with the retained instance across
* configuration change
*/
public static class NewUserFragment extends CommonRetainedFragmentWithTaskAndProgress {
public static class NewUserFragment extends CommonRetainedFragmentWithTaskAndProgress implements
LoginActionHandler {
private static final String TAG = NewUserFragment.class.getSimpleName();

static WeakReference<NewUserFragment> sCurrentInstance;
static ObjectAccessor<NewUserFragment> sCurrentInstanceAccessor = new ObjectAccessor<NewUserFragment>() {
private static final long serialVersionUID = 1L;

@Override
public NewUserFragment run() {
return sCurrentInstance == null ? null : sCurrentInstance.get();
}
};

boolean mDelayedLoginProcessing = false;
AccountTroveboxResponse mLastResponse;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sCurrentInstance = new WeakReference<NewUserFragment>(this);
}

@Override
public void onDestroy() {
if (sCurrentInstance != null) {
if (sCurrentInstance.get() == NewUserFragment.this || sCurrentInstance.get() == null) {
CommonUtils.debug(TAG, "Nullify current instance");
sCurrentInstance = null;
} else {
CommonUtils.debug(TAG,
"Skipped nullify of current instance, such as it is not the same");
}
}
super.onDestroy();
}

@Override
public String getLoadingMessage() {
return CommonUtils.getStringResource(R.string.signup_message);
Expand All @@ -95,6 +132,26 @@ public void createNewUser(String username, String email, String password) {
startRetainedTask(new NewUserTask(username, email, password));
}

@Override
public void processLoginCredentials(com.trovebox.android.app.model.Credentials credentials) {
Activity activity = getSupportActivity();
credentials.saveCredentials(activity);
LoginUtils.onLoggedIn(activity, true);
}

void processLoginResonse(Activity activity) {
LoginUtils.processSuccessfulLoginResult(mLastResponse, sCurrentInstanceAccessor,
activity);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (mDelayedLoginProcessing) {
mDelayedLoginProcessing = false;
processLoginResonse(getSupportActivity());
}
}
class NewUserTask extends RetainedTask {
String username, password, email;
AccountTroveboxResponse result;
Expand Down Expand Up @@ -124,15 +181,14 @@ protected Boolean doInBackground(Void... params) {
protected void onSuccessPostExecuteAdditional() {
try
{
mLastResponse = result;
Activity activity = getSupportActivity();
// save credentials.
result.saveCredentials(activity);

// start new activity
startActivity(new Intent(activity,
MainActivity.class));
LoginUtils.sendLoggedInBroadcast(activity);
activity.finish();
if (activity != null) {
processLoginResonse(activity);
} else {
TrackerUtils.trackErrorEvent("activity_null", TAG);
mDelayedLoginProcessing = true;
}
} catch (Exception e)
{
GuiUtils.error(TAG, e);
Expand Down
Loading

0 comments on commit 11447ec

Please sign in to comment.