Solucionado problema de primera carga de pictogramas usando inner clases

parent 9239a071
...@@ -302,7 +302,7 @@ public class Device extends SQLiteOpenHelper { ...@@ -302,7 +302,7 @@ public class Device extends SQLiteOpenHelper {
for (User user : prev_users) for (User user : prev_users)
db.execSQL("DELETE FROM users_detail WHERE id_stu=" + user.get_id_stu() + " AND id_sup=" + user.get_id_sup()); db.execSQL("DELETE FROM users_detail WHERE id_stu=" + user.get_id_stu() + " AND id_sup=" + user.get_id_sup());
ImgDownloader downloader = new ImgDownloader(this.context, imgListener, this.getClass().getSimpleName()); ImgDownloader downloader = new ImgDownloader(this.context, imgListener);
downloader.execute(imgs); downloader.execute(imgs);
db.close(); db.close();
......
...@@ -54,7 +54,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -54,7 +54,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
try { try {
Log.i(this.getClass().getName(), "local vocabulary"); Log.i(this.getClass().getName(), "local vocabulary");
PCBcontext.getPcbdb().getStudentVocabulary(this); PCBcontext.getPcbdb().getStudentVocabulary(this);
listener.loadComplete(this.getClass().getSimpleName()); listener.loadComplete();
} catch (JSONException e) { } catch (JSONException e) {
Log.e(this.getClass().getName(),"Local vocabulary recover failed: "+e.getMessage()); Log.e(this.getClass().getName(),"Local vocabulary recover failed: "+e.getMessage());
} }
...@@ -102,7 +102,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -102,7 +102,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
imgs.add(new Img(picto.get_id(), picto.get_url(), Img.VOCABULARY)); imgs.add(new Img(picto.get_id(), picto.get_url(), Img.VOCABULARY));
} }
ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener, this.getClass().getSimpleName()); ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener);
downloader.execute(imgs); downloader.execute(imgs);
PCBcontext.getPcbdb().setStudentVocabulary(this); PCBcontext.getPcbdb().setStudentVocabulary(this);
} }
...@@ -116,7 +116,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -116,7 +116,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
Vector<Img> imgs=new Vector<Img>(1); Vector<Img> imgs=new Vector<Img>(1);
imgs.add(new Img(pic.get_id(), pic.get_url(), Img.VOCABULARY)); imgs.add(new Img(pic.get_id(), pic.get_url(), Img.VOCABULARY));
ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener, this.getClass().getSimpleName()); ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener);
downloader.execute(imgs); downloader.execute(imgs);
loadPicto(pic); loadPicto(pic);
......
...@@ -42,7 +42,7 @@ import java.util.Vector; ...@@ -42,7 +42,7 @@ import java.util.Vector;
* @version 1.0 * @version 1.0
*/ */
public class LoginActivity extends FragmentActivity implements iRestapiListener, iImgDownloaderListener { public class LoginActivity extends FragmentActivity implements iRestapiListener {
private static final String TAG_ID = "id"; private static final String TAG_ID = "id";
private static final String TAG_NAME = "name"; private static final String TAG_NAME = "name";
...@@ -206,28 +206,63 @@ public class LoginActivity extends FragmentActivity implements iRestapiListener, ...@@ -206,28 +206,63 @@ public class LoginActivity extends FragmentActivity implements iRestapiListener,
} }
// Si sólo hay 1 estudiante, cargo el st_id // Si sólo hay 1 estudiante, cargo el st_id
if (students_supervisors.length() == 1){ if (students_supervisors.length() == 1)
//User student = PCBcontext.getDevice().findUser(st_id_int, sup_id); PCBcontext.set_user(student, token, new iImgDownloaderListener() {
PCBcontext.set_user(student, token, this); @Override
} else{ public void loadComplete() {
Log.d(LOG_TAG, "Images: Load images complete");
progressDialog.dismiss();
setContentView(R.layout.activity_login);
}
// De la interfaz iImgDownloaderListener
@Override
public void loadImg(Img img) {
}
}); else{
student = new User(-1, "", "", "", "M", "es-es", "", student = new User(-1, "", "", "", "M", "es-es", "",
sup_id, "", "", "", "M", "es-es", ""); sup_id, "", "", "", "M", "es-es", "");
PCBcontext.set_user(student, token, this); PCBcontext.set_user(student, token, null);
} }
//Device device = new Device(getApplicationContext(), null, 1); //Device device = new Device(getApplicationContext(), null, 1);
if (users.size() > 0){ if (users.size() > 0){
PCBcontext.getDevice().synchronizeUsers(users, this); PCBcontext.getDevice().synchronizeUsers(users, new iImgDownloaderListener() {
@Override
public void loadComplete() {
}
@Override
public void loadImg(Img img) {
Log.d(LOG_TAG, "Load a single image with ID: " + img.get_id());
// Aquí filtro si sólo tiene un único usuario o varios
Vector<User> users = null;
try {
users = PCBcontext.getDevice().getUsers();
if (users.size() > 1) {
setContentView(R.layout.activity_login);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
PCBcontext.getDevice().deleteDeprecatedImgs(); PCBcontext.getDevice().deleteDeprecatedImgs();
// Si sólo hay 1 estudiante paso a Pictogram // Si sólo hay 1 estudiante paso a Pictogram
if (users.size() == 1){ if(users.size()==1)
//close the progress dialog
progressDialog.dismiss();
Intent pictogramActivity = new Intent(this, PictogramActivity.class); {
startActivity(pictogramActivity); //close the progress dialog
} progressDialog.dismiss();
} else{
Intent pictogramActivity = new Intent(this, PictogramActivity.class);
startActivity(pictogramActivity);
}
}else{
AlertDialog.Builder builder = AlertDialog.Builder builder =
new AlertDialog.Builder(this); new AlertDialog.Builder(this);
...@@ -254,44 +289,6 @@ public class LoginActivity extends FragmentActivity implements iRestapiListener, ...@@ -254,44 +289,6 @@ public class LoginActivity extends FragmentActivity implements iRestapiListener,
public void result(JSONObject result) { public void result(JSONObject result) {
} }
@Override
public void loadComplete(String name) {
Log.d(LOG_TAG, "Images: Load images " + name + " complete");
try {
// Aquí filtro si sólo tiene un único usuario o varios
Vector<User> users = PCBcontext.getDevice().getUsers();
if (users.size()>1){
//close the progress dialog
progressDialog.dismiss();
setContentView(R.layout.activity_login);
}
}catch(NullPointerException e){
e.printStackTrace();
Log.e(LOG_TAG, "Las imágenes de usuarios todavía no se han cargado o fallo en los fragments");
} catch(JSONException e){
e.printStackTrace();
Log.e(LOG_TAG, "Fallo al procesar JSON");
}
}
// De la interfaz iImgDownloaderListener
@Override
public void loadImg(Img img) {
Log.d(LOG_TAG, "Load a single image with ID: " + img.get_id());
// Aquí filtro si sólo tiene un único usuario o varios
Vector<User> users = null;
try {
users = PCBcontext.getDevice().getUsers();
if (users.size()>1){
setContentView(R.layout.activity_login);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override protected void onResume() { @Override protected void onResume() {
super.onResume(); super.onResume();
......
...@@ -55,7 +55,7 @@ import com.yottacode.pictogram.tools.PCBcontext; ...@@ -55,7 +55,7 @@ import com.yottacode.pictogram.tools.PCBcontext;
/** /**
* A login screen that offers login via email/password. * A login screen that offers login via email/password.
*/ */
public class SerialActivity extends Activity implements iRestapiListener, iImgDownloaderListener { public class SerialActivity extends Activity implements iRestapiListener {
// UI references. // UI references.
private EditText mSerialViewMail; private EditText mSerialViewMail;
...@@ -278,9 +278,32 @@ public class SerialActivity extends Activity implements iRestapiListener, iImgDo ...@@ -278,9 +278,32 @@ public class SerialActivity extends Activity implements iRestapiListener, iImgDo
//User student = PCBcontext.getDevice().findUser(st_id_int, -1); //User student = PCBcontext.getDevice().findUser(st_id_int, -1);
//Log.d(LOG_TAG, "student id:" + student.get_id_stu()); //Log.d(LOG_TAG, "student id:" + student.get_id_stu());
PCBcontext.set_user(student, jsonToken, this); PCBcontext.set_user(student, jsonToken, new iImgDownloaderListener() {
@Override
public void loadComplete() {
progressDialog.dismiss();
Intent pictogramActivity = new Intent(SerialActivity.this, PictogramActivity.class);
startActivity(pictogramActivity);
}
PCBcontext.getDevice().synchronizeUsers(users, this); @Override
public void loadImg(Img image) {
}
});
PCBcontext.getDevice().synchronizeUsers(users, new iImgDownloaderListener() {
@Override
public void loadComplete() {
}
@Override
public void loadImg(Img image) {
Log.d(LOG_TAG, "Load a single image with ID: " + image.get_id());
setContentView(R.layout.activity_serial);
}
});
PCBcontext.getDevice().deleteDeprecatedImgs(); PCBcontext.getDevice().deleteDeprecatedImgs();
} else{ } else{
...@@ -327,30 +350,6 @@ public class SerialActivity extends Activity implements iRestapiListener, iImgDo ...@@ -327,30 +350,6 @@ public class SerialActivity extends Activity implements iRestapiListener, iImgDo
AlertDialog alert = builder.create(); AlertDialog alert = builder.create();
alert.show(); alert.show();
} }
@Override
public void loadComplete(String collection) {
Log.d(LOG_TAG, "Images: load " + collection + " images collection complete."+ " Waiting for "+com.yottacode.pictogram.grammar.Vocabulary.class.getSimpleName()+" collection");
if (collection.equalsIgnoreCase(com.yottacode.pictogram.grammar.Vocabulary.class.getSimpleName())) {
/*
User student = PCBcontext.getDevice().findUser(st_id, -1);
Log.d(LOG_TAG, "student:" + student.get_name_stu());
PCBcontext.set_user(student, jsonToken, null);
*/
//close the progress dialog
progressDialog.dismiss();
// Entro directamente a Pictogram con ese alumno
Intent pictogramActivity = new Intent(this, PictogramActivity.class);
startActivity(pictogramActivity);
}
}
@Override
public void loadImg(Img image) {
Log.d(LOG_TAG, "Load a single image with ID: " + image.get_id());
setContentView(R.layout.activity_serial);
}
} }
......
...@@ -141,7 +141,7 @@ public class StudentFragmentGrid extends Fragment{ ...@@ -141,7 +141,7 @@ public class StudentFragmentGrid extends Fragment{
PCBcontext.set_user(student, jsonToken, new iImgDownloaderListener() { PCBcontext.set_user(student, jsonToken, new iImgDownloaderListener() {
@Override @Override
public void loadComplete(String collection ) { public void loadComplete( ) {
//Vocabulary vocabulary = PCBcontext.getVocabulary(); //Vocabulary vocabulary = PCBcontext.getVocabulary();
//Log.d(LOG_TAG, "Vocabulario de tam:" + vocabulary.size()); //Log.d(LOG_TAG, "Vocabulario de tam:" + vocabulary.size());
progressDialog.dismiss(); progressDialog.dismiss();
......
...@@ -32,10 +32,9 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> { ...@@ -32,10 +32,9 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
Context context; Context context;
ActivityManager.MemoryInfo mi; ActivityManager.MemoryInfo mi;
ActivityManager activityManager; ActivityManager activityManager;
String collection;
public ImgDownloader(Context context, iImgDownloaderListener listener, String collection) {
this.collection=collection; public ImgDownloader(Context context, iImgDownloaderListener listener) {
this.imgListener = listener; this.imgListener = listener;
this.context=context; this.context=context;
this.mi = new ActivityManager.MemoryInfo(); this.mi = new ActivityManager.MemoryInfo();
...@@ -100,9 +99,9 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> { ...@@ -100,9 +99,9 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
@Override @Override
protected void onPostExecute(Img img) { protected void onPostExecute(Img img) {
Log.d(this.getClass().getCanonicalName(), "Images: Inside OnPostExecute() Collection: "+ this.collection); Log.d(this.getClass().getCanonicalName(), "Images: Inside OnPostExecute()");
if (imgListener!=null) if (imgListener!=null)
if(img == null) imgListener.loadComplete(this.collection); if(img == null) imgListener.loadComplete();
else imgListener.loadImg(img); else imgListener.loadImg(img);
} }
......
...@@ -9,6 +9,6 @@ import java.util.LinkedList; ...@@ -9,6 +9,6 @@ import java.util.LinkedList;
* Created by emblanco on 24/09/15. * Created by emblanco on 24/09/15.
*/ */
public interface iImgDownloaderListener { public interface iImgDownloaderListener {
public void loadComplete(String collection); // for loading the vocabulary public void loadComplete(); // for loading the vocabulary
public void loadImg(Img image); // for loading one image public void loadImg(Img image); // for loading one image
} }
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