Commit 1ad9f904 by root

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

parents 5c9d3a95 f296a016
Showing with 0 additions and 4426 deletions
package com.yottacode.net;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
* Created by amontejo on 27/01/16.
*/
public class FakeSSLTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(final X509Certificate[] arg0,
final String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(final X509Certificate[] arg0,
final String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public final X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
}
\ No newline at end of file
package com.yottacode.net;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import android.util.Log;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
class RelaxedHostNameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
public class SSLDummyContext {
static SSLContext mySSLContext = null;
public static void init(boolean ssl) {
try {
mySSLContext = SSLContext.getInstance("TLS");
if (!ssl) {
mySSLContext.init(null, new TrustManager[]{new FakeSSLTrustManager()}, new SecureRandom());
HttpsURLConnection.setDefaultHostnameVerifier(new RelaxedHostNameVerifier());
HttpsURLConnection.setDefaultSSLSocketFactory(mySSLContext.getSocketFactory());
} else
mySSLContext.init(null, null, null);
} catch (NoSuchAlgorithmException e) {
Log.e(e.getClass().getName(), e.getClass().getCanonicalName() + ": " + e.getMessage());
} catch (KeyManagementException e) {
Log.e(e.getClass().getName(), e.getClass().getCanonicalName() + ": " + e.getMessage());
}
}
public static SSLContext get() {
return mySSLContext;
}
}
\ No newline at end of file
package com.yottacode.net;
import android.util.Log;
import com.github.nkzawa.socketio.client.Ack;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import com.github.nkzawa.emitter.Emitter;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import java.security.SecureRandom;
/**
* This listener logs all received answers from the message it is registered to
* It is usually not used
*/
class DefaultListener implements Emitter.Listener {
private String msg;
public DefaultListener(String msg) {
super();
this.msg = msg;
}
@Override
public void call(Object... args) {
Log.d(this.getClass().getName(),"Received message:"+this.msg);
for (Object arg : args)
Log.d(this.getClass().getName(), "Argument:" + arg);
}
}
/**
* Websocket Room based on Socket IO
* @author Fernando Martinez Santiago
* @version 1.0
*/
public class SailsSocketsIO {
private Socket socket=null;
private static final String EMIT_METHOD="post";
/**
* Registers a connection listener to default connection messages
* @param socket
* @param connectListener
*/
private void registerMessages(Socket socket, Emitter.Listener connectListener, Emitter.Listener errorListener) {
socket.on(Socket.EVENT_CONNECT, connectListener)
.on(Socket.EVENT_CONNECT_ERROR, errorListener)
.on(Socket.EVENT_CONNECT_TIMEOUT, errorListener)
.on(Socket.EVENT_ERROR, errorListener)
.on(Socket.EVENT_DISCONNECT, errorListener);
}
/**
* Registers a connection listener to a given message
* @param message
* @param listener
*/
public void registerMessage(String message, Emitter.Listener listener) {
Log.d(this.getClass().getName(), " Listening:"+message);
socket.on(message, listener);
}
public SailsSocketsIO(String sails_socket_io_url, String ptransport, Emitter.Listener connectListener, Emitter.Listener errorListener) {
final String sdk_version= "0.11.0";
final String sdk_module="node";
final String sdk_language="java";
final String transport=ptransport;
final String query_version =
"__sails_io_sdk_version"+"=" + sdk_version + '&' +
"__sails_io_sdk_platform"+"=" + sdk_module+ '&' +
"__sails_io_sdk_language" + '=' + sdk_language;
try {
SSLContext mySSLContext = SSLDummyContext.get();
IO.setDefaultSSLContext(mySSLContext);
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
opts.sslContext = mySSLContext;
opts.secure=true;
opts.query = query_version;
opts.transports = new String[] {transport};
this.socket = IO.socket(sails_socket_io_url, opts);
this.socket.connect();
this.registerMessages(socket, connectListener, errorListener);
} catch (URISyntaxException e) {
Log.e(e.getClass().getName(), e.getClass().getCanonicalName()+": "+e.getMessage() );
}
}
public void emit(String msg, Object params, Ack ack) throws JSONException {
JSONObject obj=new JSONObject().put("url", msg).put("data", params);
Log.d(this.getClass().getName(), "Emitted messsage:" + obj);
socket.emit(this.EMIT_METHOD, obj, ack);
}
public void destroy() {
this.socket.disconnect();
this.socket.off();
}
}
package com.yottacode.net;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* This interface specifies which are the methods to be implemented by classes receiving
* responses to API requests.
*
* Generally, it is extended by Activities from which API requests are performed
*
* @author dofer
*/
public interface iRestapiListener {
public void preExecute();
public void result(JSONArray result);
public void result(JSONObject result);
public void error(Exception e);
}
package com.yottacode.pictogram.action;
import android.util.Log;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
/**
* User actions that happens when the user is online --> they are sent to the server by websockets (Room class)
* It is required, inter alia, to support session state diagram such
* as is depicted at http://scm.ujaen.es/files/note/1042/Estados_y_acciones.pdf
* and http://scm.ujaen.es/softuno/pictogram/wikis/LUActions
* @author Fernando Martinez Santiago
* @version 1.0
* @see Room
*/
public abstract class Action {
protected String type;
/**
* It creates an action for any user, both online and offline actions.
*
* @param type
*/
public Action(String type){
this.type=type;
}
public String get_type() { return this.type;}
public abstract String get_action();
protected JSONObject get_json() {
final String param_id_stu="id_stu";
final String param_id_sup="id_sup";
final String param_id_dev="id_dev";
final String param_timestamp="timestamp";
final Date currentTime = new Date();
SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
try {
JSONObject jsonObject = new JSONObject()
.put(param_id_stu, PCBcontext.getPcbdb().getCurrentUser().get_id_stu())
.put(param_timestamp, datetime.format(currentTime));
Log.d("TIMESTAMP-----------> ", datetime.format(currentTime));
if (PCBcontext.getPcbdb().getCurrentUser().has_supervisor())
jsonObject.put(param_id_sup,PCBcontext.getPcbdb().getCurrentUser().get_id_sup());
//TODO Decidir qué almacenar con DEVICE
//if (PCBcontext.getDevice().getDeviceID()!=null)
//jsonObject.put(param_id_dev, PCBcontext.getDevice().getDeviceID());
return jsonObject;
}catch(JSONException e) {
Log.e(this.getClass().getCanonicalName(),e.getMessage());
return null;
}
}
/**
*
* @return the whole description of the action, including the action type (required for sending batch actions,
* more info at http://scm.ujaen.es/softuno/pictogram/wikis/RestapivalidActionsBatch)
*/
public JSONObject getDescription() {
final String param_action= "action";
final String param_attributes= "attributes";
try {
return (new JSONObject().put(param_action, this.type)
.put(param_attributes, this.get_json()));
} catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(),e.getMessage());
}
return null;
}
}
\ No newline at end of file
package com.yottacode.pictogram.action;
import android.util.Log;
import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Hashtable;
import java.util.Vector;
/**
* Created by emblanco on 5/10/15.
* Modified by Fernando on 10/11/15
* More info at http://scm.ujaen.es/softuno/pictogram/wikis/LUActions
*/
public class ActionLog implements iRestapiListener {
Vector<JSONObject> actions_buffer;
public ActionLog() {
actions_buffer=PCBcontext.getPcbdb().loadActions();
if (PCBcontext.getNetService().online()) batch();
}
// String constant for logs
private static final String LOG_TAG = ActionLog.class.getCanonicalName();
/**
* Online action. It is send by using web sockets
* @param action
*/
public void log(Action action) {
if (PCBcontext.getRoom().inRoom())
PCBcontext.getRoom().emit(action);
else {
// If there is no room, the action is stored into the local DB
PCBcontext.getPcbdb().insertAction(action);
actions_buffer.add(action.getDescription());
Log.i(this.getClass().getCanonicalName(), " Batch action included: " + action.getDescription());
}
}
public void batch() {
if (!actions_buffer.isEmpty()) {
Hashtable<String, String> params=new Hashtable<>(1);
String url="stu/actions_batch";
JSONArray actions= new JSONArray();
for (JSONObject action: actions_buffer)
actions.put(action);
//actions= "{actions: [" + actions.substring(1) + "]}";
try {
params.put("json",new JSONObject().put("actions",actions).toString());
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(), " Batch action error: " + e.getMessage());
}
Log.i(this.getClass().getCanonicalName()," Sending batch actions: "+url+": "+actions);
PCBcontext.getRestapiWrapper().ask(url, params, "post", true, this);
}
}
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
try {
Log.i(this.getClass().getName(),
result.has("total") ? "Uploaded actions (batch):" + result.getString("total")
: "Error uploading actions. Message from server: "+result.getString("error"));
}catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(),e.getLocalizedMessage());
}
PCBcontext.getPcbdb().deleteActions();
actions_buffer.clear();
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(),"Error:"+e.getMessage()+" on actions="+this.actions_buffer);
}
}
package com.yottacode.pictogram.action;
import android.util.Log;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
/**
* User actions regarding a pictogram that happens when the user is online --> they are sent to the server by websockets (Room class)
* It is required, inter alia, to support session state diagram such
* as is depicted at http://scm.ujaen.es/files/note/1042/Estados_y_acciones.pdf
* and http://scm.ujaen.es/softuno/pictogram/wikis/LUActions
* @author Fernando Martinez Santiago
* @version 1.0
* @see Room
*/
public abstract class PictoAction extends Action {
private Picto picto;
private float gps_lat;
private float gps_lon;
/**
* It creates an action for any user regarding a picto, both online and offline actions.
*
* @param type
* @param picto
*/
public PictoAction(String type, Picto picto){
super(type);
this.picto=picto;
}
@Override
public JSONObject get_json(){
final String param_picto="picto";
try {
return super.get_json().put(param_picto, this.get_json_picto());
}catch(JSONException e) {
Log.e(this.getClass().getCanonicalName(),e.getLocalizedMessage());
return null;
}
}
public JSONObject get_json_picto() throws JSONException {
final String param_id_json="id";
final String param_picto="picto";
final String param_expression="expression";
final String param_expr_lang="lang";
final String param_expr_text="text";
final String param_attrs="attributes";
final String param_picto_id="id";
final String param_picto_uri="uri";
final String param_picto_cat="category";
JSONObject subsubPicto = new JSONObject().put(param_picto_id, picto.get_id())
.put(param_picto_uri, picto.get_url())
.put(param_picto_cat, picto.get_category());
JSONObject attributes = new JSONObject(picto.get_json_attrs());
JSONObject expression = new JSONObject().put(param_expr_lang,PCBcontext.getPcbdb().getCurrentUser().get_lang_stu())
.put(param_expr_text,picto.get_translation());
JSONObject subPicto = new JSONObject().put(param_id_json, 1470)
.put(param_picto, subsubPicto)
.put(param_expression,expression)
.put(param_attrs, attributes);
return subPicto;
}
}
\ No newline at end of file
package com.yottacode.pictogram.action;
import android.util.Log;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
/**
* User actions regarding a pictogram that happens when the user is online --> they are sent to the server by websockets (Room class)
* It is required, inter alia, to support session state diagram such
* as is depicted at http://scm.ujaen.es/files/note/1042/Estados_y_acciones.pdf
* and http://scm.ujaen.es/softuno/pictogram/wikis/LUActions
* @author Fernando Martinez Santiago
* @version 1.0
* @see Room
*/
public class PictosAction extends Action {
LinkedList<Picto> pictos;
private float gps_lat;
private float gps_lon;
//Pictos Action types
private static final String SHOW="Show";
private static final String ACTION="/stu/action";
/**
* It creates an action for any user regarding a set of pictos, both online and offline actions.
*
* @param pictos
*/
public PictosAction( LinkedList<Picto> pictos){
super(SHOW);
this.pictos=pictos;
}
@Override
public JSONObject get_json() {
final String param_pictos="pictos";
try{
JSONArray ja = new JSONArray();
for(Picto picto : pictos) {
JSONObject jsonpicto = get_json_picto(picto);
ja.put(jsonpicto);
}
return super.get_json().put(param_pictos, ja);
} catch (JSONException e) {
Log.e(PictosAction.class.getCanonicalName(),e.getMessage());
return null;
}
}
private JSONObject get_json_picto(Picto picto) throws JSONException {
final String param_id_json="id";
final String param_picto="picto";
final String param_attrs="attributes";
final String param_picto_id="id";
final String param_picto_uri="uri";
final String param_picto_cat="category";
JSONObject subsubPicto = new JSONObject().put(param_picto_id, picto.get_id())
.put(param_picto_uri, picto.get_url())
.put(param_picto_cat, picto.get_category());
JSONObject attributes = new JSONObject(picto.get_json_attrs());
// Here add sup, dev...
JSONObject subPicto = new JSONObject().put(param_id_json, 1470)
.put(param_picto, subsubPicto)
.put(param_attrs, attributes);
return subPicto;
}
public String get_action() {return ACTION;}
}
package com.yottacode.pictogram.action;
import android.util.Log;
import com.github.nkzawa.emitter.Emitter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.github.nkzawa.socketio.client.Ack;
import com.yottacode.pictogram.dao.PCBDBHelper;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.net.SailsSocketsIO;
import com.yottacode.pictogram.tools.PCBcontext;
import java.sql.SQLDataException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
/**
* Websocket Room based on SailsSocketsIO
* It is required, inter alia, to support session state diagram such
* as is depicted at http://scm.ujaen.es/files/note/1042/Estados_y_acciones.pdf
* and http://scm.ujaen.es/softuno/pictogram/wikis/LUActions
* @author Fernando Martinez Santiago
* @version 1.0
*/
public class Room {
protected SailsSocketsIO socket=null;
protected boolean inRoom=false;
protected Hashtable<String, Emitter.Listener> listeners;
public Room( ) {
listeners=new Hashtable<>();
reconnect();
}
private JSONObject common_data(String action, JSONObject attributes) throws JSONException {
final String action_param="action";
final String attributes_param="attributes";
return new JSONObject().put(action_param, action).put(attributes_param, attributes);
}
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{
this.socket.emit(action.get_action(), this.common_data(action.get_type(), action.get_json()).put(token_param, token), new Ack() {
@Override
public void call(Object... args) {
for (Object arg : args)
Log.d(this.getClass().getName(), "Ack messsage:" + arg);
}
});
} catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + e.getMessage() + "--" + e.getLocalizedMessage());
}
}
/**
* Reconnect to the room. It is useful if the server connection is lost for a time
*/
public void reconnect() {
final String transport="polling";
if (this.socket!=null) this.socket.destroy();
this.socket=new SailsSocketsIO(PCBcontext.getRestapiWrapper().getServer(), transport, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.i(this.getClass().getName(), "Reconnect successful");
subscribe();
listen_again();
inRoom=true;
}
}, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.i(this.getClass().getName(), "Reconnect unsuccesful: "+args[0]);
inRoom=false;
}
});
}
public boolean inRoom() {return this.inRoom;}
public void listen(String msg, Emitter.Listener listener) {
this.socket.registerMessage(msg, listener);
this.listeners.put(msg, listener);
}
private void listen_again() {
for (String msg: this.listeners.keySet()) {
Log.i(this.getClass().getName(), "Listening to "+msg);
this.socket.registerMessage(msg, this.listeners.get(msg));
}
}
void subscribe() {
try {
SubscribeAction action = new SubscribeAction();
this.emit(action);
}catch (Exception e) {
Log.e(this.getClass().getCanonicalName(), e.getMessage());
}
}
public void finalize() throws java.lang.Throwable {
super.finalize();
if (this.socket!=null) this.socket.destroy();
}
}
package com.yottacode.pictogram.action;
import com.yottacode.pictogram.dao.Picto;
/**
* User actions regarding a pictogram that happens when the user is online --> they are sent to the server by websockets (Room class)
* It is required, inter alia, to support session state diagram such
* as is depicted at http://scm.ujaen.es/files/note/1042/Estados_y_acciones.pdf
* and http://scm.ujaen.es/softuno/pictogram/wikis/LUActions
* @author Fernando Martinez Santiago
* @version 1.0
* @see Room
*/
public class SubscribeAction extends Action {
//Picto Action types
public static final String SUBSCRIBE="subscribe";
public static final String ACTION="/stu/subscribe";
public SubscribeAction( ){
super(SUBSCRIBE);
}
public String get_action() {return ACTION;}
}
\ No newline at end of file
package com.yottacode.pictogram.action;
import android.util.Log;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException;
import org.json.JSONObject;
/**
* User actions regarding a pictogram that happens when the user is online --> they are sent to the server by websockets (Room class)
* It is required, inter alia, to support session state diagram such
* as is depicted at http://scm.ujaen.es/files/note/1042/Estados_y_acciones.pdf
* and http://scm.ujaen.es/softuno/pictogram/wikis/LUActions
* @author Fernando Martinez Santiago
* @version 1.0
* @see Room
*/
public class TalkAction extends PictoAction {
//Picto Action types
public static final String ADD="Add";
public static final String DELETE="Delete";
public static final String SELECT="Select";
private static final String ACTION="/stu/action";
public TalkAction(String type, Picto picto){
super(type, picto);
}
public String get_action() {return ACTION;}
}
\ No newline at end of file
package com.yottacode.pictogram.action;
import android.util.Log;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException;
import org.json.JSONObject;
/**
* User actions regarding a pictogram that happens when the user is online --> they are sent to the server by websockets (Room class)
* It is required, inter alia, to support session state diagram such
* as is depicted at http://scm.ujaen.es/files/note/1042/Estados_y_acciones.pdf
* and http://scm.ujaen.es/softuno/pictogram/wikis/LUActions
* @author Fernando Martinez Santiago
* @version 1.0
* @see Room
*/
public class VocabularyAction extends PictoAction {
//Picto Action types
public static final String ADD="add";
public static final String ALTERATTRS="update";
private static final String ACTION="/stu/vocabulary";
public VocabularyAction(String type, Picto picto){
super(type, picto);
}
public String get_action() {return ACTION;}
}
\ No newline at end of file
package com.yottacode.pictogram.dao;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.gui.LoginActivity;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.net.ImgDownloader;
import com.yottacode.pictogram.net.iImgDownloaderListener;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException;
/**
* Data Access Object to manage Pictogram Communicator Board database regarding App information that is not user-dependent
* This class requires:
* The script to create the DB allocated in res/raw/pcbdb_create.sql
* The entries db_name and db_script_error in strings.xml
*
* @author Fernando Martinez Santiago
* @version 1.1
*/
public class Device extends SQLiteOpenHelper {
Context context;
final static class PARAMS {
static String keyword="key";
static String deviceID="deviceID";
static String serial="serial";
static String stu_id="last__stu_id";
static String sup_id="last__sup_id";
}
/**
* Create a helper object to create, open, and/or manage a database.
*
* @param context the context of the activity
* @param factory null
* @param version 1
*/
public Device(Context context, CursorFactory factory, int version) {
super(context, DeviceHelper.getDBName(context), factory, version);
if (DeviceHelper.force_create(context)) {
Log.i(this.getClass().getCanonicalName(),"Forcing create new Database "+DeviceHelper.getDBName(context));
context.deleteDatabase(DeviceHelper.getDBName(context));
Log.i(this.getClass().getCanonicalName(), "Database dropped");
}
this.context = context;
}
/**
* Set the value of a param.
*
* @return serial the official Yotta device ID or null if it is not specified
*/
private void setParam(String param, String value) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values=new ContentValues(2);
values.put("key", param);
values.put("value", value);
long result=db.replace("params", null, values);
if (result==-1) Log.e(this.getClass().getCanonicalName(),"Error when updating param "+param+"="+value);
db.close();
getParamValue(param);
}
/**
* Retrieve the value of a param.
*
* @return value of key param
*/
public String getParamValue(String key) {
SQLiteDatabase db = this.getReadableDatabase();
String value;
Cursor cursor = db.query("params", new String[]{"value"}, "key=?", new String[]{key}, null, null, null, null);
if (cursor.getCount() == 0){
value = null;
Log.e(this.getClass().getCanonicalName(), "Error when getting param " + key);
}else {
cursor.moveToFirst();
value = cursor.getString(0);
}
db.close();
return value;
}
/**
* Save the yotta ID of the device, if it is official.
*
* @param serial the official Yotta device ID
*/
public void setSerial(String serial) {
setParam(PARAMS.serial, serial);
}
/**
* Retrieve de serial of the device, if it is registered and official. It returns NULL in other cases.
*
* @return serial the official Yotta device ID or null if it is not specified
*/
public String getSerial() {
return getParamValue(PARAMS.serial);
}
/**
* Save the keyord to escape from the kiosk mode
* @param keyword to chage in DB
*/
public void setKeyword(String keyword) {
setParam(PARAMS.keyword, keyword);
}
/**
* Retrieve the keyword use to escape the kiosk mode
* @author emblanco,fernando
* @return keyword
*/
public String getKeyword() {
return getParamValue(PARAMS.keyword);
}
/**
* Save the device ID, such as the Android ID.
* @param deviceID the platform dependent ID
* @see Device#getDeviceID()
*/
public void setDeviceID(String deviceID) {
setParam(PARAMS.deviceID, deviceID);
}
/**
* @return the platform device ID, such as Android ID, if it is registered, or null if it is not specified.
* @see Device#setDeviceID(String)
*/
public String getDeviceID() {
return getParamValue(PARAMS.deviceID);
}
/**
* Save the id of the student selected
* @param stu_id of the device.
*/
public void setLastStuId(int stu_id) {
setParam(PARAMS.stu_id, String.valueOf(stu_id));
}
/**
* Save the id of the last supervisor
* @param sup_id of the device.
*
*/
public void setLastSupId(int sup_id) {
setParam(PARAMS.sup_id, String.valueOf(sup_id));
}
/**
* Retrieve de last logged user
* @author Miguel Ángel
* @version 1.0
* @return user_id selected or 0.
*/
public int getLastStuId() {
return Integer.valueOf(getParamValue(PARAMS.stu_id));
}
/**
* Retrieve the last supervisor id selected in Login
*
* @return sup_id selected or 0.
*/
public int getLastSupId() {
return Integer.parseInt(getParamValue(PARAMS.sup_id));
}
/**
* @param id_stu
* @param id_sup
* @return the information of an specific user, if they are available in the local DB
* @see Device#getUsers()
*/
public User findUser(int id_stu, int id_sup) throws JSONException {
SQLiteDatabase db = this.getReadableDatabase();
User user = null;
Cursor cursor = db.query("users_detail", null, null, null, null, null, null, null);
while (cursor.moveToNext() && user == null)
if (cursor.getInt(0) == id_stu && cursor.getInt(9) == id_sup)
user = new User(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
cursor.getInt(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17));
cursor.close();
db.close();
return user;
}
public Vector<User> recoverStudents(Integer id_sup) throws JSONException{
SQLiteDatabase db = this.getReadableDatabase();
Vector<User> users = new Vector<>();
Cursor cursor = db.query("users_detail", null, null, null, null, null, null, null);
while (cursor.moveToNext())
if (cursor.getInt(9) == id_sup)
users.add(new User(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
cursor.getInt(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17)));
cursor.close();
db.close();
return users;
}
/**
* @param auser
* @param apwd
* @return information of a specific user, if it is available in the local DB
*/
public Vector<User> findUser(String auser, String apwd) throws JSONException, LoginException {
SQLiteDatabase db = this.getReadableDatabase();
boolean user_found=false;
Vector<User> users=null;
Cursor cursor = db.query("supervisor", null, null, null, null, null, null, null);
while (cursor.moveToNext() && users == null && !user_found) {
user_found = cursor.getString(1).equalsIgnoreCase(auser);
if (user_found)
if (cursor.getString(2)!=null && cursor.getString(2).equals(apwd)) {
users = recoverStudents(cursor.getInt(0));
if (users.size() == 0)
throw new LoginException("Supervisor hasn't got students", LoginException.NO_STUDENTS);
}
else throw new LoginException("Supervisor password incorrect", LoginException.BAD_LOGIN);
}
cursor.close();
if (!user_found) {
cursor = db.query("student", null, null, null, null, null, null, null);
while (cursor.moveToNext() && users == null && !user_found) {
user_found = cursor.getString(1).equalsIgnoreCase(auser);
if (user_found)
if (cursor.getString(2)!=null && cursor.getString(2).equals(apwd)) {
users = new Vector<>(1);
users.add(new User(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
User.NO_SUPERVISOR, "", "", "", "", "", "", "", ""));
}
else throw new LoginException("Student password incorrect", LoginException.BAD_LOGIN);
}
cursor.close();
}
if (!user_found) throw new LoginException("User not found", LoginException.BAD_LOGIN);
db.close();
return users;
}
/**
* @return a Vector<User> with the whole user who has been registered.
* @see User
* @see com.yottacode.pictogram.dao.PCBDBHelper
*/
public Vector<User> getUsers() throws JSONException {
SQLiteDatabase db = this.getReadableDatabase();
User user;
Cursor cursor = db.query("users_detail", null, null, null, null, null, null, null);
Vector<User> users = new Vector<User>(cursor.getCount());
while (cursor.moveToNext()) {
user = new User(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
cursor.getInt(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17));
users.add(user);
}
cursor.close();
db.close();
return users;
}
public void insertUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("INSERT INTO users_detail values (" +
user.get_id_stu() + ", " +
"'" + user.get_nickname_stu() + "', " +
(user.get_pwd_stu()==null || user.get_pwd_stu().length()==0 ? "NULL" : "'"+user.get_pwd_stu()+"'") + ", "+
"'" + user.get_name_stu() + "', " +
"'" + user.get_surname_stu() + "', " +
"'" + user.get_url_img_stu() + "', " +
"'" + user.get_gender_stu() + "', " +
"'" + user.get_lang_stu() + "', " +
"'" + user.get_json_attrs() + "', " +
"'" + user.get_id_sup() + "', " +
"'" + user.get_email_sup() + "', " +
"'" + user.get_pwd_sup() + "', " +
"'" + user.get_name_sup() + "', " +
"'" + user.get_surname_sup() + "', " +
"'" + user.get_url_img_sup() + "'," +
"'" + user.get_gender_sup() + "'," +
"'" + user.get_lang_sup() + "'," +
"'" + user.get_tts_engine_sup() + "'" +
")");
db.close();
}
/**
* delete the list of images (students, supervisors, pictograms) which are no longer used
*/
public void deleteDeprecatedImgs() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query("deprecated_images", null, null, null, null, null, null);
while (cursor.moveToNext()) {
String type = cursor.getString(1);
String folder = type.equals("stu") ? Img.STUDENT : type.equals("sup") ? Img.SUPERVISOR : Img.VOCABULARY;
(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();
db.delete("deprecated_images", null, null);
db.close();
}
/**
* Execute the script of the database
* IMPORTANT: every DDL sentence has to finish with the sequence: ;--
*
* @param database picto.db helper
* @param inputStream the stream where the ddl of picto.db is depicted
* @throws IOException
*/
protected static void executeSQLScript(SQLiteDatabase database, InputStream inputStream)
throws IOException {
Vector<String> commands = new Vector<String>(50);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String command = "";
do {
String line = r.readLine();
if (line == null) break;
if (line.equals(";--")) {
commands.add(command + ";");
command = "";
} else if (!line.startsWith("--")) command += line + " ";
} while (true);
r.close();
for (String sqlStatement : commands)
database.execSQL(sqlStatement);
Log.i(Device.class.getName(), "Database created");
}
/**
* @param url
* @return the local parth where the picto is located or null
*/
public String getPictoPath(String url) {
String path;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("picto", new String[]{"path"}, "'url'=?", new String[]{url}, null, null, null, null);
path = cursor.getCount() > 0 ? cursor.getString(0) : null;
cursor.close();
db.close();
return path;
}
/**
* get the next local id for a new local pictogram
*
* @see com.yottacode.pictogram.dao.Picto
*/
public int getNextLocalPictoID() {
SQLiteDatabase db = this.getReadableDatabase();
int next_key;
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;
cursor.close();
db.close();
return next_key;
}
@Override
public void onCreate(SQLiteDatabase db) throws RuntimeException {
try {
executeSQLScript(db, DeviceHelper.getDBScriptStream(this.context));
Img.mkDirs(this.context);
copyCoreVocabulary();
} catch (java.io.IOException io) {
throw new RuntimeException("Update database ir cached Images error", io);
}
}
private void copyCoreVocabulary() {
AssetManager assetManager = this.context.getAssets();
String[] files = null;
int size=0;
int filesok=0;
try {
files = assetManager.list(context.getResources().getString(R.string.core_vocabulary));
} catch (IOException e) {
Log.e(this.getClass().getCanonicalName(), "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(context.getResources().getString(R.string.core_vocabulary)+File.separatorChar+filename);
File outFile = new File(Img.path(this.context, Img.VOCABULARY)+filename);
out = new FileOutputStream(outFile);
size+=copyFile(in, out);
} catch(IOException e) {
Log.e(this.getClass().getCanonicalName(), "Failed to copy asset Image: " + context.getResources().getString(R.string.core_vocabulary)+File.separatorChar+filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(this.getClass().getCanonicalName(), "Failed to copy asset Image: " + filename, e);
}
}
if (out != null) {
try {
out.close();
filesok++;
} catch (IOException e) {
Log.e(this.getClass().getCanonicalName(), "Failed to copy asset Image: " + filename, e);
}
}
}
}
Log.i(Device.class.getName(), "Core vocabulary Images installed ("+filesok+" pictos,"+(size/1024)+" KB)");
}
private int copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[8192];
int read;
int size=0;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
size+=read;
}
return size;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.yottacode.pictogram.dao;
/**
* Created by Fernando on 15/03/2016.
*/
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);
this.code=code;
}
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();
}
}
package com.yottacode.pictogram.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
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;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.grammar.Vocabulary;
import com.yottacode.pictogram.action.Action;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Platform abstraction (Android)
* @see PCBDBHelper
* *
*
* @author Fernando Martinez Santiago
* @version 1.0
*/
class DeviceHelper {
static String getTimestamp() {
return (new android.text.format.Time()).toString();
}
static String getDBName(Context context) {
return context.getResources().getString(R.string.db_name);
}
static InputStream getDBScriptStream(Context context) {
return context.getResources().openRawResource(R.raw.pcbdb_create);
}
static boolean force_create(Context context) {
return context.getResources().getBoolean(R.bool.force_db_create);
}
}
/**
* Data Access Object to manage Pictogram Communicator Board database regarding a logged user
* This class requires:
* The script to create the DB allocated in res/raw/pcbdb_create.sql
* The entries db_name and db_script_error in strings.xml
*
* @author Fernando Martinez Santiago
* @version 1.0
*/
public class PCBDBHelper extends SQLiteOpenHelper {
User currentUser;
boolean user_online; //true if the given user logged into the server
/**
* Create a helper object to create, open, and/or manage a database.
*
* @param factory null
* @param version 1
* @param user the user of the PCB, if any. If not, last user is loaded from the DB
*/
public PCBDBHelper(CursorFactory factory, int version, User user) {
super(PCBcontext.getContext(), DeviceHelper.getDBName(PCBcontext.getContext()), factory, version);
this.user_online=false;
this.setCurrentUser(user == null ? this.getCurrentUser() : user);
}
public boolean isUser_online() {return this.user_online;}
public void user_online(boolean user_online) {this.user_online=user_online;}
/**
* Save the current user of the PCB. It is required to retrieve the last user.
*
* @param user the current PCB user (Student or Supervisor with a given Student collection)
* @see PCBDBHelper#getCurrentUser()
*/
private void setCurrentUser(User user) {
this.currentUser = user;
PCBcontext.getDevice().setLastStuId(user.get_id_stu());
PCBcontext.getDevice().setLastSupId(user.get_id_sup());
}
/**
* @return the last user stored into the DB
* @see PCBDBHelper#getCurrentUser()
*/
public User loadLastUser() {
String stu=String.valueOf(PCBcontext.getDevice().getLastStuId()), sup=String.valueOf(PCBcontext.getDevice().getLastSupId());
User last_user = null;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("users_detail", null, "id_stu=? AND id_sup=?", new String[]{stu, sup}, null, null, null, null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
try {
last_user = new User(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),
cursor.getInt(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), cursor.getString(13), cursor.getString(14), cursor.getString(15), cursor.getString(16), cursor.getString(17));
} catch (JSONException e) {
Log.e(this.getClass().getName(), e.getMessage() + " BAD FORMED JSON: " + cursor.getString(5));
System.exit(-1);
}
}
cursor.close();
db.close();
return last_user;
}
/**
* @return the current user who has been logged into the PCB
* @see PCBDBHelper#setCurrentUser(User)
*/
public User getCurrentUser() {
if (this.currentUser == null) this.currentUser = loadLastUser();
return this.currentUser;
}
/**
* Insert a new action
*
* @param action to be inserted
* @see Action
*/
public void insertAction(Action action) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values=new ContentValues(1);
values.put("json_action", action.getDescription().toString());
db.insert("action", null, values);
db.close();
}
/**
* Load saved actions
* @see Action
*/
public Vector<JSONObject> loadActions() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("action", null, null, null, null, null, null, null);
Vector<JSONObject> actions= new Vector(cursor.getCount());
Log.i(this.getClass().getName(), "Local action recovering " + cursor.getCount() + " actions from local DB");
cursor.moveToFirst();
while (cursor.moveToNext())
try {
actions.add(new JSONObject(cursor.getString(0)));
} catch (JSONException e) {
Log.e(this.getClass().getName(), "Recovering batch actions:"+e.getMessage());
}
cursor.close();
db.close();
return actions;
}
/**
* Delete de locally storaged action
* @return the number of deleted rows
*/
public int deleteActions() {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("action",null,null);
}
/**
* the collection (set of pictos) of the current student
*
* @see com.yottacode.pictogram.dao.Picto
*/
public Vocabulary getStudentVocabulary(Vocabulary vocabulary ) throws JSONException {
int id_stu = this.getCurrentUser().get_id_stu();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("collection_detail", null, "id_stu=?", new String[]{String.valueOf(id_stu)}, null, null, null, null);
Log.i(this.getClass().getName(), "Local recovering " + cursor.getCount() + " pictos for student " + id_stu + " from local DB");
cursor.moveToFirst();
while (cursor.moveToNext()) {
Picto picto = new Picto(cursor.getInt(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
vocabulary.loadPicto(picto);
}
cursor.close();
db.close();
return vocabulary;
}
/**
* Set/update the set of pictos of the current student. Pictos which are no longer used are dropped from the DB
*
* @param vocabulary the vocabulary, the configuration of the Student
* @see com.yottacode.pictogram.dao.Picto
*/
public void setStudentVocabulary(Vocabulary vocabulary) {
SQLiteDatabase db = this.getWritableDatabase();
int id_stu = this.getCurrentUser().get_id_stu();
int seconds1 = Calendar.getInstance().get(Calendar.SECOND);
db.delete("collection", "id_stu=" + id_stu, null);
int newsize=0;
ContentValues values=new ContentValues(5);
values.put("id_stu", id_stu);
for (Picto picto : vocabulary) {
newsize++;
values.put("id_picto", picto.get_id());
values.put("url", picto.get_url());
values.put("translation",picto.get_translation());
values.put("attributes",picto.get_json_attrs());
db.insert("collection_detail", null, values);
}
int seconds2 = Calendar.getInstance().get(Calendar.SECOND);
Log.i(this.getClass().getName(), " Local student vocabulary updated, id:" + id_stu + ", cats: " + vocabulary.size() + " time:" + (seconds2 - seconds1) + " secs. Size: " + newsize + " read only?" + db.isReadOnly());
db.close();
}
/**
* Insert/update a picto of the current student.
*
* @param picto added to the Student collection
* @see com.yottacode.pictogram.dao.Picto
*/
public void addPicto(Picto picto) {
int id_stu = this.getCurrentUser().get_id_stu();
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values=new ContentValues(6);
values.put("id_stu", id_stu);
values.put("id_picto", picto.get_id());
values.put("url", picto.get_url());
values.put("translation",picto.get_translation());
values.put("attributes",picto.get_json_attrs());
db.insert("collection_detail", null, values);
db.close();
}
/**
* Delete a picto of the current collection's student. Pictos which are no longer used are dropped from the DB
*
* @param picto_id to be deleted from the configuration of the Student
* @see com.yottacode.pictogram.dao.Picto
*/
public void deletePicto(int picto_id) {
int id_stu = this.getCurrentUser().get_id_stu();
SQLiteDatabase db = this.getWritableDatabase();
db.delete("collection","id_stu=? AND id_picto=?",
new String[]{ Integer.toString(this.currentUser.get_id_stu()),
Integer.toString(picto_id)});
db.close();
}
/**
* Update a picto of the current collection's student.
*
* @param picto_id to be modified from the configuration of the Student
* @param attrs new picto attributes
* @see com.yottacode.pictogram.dao.Picto
*/
public void modifyPicto(int picto_id, String attrs) {
int id_stu = this.getCurrentUser().get_id_stu();
SQLiteDatabase db = this.getWritableDatabase();
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);//+ "::"+ Arrays.toString(Thread.currentThread().getStackTrace()));
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) throws RuntimeException {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
\ No newline at end of file
package com.yottacode.pictogram.dao;
import android.graphics.Color;
import android.util.Log;
import com.yottacode.pictogram.action.VocabularyAction;
import com.yottacode.pictogram.net.PictoUploader;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
/**
* A object which represents a pictogram
* *
* @author Fernando Martinez Santiago
* @version 1.0
*/
public class Picto extends Img {
// String constant for logs
private final String LOG_TAG = this.getClass().getSimpleName(); // Or .getCanonicalName()
public final static class JSON_ATTTRS {
public static String CATEGORY = "id_cat";
public static String COLUMN = "coord_x";
public static String ROW = "coord_y";
public static String FREE_COLUMN = "free_category_coord_x";
public static String FREE_ROW = "free_category_coord_y";
public static String MAGNIFY = "magnify";
public static String HIGHLIGHT = "highlight";
public static String STATUS = "status";
public static String COLOR = "color";
public static String PCB_STATUS_MODIFICATION="pcb_status_modification";
}
public final static class JSON_ATTTR_STATUS_VALUES {
static String ENABLED = "enabled";
static String DISABLED = "disabled";
static String INVISIBLE = "invisible";
}
public final static int NO_CATEGORY=-1;
public final static int ROW_UNCATEGORIZED_CONCEPTS=0;
private JSONObject attributes;
private String translation;
public Picto(int id, String url, String translation, int cat, int row, int column, int freeRow, int freeColumn) throws JSONException {
this(id, url, translation, new JSONObject()
.put(JSON_ATTTRS.CATEGORY, cat)
.put(JSON_ATTTRS.COLUMN, column)
.put(JSON_ATTTRS.ROW, row)
.put(JSON_ATTTRS.FREE_ROW, freeRow)
.put(JSON_ATTTRS.FREE_COLUMN, freeColumn)
.put(JSON_ATTTRS.STATUS, JSON_ATTTR_STATUS_VALUES.ENABLED));
}
public Picto(int id, String url,String translation, String attributes) throws JSONException {
this(id, url, translation, new JSONObject(attributes));
}
public Picto(int id, String url,String translation, JSONObject attributes) {
super(id,url,Img.VOCABULARY);
this.translation=translation;
this.attributes = attributes;
try {
if (this.attributes.get(JSON_ATTTRS.CATEGORY)==null)
this.attributes.put(JSON_ATTTRS.CATEGORY, Picto.NO_CATEGORY);
} catch (JSONException e) {
Log.e(this.getClass().getName(), e.toString());
}
}
/**
*
* @return true if it's a local pictogram
*/
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
*/
public String get_url() {return this.url;}
/**
*
* @return the translation of the picto to a given language
*/
public String get_translation() {return this.translation;}
/**
*
* @return a JSON attributes of the picto
*/
public String get_json_attrs() {
return this.attributes.toString();
}
/**
* JSON attrs of the picto except the param regarding is locally modified since this is not saved into the server
*
*
*/
public String get_json_server_attrs() {
JSONObject json = null;
try {
json = new JSONObject(this.attributes.toString());
} catch (JSONException e) {
e.printStackTrace();
}
json.remove(JSON_ATTTRS.PCB_STATUS_MODIFICATION);
return json.toString();
}
/**
* Set JSON attribute of the picto
* @param attr attributes modified
*/
public void set_json_attr(JSONObject attr) {
this.attributes=attr;
}
/**
*
* @return the category of the picto
*/
public int get_category() {
try {
return this.attributes.getInt(JSON_ATTTRS.CATEGORY);
} catch (JSONException e) {
return Picto.NO_CATEGORY;
}
}
/**
*
* @return the magnifiy property of a picto
*/
public boolean is_magnify() {
try {
return Boolean.parseBoolean(this.attributes.getString(JSON_ATTTRS.MAGNIFY));
} catch (JSONException e) {
return false;
}
}
/**
*
* @return if the picto is enabled
*/
public boolean is_enabled() {
try {
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTR_STATUS_VALUES.ENABLED);
} catch (JSONException e) {
return false;
}
}
/**
*
* @return if the picto is disabled
*/
public boolean is_disabled() {
try {
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTR_STATUS_VALUES.DISABLED);
} catch (JSONException e) {
return false;
}
}
/**
*
* @return if the picto is visible
*/
public boolean is_invisible() {
try {
return this.attributes.getString(JSON_ATTTRS.STATUS).equals(JSON_ATTTR_STATUS_VALUES.INVISIBLE) ||
(this.is_category() && !PCBcontext.getVocabulary().isVisibleCategory(this.get_id()));
} catch (JSONException e) {
return false;
}
}
/**
*
* @return the highlight property of a picto
*/
public boolean is_highlight() {
try {
return Boolean.parseBoolean(this.attributes.getString(JSON_ATTTRS.HIGHLIGHT));
} catch (JSONException e) {
return false;
}
}
/**
*
* @return the status of a picto (enabled | disabled | invisible)
*/
public String get_status() {
try {
return this.attributes.getString(JSON_ATTTRS.STATUS);
} catch (JSONException e) {
return "enabled"; // By default
}
}
/**
*
* @return the row of the picto
*/
public int get_row() {
return this.attributes.optInt(JSON_ATTTRS.ROW, -1);
}
/**
*
* @return the column of the picto
*/
public int get_column() {
return this.attributes.optInt(JSON_ATTTRS.COLUMN, -1);
}
/**
*
* @return the free row of the picto
*/
public int getFreeRow() {
return this.attributes.optInt(JSON_ATTTRS.FREE_ROW, -1);
}
/**
*
* @return the free column of the picto
*/
public int getFreeColumn() {
return this.attributes.optInt(JSON_ATTTRS.FREE_COLUMN, -1);
}
/**
*
* @return the color of the picto
*/
public int get_color() {
try {
String color = this.attributes.getString(JSON_ATTTRS.COLOR);
int icolor = Color.parseColor(color);
return icolor;
} catch (JSONException e) {
return -1;
}
}
/**
*
* @return the lighter color of the picto
*/
public int get_lighter_color() {
final int LIGHT=0x22;
int color = this.get_color();
int red = Color.red(color)+LIGHT, blue = Color.blue(color)+LIGHT, green = Color.green(color)+LIGHT;
return Color.rgb(red, green, blue);
}
/**
*
* @return the darkner color of the picto
*/
public int get_darkner_color() {
/*
final int DARK=-0x22;
int color = this.get_color();
int red = Color.red(color)+DARK, blue = Color.blue(color)+DARK, green = Color.green(color)+DARK;
return Color.rgb(red,green,blue);
*/
/*
String hexColor = String.format("#%06X", (0xFFFFFF & this.get_color()));
String hexColorDarker = String.format("#%06X", (0xFFFFFF & darker(this.get_color(), (float)0.9)));
Log.d(LOG_TAG, "Color actual: " + hexColor);
Log.d(LOG_TAG, "Color darker: " + hexColorDarker);
*/
return darker(this.get_color(), (float) 0.9);
}
public static int darker (int color, float factor) {
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
return Color.argb(a,
Math.max((int) (r * factor), 0),
Math.max((int) (g * factor), 0),
Math.max((int) (b * factor), 0));
}
/**
* A picto is a category iif:
* (i) it has not got category
* (ii) it is not an uncategorized concept (such as "yes","no","thank you")
* @return
*/
public boolean is_category() {
return this.get_category()==Picto.NO_CATEGORY &&
this.get_column() != Picto.ROW_UNCATEGORIZED_CONCEPTS &&
this.getFreeColumn() == -1 &&
this.getFreeRow() == -1;
}
/**
*
* @overide
* Two user are equals whenever they have the same id
*/
public boolean equals(Object o) {
return ((Picto)o).id==this.id;
}
/**
* toString method
*/
public String toString(){
return "(" + get_id() + ") - ["+ get_row() +","+ get_column()+"]" + get_translation() + "(Cat: " + get_category() + ") - " + get_url() + " --- " + get_json_attrs();
}
/**
* modify locally the status of the picto
* @return true if current status is enabled. False in other case.
*/
public boolean alter_status() {
String status=is_enabled() ? JSON_ATTTR_STATUS_VALUES.DISABLED : JSON_ATTTR_STATUS_VALUES.ENABLED;
Log.i(this.getClass().getCanonicalName(),"Picto id. "+get_id()+" status enabled/disabled modified to "+is_enabled());
try {
this.attributes.put(JSON_ATTTRS.STATUS, status);
set_local_status(true);
if (!is_local()) {
new PictoUploader(this).uploadState();
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ALTERATTRS, this));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(),e.getMessage());
}
return is_enabled();
}
/**
*
* @return true if the status of picto was modified from the PCB
*/
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 set_local_status(boolean local) {
if (local)
try {
this.attributes.put(JSON_ATTTRS.PCB_STATUS_MODIFICATION, true);
PCBcontext.getPcbdb().modifyPicto(this.get_id(), this.get_json_attrs());
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(), e.getMessage());
}
else {
this.attributes.remove(JSON_ATTTRS.PCB_STATUS_MODIFICATION);
PCBcontext.getPcbdb().modifyPicto(this.get_id(), this.get_json_attrs());
}
}
}
package com.yottacode.pictogram.dao;
import java.io.IOException;
import org.json.JSONObject;
import org.json.JSONException;
import android.content.Context;
import android.graphics.Bitmap;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.grammar.Vocabulary;
/**
* A user of the PCB. A user can be an Student or a Supervisor using a student configuration
* *
* @author Fernando Martinez Santiago
* @version 1.1
*/
public class User {
public static final int NO_SUPERVISOR = -1;
public boolean has_supervisor() {
return this.get_id_sup()!=User.NO_SUPERVISOR;
}
public final static class JSON_STUDENT_ATTTRS{
static String CATEGORIES = "categories";
static String INPUT_FEEDBACK = "input feedback";
static String INPUT_SELECTION = "input selection";
static String PICTOGRAM_SIZE = "pictogram size";
static String TTS_ENGINE = "tts engine";
static String TTS_VOICE = "tts voice";
}
private Img img_stu;
private String nickname_stu, pwd_stu, name_stu, surname_stu, gender_stu, lang_stu;
private JSONObject attributes_stu;
private Img img_sup;
private String email_sup, pwd_sup, name_sup, surname_sup, gender_sup, lang_sup, tts_engine_sup;
public User(int id_stu, String nickname_stu, String pwd_stu, String name_stu, String surname_stu, String url_img_stu, String gender_stu, String lang_stu, String attributes_stu) throws JSONException {
this(id_stu, nickname_stu, pwd_stu, name_stu, surname_stu, url_img_stu, gender_stu, lang_stu, attributes_stu,User.NO_SUPERVISOR,"","","","","","M","es-es",null);
}
public User(int id_stu, String nickname_stu, String pwd_stu, String name_stu, String surname_stu, String url_img_stu, String gender_stu, String lang_stu, String attributes_stu,
int id_sup, String email_sup, String pwd_sup, String name_sup, String surname_sup, String url_img_sup, String gender_sup, String lang_sup, String tts_engine_sup) throws JSONException {
this.img_stu=new Img(id_stu,url_img_stu,Img.STUDENT);
this.nickname_stu=nickname_stu;
this.pwd_stu=pwd_stu;
this.name_stu=name_stu;
this.surname_stu=surname_stu;
this.gender_stu=gender_stu;
this.lang_stu=lang_stu;
this.attributes_stu= attributes_stu!=null && attributes_stu.trim().length()>0 ? new JSONObject(attributes_stu) : new JSONObject();
this.img_sup=new Img(id_sup,url_img_sup,Img.SUPERVISOR);
this.email_sup=email_sup;
this.pwd_sup=pwd_sup;
this.name_sup=name_sup;
this.surname_sup=surname_sup;
this.gender_sup=gender_sup;
this.lang_sup=lang_sup;
this.tts_engine_sup=tts_engine_sup;
}
/**
*
* @return id of the student
*/
public int get_id_stu() {return this.img_stu.get_id();}
/**
*
* @return nickname (username) of the student
*/
public String get_nickname_stu() {return this.nickname_stu;}
/**
*
* @return pwd of the student
*/
public String get_pwd_stu() {return this.pwd_stu;}
/**
* @ return the RESTAPI operator for the current student
*/
public String get_restapi_operation_stu() {
return "stu/"+this.get_id_stu();
}
/**
*
* @return Img of the student
*/
public Img get_img_stu() {return this.img_stu;}
/**
*
* @return name of the student
*/
public String get_name_stu() {return this.name_stu;}
/**
*
* @return surname of the student
*/
public String get_surname_stu() {return this.surname_stu;}
/**
*
* @return url of the picture of the student profile
*/
public String get_url_img_stu() {return this.img_stu.get_url();}
/**
*
* @return Img of the student
*/
public Bitmap get_bitmap_stu(Context context) throws IOException {return this.img_stu.get_bitmap(context);}
/**
*
* @return Img of the student
*/
public Img get_Img_stu() {return this.img_stu;}
/**
*
* @return gender of the student
*/
public String get_gender_stu() {return this.gender_stu;}
/**
*
* @return language of the student
*/
public String get_lang_stu() {return this.lang_stu;}
/**
*
* @return id of the supervisor
*/
public int get_id_sup() {return this.img_sup.get_id();}
/**
*
* @return nickname (username) of the student
*/
public String get_email_sup() {return this.email_sup;}
/**
*
* @return pwd of the student
*/
public String get_pwd_sup() {return this.pwd_sup;}
/**
*
* @return name of the supervisor
*/
public String get_name_sup() {return this.name_sup;}
/**
*
* @return surname of the supervisor
*/
public String get_surname_sup() {return this.surname_sup;}
/**
*
* @return url of image of the supervisor profile
*/
public String get_url_img_sup() {return this.img_sup.get_url();}
/**
*
* @return Bitmap of the supervisor
*/
public Bitmap get_bitmap_sup(Context context) throws IOException {return this.img_sup.get_bitmap(context);}
/**
*
* @return Img of the supervisor
*/
public Img get_Img_sup() {return this.img_sup;}
/**
*
* @return gender of the supervisor
*/
public String get_gender_sup() {return this.gender_sup;}
/**
*
* @return language of the supervisor
*/
public String get_lang_sup() {return this.lang_sup;}
/**
*
* @return TTS engine of the supervisor
*/
public String get_tts_engine_sup() {return this.tts_engine_sup;}
/**
*
* @return a JSON attributes of the user
*/
public String get_json_attrs() {
return this.attributes_stu.toString();
}
/**
*
* @return a JSON attribute of the student or NULL if the given attribute is not available
*/
public String get_json_attr(String attr) {
try {
return this.attributes_stu.getString(attr);
} catch (JSONException e) {
return null;
}
}
/**
*
* @return true if the collection is organized by categories (default: True)
*/
public boolean has_categories() {
return this.attributes_stu.optBoolean(JSON_STUDENT_ATTTRS.CATEGORIES, true);
}
/**
*
* @return input feedback of the student configuration (default: "vibration")
*/
public String get_input_feedback() {
try {
return this.attributes_stu.getString(JSON_STUDENT_ATTTRS.INPUT_FEEDBACK);
} catch (JSONException e) {
return "vibration";
}
}
/**
*
* @return input selection of the student configuration (default: "click")
*/
public String get_input_selection() {
try {
return this.attributes_stu.getString(JSON_STUDENT_ATTTRS.INPUT_SELECTION);
} catch (JSONException e) {
return "click";
}
}
/**
*
* @return pictogram size of the student configuration (default: "normal")
*/
public String get_pictogram_size() {
try {
return this.attributes_stu.getString(JSON_STUDENT_ATTTRS.PICTOGRAM_SIZE);
} catch (JSONException e) {
return "normal";
}
}
public boolean is_supervisor() {
return get_id_sup()!=User.NO_SUPERVISOR;
}
/**
*
* @overide
* Two user are equals whenever they have the same student and supervisor id
*/
public boolean equals(Object o) {
return ((User)o).img_stu.get_id()==this.img_stu.get_id() && ((User)o).img_sup.get_id()==this.img_sup.get_id();
}
}
\ No newline at end of file
package com.yottacode.pictogram.grammar;
import android.os.AsyncTask;
import android.util.Log;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.action.VocabularyAction;
import com.yottacode.pictogram.dao.PCBDBHelper;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.net.ImgDownloader;
import com.yottacode.pictogram.net.PictoUploader;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.action.Room;
import com.yottacode.pictogram.net.iImgDownloaderListener;
import com.yottacode.pictogram.tools.PCBcontext;
import com.yottacode.tools.GUITools;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import java.util.Vector;
/**
* PCB Vocabulary manager
* @author Fernando Martinez Santiago
* @version 1.0
*/
public class Vocabulary implements Iterable<Picto> {
Hashtable<Integer,LinkedList<Picto>> pictos;
static int DEFAULT_VOCABULARY_SIZE=200;
VocabularyTalk talk;
iImgDownloaderListener imgListener;
/**
* Creates a new vocabulary and download/load the vocabulary and the corresponding pictos
* @param listener
*/
public Vocabulary(iImgDownloaderListener listener) {
this.pictos = new Hashtable<>(Vocabulary.DEFAULT_VOCABULARY_SIZE);
this.imgListener=listener;
if (PCBcontext.getNetService().online()) {
synchronize();
}else
try {
PCBcontext.getPcbdb().getStudentVocabulary(this);
if (listener!=null)
listener.loadComplete();
} catch (JSONException e) {
Log.e(this.getClass().getName(),"Local vocabulary recover failed: "+e.getMessage());
listener.error(e);
}
}
public void listen(Room room, iVocabularyListener listener) {
iVocabularyListener listeners[] = {new iVocabularyListener() {
@Override
public void change(action action, int picto_cat, int picto_id, JSONObject args) {
switch (action) {
case delete: {
removePicto(picto_cat, picto_id);
break;
}
case update:{
Log.i(this.getClass().getCanonicalName(), "Picto update "+args.toString());
try {
modifyAttsPicto(picto_cat, picto_id, args.getJSONObject("attributes"));
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
case add:{
try{
String text= args.getJSONObject("expression").getString("text");
String uri=args.getJSONObject("picto").getString("uri");
JSONObject attrs_picto = args.getJSONObject("attributes");
addPicto(new Picto(picto_id, uri, text, attrs_picto),ImgDownloader.tsource.remote);
} catch (JSONException e) {
Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + "--" + e);
// System.exit(-1);
}
break;
}
}
}
},listener};
talk = new VocabularyTalk(room, listeners);
}
/**
* UPload local status modifications and new pictos. Note that when
* a picto is uploaded is not required to delete from the local PCB
* because the PCB will get the remote vocabulary at the end of the process
* The only issue is: what happens whether the upload fails? --> The image will be lost.
* TODO: keep record of failed uploaded images to re-include as local pictos
*/
private void synchronize_upload() {
try {
PCBcontext.getPcbdb().getStudentVocabulary(this);
} catch (JSONException e) {
Log.e(this.getClass().getName(), " Picto json error from local storage: " + e.getMessage());
}
for (Picto picto: this) {
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() + " Local status?" + picto.local_status());
}
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(PCBcontext.getContext());
} catch (IOException e) {
e.printStackTrace();
Log.e(this.getClass().getName(), " Picto json error from server: " + picto.toString());
}
}
}
/**
* the vocabulary is (i) updated to and (ii) downloaded from the server.
*/
public void synchronize() {
synchronize_upload();
//download
final String picto_str="/pictos";
String operation=PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()+picto_str;
PCBcontext.getRestapiWrapper().ask(operation, new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
if (result != null) {
final String jpicto = "picto";
final String jid = "id";
final String juri = "uri";
final String jattributes = "attributes";
final String jexpression = "expression";
final String jexpression_text = "text";
Picto[] pictos = new Picto[result.length()];
JSONObject picto = null, expression = null, attributes = null;
JSONObject ojpicto=null;
try {
for (int i=0; i < result.length(); i++) {
ojpicto=result.getJSONObject(i);
picto = ojpicto.getJSONObject(jpicto);
expression = ojpicto.getJSONObject(jexpression);
attributes = ojpicto.getJSONObject(jattributes);
pictos[i] = new Picto(picto.getInt(jid),
picto.getString(juri),
expression.getString(jexpression_text),
attributes);
}
synchronizeImgs(pictos);
PCBcontext.getPcbdb().setStudentVocabulary(Vocabulary.this);
Log.i(this.getClass().getName(), " Pictos downloaded: " + result.length());
} catch (JSONException e) {
StackTraceElement traces[] = e.getStackTrace();
for (StackTraceElement s: traces)
Log.e(s.getClassName()+"."+s.getFileName()+"."+s.getLineNumber(),s.toString());
Log.e(this.getClass().getName(), " Picto JSON error from server: " + ojpicto.toString());
this.error(e);
}
}
}
@Override
public void result(JSONObject result) {
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getName(), " Server RESTAPI error: " + e.getLocalizedMessage());
Vocabulary.this.imgListener.error(e);
}
});
}
/**
* synchronize the local collection with the downloaded remote collection
*
* @param updated_collection is the downloaded remote collection
* @throws JSONException in case of user.attributes json parsing error
*/
private void synchronizeImgs(Picto updated_collection[]) throws JSONException {
Vector<Img> imgs=new Vector<Img>(updated_collection.length);
this.pictos.clear();
for (Picto updated_picto: updated_collection) {
LinkedList<Picto> pictos_cat;
Picto picto = new Picto(updated_picto.get_id(),
updated_picto.get_url(),
updated_picto.get_translation(),
updated_picto.get_json_attrs());
if (pictos.containsKey(picto.get_category()))
pictos_cat = pictos.get(picto.get_category());
else {
pictos_cat = new LinkedList<>();
pictos.put(new Integer(picto.get_category()),pictos_cat);
}
pictos_cat.add(picto);
imgs.add(new Img(picto.get_id(), picto.get_url(), Img.VOCABULARY));
}
Log.d(this.getClass().getName(), "Vocabulary size: " + updated_collection.length);
ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener,ImgDownloader.tsource.remote);
downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, imgs);
}
public int size() { return this.pictos.size();}
/**
* It includes/updates a new picto to the user collection: download the image from remote or local storage, add the picto to the current vocabulary and update the database
* @param pic
*/
public void addPicto(Picto pic, ImgDownloader.tsource source){
addPicto(pic, source, this.imgListener);
}
public void addPicto(Picto pic, ImgDownloader.tsource source, iImgDownloaderListener imgListener){
Vector<Img> imgs=new Vector<Img>(1);
imgs.add(new Img(pic.get_id(), pic.get_url(), Img.VOCABULARY));
ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener,source);
downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, imgs);
loadPicto(pic);
PCBcontext.getPcbdb().addPicto(pic);
}
/**
*
* @param pic_cat
* @return
*/
private boolean empty_category(int pic_cat) {return this.pictos.get(pic_cat)==null;}
private int find_picto_index(int pic_cat, int pic_id) {
LinkedList<Picto> pictos_cat=this.pictos.get(pic_cat);
int index=-1;
for (int i=0; i<pictos_cat.size() && index==-1; i++)
if (pictos_cat.get(i).get_id()==pic_id) index=i;
return index;
}
/**
* It removes de given pic from the user collection
* @param pic_id
*/
public void removePicto(int pic_cat, int pic_id){
LinkedList<Picto> pictos_cat=this.pictos.get(pic_cat);
int index = find_picto_index(pic_cat, pic_id);
if (index>=0) { //puede ocurrir que se intente borrar un pictograma dos veces
pictos_cat.remove(index);
PCBcontext.getPcbdb().deletePicto(pic_id);
}
else
Log.i(this.getClass().getCanonicalName(),"Trying to delete an unregistered picto:"+pic_id+" cat:"+pic_cat);
}
/**
* It modifies de given pic from the user collection
* @param pic_cat
* @param pic_id
* @param attrs
*/
public void modifyAttsPicto(int pic_cat, int pic_id, JSONObject attrs) {
int index=find_picto_index(pic_cat, pic_id);
if (index>=0) { //puede ocurrir que se intente modificar un pictograma que fue borrado
Picto picto = this.pictos.get(pic_cat).get(index);
picto.set_json_attr(attrs);
PCBcontext.getPcbdb().modifyPicto(pic_id, attrs.toString());
}
else
Log.i(this.getClass().getCanonicalName(),"Trying to modify an unregistered picto:"+pic_id+" cat:"+pic_cat);
}
/**
* It loads into the vocabulary a given picto which was stored in the local db.
* This method is called by the PCDBHelper when the collection is loaded
* @param picto
* @seealso com.yottacode.pictogram.dao.PCBDBHelper.getStudentVocabulary
*/
public void loadPicto(Picto picto) {
LinkedList<Picto> pictos_cat;
if (this.pictos.containsKey(picto.get_category()))
pictos_cat = pictos.get(picto.get_category());
else {
pictos_cat = new LinkedList();
this.pictos.put(new Integer(picto.get_category()),pictos_cat);
}
pictos_cat.add(picto);
}
/**
*
* @return list of pictos which should be selectable at the beginning of a sentence. Empty categories are removed
*/
public LinkedList<Picto> startSentence(){
if (PCBcontext.getPcbdb().getCurrentUser().has_categories()) {
return this.pictos.get(new Integer(Picto.NO_CATEGORY));
} else {
LinkedList<Picto> freePictos = new LinkedList<>();
for (LinkedList<Picto> category : pictos.values()) {
for (Picto picto : category) {
if (picto.getFreeRow() != -1 && picto.getFreeColumn() != -1) {
freePictos.add(picto);
}
}
}
return freePictos;
}
}
/**
* A category is visible iif it has at least an enabled concept
* @param id
* @return
*/
public boolean isVisibleCategory(int id) {
if (empty_category(id)) return false;
if (PCBcontext.getPcbdb().getCurrentUser().is_supervisor()) return true;
boolean visible=false;
for (Picto picto : this.pictos.get(id)) {
visible=!picto.is_invisible();
if (visible) break;
}
return visible;
}
/*
* It saves locally a new picto obtained from the PCB
*/
public Picto saveLocalPicto(String url, String exp, int cat, int coord_x, int coord_y, int free_category_coord_x, int free_category_coord_y, final iLocalPicto listener) {
int id= PCBcontext.getDevice().getNextLocalPictoID();
final Picto picto[]=new Picto[1];
try {
picto[0] = new Picto(id, url, exp, cat, coord_x, coord_y, free_category_coord_x, free_category_coord_y);
addPicto(picto[0], ImgDownloader.tsource.local, new iImgDownloaderListener() {
@Override
public void loadComplete() {
listener.saved(picto[0]);
}
@Override
public void loadImg(Img image) {
}
@Override
public void error(Exception e) {
GUITools.show_alert(PCBcontext.getContext(), R.string.serverError, e.getMessage());
Log.e(this.getClass().getCanonicalName(), "Server error:"+ e.getLocalizedMessage());
}
});
} catch (Exception e) {
picto[0]=null;
e.printStackTrace();
Log.e(Vocabulary.class.getCanonicalName(), e.getMessage());
}
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ADD, picto[0]));
return picto[0];
}
/**
*
* @param picto
* @return set of pictos which should be selectable in a given sentence state
*/
public LinkedList<Picto> next(Picto picto){
return picto.is_category() ? this.pictos.get(picto.get_id()) : startSentence() ;
}
@Override
public Iterator<Picto> iterator() {
return new VocabularyIterator(this.pictos);
}
@Override
public String toString() {
String vocabulary = "{";
Set<Integer> cats = this.pictos.keySet();
for (Integer cat : cats) {
vocabulary += "{\"id category\" : \"" + cat + "\", \"picto\": [";
int i = 0;
for (Picto picto : this.pictos.get(cat)) {
i++;
vocabulary += "{\"id\" : \"" + picto.get_id() + "\"" + ", \"translation\" : \"" + picto.get_translation() + "\"}";
if (i < this.pictos.size()) vocabulary += ", ";
}
vocabulary += "]";
}
vocabulary += "}";
return vocabulary;
}
}
package com.yottacode.pictogram.grammar;
import android.util.Log;
import com.yottacode.pictogram.dao.Picto;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
/**
* PCB Vocabulary manager
* @author Fernando Martinez Santiago
* @version 1.0
*/
public class VocabularyIterator implements Iterator<Picto> {
Enumeration<Integer> keys;
Hashtable<Integer,LinkedList<Picto>> pictos;
Integer category;
int location;
VocabularyIterator(Hashtable<Integer,LinkedList<Picto>> pictos) {
this.keys = pictos.keys();
if (pictos.size()>1) {
this.pictos=pictos;
this.category = this.keys.nextElement();
}
else {
this.category=-1;
this.pictos=new Hashtable<>(1);
this.pictos.put(this.category, new LinkedList<Picto>());
}
this.location=0;
}
@Override
public boolean hasNext() {
return this.location<pictos.get(category).size() || keys.hasMoreElements();
}
@Override
public Picto next() {
Picto picto;
if ( this.location==pictos.get(category).size()) {
this.location = 0;
this.category = this.keys.nextElement();
}
picto=pictos.get(category).get(location++);
return picto;
}
@Override
public void remove() {
}
}
\ No newline at end of file
package com.yottacode.pictogram.grammar; import android.util.Log; import com.github.nkzawa.emitter.Emitter; import org.json.JSONException;import org.json.JSONObject; import com.yottacode.pictogram.dao.Picto;import com.yottacode.pictogram.action.Room; /** * Websocket Vocabulary Room based on Room * @author Fernando Martinez Santiago * @version 1.0 */public class VocabularyTalk implements Emitter.Listener { private static final String URL ="vocabulary"; private Room room; iVocabularyListener listeners[]; public VocabularyTalk(Room room, iVocabularyListener listeners[]) { this.room = room; this.room.listen(URL, this); this.listeners=listeners; } @Override public void call(Object... args) { final String param_action="action"; final String param_attributes="attributes"; final String param_picto="picto"; final String param_stu_picto="stu_picto"; final String param_picto_id="id"; final String param_picto_cat="id_cat"; final String action_update="update"; final String action_add="add"; final String action_delete="delete"; JSONObject msg = (JSONObject) args[0]; try { Log.i(this.getClass().getName(), "raw Received message " +msg.toString()); String action = msg.getString(param_action).toLowerCase(); JSONObject stu_picto= msg.getJSONObject(param_attributes).getJSONObject(param_stu_picto); JSONObject attrs_stu_picto = stu_picto.optJSONObject(param_attributes); JSONObject attrs_picto = stu_picto.optJSONObject(param_picto); int picto_id = attrs_picto.getInt(param_picto_id); int picto_cat = attrs_stu_picto!=null ? attrs_stu_picto.optInt(param_picto_cat, Picto.NO_CATEGORY) : 0; Log.i(this.getClass().getName(), "Received message '" + action + "' for picto " + picto_id + " (cat " + picto_cat + ", attrs: " + attrs_picto); for (iVocabularyListener listener: this.listeners) listener.change(action.equals(action_update) ? iVocabularyListener.action.update : action.equals(action_add) ? iVocabularyListener.action.add : iVocabularyListener.action.delete , picto_cat, picto_id, stu_picto); } catch (JSONException e) { Log.e(this.getClass().getCanonicalName(), e.getClass().getCanonicalName() + "--" + e); } }}
\ No newline at end of file
package com.yottacode.pictogram.grammar;
import com.yottacode.pictogram.dao.Picto;
/**
* Created by Fernando on 14/03/2016.
*/
public interface iLocalPicto {
public void saved(Picto localPicto);
}
package com.yottacode.pictogram.grammar;
import com.yottacode.pictogram.dao.Picto;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Hashtable;
import java.util.LinkedList;
/**
* Vocabulary Listener
* @author Fernando Martinez Santiago
* @version 1.0
*/
public interface iVocabularyListener {
public enum action {delete,add,update}
public void change(iVocabularyListener.action action, int picto_cat, int picto_id, JSONObject args);
}
package com.yottacode.pictogram.kiosk;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;
/**
* Created by emblanco on 14/10/15.
*/
public class AppContext extends Application {
private AppContext instance;
private PowerManager.WakeLock wakeLock;
private OnScreenOffReceiver onScreenOffReceiver;
@Override
public void onCreate() {
super.onCreate();
instance = this;
registerKioskModeScreenOffReceiver();
startKioskService(); // Service for restarting the app when another app go to foreground
}
private void registerKioskModeScreenOffReceiver() {
// register screen off receiver
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
onScreenOffReceiver = new OnScreenOffReceiver();
registerReceiver(onScreenOffReceiver, filter);
}
public PowerManager.WakeLock getWakeLock() {
if(wakeLock == null) {
// lazy loading: first call, create wakeLock via PowerManager.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "wakeup");
}
return wakeLock;
}
// Service for restarting the app when another app go to foreground
private void startKioskService() {
startService(new Intent(this, KioskService.class));
}
}
\ No newline at end of file
package com.yottacode.pictogram.kiosk;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.yottacode.pictogram.gui.MainActivity;
/**
* Created by emblanco on 13/10/15.
*
*
* Recieve event when the device boot and starts MainActivity
*
*
*/
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
\ No newline at end of file
package com.yottacode.pictogram.kiosk;
import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import com.yottacode.pictogram.gui.PictogramActivity;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created by emblanco on 15/10/15.
*
* Since Android 4 there is no effective method to deactivate the home button.
* That is the reason why we need to do a little hack.
* In general the idea is to detect when a new application is in foreground and restart
* your activity immediately.
*
* The thread checks every two seconds if the app is running in foreground.
* If not, the thread will immediately recreate your activity.
*
*/
public class KioskService extends Service {
private static final long INTERVAL = TimeUnit.SECONDS.toMillis(2); // periodic interval to check in seconds -> 2 seconds
private static final String TAG = KioskService.class.getSimpleName();
private static final String PREF_KIOSK_MODE = "pref_kiosk_mode";
private Thread t = null;
private Context ctx = null;
private boolean running = false;
@Override
public void onDestroy() {
Log.i(TAG, "Stopping service 'KioskService'");
running =false;
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Starting service 'KioskService'");
running = true;
ctx = this;
// start a thread that periodically checks if your app is in the foreground
t = new Thread(new Runnable() {
@Override
public void run() {
do {
handleKioskMode();
try {
Thread.sleep(INTERVAL);
} catch (InterruptedException e) {
Log.i(TAG, "Thread interrupted: 'KioskService'");
}
}while(running);
stopSelf();
}
});
t.start();
return Service.START_NOT_STICKY;
}
private void handleKioskMode() {
// is Kiosk Mode active?
if(isKioskModeActive(this)) {
// is App in background?
if(isInBackground()) {
restoreApp(); // restore!
}
}
}
private boolean isInBackground() {
ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
return (!ctx.getApplicationContext().getPackageName().equals(componentInfo.getPackageName()));
}
private void restoreApp() {
// Restart activity
Intent i = new Intent(ctx, PictogramActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
}
public boolean isKioskModeActive(final Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
return sp.getBoolean(PREF_KIOSK_MODE, false);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
\ No newline at end of file
package com.yottacode.pictogram.kiosk;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
/**
* Created by emblanco on 14/10/15.
*
*
Short power button press:
It can detect a short button press by handling the ACTION_SCREEN_OFF intent and kick the screen
back to life with acquiring a wake lock.
*
*
*
*/
public class OnScreenOffReceiver extends BroadcastReceiver {
private static final String PREF_KIOSK_MODE = "pref_kiosk_mode";
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_SCREEN_OFF.equals(intent.getAction())){
AppContext ctx = (AppContext) context.getApplicationContext();
// is Kiosk Mode active?
if(isKioskModeActive(ctx)) {
wakeUpDevice(ctx);
}
}
}
private void wakeUpDevice(AppContext context) {
PowerManager.WakeLock wakeLock = context.getWakeLock(); // get WakeLock reference via AppContext
if (wakeLock.isHeld()) {
wakeLock.release(); // release old wake lock
}
// create a new wake lock...
wakeLock.acquire();
// ... and release again
wakeLock.release();
}
private boolean isKioskModeActive(final Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
return sp.getBoolean(PREF_KIOSK_MODE, false);
}
}
package com.yottacode.pictogram.net;
import android.app.ActivityManager;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.tools.Img;
import com.yottacode.pictogram.tools.PCBcontext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;
/**
* Image downloader from the server or local for pictograms and student's and supervisor's profile pictures.
* Permissions required in AndroidManifest.xml:
* <uses-permission android:name="android.permission.INTERNET" />
*/
public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
iImgDownloaderListener imgListener;
public static enum status {downloading, downloaded_ok, downloaded_failed}
public static enum tsource{remote,local}
public status current;
private boolean force_download;
Context context;
ActivityManager.MemoryInfo mi;
ActivityManager activityManager;
tsource source;
public ImgDownloader(Context context, iImgDownloaderListener listener, tsource source) {
this.imgListener = listener;
this.context=context;
this.mi = new ActivityManager.MemoryInfo();
this.activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
this.force_download=context.getResources().getBoolean(R.bool.force_img_download);
this.source=source;
}
@Override
protected void onPreExecute() {
}
protected Img doInBackground( Vector<Img> imgs) {
InputStream is=null;
Log.d(this.getClass().getCanonicalName(), "Required images: " + imgs.size());
this.current= ImgDownloader.status.downloading;
int i=0,j=0,allsize=0;
Long availableMegs = mi.availMem / 1048576L;
int seconds = Calendar.getInstance().get(Calendar.SECOND);
try {
for (Img img: imgs) {
if (!img.exists_bitmap(this.context) || this.force_download) try {
this.activityManager.getMemoryInfo(mi);
if (this.source==source.remote) {
String surl = context.getResources().getString(R.string.server) + "/" + img.get_url();
URL url = new URL(surl);
URLConnection ucon = url.openConnection();
is = ucon.getInputStream();
}else {
File file=new File(img.get_url());
is=new FileInputStream(file);
}
int size=img.save_bitmap(this.context, is);
allsize+=size;
i++;
} catch (IOException e) {
j++;
Log.e(this.getClass().getCanonicalName(), img.get_url() + " was not downloaded: "+e);
} finally {
if (is != null) is.close();
}
}
this.current= status.downloaded_ok;
} catch (IOException e) {
Log.d(this.getClass().getCanonicalName(), "Error: " + e);
this.current= status.downloaded_failed;
}
seconds=Calendar.getInstance().get(Calendar.SECOND)-seconds;
Log.i(this.getClass().getCanonicalName(),
"Images Downloaded: " + i+"/"+imgs.size()+ " (size:"+allsize/1024+" Kbytes)"+
". Cached: "+ (imgs.size()-i)+"/"+imgs.size()+
". Download failed: "+ j+"/"+imgs.size()+
". Memory required:"+((mi.availMem / 1048576L)-availableMegs)+" MB"+
". Used time: "+seconds+" seconds at "+new SimpleDateFormat("HH:mm:ss"));
return imgs.size() > 1 ? null
: imgs.get(0);
}
@Override
protected Img doInBackground(Vector... params) {
doInBackground(params[0]);
return null;
}
@Override
protected void onPostExecute(Img img) {
if (imgListener!=null)
if(img == null) imgListener.loadComplete();
else imgListener.loadImg(img);
}
}
package com.yottacode.pictogram.net;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.yottacode.net.RestapiWrapper;
import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.dao.LoginException;
import com.yottacode.pictogram.dao.User;
import com.yottacode.pictogram.gui.PictogramActivity;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Background services to be executed every "delay" seconds. Tasks to be executed:
* If offline mode (there is no server connection):
* - keep local record of actions
* If online mode:
* - reload vocabulary
* - move records into the server of actions and purge the actions pool
* @author Fernando Martinez Santiago
* @version 1.0
*/
public class NetService implements Runnable {
public static final String PREFS_NAME = "MyPrefsFile";
private static NotificationCompat.Builder builder;
static final String ping_session="server/ping";
private boolean updated;
public NetService(int delay) {
this.updated=RestapiWrapper.ping(PCBcontext.getContext().getResources().getString(R.string.server), ping_session);
this.builder = new NotificationCompat.Builder(PCBcontext.getContext());
Intent resultIntent = new Intent(PCBcontext.getContext(), PictogramActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(PCBcontext.getContext());
stackBuilder.addParentStack(PictogramActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
Log.i(this.getClass().getName(), "Checking Pictogram server access...");
Log.i(this.getClass().getName(), this.updated ? "Pictogram server access ok" : "Pictogram server access failed, Internet connection available?");
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.scheduleWithFixedDelay(this, 0, delay, TimeUnit.SECONDS);
}
public void login() {
User user=PCBcontext.getPcbdb().getCurrentUser();
if (user.is_supervisor())
ServerLogin.login_supervisor(user.get_email_sup(), user.get_pwd_sup(), new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
PCBcontext.getPcbdb().user_online(true);
}
@Override
public void result(JSONObject result) {
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getSimpleName(), "Error un when server login:" + e.getMessage());
if (e instanceof LoginException) PCBcontext.restart_app();
}
});
else ServerLogin.login_student(user.get_nickname_stu(), user.get_pwd_stu(), new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
PCBcontext.getPcbdb().user_online(true);
}
@Override
public void result(JSONObject result) {
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getSimpleName(),"Error un when server login:"+e.getMessage());
if (e instanceof LoginException) PCBcontext.restart_app();;
}
});
}
public boolean online() {return updated;}
/**
* ping to the server by using a restapi call. If ok, the call will return the empty set
*/
@Override
public void run() {
PCBcontext.getRestapiWrapper().ask(ping_session, new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
if (!updated) {
updated = true;
if (PCBcontext.is_user_logged()) //si el usuario aun no hizo login, en realidad no es necesario hacer nada
// Comprobar si hay usuario offline, para hacer login transparente
if (PCBcontext.is_user_offline()){
login();
} else if (PCBcontext.is_user_online()){
Log.i(this.getClass().getName(), "PCB reconnect");
PCBcontext.getRoom().reconnect();
PCBcontext.getVocabulary().synchronize();
PCBcontext.getActionLog().batch();
}
}
Log.i(this.getClass().getName(), "PCB status checked. Updated? " + updated);
notifyStatus();
}
@Override
public void error(Exception e) {
updated = false;
Log.i(this.getClass().getName(), "PCB offline because exception happens: " + e.getMessage());
notifyStatus();
}
});
}
public void notifyStatus() {
int notifyID = 1;
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() + ")";
}
if (updated)
builder.setSmallIcon(R.drawable.application_online)
.setContentTitle(PCBcontext.getContext().getResources().getString(R.string.pictogram_online))
.setContentText(user);
else
builder.setSmallIcon(R.drawable.application_offline)
.setContentTitle(PCBcontext.getContext().getResources().getString(R.string.pictogram_offline))
.setContentText(user);
NotificationManager mNotificationManager =
(NotificationManager) PCBcontext.getContext().getSystemService(PCBcontext.getContext().NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID, builder.build());
}
public void closeNotifyStatus(){
int notifyID = 1;
NotificationManager mNotificationManager =
(NotificationManager) PCBcontext.getContext().getSystemService(PCBcontext.getContext().NOTIFICATION_SERVICE);
mNotificationManager.cancel(notifyID);
}
}
package com.yottacode.pictogram.net;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.google.gson.JsonObject;
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.VocabularyAction;
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;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
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;
}
private int uploadImg( Img img) throws UnsupportedEncodingException {
int img_id;
Bitmap bmp=null;
if (!img.get_filetype().equalsIgnoreCase("png"))
throw new UnsupportedEncodingException("Extension "+img.get_filetype()+" is not supported. Only png files");
Ion ion = Ion.getDefault(PCBcontext.getContext());
try {
bmp=img.get_bitmap(PCBcontext.getContext());
} catch (IOException e) {
Log.e(Img.class.getCanonicalName(), "Error when decoding "+img.file_name());
}
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());
Response<JsonObject> response=null;
try {
response=ion.with(PCBcontext.getContext())
.load("POST", PCBcontext.getContext().getResources().getString(R.string.server) + "/picto/upload")
.setMultipartParameter("filename", Integer.toString(img.get_id()))
.setMultipartParameter("extension", img.get_filetype())
.setMultipartParameter("owner", Integer.toString(PCBcontext.getPcbdb().getCurrentUser().get_id_sup()))
.setMultipartParameter("folder", "custompictos")
.setMultipartParameter("source", "2")
.setMultipartParameter("token", PCBcontext.getRestapiWrapper().getToken())
.setMultipartFile("file", "image/png", file)
.asJsonObject()
.withResponse().get();
if (response != null && response.getHeaders().code() == 200) {
Log.i(this.getClass().getCanonicalName(), "Uploaded image result: " + response.getHeaders() + ":" + response.getResult());
img_id=response.getResult().get("id").getAsInt();
} else {
img_id=-1;
Log.i(this.getClass().getCanonicalName(), "Uploaded image failed ");
if (response != null)
Log.i(this.getClass().getCanonicalName(), "Uploaded image failed, headers: " + response.getHeaders());
}
} catch (InterruptedException e) {
Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage()+ "Code: "+
(response == null ? -1 : response.getHeaders().code()));
img_id=-1;
} catch (ExecutionException e) {
Log.e(this.getClass().getCanonicalName(), "Image upload error: " + e.getMessage()+
(response == null ? -1 : response.getHeaders().code()));
img_id=-1;
}
// ion.dump();
return img_id;
}
/**
* if a picto was included from the PCB, the translation is uploaded to the server
*/
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());
if (PCBcontext.getPcbdb().getCurrentUser().has_categories())
json_attrs.put(Picto.JSON_ATTTRS.CATEGORY, picto.get_category())
.put(Picto.JSON_ATTTRS.COLUMN, picto.get_column())
.put(Picto.JSON_ATTTRS.ROW, picto.get_row());
else
json_attrs.put(Picto.JSON_ATTTRS.CATEGORY, picto.NO_CATEGORY)
.put(Picto.JSON_ATTTRS.FREE_COLUMN, picto.getFreeColumn())
.put(Picto.JSON_ATTTRS.FREE_ROW, picto.getFreeRow());
params.put("json", new JSONObject().put("attributes",json_attrs).toString());
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(), " Error: " + e.getLocalizedMessage());
}
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto/"+id_picto, params, "post", true, new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@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());
listener.success(false);
}
});
}
/**
* if the a picto was included from the PCB, the translation is uploaded to the server
*/
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));
params.put("lang", PCBcontext.getPcbdb().getCurrentUser().get_lang_stu());
params.put("text", picto.get_translation());
PCBcontext.getRestapiWrapper().ask("picto/exp", params, "post", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
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"));
listener.success(false);
}
});
}
/**
*Try to Upload local picto. It requires:
* i) to upload the image,
* ii) to upload the attributes
* iii) to upload the expression
*
**/
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) {
PCBcontext.getPcbdb().deletePicto(local_img_id);
PictoUploader.this.picto.delete_bitmap(PCBcontext.getContext());
PictoUploader.this.picto.update_id(img_id);
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
*
*/
public void uploadState( ){
Hashtable<String, String> params = new Hashtable<String, String>(1);
params.put("attributes", picto.get_json_server_attrs());
params.put("id_stu", Integer.toString(PCBcontext.getPcbdb().getCurrentUser().get_id_stu()));
params.put("id_pic", Integer.toString(this.picto.get_id()));
Log.i(this.getClass().getCanonicalName(), "Picto Uploading " + params.toString());
PCBcontext.getRestapiWrapper().ask(
PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto",
params, "put", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
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());
}
}
);
}
}
package com.yottacode.pictogram.net;
import android.util.Log;
import com.yottacode.net.RestapiWrapper;
import com.yottacode.net.iRestapiListener;
import com.yottacode.pictogram.dao.LoginException;
import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Hashtable;
/**
* Created by Fernando on 01/04/2016.
*/
public class ServerLogin {
public static void login_student(String username, String password, iRestapiListener listener) {
login("stu/login", null, username, password, listener);
}
public static void login_supervisor(String email, String password, iRestapiListener listener) {
login("sup/login", email, null, password, listener);
}
private static void login(String operation, String email, String username, String password, final iRestapiListener listener){
Hashtable<String, String> postDataParams = new Hashtable<String, String>();
if (email!=null) postDataParams.put("email", email);
if (username!=null) postDataParams.put("username", username);
postDataParams.put("password", password);
PCBcontext.getRestapiWrapper().ask(operation, postDataParams, "post", new iRestapiListener() {
@Override
public void preExecute() {
listener.preExecute();
}
@Override
public void result(JSONArray result) {
listener.result(result);
}
@Override
public void result(JSONObject result) {
try {
if (PCBcontext.is_user_offline()) {
final String TAG_TOKEN="token";
PCBcontext.getPcbdb().user_online(true);
PCBcontext.getRestapiWrapper().setToken(result.getString(TAG_TOKEN));
PCBcontext.getVocabulary().synchronize();
}
listener.result(result);
} catch (JSONException e) {
listener.error(e);
}
}
@Override
public void error(Exception e) {
listener.error(new LoginException(e.getMessage(),e.getMessage().contains("User without students")
? LoginException.NO_STUDENTS
: e.getMessage().contains("failed to connect")
? LoginException.TIME_OUT
: LoginException.BAD_LOGIN));
}
});
}
}
package com.yottacode.pictogram.net;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.Img;
import java.util.LinkedList;
/**
* Created by emblanco on 24/09/15.
* MOdified by dofer on 27/06/16.
*/
public interface iImgDownloaderListener {
public void loadComplete(); // for loading the vocabulary
public void loadImg(Img image); // for loading one image
public void error(Exception err); //error happens
}
package com.yottacode.pictogram.net;
/**
* Created by Fernando on 28/07/2016.
*/
public interface iPictoUploaderListener {
void success(boolean success);
}
package com.yottacode.pictogram.tools;
/**
* Created by miguelangelgarciacumbreras on 25/7/15.
*/
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
/**
* Image with support for filtering.
*/
public class FilteredImage {
private Bitmap image, mutableBitmap;
private int width;
private int height;
private Bitmap.Config config;
private int[] colorArray;
/**
* Constructor.
*
* @param img the original image
*/
public FilteredImage(Bitmap img) {
this.image = img;
width = img.getWidth();
height = img.getHeight();
config = img.getConfig();
colorArray = new int[width * height];
image.getPixels(colorArray, 0, width, 0, 0, width, height);
mutableBitmap = image.copy(Bitmap.Config.ARGB_8888, true);
applyHighlightFilter();
//addBorder(5, Color.RED);
}
/**
* Get the color for a specified pixel.
*
* @param x x
* @param y y
* @return color
*/
public int getPixelColor(int x, int y) {
return colorArray[y * width + x];
}
/**
* Gets the image.
*
* @return Returns the image.
*/
public Bitmap getImage() {
return mutableBitmap;
}
/**
* Applies highlight filter to the image.
*/
private void applyHighlightFilter() {
int a;
int r;
int g;
int b;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int c = getPixelColor(x, y);
a = Color.alpha(c);
r = Color.red(c);
g = Color.green(c);
b = Color.blue(c);
r = (int) (r * 0.4);
g = (int) (g * 0.4);
b = (int) (b * 1.6);
if (r > 255) {
r = 255;
}
if (r < 0) {
r = 0;
}
if (g > 255) {
g = 255;
}
if (g < 0) {
g = 0;
}
if (b > 255) {
b = 255;
}
if (b < 0) {
b = 0;
}
int resultColor = Color.argb(a, r, g, b);
//Log.e("applyHighlightFilter", "x:" + x + "/y:" + y + "/resultColor:" + resultColor);
mutableBitmap.setPixel(x, y, resultColor);
}
}
}
private void addBorder(int borderSize, int color) {
mutableBitmap = Bitmap.createBitmap(this.width + borderSize * 2, this.height + borderSize * 2, this.config);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawColor(color);
canvas.drawBitmap(image, borderSize, borderSize, null);
}
}
\ No newline at end of file
package com.yottacode.pictogram.tools;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.yottacode.tools.FileTools;
import com.yottacode.tools.BitmapTools;
/**
* Img
* @author Fernando
*/
public class Img {
static public String VOCABULARY="vocabulary";
static public String STUDENT="student";
static public String SUPERVISOR="supervisor";
static public float MAX_WIDTH=100;
public static final void mkDirs(Context context) {
File file;
file = new File(path(context, Img.VOCABULARY));
FileTools.deleteDirectory(file);
file.mkdirs();
file = new File(path(context, Img.STUDENT));
FileTools.deleteDirectory(file);
file.mkdirs();
file = new File(path(context, Img.SUPERVISOR));
FileTools.deleteDirectory(file);
file.mkdirs();
}
protected int id;
protected String url;
String type;
final static String FILETYPE="png";
Bitmap bitmap;
/**
*
* @param id
* @param url
* @param type is Vocabulary, Student or Supervisor
*/
public Img(int id, String url, String type) {
this.id = id;
this.url = url;
this.type=type;
this.bitmap=null;
}
public String file_name() { return this.id+"."+Img.FILETYPE; }
public int get_id() { return this.id;}
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_filetype() { return Img.FILETYPE;}
/**
* Load if necessary the bitmap from disk, and it is returned. IIf it is not available, return null
* @param context
* @return the bitmap
* @throws FileNotFoundException
*/
public Bitmap get_bitmap(Context context) throws IOException {
if (this.bitmap==null) {
File file = file(context);
if (file.exists()) {
FileInputStream is = new FileInputStream(file);
this.bitmap = BitmapFactory.decodeStream(is);
is.close();
}
}
return this.bitmap;
}
/**
* Delete the bitmap from disk
* @param context
*/
public void delete_bitmap(Context context) {
File file = file(context);
file.delete();
}
/**
*
* @param context
* @return true if the bitmap is locally stored
*/
public boolean exists_bitmap(Context context) {
return file(context).exists();
}
public static String path(Context context, String type) {
String path = context.getFilesDir()+File.separator +"pictures" + File.separatorChar + type + File.separatorChar;
return path;
}
/**
* The local file where the img is saved. It depends on the image type
* @param context
* @return
*/
public File file(Context context) {
String path=path(context, this.type);
return new File(path, file_name());
}
/**
* Save an image in disk
* @param context
* @param is the stream where the image is available
* @throws IOException
*/
public int save_bitmap(Context context, InputStream is) throws IOException {
File file = file(context);
FileOutputStream os = new FileOutputStream(file);
try {
this.bitmap=BitmapFactory.decodeStream(is);
if (this.bitmap.getWidth()>MAX_WIDTH) {
this.bitmap = new BitmapTools(this.bitmap)
.rescale(MAX_WIDTH / (float) this.bitmap.getWidth())
.get();
}
}catch(java.lang.OutOfMemoryError err) {
Log.e(Img.class.getCanonicalName(), "Out of memory when decoding "+this.get_url());
}
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
this.bitmap.setHasAlpha(true);
this.bitmap.compress(Bitmap.CompressFormat.PNG, 50, outstream);
byte[] byteArray = outstream.toByteArray();
os.write(byteArray);
outstream.close();
os.close();
System.gc();
return byteArray.length;
}
/**
*
* @overide
* Two img are equals whenever they have the same id and type
*/
public boolean equals(Object o) {
return ((Img)o).id==this.id && ((Img)o).type.equals(this.type);
}
/**
* @overide
*/
public String toString() {return this.type+"."+this.id;}
}
package com.yottacode.pictogram.tools;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.yottacode.net.RestapiWrapper;
import com.yottacode.net.SSLDummyContext;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.action.ActionLog;
import com.yottacode.pictogram.dao.Device;
import com.yottacode.pictogram.dao.PCBDBHelper;
import com.yottacode.pictogram.dao.User;
import com.yottacode.pictogram.grammar.Vocabulary;
import com.yottacode.pictogram.gui.SerialActivity;
import com.yottacode.pictogram.net.NetService;
import com.yottacode.pictogram.action.Room;
import com.yottacode.pictogram.net.iImgDownloaderListener;
public final class PCBcontext {
private static Context context;
private static PCBDBHelper pcbdb;
private static Device device=null;
private static NetService service;
private static Room room;
private static RestapiWrapper wrapper;
private static Vocabulary vocabulary;
private static ActionLog actionLog;
private static boolean init=false;
/**
* Init method for passing params to the singleton
* @param c @TODO application or activity context?
*/
public static void init(Context c){
if (!init) {
init = true;
context = c;
device = new Device(c, null, 1);
SSLDummyContext.init(context.getResources().getBoolean(R.bool.ssl_connect));
wrapper = new RestapiWrapper(context.getResources().getString(R.string.server), null);
service = new NetService(context.getResources().getInteger(R.integer.netservice_timing));
device.deleteDeprecatedImgs();
Log.i(PCBcontext.class.getCanonicalName(), "PCB context started. It's required" +
"set_user method call");
} else {
Log.e(PCBcontext.class.getClass().getCanonicalName(), "Init method was previously" +
"invoked! Please, check your code");
}
}
/**
* @param student
* @param listener
*/
public static void set_user(User student, String token, iImgDownloaderListener listener) {
if (!init) {
throw new java.lang.AssertionError("init must be called once previously ");
}
Log.i(PCBcontext.class.getCanonicalName(), "User set at student " + student.get_name_stu());
wrapper.setToken(token);
pcbdb = new PCBDBHelper(null, 1, student);
pcbdb.user_online(token!=null);
room = new Room();
actionLog = new ActionLog();
vocabulary = new Vocabulary(listener);
getNetService().notifyStatus();
}
/**
*
* @return true if a given user has been logged into the system (the login window was successfully filled)
*/
public static boolean is_user_logged() {
return (init && pcbdb!=null);
}
/**
*
*/
public static void unset_user() {
pcbdb = null;
room = null;
vocabulary = null;
getNetService().notifyStatus();
}
/**
* if the user becomes from offline to online and the login is failed, then the user relogin in
* the app. The most frequent reason is a password change while the user have been logged
* offline
*/
public static void restart_app() {
SerialActivity.resetDefaultUser();
Intent serialActivity = new Intent(PCBcontext.getContext(), SerialActivity.class);
serialActivity.putExtra("resetPrevUser", true);
PCBcontext.getContext().startActivity(serialActivity);
}
/**
* @return true if the user is logged offline and it has not been online previously. False in
* other case (user not logged, user logged online, user offline but it was previously online
* logged
*/
public static boolean is_user_offline() {
if (pcbdb == null) {
return false;
} else {
return !getPcbdb().isUser_online();
}
}
public static boolean is_user_online() {
return pcbdb != null && getPcbdb().isUser_online();
}
/**
* @return context
* @TODO complete documentation
*/
public static Context getContext() {
if (context == null) {
throw new java.lang.NullPointerException("Context is null. PCBcontext.init must be" +
"invoked previously");
}
return context;
}
/**
* @return device
* @TODO complete documentation
*/
public static Device getDevice() {
if (device == null) {
throw new java.lang.NullPointerException("Device is null. PCBcontext.init must be" +
"invoked previously");
}
return device;
}
/**
* @return PCBDB
* @TODO complete documentation
*/
public static PCBDBHelper getPcbdb() {
if (context == null) {
throw new java.lang.NullPointerException("PCBDB is null. PCBcontext.set_user must be" +
"invoked previously");
}
return pcbdb;
}
/**
* @return NetService
* @TODO complete documentation
*/
public static NetService getNetService(){
if (service == null) {
throw new java.lang.NullPointerException("NetService is null. PCBcontext.set_user" +
"must be invoked previously");
}
return service;
}
/**
* @return RestapiWrapper
* @TODO complete documentation
*/
public static RestapiWrapper getRestapiWrapper(){
if (wrapper == null) {
throw new java.lang.NullPointerException("RestapiWrapper is null. PCBcontext.init" +
"must be invoked previously");
}
return wrapper;
}
/**
* @return Room
* @TODO complete documentation
*/
public static Room getRoom(){
if (room == null) {
throw new java.lang.NullPointerException("Room is null. PCBcontext.set_user must" +
"be invoked previously");
}
return room;
}
/**
* @return Vocabulary
* @TODO complete documentation
*/
public static Vocabulary getVocabulary(){
if (vocabulary == null) {
throw new java.lang.NullPointerException("Vocabulary is null. PCBcontext.set_user" +
"must be invoked previously");
}
return vocabulary;
}
/**
* @return ActionLog
* @TODO complete documentation
*/
public static ActionLog getActionLog(){
if (actionLog == null) {
throw new java.lang.NullPointerException("ActionLog is null. PCBcontext.set_user" +
"must be invoked previously");
}
return actionLog;
}
}
package com.yottacode.pictogram.tts;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import android.annotation.TargetApi;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.content.Context;
import android.speech.tts.Voice;
import android.util.Log;
import android.widget.ArrayAdapter;
/**
* PCB TTS service
* @author Fernando Martinez Santiago
* @version 1.0
*/
public class TTSHelper {
TextToSpeech ttobj;
public boolean engine_ok;
public boolean voice_ok=true;
Voice voice;
public void createTTS(Context context, String engine) {
this.destroy();
/* this.ttobj = new TextToSpeech(
context,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
ttobj.setLanguage(Locale.getDefault());
}
}
}, engine);*/
this.engine_ok=true;
Log.e(context.getApplicationInfo().processName,"TTS engine "+engine+" loaded");
}
public TTSHelper(Context context) {
createTTS(context ,null);
}
public TTSHelper(Context context, String engine) {
try {
createTTS(context ,engine);
}catch (Exception e) {
createTTS(context ,null);
Log.e(context.getApplicationInfo().processName, "Engine "+engine+" error",e);
this.engine_ok=false;
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setVoice(Context context, String voice) {
try {
Set<Voice> voices = this.ttobj.getVoices();
if (voices!=null)
for (Voice avoice : voices) {
Log.e(context.getApplicationInfo().processName,"Voice name "+avoice.getName());
if (avoice.getName().compareTo(voice) == 0) {
this.ttobj.setVoice(avoice);
this.voice_ok=true;
Log.e(context.getApplicationInfo().processName,"Voice "+voice+" loaded");
break;
}
}
else
Log.e(context.getApplicationInfo().processName,"Voice "+voice+" is not available");
}catch(Exception e) {
Log.e(context.getApplicationInfo().processName,"Voice "+voice+" error",e);
this.voice_ok=false;
}
}
public boolean isVoice_ok() { return this.voice_ok;}
public boolean isEngine_ok() { return this.engine_ok;}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void speakText(String toSpeak){
this.ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH,null, toSpeak.hashCode()+"");
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void show(ArrayAdapter mArrayAdapter) {
String tts_info[] = new String[25];
int i=0;
List<TextToSpeech.EngineInfo> engines = this.ttobj.getEngines();
for (Iterator<TextToSpeech.EngineInfo> iter = engines.iterator(); iter.hasNext(); ) {
TextToSpeech.EngineInfo element = iter.next();
Log.i("TTSHelper","TTS: "+element.name + " label:"+element.label);
mArrayAdapter.add("TTS: "+element.name + " label:"+element.label);
}
if (this.ttobj.getVoice()!=null) {
Log.i("TTSHelper", "Default voice:" + this.ttobj.getDefaultVoice().getName());
mArrayAdapter.add("Current voice:" + this.ttobj.getVoice().getName());
Set<Voice> voices = this.ttobj.getVoices();
if (voices != null)
for (Voice voice : voices) {
Log.i("TTSHelper", "Voice: " + voice.getName() + " locale:" + voice.getLocale());
mArrayAdapter.add("Voice: " + voice.getName() + " locale:" + voice.getLocale());
}
}
}
public void destroy() {
if(ttobj !=null){
ttobj.stop();
ttobj.shutdown();
}
this.ttobj=null;
}
}
\ No newline at end of file
package com.yottacode.tools;
import android.graphics.Bitmap;
import android.graphics.Matrix;
/**
* Created by Fernando on 15/03/2016.
*/
public class BitmapTools {
Bitmap bitmap;
public BitmapTools(Bitmap bitmap) { this.bitmap=bitmap;}
public Bitmap get() {return this.bitmap;}
public BitmapTools resize(int w, int h) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = w;
int newHeight = h;
// calculamos el escalado de la imagen destino
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// para poder manipular la imagen
// debemos crear una matriz
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// volvemos a crear la imagen con los nuevos valores
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
width, height, matrix, true);
this.bitmap=resizedBitmap;
return this;
}
public BitmapTools rescale(float scale) {
int width = (int) (scale * (float) this.bitmap.getWidth());
int height = (int) (scale * (float) this.bitmap.getHeight());
return resize(width,height);
}
public BitmapTools paintSquare(int thickness, int color ) {
for (int i = 0; i < this.bitmap.getWidth(); i++)
for (int t = 1; t <= thickness; t++) {
this.bitmap.setPixel(i, 0 + t-1, color);
this.bitmap.setPixel(i, this.bitmap.getHeight() - t, color);
}
for (int i = 0; i < this.bitmap.getHeight(); i++)
for (int t = 1; t <= thickness; t++) {
this.bitmap.setPixel(0 + t-1, i, color);
this.bitmap.setPixel(this.bitmap.getWidth() - t, i, color);
}
return this;
}
}
package com.yottacode.tools;
import java.io.File;
/**
* Created by Fernando on 14/03/2016.
*/
public class FileTools {
public static boolean deleteDirectory(File path) {
if( path.exists() ) {
File[] files = path.listFiles();
if (files == null) {
return true;
}
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return( path.delete() );
}
}
package com.yottacode.tools;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
/**
* Created by Fernando on 01/04/2016.
*/
public class GUITools {
public interface iOKListener {
public void ok();
}
public static void show_alert(Context context, int resource_msg, String additional_msg, final iOKListener oklistener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
String msg = context.getString(resource_msg);
if (additional_msg != null) msg += ": " + additional_msg;
Log.i(GUITools.class.getName(), "Alert:" + msg);
builder.setMessage(msg)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (oklistener != null) oklistener.ok();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public static void show_alert(Context context, int resource_msg) {
show_alert(context, resource_msg, null);
}
public static void show_alert(Context context, int resource_msg, String additional_msg) {
show_alert(context, resource_msg, additional_msg,null);
}
}
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