bug #509, #510, #512, #507, #498 fixed. Working on #503

parent 01353467
...@@ -40,13 +40,13 @@ import javax.net.ssl.HttpsURLConnection; ...@@ -40,13 +40,13 @@ import javax.net.ssl.HttpsURLConnection;
*/ */
/** /**
* Modificado por: magc *
* LastUpdate: 22 de enero de 2016 * LastUpdate: 22 de enero de 2016
*/ */
public class RestapiWrapper { public class RestapiWrapper {
String server; String server;
String token; String token;
public static final int TIME_OUT=10000; public static final int TIME_OUT=20000;
private static final String SERVER_RESULT="result"; private static final String SERVER_RESULT="result";
private static final String SERVER_ERROR="error"; private static final String SERVER_ERROR="error";
......
...@@ -334,11 +334,8 @@ public class Device extends SQLiteOpenHelper { ...@@ -334,11 +334,8 @@ public class Device extends SQLiteOpenHelper {
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
String type = cursor.getString(1); String type = cursor.getString(1);
String folder = type.equals("stu") ? Img.STUDENT : type.equals("sup") ? Img.SUPERVISOR : Img.VOCABULARY; String folder = type.equals("stu") ? Img.STUDENT : type.equals("sup") ? Img.SUPERVISOR : Img.VOCABULARY;
try {
(new Img(cursor.getInt(0), null, folder)).delete_bitmap(this.context); (new Img(cursor.getInt(0), null, folder)).delete_bitmap(this.context);
} catch (IOException e) {
e.printStackTrace();
}
Log.i(this.getClass().getCanonicalName(), "Image file " + cursor.getString(1) + "." + cursor.getInt(0) + " deleted"); Log.i(this.getClass().getCanonicalName(), "Image file " + cursor.getString(1) + "." + cursor.getInt(0) + " deleted");
} }
cursor.close(); cursor.close();
...@@ -400,7 +397,7 @@ public class Device extends SQLiteOpenHelper { ...@@ -400,7 +397,7 @@ public class Device extends SQLiteOpenHelper {
Cursor cursor = db.query("picto", new String[]{"(SELECT MIN(id) FROM picto) AS MIN"}, null, null, null, null, null, "1"); Cursor cursor = db.query("picto", new String[]{"(SELECT MIN(id) FROM picto) AS MIN"}, null, null, null, null, null, "1");
cursor.moveToFirst(); cursor.moveToFirst();
next_key=cursor.getInt(cursor.getColumnIndex("MIN"))-1; next_key=cursor.getInt(cursor.getColumnIndex("MIN"))-1;
if (next_key>0) next_key=-1; if (next_key>=0) next_key=-1;
cursor.close(); cursor.close();
db.close(); db.close();
......
...@@ -6,6 +6,7 @@ package com.yottacode.pictogram.dao; ...@@ -6,6 +6,7 @@ package com.yottacode.pictogram.dao;
public class LoginException extends Exception{ public class LoginException extends Exception{
public static final int BAD_LOGIN=1; public static final int BAD_LOGIN=1;
public static final int NO_STUDENTS=2; public static final int NO_STUDENTS=2;
public static final int TIME_OUT=3;
int code; int code;
public LoginException(String msg, int code) { public LoginException(String msg, int code) {
super(msg); super(msg);
...@@ -15,6 +16,7 @@ public class LoginException extends Exception{ ...@@ -15,6 +16,7 @@ public class LoginException extends Exception{
public boolean login_failed() {return this.code==LoginException.BAD_LOGIN;} public boolean login_failed() {return this.code==LoginException.BAD_LOGIN;}
public boolean no_supervisor_students() {return this.code==LoginException.NO_STUDENTS;} public boolean no_supervisor_students() {return this.code==LoginException.NO_STUDENTS;}
public boolean time_out() {return this.code==LoginException.TIME_OUT;}
public String getLocalizedMessage() { public String getLocalizedMessage() {
return super.getLocalizedMessage()+" Login ok:"+!login_failed()+" Supervisor without students:"+no_supervisor_students(); return super.getLocalizedMessage()+" Login ok:"+!login_failed()+" Supervisor without students:"+no_supervisor_students();
......
...@@ -88,6 +88,9 @@ public class Picto extends Img { ...@@ -88,6 +88,9 @@ public class Picto extends Img {
*/ */
public int get_id() {return this.id;} 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
......
...@@ -122,11 +122,11 @@ public class Vocabulary implements Iterable<Picto> { ...@@ -122,11 +122,11 @@ public class Vocabulary implements Iterable<Picto> {
Log.i(this.getClass().getCanonicalName(), "Picto status modified while offline. Picto translation: '" + Log.i(this.getClass().getCanonicalName(), "Picto status modified while offline. Picto translation: '" +
picto.get_translation() + "', id:" + picto.get_id() + " Local status?" + picto.local_status()); picto.get_translation() + "', id:" + picto.get_id() + " Local status?" + picto.local_status());
} }
if (picto.get_id() < 0) //id<0 iif it is a local id if (picto.is_local()) //id<0 iif it is a local id
try { try {
Log.i(this.getClass().getCanonicalName(), "Picto added while offline. Picto translation: '" + Log.i(this.getClass().getCanonicalName(), "Picto added while offline. Picto translation: '" +
picto.get_translation() + "', id:" + picto.get_id() + " Local status?" + picto.local_status()); picto.get_translation() + "', id:" + picto.get_id() + " Local status?" + picto.local_status());
new PictoUploader(picto).upload(); new PictoUploader(picto).upload(PCBcontext.getContext());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
Log.e(this.getClass().getName(), " Picto json error from server: " + picto.toString()); Log.e(this.getClass().getName(), " Picto json error from server: " + picto.toString());
......
...@@ -604,7 +604,8 @@ public class PictogramActivity extends Activity implements iVocabularyListener, ...@@ -604,7 +604,8 @@ public class PictogramActivity extends Activity implements iVocabularyListener,
Log.d(LOG_TAG, "No tengo pictograma. Abro carrete..."); Log.d(LOG_TAG, "No tengo pictograma. Abro carrete...");
int cols = getResources().getInteger(R.integer.columns); int cols = getResources().getInteger(R.integer.columns);
addPicto(position % cols, (int) (position / cols)); addPicto(position % cols, (int) (position / cols));
} else }
else
Toast.makeText(PictogramActivity.this, PictogramActivity.this.getResources().getString(R.string.notNewCats), Toast.LENGTH_SHORT).show(); Toast.makeText(PictogramActivity.this, PictogramActivity.this.getResources().getString(R.string.notNewCats), Toast.LENGTH_SHORT).show();
} else { } else {
...@@ -794,7 +795,7 @@ public class PictogramActivity extends Activity implements iVocabularyListener, ...@@ -794,7 +795,7 @@ public class PictogramActivity extends Activity implements iVocabularyListener,
public void saved(Picto localPicto) { public void saved(Picto localPicto) {
PictogramActivity.this.refresh(); PictogramActivity.this.refresh();
try { try {
new PictoUploader(localPicto).upload(); new PictoUploader(localPicto).upload(PictogramActivity.this);
} catch (IOException e) { } catch (IOException e) {
Log.e(Vocabulary.class.getCanonicalName(), e.getMessage()); Log.e(Vocabulary.class.getCanonicalName(), e.getMessage());
} }
......
...@@ -122,6 +122,8 @@ public class SerialActivity extends Activity { ...@@ -122,6 +122,8 @@ public class SerialActivity extends Activity {
GUITools.show_alert(SerialActivity.this, R.string.noStudentsError); GUITools.show_alert(SerialActivity.this, R.string.noStudentsError);
else else
GUITools.show_alert(SerialActivity.this, R.string.serverError, e.getMessage()); GUITools.show_alert(SerialActivity.this, R.string.serverError, e.getMessage());
else
GUITools.show_alert(SerialActivity.this, R.string.serverError, e.getMessage());
} }
}); });
} else //offline } else //offline
...@@ -217,6 +219,8 @@ public class SerialActivity extends Activity { ...@@ -217,6 +219,8 @@ public class SerialActivity extends Activity {
GUITools.show_alert(SerialActivity.this, R.string.loginErrorMsg); GUITools.show_alert(SerialActivity.this, R.string.loginErrorMsg);
else else
GUITools.show_alert(SerialActivity.this, R.string.serverError, e.getMessage()); GUITools.show_alert(SerialActivity.this, R.string.serverError, e.getMessage());
else
GUITools.show_alert(SerialActivity.this, R.string.serverError, e.getMessage());
Log.e(this.getClass().getCanonicalName(), "Server error:"+ e.getLocalizedMessage()); Log.e(this.getClass().getCanonicalName(), "Server error:"+ e.getLocalizedMessage());
}}); }});
......
package com.yottacode.pictogram.net; package com.yottacode.pictogram.net;
import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.util.Log; import android.util.Log;
...@@ -14,6 +15,7 @@ import com.yottacode.pictogram.dao.Picto; ...@@ -14,6 +15,7 @@ import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.Img; import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext; import com.yottacode.pictogram.tools.PCBcontext;
import com.yottacode.tools.BitmapTools; import com.yottacode.tools.BitmapTools;
import com.yottacode.tools.GUITools;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -25,11 +27,14 @@ import java.io.UnsupportedEncodingException; ...@@ -25,11 +27,14 @@ import java.io.UnsupportedEncodingException;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
/** /**
* Created by Fernando on 02/03/2016. * Created by Fernando on 02/03/2016.
*/ */
public class PictoUploader { public class PictoUploader {
Picto picto=null; Picto picto=null;
public PictoUploader(Picto picto) { public PictoUploader(Picto picto) {
this.picto=picto; this.picto=picto;
} }
...@@ -49,7 +54,7 @@ public class PictoUploader { ...@@ -49,7 +54,7 @@ public class PictoUploader {
} }
Log.i(this.getClass().getCanonicalName(), "Uploading Picto img" + img.file_name() + " from " + img.get_type() + "size:" + bmp.getWidth() + " " + bmp.getHeight()); Log.i(this.getClass().getCanonicalName(), "Uploading Picto img " + img.file_name() + " from " + img.get_type() + "- Size:" + bmp.getWidth() + " " + bmp.getHeight());
File file = img.file(PCBcontext.getContext()); File file = img.file(PCBcontext.getContext());
...@@ -93,9 +98,9 @@ public class PictoUploader { ...@@ -93,9 +98,9 @@ public class PictoUploader {
/** /**
* if the a picto was included from the PCB, the translation is uploaded to the server * if a picto was included from the PCB, the translation is uploaded to the server
*/ */
private void uploadAttributes(int id_picto) { private void uploadAttributes(int id_picto, final iPictoUploaderListener listener) {
Hashtable<String, String> params = new Hashtable<String, String>(4); Hashtable<String, String> params = new Hashtable<String, String>(4);
try { try {
JSONObject json_attrs =new JSONObject().put("status",picto.get_status()); JSONObject json_attrs =new JSONObject().put("status",picto.get_status());
...@@ -126,12 +131,14 @@ public class PictoUploader { ...@@ -126,12 +131,14 @@ public class PictoUploader {
@Override @Override
public void result(JSONObject result) { public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), " Attributes uploaded: " + result.toString()); Log.i(this.getClass().getCanonicalName(), " Attributes uploaded: " + result.toString());
listener.success(true);
} }
@Override @Override
public void error(Exception e) { public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), " Error uploading attributes: " + e.getLocalizedMessage()); Log.e(this.getClass().getCanonicalName(), " Error uploading attributes: " + e.getLocalizedMessage());
PCBcontext.getVocabulary().addPicto(picto, ImgDownloader.tsource.local); listener.success(false);
} }
}); });
} }
...@@ -139,7 +146,7 @@ public class PictoUploader { ...@@ -139,7 +146,7 @@ public class PictoUploader {
/** /**
* if the a picto was included from the PCB, the translation is uploaded to the server * if the a picto was included from the PCB, the translation is uploaded to the server
*/ */
private void uploadTranslation( int id_picto) { private void uploadTranslation( int id_picto, final iPictoUploaderListener listener) {
final Hashtable<String, String> params = new Hashtable<String, String>(3); final Hashtable<String, String> params = new Hashtable<String, String>(3);
params.put("picto", Integer.toString(id_picto)); params.put("picto", Integer.toString(id_picto));
...@@ -157,12 +164,13 @@ public class PictoUploader { ...@@ -157,12 +164,13 @@ public class PictoUploader {
@Override @Override
public void result(JSONObject result) { public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), "Uploaded translation result: " + result.toString()); Log.i(this.getClass().getCanonicalName(), "Uploaded translation result: " + result.toString());
listener.success(true);
} }
@Override @Override
public void error(Exception e) { public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Error uploading translation: " + e.getLocalizedMessage()+" Picto:"+params.get("picto")+" Lang:"+params.get("lang")+" Text:"+params.get("text")); Log.e(this.getClass().getCanonicalName(), "Error uploading translation: " + e.getLocalizedMessage()+" Picto:"+params.get("picto")+" Lang:"+params.get("lang")+" Text:"+params.get("text"));
PCBcontext.getVocabulary().addPicto(picto, ImgDownloader.tsource.local); listener.success(false);
} }
}); });
} }
...@@ -174,17 +182,36 @@ public class PictoUploader { ...@@ -174,17 +182,36 @@ public class PictoUploader {
* iii) to upload the expression * iii) to upload the expression
* *
**/ **/
public void upload() throws IOException { public void upload(final Context context) throws IOException {
int local_img_id = this.picto.get_id(); final int local_img_id = this.picto.get_id();
int img_id = uploadImg(this.picto); final int img_id = uploadImg(this.picto);
if (img_id > 0) { iPictoUploaderListener listener = new iPictoUploaderListener() {
int elements_uploaded = 0;
@Override
public void success(boolean success) {
if (success) elements_uploaded++;
else
GUITools.show_alert(context, R.string.upload_error,PictoUploader.this.picto.get_translation());
if (elements_uploaded == 2) {
PictoUploader.this.picto.update_id(img_id);
PCBcontext.getPcbdb().deletePicto(local_img_id); PCBcontext.getPcbdb().deletePicto(local_img_id);
uploadAttributes(img_id); PictoUploader.this.picto.delete_bitmap(PCBcontext.getContext());
uploadTranslation(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());
}
} }
};
if (img_id > 0) {
uploadAttributes(img_id, listener);
uploadTranslation(img_id, listener);
}
else {
GUITools.show_alert(context, R.string.upload_error, PictoUploader.this.picto.get_translation());
} }
}
/** /**
* if the status of a given picto was modifed from the PCB it is uploaded to the server * if the status of a given picto was modifed from the PCB it is uploaded to the server
......
...@@ -62,6 +62,8 @@ public class ServerLogin { ...@@ -62,6 +62,8 @@ public class ServerLogin {
public void error(Exception e) { public void error(Exception e) {
listener.error(new LoginException(e.getMessage(),e.getMessage().contains("User without students") listener.error(new LoginException(e.getMessage(),e.getMessage().contains("User without students")
? LoginException.NO_STUDENTS ? LoginException.NO_STUDENTS
: e.getMessage().contains("failed to connect")
? LoginException.TIME_OUT
: LoginException.BAD_LOGIN)); : LoginException.BAD_LOGIN));
} }
}); });
......
package com.yottacode.pictogram.net;
/**
* Created by Fernando on 28/07/2016.
*/
public interface iPictoUploaderListener {
void success(boolean success);
}
...@@ -87,9 +87,8 @@ public class Img { ...@@ -87,9 +87,8 @@ public class Img {
/** /**
* Delete the bitmap from disk * Delete the bitmap from disk
* @param context * @param context
* @throws FileNotFoundException
*/ */
public void delete_bitmap(Context context) throws IOException { public void delete_bitmap(Context context) {
File file = file(context); File file = file(context);
file.delete(); file.delete();
} }
......
...@@ -52,6 +52,8 @@ ...@@ -52,6 +52,8 @@
<!--Upload local img --> <!--Upload local img -->
<string name="enterImgLabel">Enter img label</string> <string name="enterImgLabel">Enter img label</string>
<string name="notNewCats">Including new categories is not allowed</string> <string name="notNewCats">Including new categories is not allowed</string>
<string name="upload_error">It cannot be uploaded. It will be tried again later</string>
<string name="upload_ok">It was correctly uploaded</string>
<!--online/offline status--> <!--online/offline status-->
<string name="pictogram_offline">Pictogram offline</string> <string name="pictogram_offline">Pictogram offline</string>
<string name="pictogram_online">Pictogram online</string> <string name="pictogram_online">Pictogram online</string>
......
...@@ -54,6 +54,8 @@ ...@@ -54,6 +54,8 @@
<!--Upload local img --> <!--Upload local img -->
<string name="enterImgLabel">Introduzca etiqueta de la imagen</string> <string name="enterImgLabel">Introduzca etiqueta de la imagen</string>
<string name="notNewCats">No puede añadir nuevas categorias</string> <string name="notNewCats">No puede añadir nuevas categorias</string>
<string name="upload_error">Pictograma local no pudo subirse al servidor. Se intentará más tarde</string>
<string name="upload_ok">Pictograma local se subió correctamente al servidor</string>
<!--online/offline status--> <!--online/offline status-->
<string name="pictogram_offline">Pictogram offline</string> <string name="pictogram_offline">Pictogram offline</string>
<string name="pictogram_online">Pictogram online</string> <string name="pictogram_online">Pictogram online</string>
......
...@@ -55,6 +55,8 @@ ...@@ -55,6 +55,8 @@
<!--Upload local img --> <!--Upload local img -->
<string name="enterImgLabel">Introduzca etiqueta de la imagen</string> <string name="enterImgLabel">Introduzca etiqueta de la imagen</string>
<string name="notNewCats">No puede añadir nuevas categorias</string> <string name="notNewCats">No puede añadir nuevas categorias</string>
<string name="upload_error">No pudo subirse al servidor. Se intentará más adelante</string>
<string name="upload_ok">Se subió correctamente al servidor</string>
<item name="maxInTape" type="integer">8</item> <item name="maxInTape" type="integer">8</item>
<string name="title_activity_img_label">img_label</string> <string name="title_activity_img_label">img_label</string>
......
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