issue #566 fixed (it was a hard one)

parent 15e19414
...@@ -35,6 +35,8 @@ public abstract class Action { ...@@ -35,6 +35,8 @@ public abstract class Action {
this.type=type; this.type=type;
} }
abstract public boolean is_local_action();
public String get_type() { return this.type;} public String get_type() { return this.type;}
public abstract String get_action(); public abstract String get_action();
protected JSONObject get_json() { protected JSONObject get_json() {
...@@ -79,4 +81,4 @@ public abstract class Action { ...@@ -79,4 +81,4 @@ public abstract class Action {
return null; return null;
} }
} }
\ No newline at end of file
...@@ -49,6 +49,8 @@ public abstract class PictoAction extends Action { ...@@ -49,6 +49,8 @@ public abstract class PictoAction extends Action {
return null; return null;
} }
} }
public boolean is_local_action() {return this.picto.is_local();}
public JSONObject get_json_picto() throws JSONException { public JSONObject get_json_picto() throws JSONException {
final String param_id_json="id"; final String param_id_json="id";
final String param_picto="picto"; final String param_picto="picto";
......
...@@ -59,6 +59,9 @@ public class PictosAction extends Action { ...@@ -59,6 +59,9 @@ public class PictosAction extends Action {
return null; return null;
} }
} }
public boolean is_local_action() {return false;}
private JSONObject get_json_picto(Picto picto) throws JSONException { private JSONObject get_json_picto(Picto picto) throws JSONException {
final String param_id_json="id"; final String param_id_json="id";
final String param_picto="picto"; final String param_picto="picto";
......
...@@ -54,20 +54,23 @@ public class Room { ...@@ -54,20 +54,23 @@ public class Room {
} }
public void emit(final Action action) { public void emit(final Action action) {
String token=PCBcontext.getRestapiWrapper().getToken(); if (!action.is_local_action()) {
final String token_param="token"; String token = PCBcontext.getRestapiWrapper().getToken();
Log.i(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString()); final String token_param = "token";
try{ Log.i(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString());
try {
this.socket.emit(action.get_action(), this.common_data(action.get_type(), action.get_json()).put(token_param, token), new Ack() { this.socket.emit(action.get_action(), this.common_data(action.get_type(), action.get_json()).put(token_param, token), new Ack() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
for (Object arg : args) for (Object arg : args)
Log.d(this.getClass().getName(), "Ack messsage:" + arg); Log.d(this.getClass().getName(), "Ack messsage:" + arg);
} }
}); });
} catch (JSONException e) { } catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage()); Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage());
}
} }
else Log.i(this.getClass().getCanonicalName(),"Local Action "+action.get_action()+" is not emitted");
} }
/** /**
......
...@@ -20,12 +20,15 @@ public class SubscribeAction extends Action { ...@@ -20,12 +20,15 @@ public class SubscribeAction extends Action {
public static final String ACTION="/stu/subscribe"; public static final String ACTION="/stu/subscribe";
@Override
public boolean is_local_action() {
return false;
}
public SubscribeAction( ){
public SubscribeAction( ){
super(SUBSCRIBE); super(SUBSCRIBE);
} }
public String get_action() {return ACTION;} public String get_action() {return ACTION;}
} }
\ No newline at end of file
...@@ -24,6 +24,10 @@ public class UnsubscribeAction extends Action { ...@@ -24,6 +24,10 @@ public class UnsubscribeAction extends Action {
super(UNSUBSCRIBE); super(UNSUBSCRIBE);
} }
@Override
public boolean is_local_action() {
return false;
}
public String get_action() {return ACTION;} public String get_action() {return ACTION;}
} }
...@@ -82,15 +82,7 @@ public class Picto extends Img { ...@@ -82,15 +82,7 @@ public class Picto extends Img {
* @return true if it's a local pictogram * @return true if it's a local pictogram
*/ */
public boolean is_local() {return this.get_id()<0;} public boolean is_local() {return this.get_id()<0;}
/**
*
* @return de id of the picto
*/
public int get_id() {return this.id;}
public void update_id(int id) {
this.id = id;
}
/** /**
* *
* @return the location of the picto * @return the location of the picto
......
...@@ -142,12 +142,6 @@ public class NetService implements Runnable { ...@@ -142,12 +142,6 @@ public class NetService implements Runnable {
} }
public void notifyStatus() { public void notifyStatus() {
String user;
if (PCBcontext.getPcbdb()!=null) {
user=PCBcontext.getPcbdb().getCurrentUser().get_name_stu();
if (PCBcontext.getPcbdb().getCurrentUser().is_supervisor())
user += " (" + PCBcontext.getPcbdb().getCurrentUser().get_name_sup() + ")";
}
device.notifyStatus(this.updated); device.notifyStatus(this.updated);
} }
public void closeNotifyStatus(){ public void closeNotifyStatus(){
......
...@@ -40,8 +40,8 @@ public class PictoUploader { ...@@ -40,8 +40,8 @@ public class PictoUploader {
this.picto=picto; this.picto=picto;
} }
private int uploadImg( Img img) throws UnsupportedEncodingException { private boolean uploadImg( Img img) throws UnsupportedEncodingException {
int img_id; boolean success;
Bitmap bmp=null; Bitmap bmp=null;
Response<JsonObject> response=null; Response<JsonObject> response=null;
if (!img.get_filetype().equalsIgnoreCase("png")) if (!img.get_filetype().equalsIgnoreCase("png"))
...@@ -72,9 +72,13 @@ public class PictoUploader { ...@@ -72,9 +72,13 @@ public class PictoUploader {
if (response != null && response.getHeaders().code() == 200) { if (response != null && response.getHeaders().code() == 200) {
Log.i(this.getClass().getCanonicalName(), "Uploaded image result: " + response.getHeaders() + ":" + response.getResult()); Log.i(this.getClass().getCanonicalName(), "Uploaded image result: " + response.getHeaders() + ":" + response.getResult());
img_id=response.getResult().get("id").getAsInt(); int img_id=response.getResult().get("id").getAsInt();
String img_uri=response.getResult().get("uri").getAsString();
img.set_url(img_uri);
img.update_id(img_id);
success=true;
} else { } else {
img_id=-1; success=false;
Log.i(this.getClass().getCanonicalName(), "Uploaded image failed "); Log.i(this.getClass().getCanonicalName(), "Uploaded image failed ");
if (response != null) if (response != null)
Log.i(this.getClass().getCanonicalName(), "Uploaded image failed, headers: " + response.getHeaders()); Log.i(this.getClass().getCanonicalName(), "Uploaded image failed, headers: " + response.getHeaders());
...@@ -82,19 +86,19 @@ public class PictoUploader { ...@@ -82,19 +86,19 @@ public class PictoUploader {
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage()+ "Code: "+ Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage()+ "Code: "+
(response == null ? -1 : response.getHeaders().code())); (response == null ? -1 : response.getHeaders().code()));
img_id=-1; success=false;
} catch (ExecutionException e) { } catch (ExecutionException e) {
Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage()+ Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage()+
(response == null ? -1 : response.getHeaders().code())); (response == null ? -1 : response.getHeaders().code()));
img_id=-1; success=false;
} catch (IOException e) { } catch (IOException e) {
Log.e(Img.class.getCanonicalName(), "Error when decoding "+img.file_name()); Log.e(Img.class.getCanonicalName(), "Error when decoding "+img.file_name());
GUITools.show_alert(PCBcontext.getContext(), R.string.imguserLoadingErrMsg); GUITools.show_alert(PCBcontext.getContext(), R.string.imguserLoadingErrMsg);
img_id=-1; success=false;
} }
// ion.dump(); // ion.dump();
return img_id; return success;
} }
...@@ -186,7 +190,7 @@ public class PictoUploader { ...@@ -186,7 +190,7 @@ public class PictoUploader {
**/ **/
public void upload(final Context context) throws IOException { public void upload(final Context context) throws IOException {
final int local_img_id = this.picto.get_id(); final int local_img_id = this.picto.get_id();
final int img_id = uploadImg(this.picto); final boolean imgUpload_success = uploadImg(this.picto);
iPictoUploaderListener listener = new iPictoUploaderListener() { iPictoUploaderListener listener = new iPictoUploaderListener() {
int elements_uploaded = 0; int elements_uploaded = 0;
...@@ -199,15 +203,14 @@ public class PictoUploader { ...@@ -199,15 +203,14 @@ public class PictoUploader {
if (elements_uploaded == 2) { if (elements_uploaded == 2) {
PCBcontext.getPcbdb().deletePicto(local_img_id); PCBcontext.getPcbdb().deletePicto(local_img_id);
PictoUploader.this.picto.delete_bitmap(PCBcontext.getContext()); PictoUploader.this.picto.delete_bitmap(PCBcontext.getContext());
PictoUploader.this.picto.update_id(img_id);
PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ADD, PictoUploader.this.picto)); PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ADD, PictoUploader.this.picto));
GUITools.show_alert(context, R.string.upload_ok,PictoUploader.this.picto.get_translation()+":"+PictoUploader.this.picto.get_id()); GUITools.show_alert(context, R.string.upload_ok,PictoUploader.this.picto.get_translation());
} }
} }
}; };
if (img_id > 0) { if (imgUpload_success) {
uploadAttributes(img_id, listener); uploadAttributes(picto.get_id(), listener);
uploadTranslation(img_id, listener); uploadTranslation(picto.get_id(), listener);
} }
else { else {
GUITools.show_alert(context, R.string.upload_error, PictoUploader.this.picto.get_translation()); GUITools.show_alert(context, R.string.upload_error, PictoUploader.this.picto.get_translation());
......
...@@ -66,6 +66,10 @@ public class Img { ...@@ -66,6 +66,10 @@ public class Img {
public String get_type() { return this.type;} public String get_type() { return this.type;}
public String get_filetype() { return Img.FILETYPE;} public String get_filetype() { return Img.FILETYPE;}
public void update_id(int id) {
this.id = id;
}
/** /**
* Load if necessary the bitmap from disk, and it is returned. IIf it is not available, return null * Load if necessary the bitmap from disk, and it is returned. IIf it is not available, return null
* @param context * @param context
......
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