working on serial/login/pictogram activity structure

parent 643a50d8
......@@ -36,13 +36,12 @@ import com.yottacode.pictogram.tools.PCBcontext;
/**
* A login screen that offers login via email/password.
*/
public class SerialActivity extends Activity implements iRestapiListener {
public class SerialActivity extends Activity {
// UI references.
private EditText mSerialViewMail;
private EditText mSerialViewPass;
Context context;
private static final String TAG_USER = "user";
private static final String TAG_ISSTUDENT = "isStudent";
......@@ -61,44 +60,26 @@ public class SerialActivity extends Activity implements iRestapiListener {
// String constant for logs
private final String LOG_TAG = this.getClass().getSimpleName(); // Or .getCanonicalName()
private String username = null;
private String password = null;
//A ProgressDialog object
private ProgressDialog progressDialog;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_serial);
Intent intent=getIntent();
String activityName=intent.getStringExtra("activity_name");
mSerialViewMail = (EditText) findViewById(R.id.serialmail);
mSerialViewPass = (EditText) findViewById(R.id.serialpass);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
username = settings.getString("username", "");
if (activityName.equals("MainActivity")){
password = settings.getString("password", "");
} else{
password = "";
}
private Intent manageSupervisorLogin(String username, String password, boolean online) {
// Escribo el último valor indicado de username
mSerialViewMail.setText(username);
final Intent loginActivity = new Intent(SerialActivity.this, LoginActivity.class);
if (online) {
final String operation = "sup/login";
Hashtable<String, String> postDataParams = new Hashtable<String, String>();
context = this;
postDataParams.put("email", username);
postDataParams.put("password", password);
if (!username.equals("") && !password.equals("")){ // Tengo usuario, sigo con este usuario
if (RestapiWrapper.ping(context.getResources().getString(R.string.server),"server/ping", new iRestapiListener() {
PCBcontext.getRestapiWrapper().ask(operation, postDataParams, "post", new iRestapiListener() {
@Override
public void preExecute() {
progressDialog = ProgressDialog.show(SerialActivity.this, SerialActivity.this.getBaseContext().getString(R.string.userLoadingMsg),
SerialActivity.this.getBaseContext().getString(R.string.userLoadingTxt), false, false);
}
@Override
......@@ -108,33 +89,69 @@ public class SerialActivity extends Activity implements iRestapiListener {
@Override
public void result(JSONObject result) {
if (SerialActivity.this.checkLogin(result)) {
String jsonToken;
JSONObject user;
try {
jsonToken = result.getString(TAG_TOKEN);
user = result.getJSONObject(TAG_USER);
} catch (JSONException e) {
e.printStackTrace();
jsonToken = null;
user = null;
Log.e(LOG_TAG, e.getMessage());
}
Log.d(LOG_TAG, "Es un SUPERVISOR. Sigo...");
Log.d(LOG_TAG, "jsonToken válido:" + jsonToken);
try {
Integer su_id = user.getInt(TAG_ID);
int su_id_int = su_id.intValue();
progressDialog.dismiss();
loginActivity.putExtra("offline", false);
loginActivity.putExtra("sup_id", su_id_int);
loginActivity.putExtra("token", jsonToken);
} catch (JSONException e) {
SerialActivity.this.error(e);
}
}
}
@Override
public void error(Exception e) {
SerialActivity.this.error(e);
}
})) {
// Tengo conexión a internet. Modo ONLINE
RestapiWrapper wrapper = PCBcontext.getRestapiWrapper();
if (username.contains("@")){
// Es un supervisor
String operation = "sup/login";
});
} else{
loginActivity.putExtra("username", username);
loginActivity.putExtra("password", password);
}
return loginActivity;
}
private Intent manageStudentLogin(final String username, final String password, boolean online)
{
final Intent pictogramActivity = new Intent(SerialActivity.this, PictogramActivity.class);
if (online) {
Log.d(LOG_TAG, "Entro con usuario y password");
String operation = "stu/login";
Hashtable<String, String> postDataParams = new Hashtable<String, String>();
postDataParams.put("email", username);
postDataParams.put("username", username);
postDataParams.put("password", password);
/*
postDataParams.put("idFirmware", _ANDROIDID);
postDataParams.put("desc", _MODEL);
postDataParams.put("serial", serial);
*/
wrapper.ask(operation, postDataParams, "post", new iRestapiListener() {
PCBcontext.getRestapiWrapper().ask(operation, postDataParams, "post", new iRestapiListener() {
@Override
public void preExecute() {
progressDialog = ProgressDialog.show(SerialActivity.this, SerialActivity.this.getBaseContext().getString(R.string.userLoadingMsg),
SerialActivity.this.getBaseContext().getString(R.string.userLoadingTxt), false, false);
}
@Override
......@@ -144,26 +161,27 @@ public class SerialActivity extends Activity implements iRestapiListener {
@Override
public void result(JSONObject result) {
Log.d(LOG_TAG, "Es un SUPERVISOR. Sigo...");
Log.d(LOG_TAG, "jsonToken válido:" + jsonToken);
Integer su_id = user.getInt(TAG_ID);
int su_id_int = su_id.intValue();
//close the progress dialog
if (!username.contains("@") && progressDialog!=null){
progressDialog.dismiss();
}
try {
pictogramActivity.putExtra("token", result.getString(TAG_TOKEN));
JSONObject user = result.getJSONObject(TAG_USER);
// Es un estudiante
st_id = user.getInt(TAG_ID);
int st_id_int = st_id.intValue();
String st_name = user.getString(TAG_NAME);
String st_surname = user.getString(TAG_SURNAME);
String st_pic = user.getString(TAG_PIC);
String st_gender = user.getString(TAG_GENDER);
String st_lang = user.getString(TAG_LANG);
String st_attributes = user.getString(TAG_ATTRIBUTES);
// Llamo a LoginActivity
Intent loginActivity = new Intent(SerialActivity.this, LoginActivity.class);
loginActivity.putExtra("offline",false);
loginActivity.putExtra("token", jsonToken);
loginActivity.putExtra("sup_id", su_id_int);
startActivity(loginActivity);
User student = new User(st_id_int, username,password, st_name, st_surname, st_pic, st_gender, st_lang, st_attributes,
User.NO_SUPERVISOR, "", "", "", "", "", "M", "es-es", "");
} catch (JSONException e) {
SerialActivity.this.error(e);
}
}
@Override
......@@ -171,52 +189,19 @@ public class SerialActivity extends Activity implements iRestapiListener {
SerialActivity.this.error(e);
}
});
} else{
// Es un estudiante
Log.d(LOG_TAG, "Entro con usuario y password");
String operation = "stu/login";
Hashtable<String, String> postDataParams = new Hashtable<String, String>();
postDataParams.put("username", username);
postDataParams.put("password", password);
/*
postDataParams.put("idFirmware", _ANDROIDID);
postDataParams.put("desc", _MODEL);
postDataParams.put("serial", serial);
*/
wrapper.ask(operation, postDataParams, "post", (iRestapiListener) context);
}
} else{
// NO Tengo conexión a internet. Modo OFFLINE
if (username.contains("@")) {
// Es un supervisor
// Llamo a LoginActivity y le paso username y password
Intent loginActivity = new Intent(this, LoginActivity.class);
loginActivity.putExtra("offline",true);
loginActivity.putExtra("username",username);
loginActivity.putExtra("password",password);
startActivity(loginActivity);
} else {
// Es un estudiante
else {
try {
Vector<User> localUsers = PCBcontext.getDevice().findUser(username, password);
// set_user, con token nulo y sin listener
PCBcontext.set_user(localUsers.elementAt(0), null, null);
// Llamo a PictogramActivity
/*
if (progressDialog!=null)
progressDialog.dismiss();
*/
Intent pictogramActivity = new Intent(SerialActivity.this, PictogramActivity.class);
startActivity(pictogramActivity);
} catch (JSONException e) {
e.printStackTrace();
} catch (LoginException e) {
AlertDialog.Builder builder = new AlertDialog.Builder(SerialActivity.this);
if (e.knonwn_student()){
// El password no es correcto
builder.setMessage("La contraseña indicada no es correcta. Inténtelo de nuevo.")
builder.setMessage(getString(R.string.passErrorMsg))
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
......@@ -224,7 +209,7 @@ public class SerialActivity extends Activity implements iRestapiListener {
});
} else {
// El usuario no existe
builder.setMessage("El usuario indicado no es correcto. Inténtelo de nuevo.")
builder.setMessage(getString(R.string.userErrorMsg))
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
......@@ -234,120 +219,59 @@ public class SerialActivity extends Activity implements iRestapiListener {
e.printStackTrace();
}
}
return pictogramActivity;
}
} else{ // No tengo aún usuario. Espero a que lo indique
Button mEntrarButton = (Button) findViewById(R.id.entrar_button);
mEntrarButton.setOnClickListener(new OnClickListener() {
protected void login(String username, String password) {
boolean online=RestapiWrapper.ping(this.getResources().getString(R.string.server),"server/ping", new iRestapiListener() {
@Override
public void onClick(View view) {
String username = mSerialViewMail.getText().toString();
String password = mSerialViewPass.getText().toString();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
public void preExecute() {
// DE PRUEBA
// Alumno
username = "faf0001";
password = "faf0001";
// Supervisor
//username = "dofer@ujaen.es";
//password = "dofer";
if (RestapiWrapper.ping(context.getResources().getString(R.string.server),"server/ping",null)) {
// Tengo conexión a internet. Modo ONLINE
RestapiWrapper wrapper = PCBcontext.getRestapiWrapper();
if (username.contains("@")){
// Es un supervisor
String operation = "sup/login";
Hashtable<String, String> postDataParams = new Hashtable<String, String>();
}
postDataParams.put("email", username);
postDataParams.put("password", password);
wrapper.ask(operation, postDataParams, "post", (iRestapiListener) context);
} else{
// Es un estudiante
String operation = "stu/login";
Hashtable<String, String> postDataParams = new Hashtable<String, String>();
@Override
public void result(JSONArray result) {
postDataParams.put("username", username);
postDataParams.put("password", password);
wrapper.ask(operation, postDataParams, "post", (iRestapiListener) context);
}
} else{
// NO Tengo conexión a internet. Modo OFFLINE
if (username.contains("@")) {
// Es un supervisor
// Llamo a LoginActivity y le paso username y password
Intent loginActivity = new Intent(SerialActivity.this, LoginActivity.class);
loginActivity.putExtra("offline",true);
loginActivity.putExtra("username",username);
loginActivity.putExtra("password",password);
startActivity(loginActivity);
} else {
// Es un estudiante
try {
Vector<User> localUsers = PCBcontext.getDevice().findUser(username, password);
// set_user, con token nulo y sin listener
PCBcontext.set_user(localUsers.elementAt(0), null, null);
// Llamo a PictogramActivity
Intent pictogramActivity = new Intent(SerialActivity.this, PictogramActivity.class);
startActivity(pictogramActivity);
} catch (JSONException e) {
e.printStackTrace();
} catch (LoginException e) {
AlertDialog.Builder builder = new AlertDialog.Builder(SerialActivity.this);
if (e.knonwn_student()){
// El password no es correcto
builder.setMessage("La contraseña indicada no es correcta. Inténtelo de nuevo.")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
} else {
// El usuario no existe
builder.setMessage("El usuario indicado no es correcto. Inténtelo de nuevo.")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
}
AlertDialog alert = builder.create();
alert.show();
e.printStackTrace();
}
}
@Override
public void result(JSONObject result) {
}
@Override
public void error(Exception e) {
SerialActivity.this.error(e);
}
});
Intent nextActivity=username.contains("@")
? manageSupervisorLogin(username,password,online)
: manageStudentLogin(username,password,online);
nextActivity.putExtra("offline",!online);
startActivity(nextActivity);
}
}
public boolean checkLogin(JSONObject result) {
// Si llego aquí es porque hay un único valor, no un array
try {
Log.d(LOG_TAG, "JSON en result:"+result.toString());
if (result.toString().contains("error")){
if (progressDialog!=null) progressDialog.dismiss();
Log.d(LOG_TAG, "JSON en result:" + result.toString());
if (result.toString().contains("error")) {
if (progressDialog != null) progressDialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(SerialActivity.this);
if (result.toString().contains("Invalid password")){
builder.setMessage("La contraseña indicada no es correcta. Inténtelo de nuevo.")
if (result.toString().contains("Invalid password")) {
builder.setMessage(this.getString(R.string.passErrorMsg))
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
} else if (result.toString().contains("User not found")){
builder.setMessage("El usuario indicado no es correcto. Inténtelo de nuevo.")
} else if (result.toString().contains("User not found")) {
builder.setMessage(this.getString(R.string.userErrorMsg))
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
......@@ -357,83 +281,12 @@ public class SerialActivity extends Activity implements iRestapiListener {
AlertDialog alert = builder.create();
alert.show();
} else{ // No hay error. Sigo
// Saco el token y los demás valores
Vector<User> users = new Vector<User>(1);
String jsonToken = result.getString(TAG_TOKEN);
JSONObject user = result.getJSONObject(TAG_USER);
try{
Boolean is_student = user.has(TAG_ISSTUDENT);
if (is_student) {
// Es un estudiante
st_id = user.getInt(TAG_ID);
int st_id_int = st_id.intValue();
String st_name = user.getString(TAG_NAME);
String st_surname = user.getString(TAG_SURNAME);
String st_pic = user.getString(TAG_PIC);
String st_gender = user.getString(TAG_GENDER);
String st_lang = user.getString(TAG_LANG);
String st_attributes = user.getString(TAG_ATTRIBUTES);
//users=PCBcontext.getDevice().getUsers();
//if (users.contains(user)) users.remove(user);
User student = new User(st_id_int, "nickname_stu","pwd_stu", st_name, st_surname, st_pic, st_gender, st_lang, st_attributes,
-1, username, password, "", "", "", "M", "es-es", "");
users.add(student);
Log.d(LOG_TAG, "jsonToken válido:" + jsonToken);
//User student = PCBcontext.getDevice().findUser(st_id_int, -1);
//Log.d(LOG_TAG, "student id:" + student.get_id_stu());
PCBcontext.set_user(student, jsonToken, new iImgDownloaderListener() {
@Override
public void loadComplete() {
if (progressDialog!=null)
progressDialog.dismiss();
Intent pictogramActivity = new Intent(SerialActivity.this, PictogramActivity.class);
startActivity(pictogramActivity);
}
@Override
public void loadImg(Img image) {
}
});
PCBcontext.getDevice().synchronizeUsers(users, new iImgDownloaderListener() {
@Override
public void loadComplete() {
result = null;
}
@Override
public void loadImg(Img image) {
Log.d(LOG_TAG, "Load a single image with ID: " + image.get_id());
setContentView(R.layout.activity_serial);
return result==null;
}
});
} else{
// Es un Supervisor
}
} catch (JSONException ex){
//Log.d(LOG_TAG, "ERROR 1");
ex.printStackTrace();
}
}
} catch (JSONException e) {
//Log.d(LOG_TAG, "ERROR 2");
e.printStackTrace();
}
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getName(), " Server restapi error: " + e.getLocalizedMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(SerialActivity.this);
......@@ -448,4 +301,47 @@ public class SerialActivity extends Activity implements iRestapiListener {
AlertDialog alert = builder.create();
alert.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_serial);
Intent intent=getIntent();
String activityName=intent.getStringExtra("activity_name");
mSerialViewMail = (EditText) findViewById(R.id.serialmail);
mSerialViewPass = (EditText) findViewById(R.id.serialpass);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String username = settings.getString("username", "");
String password = activityName.equals("MainActivity") ? settings.getString("password", "") : "";
// Escribo el último valor indicado de username
mSerialViewMail.setText(username);
if (username.equals("")) { // No tengo aún usuario. Espero a que lo indique
Button mEntrarButton = (Button) findViewById(R.id.entrar_button);
mEntrarButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String username = mSerialViewMail.getText().toString();
String password = mSerialViewPass.getText().toString();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
SerialActivity.this.login(username, password);
}
});
} else
login(username, password);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment