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

parent 01353467
......@@ -40,13 +40,13 @@ import javax.net.ssl.HttpsURLConnection;
*/
/**
* Modificado por: magc
*
* LastUpdate: 22 de enero de 2016
*/
public class RestapiWrapper {
String server;
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_ERROR="error";
......
......@@ -334,11 +334,8 @@ public class Device extends SQLiteOpenHelper {
while (cursor.moveToNext()) {
String type = cursor.getString(1);
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);
} catch (IOException e) {
e.printStackTrace();
}
(new Img(cursor.getInt(0), null, folder)).delete_bitmap(this.context);
Log.i(this.getClass().getCanonicalName(), "Image file " + cursor.getString(1) + "." + cursor.getInt(0) + " deleted");
}
cursor.close();
......@@ -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.moveToFirst();
next_key=cursor.getInt(cursor.getColumnIndex("MIN"))-1;
if (next_key>0) next_key=-1;
if (next_key>=0) next_key=-1;
cursor.close();
db.close();
......
......@@ -6,6 +6,7 @@ package com.yottacode.pictogram.dao;
public class LoginException extends Exception{
public static final int BAD_LOGIN=1;
public static final int NO_STUDENTS=2;
public static final int TIME_OUT=3;
int code;
public LoginException(String msg, int code) {
super(msg);
......@@ -15,6 +16,7 @@ public class LoginException extends Exception{
public boolean login_failed() {return this.code==LoginException.BAD_LOGIN;}
public boolean no_supervisor_students() {return this.code==LoginException.NO_STUDENTS;}
public boolean time_out() {return this.code==LoginException.TIME_OUT;}
public String getLocalizedMessage() {
return super.getLocalizedMessage()+" Login ok:"+!login_failed()+" Supervisor without students:"+no_supervisor_students();
......
......@@ -88,6 +88,9 @@ public class Picto extends Img {
*/
public int get_id() {return this.id;}
public void update_id(int id) {
this.id = id;
}
/**
*
* @return the location of the picto
......
......@@ -122,11 +122,11 @@ public class Vocabulary implements Iterable<Picto> {
Log.i(this.getClass().getCanonicalName(), "Picto status modified while offline. Picto translation: '" +
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 {
Log.i(this.getClass().getCanonicalName(), "Picto added while offline. Picto translation: '" +
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) {
e.printStackTrace();
Log.e(this.getClass().getName(), " Picto json error from server: " + picto.toString());
......
......@@ -604,7 +604,8 @@ public class PictogramActivity extends Activity implements iVocabularyListener,
Log.d(LOG_TAG, "No tengo pictograma. Abro carrete...");
int cols = getResources().getInteger(R.integer.columns);
addPicto(position % cols, (int) (position / cols));
} else
}
else
Toast.makeText(PictogramActivity.this, PictogramActivity.this.getResources().getString(R.string.notNewCats), Toast.LENGTH_SHORT).show();
} else {
......@@ -794,7 +795,7 @@ public class PictogramActivity extends Activity implements iVocabularyListener,
public void saved(Picto localPicto) {
PictogramActivity.this.refresh();
try {
new PictoUploader(localPicto).upload();
new PictoUploader(localPicto).upload(PictogramActivity.this);
} catch (IOException e) {
Log.e(Vocabulary.class.getCanonicalName(), e.getMessage());
}
......
......@@ -120,6 +120,8 @@ public class SerialActivity extends Activity {
GUITools.show_alert(SerialActivity.this, R.string.loginErrorMsg);
else if (((LoginException)e).no_supervisor_students())
GUITools.show_alert(SerialActivity.this, R.string.noStudentsError);
else
GUITools.show_alert(SerialActivity.this, R.string.serverError, e.getMessage());
else
GUITools.show_alert(SerialActivity.this, R.string.serverError, e.getMessage());
}
......@@ -215,6 +217,8 @@ public class SerialActivity extends Activity {
if (e instanceof LoginException)
if (((LoginException)e).login_failed())
GUITools.show_alert(SerialActivity.this, R.string.loginErrorMsg);
else
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());
......
package com.yottacode.pictogram.net;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
......@@ -14,6 +15,7 @@ import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
import com.yottacode.tools.BitmapTools;
import com.yottacode.tools.GUITools;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -25,11 +27,14 @@ import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.concurrent.ExecutionException;
/**
* Created by Fernando on 02/03/2016.
*/
public class PictoUploader {
Picto picto=null;
public PictoUploader(Picto picto) {
this.picto=picto;
}
......@@ -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());
......@@ -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);
try {
JSONObject json_attrs =new JSONObject().put("status",picto.get_status());
......@@ -126,12 +131,14 @@ public class PictoUploader {
@Override
public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), " Attributes uploaded: " + result.toString());
listener.success(true);
}
@Override
public void error(Exception e) {
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 {
/**
* 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);
params.put("picto", Integer.toString(id_picto));
......@@ -156,13 +163,14 @@ public class PictoUploader {
@Override
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
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"));
PCBcontext.getVocabulary().addPicto(picto, ImgDownloader.tsource.local);
listener.success(false);
}
});
}
......@@ -174,18 +182,37 @@ public class PictoUploader {
* iii) to upload the expression
*
**/
public void upload() throws IOException {
int local_img_id = this.picto.get_id();
int img_id = uploadImg(this.picto);
public void upload(final Context context) throws IOException {
final int local_img_id = this.picto.get_id();
final int img_id = uploadImg(this.picto);
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);
PictoUploader.this.picto.delete_bitmap(PCBcontext.getContext());
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) {
PCBcontext.getPcbdb().deletePicto(local_img_id);
uploadAttributes(img_id);
uploadTranslation(img_id);
PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ADD, PictoUploader.this.picto));
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
*
......
......@@ -62,7 +62,9 @@ public class ServerLogin {
public void error(Exception e) {
listener.error(new LoginException(e.getMessage(),e.getMessage().contains("User without students")
? LoginException.NO_STUDENTS
: LoginException.BAD_LOGIN));
: e.getMessage().contains("failed to connect")
? LoginException.TIME_OUT
: 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 {
/**
* Delete the bitmap from disk
* @param context
* @throws FileNotFoundException
*/
public void delete_bitmap(Context context) throws IOException {
public void delete_bitmap(Context context) {
File file = file(context);
file.delete();
}
......
......@@ -52,6 +52,8 @@
<!--Upload local img -->
<string name="enterImgLabel">Enter img label</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-->
<string name="pictogram_offline">Pictogram offline</string>
<string name="pictogram_online">Pictogram online</string>
......
......@@ -54,6 +54,8 @@
<!--Upload local img -->
<string name="enterImgLabel">Introduzca etiqueta de la imagen</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-->
<string name="pictogram_offline">Pictogram offline</string>
<string name="pictogram_online">Pictogram online</string>
......
......@@ -55,6 +55,8 @@
<!--Upload local img -->
<string name="enterImgLabel">Introduzca etiqueta de la imagen</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>
<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