Working on image upload (iv)

parent 936c5734
...@@ -37,7 +37,7 @@ android { ...@@ -37,7 +37,7 @@ android {
debug { debug {
resValue "string", "db_name", "PCB.db" resValue "string", "db_name", "PCB.db"
resValue "bool", "force_db_create", "false" resValue "bool", "force_db_create", "true"
resValue "bool", "ssl_connect", "false" resValue "bool", "ssl_connect", "false"
resValue "bool", "force_img_download", "false" resValue "bool", "force_img_download", "false"
resValue "integer", "netservice_timing", "20" resValue "integer", "netservice_timing", "20"
......
package com.yottacode.pictogram.grammar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.yottacode.pictogram.R;
import org.grammaticalframework.PGF;
import org.grammaticalframework.Linearizer;
import org.grammaticalframework.PGFBuilder;
import org.grammaticalframework.Parser;
import org.grammaticalframework.UnknownLanguageException;
import org.grammaticalframework.parser.ParseState;
import org.grammaticalframework.Trees.Absyn.Tree;
import java.io.InputStream;
public class SemanticGrammar
{
protected Activity activity;
private PGF mPGF;
Parser mParser;
Linearizer mLinearizer;
// String constant for logs
private final String LOG_TAG = this.getClass().getSimpleName(); // Or .getCanonicalName()
public void SemanticGrammar(Activity activity) {
this.activity=activity;
new LoadPGFTask().execute();
}
/**
* This class is used to load the PGF file asychronously.
* It display a blocking progress dialog while doing so.
*/
private class LoadPGFTask extends AsyncTask<Void, Void, PGF> {
private ProgressDialog progress;
protected void onPreExecute() {
// Display loading popup
this.progress =
ProgressDialog.show(activity, activity.getResources().getString(R.string.app_name)
,activity.getResources().getString(R.string.loadingGrammar),true);
}
protected PGF doInBackground(Void... a) {
int pgf_res = R.raw.supo;
InputStream is = activity.getResources().openRawResource(pgf_res);
try {
PGF pgf = PGFBuilder.fromInputStream(is, new String[] {activity.getString(R.string.pictogrammar),activity.getString(R.string.naturalgrammar)});
return pgf;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected void onPostExecute(PGF result) {
mPGF = result;
if (this.progress != null) {
this.progress.dismiss(); // Remove loading popup
try {
mParser=new Parser(mPGF, activity.getString(R.string.pictogrammar));
mLinearizer = new Linearizer(mPGF, activity.getString(R.string.naturalgrammar));
} catch (Exception e) {
Log.e(this.getClass().getSimpleName(), e.getMessage());
Toast.makeText(activity.getApplicationContext(), activity.getResources().getString(R.string.nogrammar), Toast.LENGTH_SHORT).show();
}
}
}
protected String[] translate(String[] tokens) {
try {
//parsing the tokens
ParseState mParseState = mParser.parse(tokens);
Tree[] trees = (Tree[])mParseState.getTrees();
String[] translations = new String[trees.length];
// Linearizing all the trees
for (int i = 0 ; i < trees.length ; i++) {
try {
String t = mLinearizer.linearizeString(trees[i]);
translations[i] = t;
} catch (java.lang.Exception e) {
translations[i] = "/!\\ Linearization error";
}
}
return translations;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected String[] predict(String[] tokens) {
try {
//parsing the tokens
ParseState mParseState = mParser.parse(tokens);
return mParseState.predict();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
\ No newline at end of file
...@@ -68,7 +68,7 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> { ...@@ -68,7 +68,7 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
if (!img.exists_bitmap(this.context) || this.force_download) try { if (!img.exists_bitmap(this.context) || this.force_download) try {
this.activityManager.getMemoryInfo(mi); this.activityManager.getMemoryInfo(mi);
Log.i(this.getClass().getCanonicalName(), img.get_url() + " to be downloaded");
if (this.source==source.remote) { if (this.source==source.remote) {
String surl = context.getResources().getString(R.string.server) + "/" + img.get_url(); String surl = context.getResources().getString(R.string.server) + "/" + img.get_url();
URL url = new URL(surl); URL url = new URL(surl);
......
...@@ -13,6 +13,7 @@ import com.yottacode.pictogram.tools.Img; ...@@ -13,6 +13,7 @@ import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext; import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File; import java.io.File;
...@@ -118,13 +119,17 @@ public class PictoUploader { ...@@ -118,13 +119,17 @@ 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 uploadAttributes(int id_picto) { private int uploadAttributes(int id_picto) {
final int id_stupicto[]=new int[1];
Hashtable<String, String> params = new Hashtable<String, String>(4); Hashtable<String, String> params = new Hashtable<String, String>(4);
params.put("uri", Integer.toString(uri)); try {
params.put("id_cat", Integer.toString(picto.get_category())); params.put("attributes", new JSONObject().put("id_cat", Integer.toString(picto.get_category()))
params.put("coord_x", Integer.toString(picto.get_column())); .put("coord_x", Integer.toString(picto.get_column()))
params.put("coord_y", Integer.toString(picto.get_row())); .put("coord_y", Integer.toString(picto.get_row())).toString());
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto", params, "post", new iRestapiListener() { } catch (JSONException e) {
e.printStackTrace();
}
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto/"+id_picto, params, "post", new iRestapiListener() {
@Override @Override
public void preExecute() { public void preExecute() {
} }
...@@ -137,22 +142,30 @@ public class PictoUploader { ...@@ -137,22 +142,30 @@ 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());
try {
id_stupicto[0]=result.getJSONObject("picto").getInt("id");
} catch (JSONException e) {
e.printStackTrace();
}
} }
@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());
id_stupicto[0]=-1;
} }
}); });
return id_stupicto[0];
} }
/** /**
* 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 remoteid_picto) { private void uploadTranslation( int id_stupicto) {
Hashtable<String, String> params = new Hashtable<String, String>(3); Hashtable<String, String> params = new Hashtable<String, String>(3);
params.put("picto", Integer.toString(remoteid_picto)); params.put("picto", Integer.toString(id_stupicto));
params.put("lang", PCBcontext.getPcbdb().getCurrentUser().get_lang_stu()); params.put("lang", PCBcontext.getPcbdb().getCurrentUser().get_lang_stu());
params.put("text", picto.get_translation()); params.put("text", picto.get_translation());
PCBcontext.getRestapiWrapper().ask("picto/exp", params, "post", new iRestapiListener() { PCBcontext.getRestapiWrapper().ask("picto/exp", params, "post", new iRestapiListener() {
...@@ -180,11 +193,10 @@ public class PictoUploader { ...@@ -180,11 +193,10 @@ public class PictoUploader {
*Upload local picto. It requires: i) to upload the image, ii) to upload the attributes and iii) to upload the expression *Upload local picto. It requires: i) to upload the image, ii) to upload the attributes and iii) to upload the expression
**/ **/
public void upload() throws IOException { public void upload() throws IOException {
int img_id=uploadImg(this.picto); int img_id=uploadImg(this.picto);
if (img_id>0) { if (img_id>0) {
uploadAttributes(img_id); int stupicto_id=uploadAttributes(img_id);
uploadTranslation(img_id); uploadTranslation(stupicto_id);
} }
} }
......
...@@ -53,6 +53,7 @@ public class Img { ...@@ -53,6 +53,7 @@ public class Img {
public int get_id() { return this.id;} public int get_id() { return this.id;}
public String get_url() { return this.url;} public String get_url() { return this.url;}
public void set_url(String url) {this.url=url;}
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;}
...@@ -125,7 +126,7 @@ public class Img { ...@@ -125,7 +126,7 @@ public class Img {
Log.e(Img.class.getCanonicalName(), "Out of memory when decoding "+this.get_url()); Log.e(Img.class.getCanonicalName(), "Out of memory when decoding "+this.get_url());
} }
ByteArrayOutputStream outstream = new ByteArrayOutputStream(); ByteArrayOutputStream outstream = new ByteArrayOutputStream();
this.bitmap.setHasAlpha(true); ls -l this.bitmap.setHasAlpha(true);
this.bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream); this.bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream);
byte[] byteArray = outstream.toByteArray(); byte[] byteArray = outstream.toByteArray();
os.write(byteArray); os.write(byteArray);
......
...@@ -512,7 +512,7 @@ module.exports = { ...@@ -512,7 +512,7 @@ module.exports = {
// //
add_picto: function(req, res) { add_picto: function(req, res) {
var params = req.allParams(); var params = req.allParams();
console.log(JSON.stringify(params)); console.log("Adding picto "+params.id_picto+" atts:"+params.attributes);
StuPicto.create({ StuPicto.create({
student: params.id_stu, student: params.id_stu,
picto: params.id_picto, picto: params.id_picto,
......
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