Commit 8ed0bb6e by Fernando Martínez Santiago

Merge branch 'master' of http://scm.ujaen.es/softuno/pictogram

parents cce62862 3a6eb881
......@@ -116,47 +116,50 @@ public class RestapiWrapper {
public static boolean ping(String server, String ping_op, iRestapiListener error_listener) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
boolean pingResult = GET(server + "/" + ping_op, null, error_listener)!=null;
boolean pingResult = false;
try {
pingResult = GET(server + "/" + ping_op, null)!=null;
} catch (IOException e) {
e.printStackTrace();
Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "ping failed at"+ping_op);
error_listener.error(e);
}
return pingResult;
}
public static String GET(String surl, Hashtable<String, String> params, iRestapiListener listener) {
public static String GET(String surl, Hashtable<String, String> params) throws IOException {
String result=null;
InputStream inputStream = null;
URL url = null;
try {
if (params!=null) {
if (params!=null) {
surl += '?';
for (String param : params.keySet()) {
String value = params.get(param);
surl += param + '=' + value + '&';
}
surl=surl.substring(0,surl.length()-1);
}
url = new URL(surl);
HttpURLConnection urlConnection = null;
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(60000);
urlConnection.setConnectTimeout(60000);
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
inputStream = urlConnection.getInputStream();
}
url = new URL(surl);
HttpURLConnection urlConnection = null;
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(60000);
urlConnection.setConnectTimeout(60000);
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
inputStream = urlConnection.getInputStream();
// convert inputstream to string
if (inputStream!=null) result = convertInputStreamToString(inputStream);
} catch (IOException e) {
if (listener!=null) listener.error(e);
result=e.getMessage();
}
if (inputStream!=null) result = convertInputStreamToString(inputStream);
return result;
}
public String POST(String surl, String request_method, Hashtable<String, String> params, boolean json_params) {
public String POST(String surl, String request_method, Hashtable<String, String> params, boolean json_params) throws IOException {
URL url = null;
String response = "";
try {
url = new URL(surl);
HttpURLConnection urlConnection = null;
......@@ -205,10 +208,6 @@ public class RestapiWrapper {
while ((line=br.readLine()) != null) {
response+=line;
}
} catch (IOException e) {
Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "Error:" + e.getLocalizedMessage() + " when asking for " + surl);
response=e.getMessage();
}
return response;
}
......@@ -232,6 +231,7 @@ public class RestapiWrapper {
protected boolean json_params;
protected iRestapiListener listener;
protected String result;
protected Exception error;
}
private class HttpAsyncTask extends AsyncTask<HttpAsyncTaskParams, Void, HttpAsyncTaskParams> {
......@@ -239,9 +239,15 @@ public class RestapiWrapper {
@Override
protected HttpAsyncTaskParams doInBackground(HttpAsyncTaskParams... params) {
params[0].result = params[0].request_method.equalsIgnoreCase("GET")
? GET(params[0].url, params[0].url_params, null)
: POST(params[0].url, params[0].request_method, params[0].url_params, params[0].json_params);
try {
params[0].result = params[0].request_method.equalsIgnoreCase("GET")
? GET(params[0].url, params[0].url_params)
: POST(params[0].url, params[0].request_method, params[0].url_params, params[0].json_params);
} catch (IOException e) {
Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "Error:" + e.getLocalizedMessage() + " when asking for " + params[0].url);
params[0].result=null;
params[0].error=e;
}
return params[0];
}
......@@ -249,7 +255,8 @@ public class RestapiWrapper {
@Override
protected void onPostExecute(HttpAsyncTaskParams params) {
try {
//if (params.e)
if (params.error!=null) params.listener.error(params.error);
else
if(params.result!=null) {
Log.i(LOG_TAG, "Picto JSON Result: " + params.result);
......
......@@ -35,7 +35,8 @@ public class ActionLog implements iRestapiListener {
*/
public void log(Action action) {
if (PCBcontext.getRoom().inRoom())
PCBcontext.getRoom().emit(action.get_action(), action);
// PCBcontext.getRoom().emit(action.get_action(), action);
PCBcontext.getRoom().emit(action);
else {
// If there is no room, the action is stored in local DB
......
......@@ -49,7 +49,7 @@ public class Room {
final String attributes_param="attributes";
return new JSONObject().put(action_param, action).put(attributes_param, attributes);
}
public void emit(String url, final Action action) {
/* public void emit(String url, final Action action) {
Log.i(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString());
try{
this.socket.emit(url, this.common_data(action.get_type(), action.get_json()), new Ack() {
......@@ -63,8 +63,9 @@ public class Room {
} catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage());
}
}
public void emit(final Action action, String token) {
}*/
public void emit(final Action action) {
String token=PCBcontext.getRestapiWrapper().getToken();
final String token_param="token";
Log.d(this.getClass().getName(), "Action: " + action.get_type() + " / Attributes emitted: " + action.get_json().toString());
try{
......@@ -117,7 +118,7 @@ public class Room {
void subscribe() {
try {
SubscribeAction action = new SubscribeAction();
this.emit(action, PCBcontext.getRestapiWrapper().getToken());
this.emit(action);
}catch (Exception e) {
Log.e(this.getClass().getCanonicalName(), e.getMessage());
}
......
......@@ -11,6 +11,7 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Vector;
......@@ -266,7 +267,7 @@ public class PCBDBHelper extends SQLiteOpenHelper {
ContentValues values = new ContentValues(1);
values.put("attributes",attrs);
int updates=db.update("collection",values, "id_stu=? AND id_picto=?", new String[] {Integer.toString(id_stu), Integer.toString(picto_id)});
Log.i(this.getClass().getCanonicalName(),"Modify "+updates+" Picto, id. "+picto_id+" attributes="+attrs);
Log.i(this.getClass().getCanonicalName(),"Modify "+updates+" Picto, id. "+picto_id+" attributes="+attrs);//+ "::"+ Arrays.toString(Thread.currentThread().getStackTrace()));
db.close();
}
......
......@@ -11,8 +11,6 @@ import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
/**
* A object which represents a pictogram
* *
......@@ -325,7 +323,7 @@ public class Picto extends Img {
Log.i(this.getClass().getCanonicalName(),"Picto id. "+get_id()+" status enabled/disabled modified to "+is_enabled());
try {
this.attributes.put(JSON_ATTTRS.STATUS, status);
status_modified(true);
set_local_status(true);
new PictoUploader(this).uploadState();
} catch (JSONException e) {
e.printStackTrace();
......@@ -339,14 +337,14 @@ public class Picto extends Img {
*
* @return true if the status of picto was modified from the PCB
*/
public boolean status_modified() {
public boolean local_status() {
return !this.attributes.isNull(JSON_ATTTRS.PCB_STATUS_MODIFICATION);
}
/**
* when the status is locally modified, it is required to remove when it is uploaded since it is no longer a local modification
*/
public void status_modified(boolean modified) {
public void set_local_status(boolean modified) {
if (modified)
try {
this.attributes.put(JSON_ATTTRS.PCB_STATUS_MODIFICATION, true);
......
......@@ -110,10 +110,10 @@ public class Vocabulary implements Iterable<Picto> {
Log.e(this.getClass().getName(), " Picto json error from local storage: " + e.getMessage());
}
for (Picto picto: this) {
if (picto.status_modified()) {
if (picto.local_status()) {
new PictoUploader(picto).uploadState();
Log.i(this.getClass().getCanonicalName(), "Picto status modified while offline. Picto translation: '" +
picto.get_translation() + "', id:" + picto.get_id() + " Current status:" + picto.status_modified());
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
try {
......
package com.yottacode.pictogram.net;
import android.content.Context;
import android.util.Log;
import com.google.gson.JsonObject;
......@@ -8,10 +7,8 @@ import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.Response;
import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.action.PictoAction;
import com.yottacode.pictogram.action.VocabularyAction;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.grammar.Vocabulary;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
......@@ -20,7 +17,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
......@@ -161,8 +157,8 @@ public class PictoUploader {
if (img_id > 0) {
uploadAttributes(img_id);
uploadTranslation(img_id);
PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ADD, PictoUploader.this.picto));
}
else PCBcontext.getVocabulary().addPicto(picto, ImgDownloader.tsource.local);
}
/**
......@@ -186,22 +182,19 @@ public class PictoUploader {
@Override
public void result(JSONArray result) {
Log.i(this.getClass().getCanonicalName(), "Upload Picto ok" + PictoUploader.this.picto.get_id());
picto.status_modified(false);
}
}
@Override
public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), "Upload ok Picto " + PictoUploader.this.picto.get_id());
picto.status_modified(false);
Log.i(PictoUploader.this.getClass().getCanonicalName(), "Upload ok Picto " + PictoUploader.this.picto.get_id());
picto.set_local_status(false);
PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ALTERATTRS,PictoUploader.this.picto));
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Picto Error: " + e.getLocalizedMessage());
picto.status_modified(true);
}
}
}
);
......
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