Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
@ctumwebaze Updated error messages for #30
Browse files Browse the repository at this point in the history
  • Loading branch information
rdsubhas authored and snap-ci committed Jul 2, 2014
1 parent f4ffb23 commit 2729178
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 254 deletions.
2 changes: 1 addition & 1 deletion RapidFTR-Android/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
resourceId="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
unless="sdk.dir"
/>

Expand Down
2 changes: 1 addition & 1 deletion RapidFTR-Android/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
Expand Down
14 changes: 10 additions & 4 deletions RapidFTR-Android/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

<!-- Login -->
<string name="log_in">Log In</string>
<string name="login_progress">Logging in</string>
<string name="login_online_progress">Trying to login online</string>
<string name="login_online_success">Online login successful</string>
<string name="login_online_failed">Online login failed, will try to login offline</string>
<string name="login_online_no_connectivity">No connectivity, will try to login offline</string>

<string name="login_migrate_progress">You are now a verified user, migrating your data</string>
<string name="login_migrate_failed">Failed to migrate data, logging in offline, please sync data</string>
<string name="login_save_failed">Failed to save your details, please contact the system administrator</string>
<string name="login_save_failed">Failed to complete the login, please contact your system administrator</string>

<string name="login_form_progress">Loading latest form sections</string>
<string name="login_form_failed">Failed to load latest form sections, you can still keep using older forms</string>
<string name="login_online_success">Online login successful</string>

<string name="login_offline_progress">Trying to login offline</string>
<string name="login_offline_success">Offline login successful</string>
<string name="login_invalid">Login failed, Incorrect username or password</string>
<string name="login_offline_no_user">Login failed, first time users please login online to enable your account for offline mode</string>
<string name="login_offline_failed">Login failed, Incorrect username or password</string>

<!-- Others, need to organize/group as we go -->
<string name="log_out">Log Out</string>
Expand Down
132 changes: 82 additions & 50 deletions RapidFTR-Android/src/main/java/com/rapidftr/bean/LoginTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.google.common.base.Throwables;
import com.google.common.io.CharStreams;
import com.rapidftr.RapidFtrApplication;
import com.rapidftr.model.User;
import com.rapidftr.service.FormService;
import com.rapidftr.service.LoginService;
import com.rapidftr.task.MigrateUnverifiedDataToVerified;
import com.rapidftr.utils.http.FluentResponse;
import lombok.Getter;
import lombok.NonNull;
import org.androidannotations.annotations.*;
import org.json.JSONObject;

Expand All @@ -22,6 +25,19 @@
@EBean
public class LoginTask {

protected static class LoginException extends RuntimeException {
@Getter protected Integer messageId = null;

public LoginException(String message, Throwable cause) {
super(message, cause);
}

public LoginException(int messageId, Throwable cause) {
super(cause);
this.messageId = messageId;
}
}

@Bean
protected ConnectivityBean connectivityBean;

Expand All @@ -33,97 +49,110 @@ public class LoginTask {

protected ProgressDialog progressDialog;

public User login(String userName, String password, String url) {
public void login(String userName, String password, String url) {
createProgressDialog();
User onlineUser = loadOnline(userName, password, url);
User offlineUser = loadOffline(userName, password);
User finalUser = finalizeLogin(onlineUser, offlineUser);
if (!loginOnline(userName, password, url)) {
loginOffline(userName, password);
}
dismissProgressDialog();
return finalUser;
}

protected boolean loginOnline(String userName, String password, String url) {
try {
notifyProgress(login_online_progress);
User user = loadOnline(userName, password, url);
migrateIfVerified(user);
cacheForOffline(user);
loadFormSections();
notifyToast(login_online_success);
return true;
} catch (LoginException e) {
Log.e(APP_IDENTIFIER, "Failed to login online", e);
notifyToast(e);
return false;
}
}

protected boolean loginOffline(String userName, String password) {
try {
notifyProgress(login_offline_progress);
User user = loadOffline(userName, password);
cacheForOffline(user);
notifyToast(login_offline_success);
return true;
} catch (LoginException e) {
Log.e(APP_IDENTIFIER, "Failed to login offline", e);
notifyToast(e);
return false;
}
}

protected User loadOnline(String userName, String password, String url) {
if (!connectivityBean.isOnline()) {
notifyProgress(login_online_failed);
return null;
throw new LoginException(login_online_no_connectivity, null);
}

try {
FluentResponse response = new LoginService().login(application, userName, password, url);
String responseAsString = CharStreams.toString(new InputStreamReader(response.getEntity().getContent()));

if (!response.isSuccess()) {
notifyProgress(login_online_failed);
notifyToast(responseAsString);
return null;
throw new LoginException(responseAsString, null);
}

return new User(userName, password, true, url).read(responseAsString);
} catch (Exception e) {
Log.d(APP_IDENTIFIER, "Online login failed", e);
notifyToast(login_online_failed);
return null;
Throwables.propagateIfInstanceOf(e, LoginException.class);
throw new LoginException(login_online_failed, null);
}
}

protected User loadOffline(String userName, String password) {
User user = new User(userName, password);
if (!user.exists()) {
throw new LoginException(login_offline_no_user, null);
}

try {
return new User(userName, password).load();
} catch (Exception e) {
return null;
return user.load();
} catch(Exception e) {
throw new LoginException(login_offline_failed, e);
}
}

protected User finalizeLogin(User onlineUser, User offlineUser) {
boolean isMigrated = migrateIfVerified(onlineUser, offlineUser);
boolean isOnlineLogin = onlineUser != null && isMigrated;
User finalUser = isOnlineLogin ? onlineUser : offlineUser;
boolean isLoggedIn = cacheForOffline(finalUser);

if (isOnlineLogin)
loadFormSections();

notifyToast(isLoggedIn ? (isOnlineLogin ? login_online_success : login_offline_success) : login_invalid);
return isLoggedIn ? finalUser : null;
}
protected void migrateIfVerified(@NonNull User onlineUser) {
User offlineUser = null;
try {
offlineUser = loadOffline(onlineUser.getUserName(), onlineUser.getPassword());
} catch (LoginException e) {
return;
}

protected boolean migrateIfVerified(User onlineUser, User offlineUser) {
if (onlineUser != null && offlineUser != null && onlineUser.isVerified() && !offlineUser.isVerified()) {
if (offlineUser != null && onlineUser.isVerified() && !offlineUser.isVerified()) {
try {
notifyProgress(login_migrate_progress);
new MigrateUnverifiedDataToVerified(new JSONObject(onlineUser.asJSON()), offlineUser).execute();
} catch (Exception e) {
Log.e(APP_IDENTIFIER, "Migrate failed", e);
notifyToast(login_migrate_failed);
return false;
throw new LoginException(login_migrate_failed, null);
}
}

return true;
}

protected boolean cacheForOffline(User user) {
if (user == null) return false;
protected void cacheForOffline(@NonNull User user) {
try {
user.save();
application.setCurrentUser(user);
return true;
} catch (Exception e) {
Log.e(APP_IDENTIFIER, "Failed to save user details", e);
notifyToast(login_save_failed);
return false;
throw new LoginException(login_save_failed, e);
}
}

protected boolean loadFormSections() {
protected void loadFormSections() {
try {
notifyProgress(login_form_progress);
new FormService(application).getPublishedFormSections();
return true;
} catch (Exception e) {
Log.e(APP_IDENTIFIER, "Failed to get form sections", e);
notifyToast(login_form_failed);
return false;
throw new LoginException(login_form_failed, e);
}
}

Expand All @@ -132,7 +161,6 @@ protected void createProgressDialog() {
progressDialog = new ProgressDialog(activity);
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.setMessage(application.getString(login_progress));
progressDialog.show();
}

Expand All @@ -147,12 +175,16 @@ public void notifyProgress(Integer message) {
}

@UiThread
public void notifyToast(Integer resId) {
Toast.makeText(application, resId, Toast.LENGTH_SHORT).show();
public void notifyToast(LoginException e) {
if (e.getMessageId() != null) {
notifyToast(e.getMessageId());
} else {
Toast.makeText(application, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}

@UiThread
public void notifyToast(String message) {
public void notifyToast(int message) {
Toast.makeText(application, message, Toast.LENGTH_SHORT).show();
}

Expand Down
Loading

0 comments on commit 2729178

Please sign in to comment.