Thread delay fixed

parent ac0ad839
...@@ -11,6 +11,7 @@ package com.yottacode.net; ...@@ -11,6 +11,7 @@ package com.yottacode.net;
import java.net.URL; import java.net.URL;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.concurrent.Executor;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -73,7 +74,7 @@ public class RestapiWrapper { ...@@ -73,7 +74,7 @@ public class RestapiWrapper {
httpAsyncTaskParams.listener=listener; httpAsyncTaskParams.listener=listener;
httpAsyncTaskParams.url_params=params; httpAsyncTaskParams.url_params=params;
httpAsyncTaskParams.url=this.server + '/' + operation; httpAsyncTaskParams.url=this.server + '/' + operation;
new HttpAsyncTask().execute(httpAsyncTaskParams); new HttpAsyncTask().executeOnExecutor(HttpAsyncTask.THREAD_POOL_EXECUTOR,httpAsyncTaskParams);
} }
public void ask(String operation, iRestapiListener listener) { public void ask(String operation, iRestapiListener listener) {
......
...@@ -7,12 +7,14 @@ import android.database.Cursor; ...@@ -7,12 +7,14 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Vector; import java.util.Vector;
import com.yottacode.pictogram.action.Action; import com.yottacode.pictogram.action.Action;
...@@ -285,9 +287,13 @@ public class Device extends SQLiteOpenHelper { ...@@ -285,9 +287,13 @@ public class Device extends SQLiteOpenHelper {
* @see com.yottacode.pictogram.dao.PCBDBHelper * @see com.yottacode.pictogram.dao.PCBDBHelper
*/ */
public void synchronizeUsers(Vector<User> users, iImgDownloaderListener imgListener) throws JSONException { public void synchronizeUsers(Vector<User> users, iImgDownloaderListener imgListener) throws JSONException {
Log.d("LoginActivity", "1:" + new SimpleDateFormat("HH:mm:ss"));
Vector<User> prev_users = getUsers(); Vector<User> prev_users = getUsers();
Vector<Img> imgs = new Vector<Img>(users.size() * 2); Vector<Img> imgs = new Vector<Img>(users.size() * 2);
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
for (User user : users) { for (User user : users) {
if (prev_users.contains(user)) { if (prev_users.contains(user)) {
updateUser(user, db); updateUser(user, db);
...@@ -303,7 +309,7 @@ public class Device extends SQLiteOpenHelper { ...@@ -303,7 +309,7 @@ public class Device extends SQLiteOpenHelper {
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); ImgDownloader downloader = new ImgDownloader(this.context, imgListener);
downloader.execute(imgs); downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,imgs);
db.close(); db.close();
} }
......
package com.yottacode.pictogram.grammar; package com.yottacode.pictogram.grammar;
import android.os.AsyncTask;
import android.os.Environment; import android.os.Environment;
import android.util.Log; import android.util.Log;
...@@ -31,7 +32,7 @@ import java.util.Vector; ...@@ -31,7 +32,7 @@ import java.util.Vector;
* @author Fernando Martinez Santiago * @author Fernando Martinez Santiago
* @version 1.0 * @version 1.0
*/ */
public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabularyListener { public class Vocabulary implements Iterable<Picto> {
Hashtable<Integer,LinkedList<Picto>> pictos; Hashtable<Integer,LinkedList<Picto>> pictos;
...@@ -61,7 +62,38 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -61,7 +62,38 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
} }
public void listen(Room room, iVocabularyListener listener) { public void listen(Room room, iVocabularyListener listener) {
iVocabularyListener listeners[] = {this,listener}; iVocabularyListener listeners[] = {new iVocabularyListener() {
@Override
public void change(action action, int picto_cat, int picto_id, JSONObject args) {
switch (action) {
case delete: {
removePicto(picto_cat, picto_id);
break;
}
case update:{
modifyAttsPicto(picto_cat, picto_id, args);
break;
}
case add:{
try{
String text= args.getJSONObject("expression").getString("text");
String uri=args.getJSONObject("picto").getString("uri");
JSONObject attrs_picto = args.getJSONObject("attributes");
addPicto(new Picto(picto_id, uri, text, attrs_picto));
} catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + "--" + e);
// System.exit(-1);
}
break;
}
}
}
},listener};
talk = new VocabularyTalk(room, listeners); talk = new VocabularyTalk(room, listeners);
} }
/** /**
...@@ -70,7 +102,53 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -70,7 +102,53 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
public void download() { public void download() {
final String picto_str="/pictos"; final String picto_str="/pictos";
String operation=PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()+picto_str; String operation=PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()+picto_str;
PCBcontext.getRestapiWrapper().ask(operation, this); PCBcontext.getRestapiWrapper().ask(operation, new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
if (result != null) {
final String jpicto = "picto";
final String jid = "id";
final String juri = "uri";
final String jattributes = "attributes";
final String jexpression = "expression";
final String jexpression_text = "text";
Picto[] pictos = new Picto[result.length()];
JSONObject picto = null;
try {
for (int i = 0; i < result.length(); i++) {
picto = result.getJSONObject(i).getJSONObject(jpicto);
JSONObject expression = result.getJSONObject(i).getJSONObject(jexpression);
JSONObject attributes = result.getJSONObject(i).getJSONObject(jattributes);
pictos[i] = new Picto(picto.getInt(jid),
picto.getString(juri),
expression.getString(jexpression_text),
attributes);
}
synchronizeImgs(pictos);
Log.i(this.getClass().getName(), " Pictos downloaded: " + result.length());
} catch (JSONException e) {
Log.e(this.getClass().getName(), " Picto json error from server: " + picto.toString());
this.error(e);
}
}
}
@Override
public void result(JSONObject result) {
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getName(), " Server restapi error: " + e.getLocalizedMessage());
}
});
} }
...@@ -83,7 +161,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -83,7 +161,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
private void synchronizeImgs(Picto updated_collection[]) throws JSONException { private void synchronizeImgs(Picto updated_collection[]) throws JSONException {
Vector<Img> imgs=new Vector<Img>(updated_collection.length); Vector<Img> imgs=new Vector<Img>(updated_collection.length);
Log.d(this.getClass().getName(), "Vocabulary size: " + updated_collection.length);
this.pictos.clear(); this.pictos.clear();
for (Picto updated_picto: updated_collection) { for (Picto updated_picto: updated_collection) {
...@@ -101,9 +179,9 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -101,9 +179,9 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
pictos_cat.add(picto); pictos_cat.add(picto);
imgs.add(new Img(picto.get_id(), picto.get_url(), Img.VOCABULARY)); imgs.add(new Img(picto.get_id(), picto.get_url(), Img.VOCABULARY));
} }
Log.d(this.getClass().getName(), "Vocabulary size: " + updated_collection.length);
ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener); ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener);
downloader.execute(imgs); downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, imgs);
PCBcontext.getPcbdb().setStudentVocabulary(this); PCBcontext.getPcbdb().setStudentVocabulary(this);
} }
...@@ -117,7 +195,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -117,7 +195,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); ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener);
downloader.execute(imgs); downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, imgs);
loadPicto(pic); loadPicto(pic);
...@@ -138,27 +216,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -138,27 +216,7 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
params.put("id", String.valueOf(PCBcontext.getPcbdb().getCurrentUser().get_id_stu())); params.put("id", String.valueOf(PCBcontext.getPcbdb().getCurrentUser().get_id_stu()));
params.put("file", String.valueOf(imageData)); params.put("file", String.valueOf(imageData));
PCBcontext.getRestapiWrapper().ask("upload", params, "post", new iRestapiListener() { PCBcontext.getRestapiWrapper().ask("upload", params, "post", null);
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
}
@Override
public void error(Exception e) {
}
});
} }
/** /**
...@@ -294,85 +352,4 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular ...@@ -294,85 +352,4 @@ public class Vocabulary implements Iterable<Picto>, iRestapiListener, iVocabular
return vocabulary; return vocabulary;
} }
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
if (result!=null) {
final String jpicto = "picto";
final String jid = "id";
final String juri = "uri";
final String jattributes = "attributes";
final String jexpression = "expression";
final String jexpression_text = "text";
Picto[] pictos = new Picto[result.length()];
JSONObject picto=null;
try {
for (int i = 0; i < result.length(); i++) {
picto = result.getJSONObject(i).getJSONObject(jpicto);
JSONObject expression = result.getJSONObject(i).getJSONObject(jexpression);
JSONObject attributes = result.getJSONObject(i).getJSONObject(jattributes);
pictos[i] = new Picto(picto.getInt(jid),
picto.getString(juri),
expression.getString(jexpression_text),
attributes);
}
this.synchronizeImgs(pictos);
Log.i(this.getClass().getName(), " Pictos downloaded: " + result.length());
} catch (JSONException e) {
Log.e(this.getClass().getName(), " Picto json error from server: " + picto.toString());
this.error(e);
}
}
}
@Override
public void result(JSONObject result) {
if (result!=null) {
Log.d("-PROPIOS-Objetodevuelto", result.toString());
}
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getName()," Server restapi error: "+e.getLocalizedMessage());
}
@Override
public void change(action action, int picto_cat, int picto_id, JSONObject args) {
switch (action) {
case delete: {
removePicto(picto_cat, picto_id);
break;
}
case update:{
modifyAttsPicto(picto_cat, picto_id, args);
break;
}
case add:{
try{
String text= args.getJSONObject("expression").getString("text");
String uri=args.getJSONObject("picto").getString("uri");
JSONObject attrs_picto = args.getJSONObject("attributes");
addPicto(new Picto(picto_id, uri, text, attrs_picto));
} catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + "--" + e);
// System.exit(-1);
}
break;
}
}
}
} }
\ No newline at end of file
...@@ -13,6 +13,7 @@ import java.io.IOException; ...@@ -13,6 +13,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Vector; import java.util.Vector;
...@@ -42,15 +43,21 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> { ...@@ -42,15 +43,21 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
this.force_download=context.getResources().getBoolean(R.bool.force_img_download); this.force_download=context.getResources().getBoolean(R.bool.force_img_download);
} }
@Override
protected void onPreExecute() {
Log.d(this.getClass().getCanonicalName(), "Pre-EXEC");
}
protected Img doInBackground( Vector<Img> imgs) { protected Img doInBackground( Vector<Img> imgs) {
InputStream is=null; InputStream is=null;
Log.d(this.getClass().getCanonicalName(), "Required images: " + imgs.size());
this.current= ImgDownloader.status.downloading; this.current= ImgDownloader.status.downloading;
int i=0,j=0; int i=0,j=0,allsize=0;
Long availableMegs = mi.availMem / 1048576L; Long availableMegs = mi.availMem / 1048576L;
int seconds = Calendar.getInstance().get(Calendar.SECOND); int seconds = Calendar.getInstance().get(Calendar.SECOND);
Log.i(this.getClass().getCanonicalName(), "Required images: " + imgs.size());
try { try {
String surl=null; String surl=null;
for (Img img: imgs) { for (Img img: imgs) {
...@@ -61,12 +68,12 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> { ...@@ -61,12 +68,12 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
this.activityManager.getMemoryInfo(mi); this.activityManager.getMemoryInfo(mi);
URL url = new URL(surl); URL url = new URL(surl);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection(); URLConnection ucon = url.openConnection();
is = ucon.getInputStream(); is = ucon.getInputStream();
img.save_bitmap(this.context, is); int size=img.save_bitmap(this.context, is);
allsize+=size;
i++; i++;
} catch (IOException e) { } catch (IOException e) {
j++; j++;
Log.e(this.getClass().getCanonicalName(), surl + " was not downloaded: "+e); Log.e(this.getClass().getCanonicalName(), surl + " was not downloaded: "+e);
...@@ -81,14 +88,14 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> { ...@@ -81,14 +88,14 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
} }
seconds=Calendar.getInstance().get(Calendar.SECOND)-seconds; seconds=Calendar.getInstance().get(Calendar.SECOND)-seconds;
Log.i(this.getClass().getCanonicalName(), Log.i(this.getClass().getCanonicalName(),
"Images: Downloaded: " + i+"/"+imgs.size()+ "LoginActivity: Downloaded: " + i+"/"+imgs.size()+ " (size:"+allsize/1024+" Kbytes)"+
". Cached: "+ (imgs.size()-i)+"/"+imgs.size()+ ". Cached: "+ (imgs.size()-i)+"/"+imgs.size()+
". Download failed: "+ j+"/"+imgs.size()+ ". Download failed: "+ j+"/"+imgs.size()+
". Memory required:"+((mi.availMem / 1048576L)-availableMegs)+" MB"+ ". Memory required:"+((mi.availMem / 1048576L)-availableMegs)+" MB"+
". Used time: "+seconds+" seconds"); ". Used time: "+seconds+" seconds at "+new SimpleDateFormat("HH:mm:ss"));
;
return imgs.size() > 1 ? null return imgs.size() > 1 ? null
: imgs.get(0); : imgs.get(0);
} }
@Override @Override
...@@ -99,7 +106,7 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> { ...@@ -99,7 +106,7 @@ 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()"); Log.d(this.getClass().getCanonicalName(), "LoginActivity: Inside OnPostExecute()"+new SimpleDateFormat("HH:mm:ss"));
if (imgListener!=null) if (imgListener!=null)
if(img == null) imgListener.loadComplete(); if(img == null) imgListener.loadComplete();
else imgListener.loadImg(img); else imgListener.loadImg(img);
......
...@@ -108,7 +108,7 @@ public class Img { ...@@ -108,7 +108,7 @@ public class Img {
* @param is the stream where the image is available * @param is the stream where the image is available
* @throws IOException * @throws IOException
*/ */
public void save_bitmap(Context context, InputStream is) throws IOException { public int save_bitmap(Context context, InputStream is) throws IOException {
File file = file(context, this.type); File file = file(context, this.type);
FileOutputStream os = new FileOutputStream(file); FileOutputStream os = new FileOutputStream(file);
...@@ -123,6 +123,7 @@ public class Img { ...@@ -123,6 +123,7 @@ public class Img {
byte[] byteArray = outstream.toByteArray(); byte[] byteArray = outstream.toByteArray();
os.write(byteArray); os.write(byteArray);
outstream.close(); outstream.close();
return byteArray.length;
} }
......
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