Skip to content

Commit

Permalink
Merge pull request #7 from sbxcloud/annotationlib
Browse files Browse the repository at this point in the history
add getCurrentUser
  • Loading branch information
lgguzman authored Mar 21, 2017
2 parents 4b5d3e0 + 4d5393e commit 829dc09
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {

minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionCode 2
versionName "2.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public void setHttpLog(boolean httpLog) {
HttpLog = httpLog;
}


public Context getContext() {
return context;
}

/**
* initialize the data for comunicate on sbxcloud.com
* @param domain the id of the domain on sbxcloud.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

import com.sbxcloud.android.sbxcloudsdk.auth.SbxAuth;
import com.sbxcloud.android.sbxcloudsdk.auth.user.SbxAuthToken;
import com.sbxcloud.android.sbxcloudsdk.auth.user.SbxEmailField;
import com.sbxcloud.android.sbxcloudsdk.auth.user.SbxNameField;
import com.sbxcloud.android.sbxcloudsdk.auth.user.SbxUsernameField;
import com.sbxcloud.android.sbxcloudsdk.exception.SbxAuthException;
import com.sbxcloud.android.sbxcloudsdk.exception.SbxConfigException;
import com.sbxcloud.android.sbxcloudsdk.net.ApiManager;
import com.sbxcloud.android.sbxcloudsdk.net.callback.SbxSimpleResponse;
import com.sbxcloud.android.sbxcloudsdk.query.annotation.SbxKey;
import com.sbxcloud.android.sbxcloudsdk.util.SbxUrlComposer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
Expand All @@ -34,6 +38,8 @@ public class SbxUser {
private int id;
private String homeFolder;
private JSONArray memberOf;
private static final String FILE_NAME=SbxUser.class.getName();
private static final String FILE_NAME_USER=FILE_NAME+"_User";

public void signUpInBackground(final SbxSimpleResponse simpleResponse) throws Exception{
SbxUrlComposer sbxUrlComposer= SbxAuth.getDefaultSbxAuth().getUrlSigIn(this);
Expand All @@ -51,7 +57,6 @@ public void onResponse(Call call, Response response) throws IOException {
JSONObject jsonObject = new JSONObject(response.body().string());
if(jsonObject.getBoolean("success")) {
updateUser(jsonObject);
sbxUser=SbxUser.this;
simpleResponse.onSuccess(SbxUser.this);
}else{
simpleResponse.onError(new Exception(jsonObject.getString("error")));
Expand All @@ -78,7 +83,6 @@ public void onResponse(Call call, Response response) throws IOException {
JSONObject jsonObject = new JSONObject(response.body().string());
if(jsonObject.getBoolean("success")) {
updateUser(jsonObject);
sbxUser=SbxUser.this;
SbxAuth.getDefaultSbxAuth().refreshToken(sbxUser);
simpleResponse.onSuccess(SbxUser.this);
}else{
Expand All @@ -94,16 +98,23 @@ public void onResponse(Call call, Response response) throws IOException {
public void logOut() throws Exception{
sbxUser=null;
SbxAuth.getDefaultSbxAuth().resetToken();
SharedPreferences sharedPreferences = SbxAuth.getDefaultSbxAuth().getContext().getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
sharedPreferences.edit().putString(FILE_NAME_USER, "").apply();
}

private void updateUser(JSONObject jsonObject) throws Exception{
public void updateUser(JSONObject jsonObject) throws Exception{
SharedPreferences sharedPreferences = SbxAuth.getDefaultSbxAuth().getContext().getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
sharedPreferences.edit().putString(FILE_NAME_USER, jsonObject.toString()).apply();
String token=jsonObject.getString("token");
JSONObject userJson=jsonObject.getJSONObject("user");
String email=userJson.getString("email");
String name=userJson.getString("name");
String login=userJson.getString("login");
Class<?> myClass =SbxUser.this.getClass();
final Field[] variables = myClass.getDeclaredFields();
for (final Field variable : variables) {

final Annotation annotation = variable.getAnnotation(SbxAuthToken.class);
Annotation annotation = variable.getAnnotation(SbxAuthToken.class);

if (annotation != null && annotation instanceof SbxAuthToken) {
try {
Expand All @@ -116,15 +127,67 @@ private void updateUser(JSONObject jsonObject) throws Exception{
throw new SbxAuthException(e);
}
}

annotation = variable.getAnnotation(SbxEmailField.class);

if (annotation != null && annotation instanceof SbxEmailField) {
try {
boolean isAccessible=variable.isAccessible();
variable.setAccessible(true);
variable.set(SbxUser.this,email);
variable.setAccessible(isAccessible);
break;
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SbxAuthException(e);
}
}

annotation = variable.getAnnotation(SbxUsernameField.class);

if (annotation != null && annotation instanceof SbxUsernameField) {
try {
boolean isAccessible=variable.isAccessible();
variable.setAccessible(true);
variable.set(SbxUser.this,login);
variable.setAccessible(isAccessible);
break;
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SbxAuthException(e);
}
}

annotation = variable.getAnnotation(SbxNameField.class);

if (annotation != null && annotation instanceof SbxNameField) {
try {
boolean isAccessible=variable.isAccessible();
variable.setAccessible(true);
variable.set(SbxUser.this,name);
variable.setAccessible(isAccessible);
break;
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SbxAuthException(e);
}
}
}

this.id=userJson.getInt("id");
this.memberOf=userJson.getJSONArray("member_of");
this.homeFolder=userJson.getString("home_folder_key");
sbxUser=SbxUser.this;
}

public static SbxUser getCurrentSbxUser()throws SbxAuthException {
if(sbxUser==null)
throw new SbxAuthException("User not login");
public static SbxUser getCurrentSbxUser()throws Exception {
if(sbxUser==null) {
SharedPreferences sharedPreferences = SbxAuth.getDefaultSbxAuth().getContext().getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
String s=sharedPreferences.getString(FILE_NAME_USER,"");
if(s.equals("")) {
return null;
}
sbxUser= new SbxUser();
sbxUser.updateUser(new JSONObject(s));
}

return sbxUser;
}

Expand Down

0 comments on commit 829dc09

Please sign in to comment.