Commit 9d42f9ba by german callejas

StudentFragmentGrid recuperado, imagenes de alumnos en lista arreglado

parent b9631968
......@@ -16,6 +16,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Pattern;
/**
* Img
......@@ -42,7 +43,7 @@ public class Img {
protected int id;
protected String url;
String type;
final static String FILETYPE="png";
protected String FILETYPE="png";
Bitmap bitmap;
/**
......@@ -56,12 +57,21 @@ public class Img {
this.url = url;
this.type=type;
this.bitmap=null;
if(url != null){
String [] parts = url.split(Pattern.quote("."));
this.FILETYPE = parts[parts.length-1];
}
}
public Img(Img i) {
this.id = i.id;
this.url = new String(i.url);
this.type=new String(i.type);
if(url != null){
String [] parts = url.split(Pattern.quote("."));
this.FILETYPE = parts[parts.length-1];
}
try {
if (PCBcontext.getContext()!=null && i.get_bitmap(PCBcontext.getContext())!=null)
this.bitmap=i.get_bitmap(PCBcontext.getContext()).copy(i.get_bitmap(PCBcontext.getContext()).getConfig(),true);
......@@ -69,13 +79,13 @@ public class Img {
Log.e(this.getClass().getCanonicalName(),e.getMessage());
}
}
public String file_name() { return this.id+"."+Img.FILETYPE; }
public String file_name() { return this.id+"."+this.FILETYPE; }
public int get_id() { return this.id;}
public String get_url() { return this.url;}
public void set_url(String url) {this.url=url;}
public String get_type() { return this.type;}
public String get_filetype() { return Img.FILETYPE;}
public String get_filetype() { return this.FILETYPE;}
public void update_id(int id) {
this.id = id;
......
......@@ -4,6 +4,7 @@ import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
......
......@@ -72,6 +72,7 @@ public class LoginActivity extends FragmentActivity {
this.getIntent().getStringExtra("surname") + " (" + this.getIntent().getStringExtra("email") + ")");
final Vector<Img> imgs = new Vector<>(1);
Log.e("DETAIL","IMG SUP: "+this.getIntent().getStringExtra("pic"));
imgs.add(new Img(
this.getIntent().getIntExtra("sup_id", -1),
this.getIntent().getStringExtra("pic"),
......@@ -82,7 +83,7 @@ public class LoginActivity extends FragmentActivity {
public void loadComplete() {
try {
supervisorPhotoView.setImageBitmap(
imgs.get(0).get_bitmap(PCBcontext.getContext())
imgs.get(0).get_bitmap(getApplicationContext())
);
supervisorPhotoView.invalidate();
} catch (IOException e) {
......
package com.yottacode.pictogram.supervisor.gui.login;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import com.yottacode.net.RestapiWrapper;
import com.yottacode.pictogram.dao.User;
import com.yottacode.pictogram.net.ImgDownloader;
import com.yottacode.pictogram.supervisor.R;
import com.yottacode.pictogram.tabletlibrary.gui.communicator.VOCA;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
import com.yottacode.tools.GUITools;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
/**
* StudentFragmentGrid implements the gridview with the students
*
* @author Miguel Ángel García Cumbreras
* @author Fernando
* @version 1.1
*/
public class StudentFragmentGrid extends Fragment {
final String TAG_ID = "id";
final String TAG_USERNAME = "username";
final String TAG_NAME = "name";
final String TAG_SURNAME = "surname";
final String TAG_ID_SCENE = "id_active_scene";
final String TAG_GENDER = "gender";
final String TAG_PIC = "pic";
final String TAG_LANG = "lang";
final String TAG_ATTRIBUTES = "attributes";
ProgressDialog progressDialog;
Vector<Integer> idStudents;
String nameStudents[];
Vector<Bitmap> imageStudents;
String licenseStudents[];
private Vector<JSONObject> downloaded_students;
private final String LOG_TAG = this.getClass().getSimpleName(); // Or .getCanonicalName()
GridView gridView;
boolean onlineStudentsOK = false;
/**
* Show students grid
*/
private void showStudentsGrid() {
CustomList adapter = new CustomList(getActivity(), nameStudents, imageStudents, licenseStudents);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
set_user(pos);
}
});
}
/**
* Set current user
* @param i Position of student in students name array (nameStudents)
*/
private void set_user(int i) {
Boolean offline = getActivity().getIntent().getBooleanExtra("offline", false);
if (offline) {
User currentUser = null;
try {
currentUser = PCBcontext.getDevice().findUser(idStudents.get(i), getActivity().getIntent().getIntExtra("sup_id", 0));
} catch (JSONException e) {
e.printStackTrace();
Log.e(StudentFragmentGrid.this.getClass().getCanonicalName(), e.getMessage());
}
PCBcontext.set_user(currentUser, null, null);
Intent pictogramActivity = new Intent(getActivity(), VOCA.class);
startActivity(pictogramActivity);
} else {
if (this.nameStudents[i].equals(getString(R.string.addStudent)))
register_student(getActivity().getIntent().getIntExtra("sup_id", 0));
else {
// checks if license is valid for this user
if(licenseStudents[i].equals("expired_trial")){
AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
builder1.setMessage(R.string.loginNoLicenseMsg);
builder1.setPositiveButton(
R.string.accept,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
return;
}
new_user(i, licenseStudents[i].equals("pro") || licenseStudents[i].equals("trial") );
download_supervisors(PCBcontext.getPcbdb().getCurrentUser().get_id_stu());
}
}
}
/**
* Register a new student using his license
* @param sup_id Supervisor id
*/
private void register_student(final int sup_id) {
AlertDialog.Builder alert = new AlertDialog.Builder(this.getActivity());
final EditText edittext = new EditText(this.getActivity());
alert.setMessage(getString(R.string.getLicenseStudent));
alert.setTitle(getString(R.string.addStudent));
alert.setView(edittext);
alert.setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//What ever you want to do with the value
String license = edittext.getText().toString().replaceAll("[^a-zA-Z0-9]", "").toUpperCase();
RestapiWrapper wrapper = PCBcontext.getRestapiWrapper();
final String operation = "stu/license/sup/" + sup_id;
Hashtable<String, String> params = new Hashtable<String, String>(1);
params.put("license", license);
wrapper.ask(operation, params, "post", new RestapiWrapper.iRestapiListener() {
@Override
public void error(RestapiWrapper.HTTPException e) {
Log.e(this.getClass().getName(), " Server restapi error. Operation: "+operation +". Message:"+ e.getLocalizedMessage());
String err = e.getLocalizedMessage();
if (err.indexOf("nvalid license") > 0)
GUITools.show_alert(getActivity(), R.string.loginNoLicenseMsg);
}
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
PCBcontext.getNetService().restart_app(false);
}
});
}
});
alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});
alert.show();
}
/**
* Change user data, load vocabulary and goes to VOCA Activity
* @param i Position on students downloaded collection
* @param license_valid
*/
private void new_user(int i, final boolean license_valid) {
JSONObject st = this.downloaded_students.get(i);
Intent intent = getActivity().getIntent();
try {
Log.i(LOG_TAG, st.toString());
PCBcontext.getPcbdb();
User new_user = new User(
st.getInt(TAG_ID),
st.getString(TAG_USERNAME),
null,
st.getString(TAG_NAME),
st.getString(TAG_SURNAME),
st.getInt(TAG_ID_SCENE),
st.getString(TAG_PIC),
st.getString(TAG_GENDER),
st.getString(TAG_LANG),
st.getString(TAG_ATTRIBUTES),
intent.getIntExtra("sup_id", 0),
intent.getStringExtra("email"),
intent.getStringExtra("password"),
intent.getStringExtra("name"),
intent.getStringExtra("surname"),
intent.getStringExtra("pic"),
intent.getStringExtra("gender"),
intent.getStringExtra("lang"),
"",
intent.getStringExtra("office")
);
Log.i(this.getClass().getCanonicalName(), "Loading vocabulary for " + new_user.get_name_stu() + " office:" + intent.getStringExtra("office"));
PCBcontext.getDevice().insertUser(new_user);
progressDialog = ProgressDialog.show(getActivity(), getString(R.string.loadingGrammar), getString(R.string.userLoadingTxt), false, false);
PCBcontext.set_user(new_user, intent.getStringExtra("token"), new ImgDownloader.iImgDownloaderListener() {
@Override
public void loadComplete() {
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
PCBcontext.getPcbdb().user_valid(license_valid);
Intent pictogramActivity = new Intent(getActivity(), VOCA.class);
startActivity(pictogramActivity);
}
@Override
public void loadImg(Img image) {
}
public void error(Exception e) {
if (progressDialog.isShowing()) progressDialog.dismiss();
GUITools.show_alert(StudentFragmentGrid.this.getActivity(), R.string.serverError, e.getMessage());
Log.e(this.getClass().getCanonicalName(), "Server error:" + e.getLocalizedMessage());
}
});
} catch (JSONException e) {
e.printStackTrace();
Log.e(StudentFragmentGrid.this.getClass().getCanonicalName(), e.getMessage());
}
}
/**
* Process JSON into collections
*/
private void show_student_list_online() {
final int listsize = downloaded_students.size() + 1; //n students + 1
final Vector<Img> imgs = new Vector<>(listsize-1);
this.nameStudents = new String[listsize];
this.idStudents = new Vector<>(listsize);
this.imageStudents = new Vector<>(listsize);
this.licenseStudents = new String[listsize];
for (int i = 0; i < downloaded_students.size(); i++) {
JSONObject st;
try {
st = downloaded_students.get(i);
Integer st_id = st.getInt(TAG_ID);
String st_name = st.getString(TAG_NAME);
String st_pic = st.getString(TAG_PIC);
nameStudents[i] = st_name;
idStudents.add(st_id);
imgs.add(new Img(st_id, st_pic, Img.STUDENT)); //it's required to download student's images
// Checks if license is trial or pro (not expired)
licenseStudents[i] = !st.getJSONObject("license").getBoolean("isValid")
? (st.getJSONObject("license").getBoolean("isTrial")
? "expired_trial"
: "expired_official")
: st.getJSONObject("license").getBoolean("isTrial")
? "trial"
: "pro";
} catch (JSONException e) {
e.printStackTrace();
Log.e(StudentFragmentGrid.this.getClass().getCanonicalName(), e.getMessage());
}
} //for
nameStudents[listsize - 1] = getString(R.string.addStudent);
idStudents.add(-1);
licenseStudents[listsize -1] = "pro";
imgs.add(new Img(-1, null, Img.STUDENT)); //it's required to download student's images
ImgDownloader downloader = new ImgDownloader(getActivity(), new ImgDownloader.iImgDownloaderListener() {
private void loaded() {
try {
if (downloaded_students.size() >= 1)
for (int i = 0; i < imgs.size(); i++)
imageStudents.add(imgs.get(i).get_bitmap(getActivity().getBaseContext()));
//imageStudents.add(imgs.get(listsize - 1).get_bitmap(getActivity().getBaseContext()));
} catch (IOException e) {
e.printStackTrace();
Log.e(StudentFragmentGrid.this.getClass().getCanonicalName(), e.getMessage());
}
if (StudentFragmentGrid.super.getView() != null)
showStudentsGrid();
else
onlineStudentsOK = true;
}
@Override
public void loadComplete() {
loaded();
}
@Override
public void loadImg(Img image) {
loaded();
}
public void error(Exception e) {
if (progressDialog.isShowing()) progressDialog.dismiss();
GUITools.show_alert(PCBcontext.getContext(), R.string.serverError, e.getMessage());
Log.e(this.getClass().getCanonicalName(), "Server error:" + e.getLocalizedMessage());
}
}, ImgDownloader.tsource.remote);
downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, imgs);
}
/**
* Recupera los supervisores de un alumno
*
* @param stu_id
* @author Germán Callejas
*/
private void download_supervisors(final int stu_id) {
String token = getActivity().getIntent().getExtras().getString("token");
RestapiWrapper wrapper = PCBcontext.getRestapiWrapper();
wrapper.setToken(token);
final String operation = "stu/" + stu_id + "/supervisors";
wrapper.ask(operation, new RestapiWrapper.iRestapiListener() {
@Override
public void error(RestapiWrapper.HTTPException e) {
Log.e(this.getClass().getName(), " Server restapi error. Operation: "+operation +". Message:"+ e.getLocalizedMessage());
GUITools.show_alert(getActivity(), R.string.loginErrorTxt, getString(R.string.serverError), new GUITools.iOKListener() {
@Override
public void ok() {
PCBcontext.getNetService().restart_app(false);
}
});
}
@Override
public void preExecute() {
}
@Override
public void result(JSONArray supervisors) {
Vector<String> idSupervisoresJSON = new Vector<>();
for (int i = 0; i < supervisors.length(); i++) {
JSONObject supervisor = null;
try {
supervisor = supervisors.getJSONObject(i);
idSupervisoresJSON.add(supervisor.getString("email"));
if (!PCBcontext.getDevice().isSupervisorOnLocal((int) supervisor.get("id"), stu_id)) {//Para comprobar si ya existe en local
User currentUser = PCBcontext.getPcbdb().getCurrentUser();
PCBcontext.getDevice().insertUser(new User(stu_id, currentUser.get_nickname_stu(), currentUser.get_pwd_stu()
, currentUser.get_name_stu(), currentUser.get_surname_stu(), currentUser.get_active_scene(), currentUser.get_url_img_stu()
, currentUser.get_gender_stu(), currentUser.get_lang_stu(), currentUser.get_json_attrs(),
(int) supervisor.get("id"), supervisor.get("email").toString(), "_", supervisor.get("name").toString(), supervisor.get("surname").toString(), supervisor.get("pic").toString(),
supervisor.get("gender").toString(), supervisor.get("lang").toString(), supervisor.get("ttsEngine").toString(), supervisor.get("office").toString()));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**Eliminar los que habia y que ya no vienen en el JSON */
try {
Hashtable<String, String> supervisorsLocal = PCBcontext.getDevice().recoverSupervisors(stu_id);
if (supervisorsLocal != null && !supervisorsLocal.isEmpty()) {
for (String email_sup : supervisorsLocal.keySet()) {
if (!idSupervisoresJSON.contains(email_sup)) {
//Coge la segunda parte ya que el formato del dato es Nombre, Apellidos;id_sup
PCBcontext.getDevice().deleteSupervisor(stu_id, Integer.parseInt(supervisorsLocal.get(email_sup).split(";")[1]));
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void result(JSONObject result) {
}
});
}
/**
* Download students from supervisor into downloaded_students
* @param sup_id Supervisor id
*/
private void download_students(final int sup_id) {
String token = getActivity().getIntent().getExtras().getString("token");
RestapiWrapper wrapper = PCBcontext.getRestapiWrapper();
wrapper.setToken(token);
final String operation = "sup/" + sup_id + "/students";
wrapper.ask(operation, new RestapiWrapper.iRestapiListener() {
@Override
public void error(RestapiWrapper.HTTPException e) {
Log.e(this.getClass().getName(), " Server restapi error. Operation: "+operation +". Message:"+ e.getLocalizedMessage());
GUITools.show_alert(getActivity(), R.string.loginErrorTxt, getString(R.string.serverError), new GUITools.iOKListener() {
@Override
public void ok() {
PCBcontext.getNetService().restart_app(false);
}
});
}
@Override
public void preExecute() {
}
@Override
public void result(JSONArray students) {
if (progressDialog != null && progressDialog.isShowing()) progressDialog.dismiss();
StudentFragmentGrid.this.downloaded_students = new Vector();
for (int i = 0; i < students.length(); i++) {
JSONObject student;
try {
student = students.getJSONObject(i);
StudentFragmentGrid.this.downloaded_students.add(student);
} catch (JSONException e) {
e.printStackTrace();
}
}
show_student_list_online();
}
@Override
public void result(JSONObject result) {
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onStart() {
super.onStart();
Intent intent = getActivity().getIntent();
Boolean offline = intent.getBooleanExtra("offline", false);
int sup_id = intent.getIntExtra("sup_id", 0);
if (offline) {
Vector<User> users;
try {
users = PCBcontext.getDevice().recoverStudents(sup_id);
Log.i(this.getClass().getCanonicalName(), "Recovering " + users.size() + " students list for " + sup_id + " from local DB");
} catch (JSONException e) {
e.printStackTrace();
users = null;
}
int i = 0;
this.nameStudents = new String[users.size()];
this.idStudents = new Vector<>(users.size());
this.imageStudents = new Vector<>(users.size());
for (User user : users) {
this.idStudents.add(user.get_id_stu());
this.nameStudents[i++] = user.get_name_stu();
try {
this.imageStudents.add(user.get_bitmap_stu(getActivity().getBaseContext()));
} catch (IOException e) {
e.printStackTrace();
Log.e(this.getClass().getName(), " Getting images error: " + e.getLocalizedMessage());
}
}
} else
download_students(sup_id);
if (offline || onlineStudentsOK) showStudentsGrid();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new_student, container, false);
gridView = (GridView) view.findViewById(R.id.loginStudentGridView);
return view;
}
@Override
public void onPause() {
super.onPause();
if ((progressDialog != null) && progressDialog.isShowing())
progressDialog.dismiss();
progressDialog = null;
}
}
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