parent 89225832
......@@ -157,11 +157,12 @@ public class RestapiWrapper {
JSONresponse = new JSONObject("{ "+SERVER_RESULT+": " + response + (responseCode == HttpURLConnection.HTTP_OK
? "}"
: ", "+SERVER_ERROR+": " + responseCode +"}"));
Log.i(LOG_TAG, "Server answer: " + JSONresponse.toString());
} catch (JSONException e) {
JSONresponse = null;
Log.e(RestapiWrapper.class.getCanonicalName(),e.getMessage());
}
Log.i(LOG_TAG, "Server answer: " + JSONresponse.toString());
return JSONresponse;
}
......
......@@ -2,6 +2,7 @@ package com.yottacode.pictogram.dao;
import android.graphics.Color;
import android.os.Environment;
import android.util.Log;
import com.yottacode.pictogram.action.VocabularyAction;
......@@ -12,6 +13,11 @@ import com.yottacode.pictogram.tools.PCBcontext;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* A object which represents a pictogram
......@@ -24,6 +30,7 @@ public class Picto extends Img {
private static final String LOG_TAG =Img.class.getName();
public static final int STUPICTO_NULL = -1;
public int get_stupicto_id() {
int stupicto_id;
try {
......@@ -90,7 +97,7 @@ public class Picto extends Img {
Log.e(LOG_TAG,e.getMessage());
}
}
public Picto(int id, String url, String translation, int cat, int row, int column, int freeRow, int freeColumn, int stupicto_id, String uri_sound, String user_avatar) throws JSONException {
public Picto(int id, String url, String translation, int cat, int row, int column, int freeRow, int freeColumn, int stupicto_id, String user_avatar) throws JSONException {
this(id, url, new JSONObject()
.put(JSON_ATTTRS.CATEGORY, cat)
.put(JSON_ATTTRS.COLUMN, column)
......@@ -101,7 +108,6 @@ public class Picto extends Img {
.put(JSON_ATTTRS.LEGEND,JSON_ATTTR_LEGEND_VALUES.NONE)
.put(JSON_ATTTRS.STUPICTO_ID,stupicto_id)
.put(JSON_ATTTRS.EXPRESSION,translation)
.put(JSON_ATTTRS.URI_SOUND,uri_sound)
.put(JSON_ATTTRS.USER_AVATAR,user_avatar)
);
}
......@@ -122,6 +128,32 @@ public class Picto extends Img {
}
public static String getAudioPictosPath() {
return Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_MUSIC+File.separator +"PictoAudio";
}
public String get_audioPath() {
String audioPath=getAudioPictosPath();
return audioPath+File.separator+"pictosound_"+PCBcontext.getPcbdb().getCurrentUser().get_id_stu()+"_"+this.get_id()+".mp3";
}
public File get_audioFile() {
File file = new File(get_audioPath());
return file.exists() ? file : null;
}
/*
* @override
*/
public void update_id(int id) {
if (get_audioFile()!=null) { //es necesario actualizar el nombre del audio, si lo hay
File from = get_audioFile();
super.update_id(id);
File to = new File(get_audioPath());
from.renameTo(to);
}
else
super.update_id(id);
}
/**
*
* @return true if it's a local pictogram
......@@ -304,11 +336,12 @@ public class Picto extends Img {
* @return the associated person of a picto
*/
public String get_user_avatar() {
String associated_user = null;
String associated_user;
try {
associated_user = this.attributes.getString(JSON_ATTTRS.USER_AVATAR);
associated_user = this.attributes.has(JSON_ATTTRS.USER_AVATAR) ?this.attributes.getString(JSON_ATTTRS.USER_AVATAR) : null;
} catch (JSONException e) {
e.printStackTrace();
associated_user=null;
}
return associated_user;
}
......@@ -365,19 +398,30 @@ public class Picto extends Img {
* @return the uri of associated sound of the picto
*/
public String getUriSound(){
String uri;
try {
return this.attributes.getString(JSON_ATTTRS.URI_SOUND);
uri = this.attributes.has(JSON_ATTTRS.URI_SOUND) && !this.attributes.getString(JSON_ATTTRS.URI_SOUND).equals("null")
? this.attributes.getString(JSON_ATTTRS.URI_SOUND) : null;
} catch (JSONException e) {
e.printStackTrace();
uri=null;
Log.e(LOG_TAG,"Error getting URI sound from JSON:"+e.getMessage());
}
return uri;
}
public void setUriSound(String uriSound){
try {
this.attributes.put(JSON_ATTTRS.URI_SOUND,uriSound);
} catch (JSONException e) {
Log.e(LOG_TAG,"Error setting uri sound:"+e.getMessage());
}
return null;
}
/**
*
* @return the uri of associated sound of the picto
*/
public void setUriSound(String path){
public void ok(String path){
try {
this.attributes.put(JSON_ATTTRS.URI_SOUND, path);
} catch (JSONException e) {
......@@ -457,6 +501,7 @@ public class Picto extends Img {
public void set_local_status(boolean local) {
if (local)
try {
this.attributes.remove(JSON_ATTTRS.STUPICTO_ID);
this.attributes.put(JSON_ATTTRS.PCB_STATUS_MODIFICATION, true);
PCBcontext.getPcbdb().modifyPicto(this.get_id(), this.get_json_attrs());
} catch (JSONException e) {
......@@ -470,4 +515,25 @@ public class Picto extends Img {
}
}
/**
* Save an picto into disk
* @param is the stream where the image is available
* @throws IOException
*/
public void save_sound(InputStream is) throws IOException {
File file = new File(get_audioPath());
if (file.exists())
file.delete();
FileOutputStream os = new FileOutputStream(file);
int count;
byte buffer[]=new byte[1024];
while ((count = is.read(buffer)) != -1) {
os.write(buffer, 0, count);
}
os.flush();
os.close();
System.gc();
}
}
......@@ -20,6 +20,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
......@@ -255,7 +256,7 @@ public class Vocabulary implements Iterable<Picto> {
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));
imgs.add(picto);
}
Log.d(this.getClass().getName(), "Vocabulary size: " + updated_collection.length);
ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener,ImgDownloader.tsource.remote);
......@@ -273,7 +274,7 @@ public class Vocabulary implements Iterable<Picto> {
public void addPicto(Picto pic, ImgDownloader.tsource source, ImgDownloader.iImgDownloaderListener imgListener){
Vector<Img> imgs=new Vector<Img>(1);
imgs.add(new Img(pic.get_id(), pic.get_url(), Img.VOCABULARY));
imgs.add(pic);
ImgDownloader downloader = new ImgDownloader(PCBcontext.getContext(), imgListener,source);
downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, imgs);
......@@ -328,7 +329,7 @@ public class Vocabulary implements Iterable<Picto> {
* @return
*/
public Picto get_picto(int pic_cat, int pic_id) {
Picto picto=null;
Picto picto=null; Log.e(LOG_TAG,"GET_PICTO:"+pic_cat+":"+Picto.NO_CATEGORY);
LinkedList<Picto> pictos_cat=this.pictos.get(pic_cat);
for (int i=0; i<pictos_cat.size() && picto==null; i++)
if (pictos_cat.get(i).get_id()==pic_id) picto=pictos_cat.get(i);
......@@ -427,10 +428,10 @@ public class Vocabulary implements Iterable<Picto> {
/*
* It saves locally a new picto obtained from the PCB
*/
public void saveLocalPicto(String url, String exp, int cat, int coord_x, int coord_y, int free_category_coord_x, int free_category_coord_y,String uri_sound,String user_avatar, final iLocalPicto listener) {
public void saveLocalPicto(String url, String exp, int cat, int coord_x, int coord_y, int free_category_coord_x, int free_category_coord_y,String user_avatar, String path_sound,final iLocalPicto listener) {
Picto prev_picto=PCBcontext.getPcbdb().getCurrentUser().has_categories() ? find_picto(cat, coord_x,coord_y) : find_picto(coord_x,coord_y); //¿estamos reemplazanddo un picto que ya existe?
Picto prev_picto=PCBcontext.getPcbdb().getCurrentUser().has_categories() ? find_picto(cat, coord_x,coord_y) : find_picto(free_category_coord_x,free_category_coord_y); //¿estamos reemplazanddo un picto que ya existe?
if (prev_picto!=null) { //El picto ya existe
removePicto(prev_picto.get_category(),prev_picto.get_id()); //borramos el picto local actual
......@@ -439,7 +440,10 @@ public class Vocabulary implements Iterable<Picto> {
int id=PCBcontext.getDevice().getNextLocalPictoID();
try {
final Picto picto = new Picto(id, url, exp, cat, coord_x, coord_y, free_category_coord_x, free_category_coord_y,prev_picto!=null ? prev_picto.get_stupicto_id() : Picto.STUPICTO_NULL,uri_sound,user_avatar);
final Picto picto = new Picto(id, url, exp, cat, coord_x, coord_y, free_category_coord_x, free_category_coord_y,prev_picto!=null ? prev_picto.get_stupicto_id() : Picto.STUPICTO_NULL,user_avatar);
if (path_sound!=null)
new File(path_sound).renameTo(new File(picto.get_audioPath()));
addPicto(picto, ImgDownloader.tsource.local, new ImgDownloader.iImgDownloaderListener() {
@Override
public void loadComplete() {
......
......@@ -7,6 +7,7 @@ import android.os.AsyncTask;
import android.util.Log;
import com.yottacode.pictogram.R;
import com.yottacode.pictogram.dao.Picto;
import com.yottacode.pictogram.tools.Img;
import java.io.File;
......@@ -51,6 +52,23 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
protected void onPreExecute() {
}
private InputStream getStreamImg(Img img) throws IOException {
String surl = context.getResources().getString(R.string.server) + "/" + img.get_url();
URL url = new URL(surl);
URLConnection ucon = url.openConnection();
return ucon.getInputStream();
}
private void downloadSoundImage(Picto picto)throws IOException {
String surl = context.getResources().getString(R.string.server) + "/" + picto.getUriSound();
URL url = new URL(surl);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
picto.save_sound(is);
if (is != null) is.close();
}
protected Img doInBackground( Vector<Img> imgs) {
InputStream is=null;
......@@ -58,21 +76,21 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
Log.d(this.getClass().getCanonicalName(), "Required images: " + imgs.size());
this.current= ImgDownloader.status.downloading;
int i=0,j=0,allsize=0;
int i=0,j=0,allsize=0,isound=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 || this.source==source.local) 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 {
is=getStreamImg(img);
}
else {
File file=new File(img.get_url());
is=new FileInputStream(file);
}
......@@ -86,10 +104,18 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
} finally {
if (is != null) is.close();
}
if (img instanceof Picto) {
Picto picto = (Picto) img;
if (picto.getUriSound() != null) {
downloadSoundImage(picto);
isound++;
}
}
}
this.current= status.downloaded_ok;
} catch (IOException e) {
Log.d(this.getClass().getCanonicalName(), "Error: " + e);
Log.e(this.getClass().getCanonicalName(), "Error: " + e);
this.current= status.downloaded_failed;
}
seconds=Calendar.getInstance().get(Calendar.SECOND)-seconds;
......@@ -98,6 +124,7 @@ public class ImgDownloader extends AsyncTask<Vector<Img>, Void, Img> {
". Cached: "+ (imgs.size()-i)+"/"+imgs.size()+
". Download failed: "+ j+"/"+imgs.size()+
". Memory required:"+((mi.availMem / 1048576L)-availableMegs)+" MB"+
". Sounds downloaded:"+isound+
". Used time: "+seconds+" seconds at "+new SimpleDateFormat("HH:mm:ss"));
return imgs.size() > 1 ? null
......
package com.yottacode.pictogram.net;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.JsonObject;
import com.koushikdutta.ion.Ion;
......@@ -12,7 +12,6 @@ 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.GUITools;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -38,7 +37,6 @@ public class PictoUploader {
private boolean uploadImg( Img img) throws UnsupportedEncodingException {
boolean success;
Bitmap bmp;
Response<JsonObject> response=null;
if (!img.get_filetype().equalsIgnoreCase("png"))
throw new UnsupportedEncodingException("Extension "+img.get_filetype()+" is not supported. Only png files");
......@@ -46,10 +44,6 @@ public class PictoUploader {
try {
bmp=img.get_bitmap(PCBcontext.getContext());
/*Log.i(LOG_TAG, "Uploading Picto img " + img.file_name() + " from " + img.get_type() + "- Size:" + bmp.getWidth() + " " + bmp.getHeight());*/
File file = img.file(PCBcontext.getContext());
......@@ -77,7 +71,7 @@ public class PictoUploader {
success=false;
Log.i(LOG_TAG, "Uploaded image failed ");
if (response != null)
Log.i(LOG_TAG, "Uploaded image failed, headers: " + response.getHeaders());
Log.e(LOG_TAG, "Uploaded image failed, headers: " + response.getHeaders().message());
}
} catch (InterruptedException e) {
Log.e(LOG_TAG, "Image upload error: " + e.getMessage()+ "Code: "+
......@@ -87,17 +81,12 @@ public class PictoUploader {
Log.e(LOG_TAG, "Image upload error: " + e.getMessage()+
(response == null ? -1 : response.getHeaders().code()));
success=false;
} catch (IOException e) {
Log.e(Img.class.getCanonicalName(), "Error when decoding "+img.file_name());
GUITools.show_alert(PCBcontext.getContext(), R.string.imguserLoadingErrMsg);
success=false;
}
// ion.dump();
return success;
}
//TODO: Refinar el metodo
private boolean uploadSound(File audioFile) throws UnsupportedEncodingException {
boolean success;
......@@ -107,13 +96,12 @@ public class PictoUploader {
if (!extension.equalsIgnoreCase("mp3"))
throw new UnsupportedEncodingException("Extension "+extension+" is not supported. Only mp3 files");
String url=PCBcontext.getContext().getResources().getString(R.string.server) + "/"+PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()+"/upload_sound/"+picto.get_id();
Log.e(LOG_TAG,"Upload "+audioFile);
Ion ion = Ion.getDefault(PCBcontext.getContext());
try {
response=ion.with(PCBcontext.getContext())
.load("POST", PCBcontext.getContext().getResources().getString(R.string.server) + "/picto/upload_sound/"+picto.get_stupicto_id())
.load("POST", url)
.setMultipartParameter("filename", audioFile.getName())
.setMultipartParameter("extension", "mp3")
.setMultipartParameter("owner", Integer.toString(PCBcontext.getPcbdb().getCurrentUser().get_id_sup()))
......@@ -125,14 +113,8 @@ public class PictoUploader {
if (response != null && response.getHeaders().code() == 200) {
Log.i(LOG_TAG, "Uploaded image result: " + response.getHeaders() + ":" + response.getResult());
//int img_id=response.getResult().get("id").getAsInt();
//String img_uri=response.getResult().get("uri").getAsString();
//img.set_url(img_uri);
//img.update_id(img_id);
Log.i(LOG_TAG, "Uploaded sound result: " + response.getHeaders() + ":" + response.getResult());
picto.setUriSound(response.getResult().getAsJsonObject("attributes").get(Picto.JSON_ATTTRS.URI_SOUND).getAsString());
success=true;
} else {
success=false;
......@@ -163,9 +145,10 @@ public class PictoUploader {
*/
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());
json_attrs.put("user_avatar",picto.get_user_avatar());
if (picto.get_user_avatar()!=null) json_attrs.put(Picto.JSON_ATTTRS.USER_AVATAR,picto.get_user_avatar());
if (PCBcontext.getPcbdb().getCurrentUser().has_categories())
json_attrs.put(Picto.JSON_ATTTRS.CATEGORY, picto.get_category())
.put(Picto.JSON_ATTTRS.COLUMN, picto.get_column())
......@@ -174,13 +157,13 @@ public class PictoUploader {
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());
params.put("json", new JSONObject().put("attributes",json_attrs).put("id_scene",44).toString());
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, " Error: " + e.getLocalizedMessage());
}
Log.i(LOG_TAG,"Uploading attributes ");
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto/"+id_picto, params, "post", true, new RestapiWrapper.iRestapiListener() {
@Override
public void preExecute() {
......@@ -188,13 +171,31 @@ public class PictoUploader {
@Override
public void result(JSONArray result) {
Log.i(LOG_TAG,"Uploading attributes array result");
}
@Override
public void result(JSONObject result) {
Log.i(LOG_TAG, " Attributes uploaded: " + result.toString());
listener.success(true,result.toString());
if(PictoUploader.this.picto.get_audioFile()!=null) {
Log.i(LOG_TAG,"Uploading sound "+PictoUploader.this.picto.get_audioFile());
File file = picto.get_audioFile(); //Obtengo el fichero de audio local
//Llamo a la subida
try {
if(uploadSound(file)) {
Log.i(LOG_TAG,"Sound uploaded");
listener.success(true,result.toString());
}else {
Log.e(LOG_TAG, "Uploading sound error");
listener.success(false,result.toString());
}
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Uploading sound error");
listener.success(false,e.toString());
}
}
else
listener.success(true,result.toString());
}
@Override
......@@ -229,7 +230,6 @@ public class PictoUploader {
@Override
public void result(JSONObject result) {
Log.i(LOG_TAG, "Uploaded translation result: " + result.toString());
listener.success(true,result.toString());
uploadAttributes(picto.get_id(), listener);
}
......@@ -246,7 +246,7 @@ public class PictoUploader {
/**
* if the a picto was modified from the PCB, the original one is modified and a new one is included
*/
private void deletePicto(final int id_stupicto, final iPictoUploaderListener listener) {
private void deleteRemotePicto(final int id_stupicto) {
final String picto_str="/picto";
String operation=PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()+picto_str+"/"+id_stupicto;
......@@ -264,62 +264,49 @@ public class PictoUploader {
@Override
public void result(JSONObject result) {
String str="Picto "+id_stupicto+" deleted";
String str="Picto "+id_stupicto+" deleted. Server side";
PictoUploader.this.picto.set_local_status(true);
try {
uploadLocalPicto();
} catch (IOException e) {
Log.e(LOG_TAG,"error after deleting remote picto:"+e.getMessage());
}
Log.i(LOG_TAG, str);
listener.success(true,str);
uploadTranslation(picto.get_id(), listener);
}
@Override
public void error(RestapiWrapper.HTTPException e) {
Log.e(LOG_TAG, "Error deleting picto: " + e.getLocalizedMessage()+" Picto:"+id_stupicto);
listener.success(false,e.getMessage());
}
});
}
/**
*Try to Upload local picto. It requires:
* i) if the picto exists, to delete the remote previous one
* ii) to upload the image,
* iii) to upload the expression
* iv) to upload the attributes
* 0) to delete the previous picto, if it exists
* i) to upload the image,
* ii) to upload the expression
* iii) to upload the attributes
* iv) upload the sound if it exists
*
**/
public void upload() throws IOException {
final int local_id = this.picto.get_id();
final int stupicto_id=this.picto.get_stupicto_id();
if (this.picto.get_stupicto_id()!= Picto.STUPICTO_NULL)
deleteRemotePicto(stupicto_id);
else
uploadLocalPicto();
}
private void uploadLocalPicto() throws IOException {
final int local_id = this.picto.get_id();
final boolean imgUpload_success = uploadImg(this.picto);
int elements_to_be_uploaded=2;
if (stupicto_id!=Picto.STUPICTO_NULL)
elements_to_be_uploaded++;
Log.i(LOG_TAG, "Local Picto to be uploaded:"+this.picto.get_translation()+"(localID:"+local_id+", new remoteID:"+this.picto.get_id()+
(stupicto_id!=Picto.STUPICTO_NULL ? ", stupicto to be deleted:"+stupicto_id+")" : " .New picto"));
iPictoUploaderListener listener = new PictoUploaderListener(local_id,elements_to_be_uploaded);
if (imgUpload_success){
if(this.picto.getUriSound() != "" && this.picto.getUriSound() != null ){ //Si el picto tiene audio en local
Log.i(LOG_TAG,"Uploading sound "+this.picto.getUriSound());
File file = new File(picto.getUriSound()); //Obtengo el fichero de audio local
boolean soundUpload_success = uploadSound(file); //Llamo a la subida
if(soundUpload_success) {
Log.i(LOG_TAG,"Sound uploaded");
}else {
Log.e(LOG_TAG, "Uploading sound error");
GUITools.show_alert(PCBcontext.getActivityContext(),R.string.upload_error, PictoUploader.this.picto.get_translation());
}
}
if (stupicto_id!=Picto.STUPICTO_NULL) {
Log.i(LOG_TAG, "Remote Picto to be deleted:"+this.picto.get_translation()+"("+stupicto_id+")");
deletePicto(stupicto_id,listener);
} else {
uploadTranslation(picto.get_id(), listener);
}
}else{
GUITools.show_alert(PCBcontext.getActivityContext(), R.string.upload_error, PictoUploader.this.picto.get_translation());
}
Log.i(LOG_TAG, "Local Picto to be uploaded:" + this.picto.get_translation() + "(localID:" + local_id + ", new remoteID:" + this.picto.get_id() );
iPictoUploaderListener listener = new PictoUploaderListener(local_id);
if (imgUpload_success)
uploadTranslation(picto.get_id(), listener);
else
listener.success(false, PCBcontext.getContext().getResources().getString(R.string.upload_error) + ":" + PictoUploader.this.picto.get_translation());
}
......@@ -370,29 +357,30 @@ public class PictoUploader {
void success(boolean success, String s);
}
public class PictoUploaderListener implements iPictoUploaderListener {
int elements_uploaded = 0;
int elements_to_be_uploaded;
int local_img_id;
PictoUploaderListener(int local_img_id, int elements) {
this.elements_to_be_uploaded=elements;
PictoUploaderListener(int local_img_id) {
this.local_img_id=local_img_id;
}
@Override
public void success(boolean success, String msg) {
if (success) elements_uploaded++;
else {
if (!success) {
int errmsg =
msg.contains("Error: Picto already in student's vocabulary")
? R.string.upload_duplicated
: R.string.upload_error;
GUITools.show_alert(PCBcontext.getActivityContext(), errmsg, PictoUploader.this.picto.get_translation());
Toast.makeText(
PCBcontext.getActivityContext(),
(PCBcontext.getActivityContext().getResources().getString(errmsg)
+":"+PictoUploader.this.picto.get_translation()), Toast.LENGTH_LONG).show();
}
if (elements_uploaded == elements_to_be_uploaded) {
PCBcontext.getPcbdb().deletePicto(local_img_id);
else {
PCBcontext.getVocabulary().addPicto(picto);
PCBcontext.getRoom().emit(new VocabularyAction(VocabularyAction.ADD, PictoUploader.this.picto));
GUITools.show_alert(PCBcontext.getActivityContext(), R.string.upload_ok,PictoUploader.this.picto.get_translation());
Toast.makeText(
PCBcontext.getActivityContext(),
(PCBcontext.getActivityContext().getResources().getString(R.string.upload_ok)
+":"+PictoUploader.this.picto.get_translation()), Toast.LENGTH_LONG).show();
}
}
};
......
......@@ -2,6 +2,7 @@ package com.yottacode.pictogram.tts;
import android.annotation.TargetApi;
import android.content.Context;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
......@@ -10,6 +11,10 @@ import android.speech.tts.Voice;
import android.util.Log;
import android.widget.ArrayAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
......@@ -22,13 +27,15 @@ import java.util.Set;
*/
public class TTSHelper {
private static final String LOG_TAG = TTSHelper.class.getName();
TextToSpeech ttobj=null;
Voice voice;
SoundPool sp=null;
Hashtable<String, Integer> recorded_sounds=null;
boolean voice_ok;
public void createTTS(final Context context, String engine, final Locale locale, final String voice) {
public void createTTS(final Context context, String engine, final Locale locale, final String voice) {
if (this.ttobj==null) {
if (this.ttobj == null) {
this.ttobj = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
public void onInit(int status) {
Log.e(this.getClass().getCanonicalName(), "TTS engine " + status);
......@@ -40,8 +47,11 @@ public class TTSHelper {
}
}, engine);
}
}
if (sp == null) {
sp = new SoundPool.Builder().build();
recorded_sounds = new Hashtable<>(3);
}
}
public TTSHelper(Context context, String engine, Locale locale,String voice) {
createTTS(context ,engine,locale,voice);
}
......@@ -142,4 +152,35 @@ public class TTSHelper {
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "TAPE_READ");
ttobj.speak(input, TextToSpeech.QUEUE_FLUSH, params, "TAPE_READ");
}
private void addRecord(final File mp3) {
try {
final FileInputStream fis = new FileInputStream(mp3);
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool sp, int sampleId, int status) {
TTSHelper.this.recorded_sounds.put(mp3.getAbsolutePath(),new Integer(sampleId));
playRecord(mp3);
try {
fis.close();
} catch (IOException e) {
Log.e(LOG_TAG,"Error playing audio "+mp3.getAbsolutePath());
}
}
});
sp.load(fis.getFD(),0,mp3.length(),1);
} catch (Exception e) {
Log.e(LOG_TAG,"READING MP3 ERROR "+e.getMessage());
e.printStackTrace();
}
}
public void playRecord(final File mp3) {
Integer mp3id=this.recorded_sounds.get(mp3.getAbsolutePath());
if (mp3id==null)
addRecord(mp3);
else
sp.play(mp3id.intValue(), 1, 1, 1, 0, 1);
}
}
......@@ -24,6 +24,7 @@ public class PictoMenu {
public static final String IS_EDIT = "is_edit";
public static final String PATH = "pathImage";
public static final String IMAGE_PICTO = "imagePicto";
public static final String PATH_SOUND = "pathSound";
PictogramActivity activity;
......@@ -102,7 +103,7 @@ public class PictoMenu {
}
public void setPicto(int row, int col, int cat, Bitmap bitmap, int id_picto){
public void setPicto(int row, int col, int free_row, int free_col, int cat, Bitmap bitmap, int id_picto){
Intent intent = new Intent(activity, EditPictoActivity.class);
intent.putExtra(ID_PICTO_IMAGE,id_picto);
......@@ -112,8 +113,9 @@ public class PictoMenu {
intent.putExtra(Picto.JSON_ATTTRS.ROW, row);
intent.putExtra(Picto.JSON_ATTTRS.COLUMN, col);
} else {
intent.putExtra(Picto.JSON_ATTTRS.FREE_ROW, row);
intent.putExtra(Picto.JSON_ATTTRS.FREE_COLUMN, col);
intent.putExtra(Picto.JSON_ATTTRS.CATEGORY, Picto.NO_CATEGORY);
intent.putExtra(Picto.JSON_ATTTRS.FREE_ROW, free_row);
intent.putExtra(Picto.JSON_ATTTRS.FREE_COLUMN, free_col);
}
/*ByteArrayOutputStream stream = new ByteArrayOutputStream();
......@@ -280,7 +282,7 @@ public class PictoMenu {
ll.removeAllViewsInLayout();
try {
setPicto(p.get_row(),p.get_column(),p.get_category(),p.get_bitmap(PCBcontext.getContext()),p.get_id());
setPicto(p.get_row(),p.get_column(),p.getFreeRow(),p.getFreeColumn(), p.get_category(),p.get_bitmap(PCBcontext.getContext()),p.get_id());
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -62,6 +62,7 @@ import com.yottacode.tools.GUITools;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -159,6 +160,7 @@ public class PictogramActivity extends Activity implements VocabularyTalk.iVocab
public void action(action action, int picto_cat, int picto_id) {
Log.i(this.getClass().getCanonicalName(), action + " from " + picto_cat + "," + picto_id + " catched");
if (action==ActionTalk.iActionListener.action.show) {
Log.i(LOG_TAG,"show message received:"+action.toString());
}
else if (PCBcontext.getPcbdb().getCurrentUser().is_mirror_on()) {
Picto picto = vocabulary.get_picto(picto_cat, picto_id);
......@@ -393,7 +395,6 @@ public class PictogramActivity extends Activity implements VocabularyTalk.iVocab
@Override
protected void onPause() {
Log.e(this.LOG_TAG,"ONPAUSE");
super.onPause();
this.pictoCategoryGridAdapter.allPictosInGrid();
this.pictoMainGridAdapter.allPictosInGrid();
......@@ -409,7 +410,6 @@ public class PictogramActivity extends Activity implements VocabularyTalk.iVocab
@Override
protected void onDestroy() {
Log.e(LOG_TAG, "destroy Pictogram Activity");
super.onDestroy();
if (tts != null) {
tts.destroy();
......@@ -819,8 +819,14 @@ public class PictogramActivity extends Activity implements VocabularyTalk.iVocab
tapeAdapter.notifyDataSetChanged();
showPictoMainGridView();
PCBcontext.getActionLog().log(new TalkAction(TalkAction.ADD, p));
if (PictogramActivity.this.feedback_read && !PictogramActivity.this.tapeAdapter.play() && !p.is_category())
PictogramActivity.this.tts.play(p.get_translation());
if (PictogramActivity.this.feedback_read && !PictogramActivity.this.tapeAdapter.play() && !p.is_category()) {
File audioFile = p.get_audioFile();
Log.e(LOG_TAG,"AUDIO:"+(audioFile!=null)+":"+p.get_audioPath());
if (audioFile != null)
PictogramActivity.this.tts.playRecord(audioFile);
else
PictogramActivity.this.tts.play(p.get_translation());
}
if (PictogramActivity.this.feedback_highlight) execHighligthFeeback(p, false);
}
......@@ -1157,7 +1163,7 @@ protected void showOnlyTape(boolean onlyTape) {
int freeRow = edit ? data.getExtras().getInt(Picto.JSON_ATTTRS.FREE_ROW) : getIntent().getIntExtra(Picto.JSON_ATTTRS.FREE_ROW, -1);
int freeColumn = edit ? data.getExtras().getInt(Picto.JSON_ATTTRS.FREE_COLUMN) : getIntent().getIntExtra(Picto.JSON_ATTTRS.FREE_COLUMN, -1);
String uri_sound = data.getExtras().getString(Picto.JSON_ATTTRS.URI_SOUND);
String path_sound = data.getExtras().getString(PictoMenu.PATH_SOUND);
String user_avatar = data.getExtras().getString(Picto.JSON_ATTTRS.USER_AVATAR);
int cat = edit ? data.getIntExtra(Picto.JSON_ATTTRS.CATEGORY, -1) : getIntent().getIntExtra(Picto.JSON_ATTTRS.CATEGORY, -1);
......@@ -1165,10 +1171,8 @@ protected void showOnlyTape(boolean onlyTape) {
String path = data.getExtras().getString(PictoMenu.PATH);
String legend = data.getExtras().getString(Picto.JSON_ATTTRS.EXPRESSION);
Log.i(EditPictoActivity.DEBUG_MESSAGE,"Antes de chooseText...DATOS--> ASS_PERSON: "+user_avatar+"..URI_SOUND: "+uri_sound+"..PATH_IMAGE: "+path+"..EXPRESION: "+legend);
//TODO: COGER URI DEL SONIDO,EL USER AVATAR Y LA PERSONA ASOCIADA AL PICTO
chooseTextAndSavePicto(path, row, col, freeRow, freeColumn, cat, legend, uri_sound,user_avatar);
chooseTextAndSavePicto(path, row, col, freeRow, freeColumn, cat, legend, path_sound ,user_avatar);
refresh();
}
break;
......@@ -1180,25 +1184,27 @@ protected void showOnlyTape(boolean onlyTape) {
* función para la edición de un texto asociado a una nueva imagen y guardar el nuevo picto
*/
public void chooseTextAndSavePicto(final String selectedImagePath, final int row, final int col, final int freeRow, final int freeColumn,
final int category, final String legend,final String uri_sound,final String user_avatar) {
final int category, final String legend,final String path_sound ,final String user_avatar) {
// Set up the buttons
int cat = category != -1 ? category : Picto.NO_CATEGORY;
Log.i(LOG_TAG,"Uploading "+legend+" at row "+"row"+", col "+col);
Log.i(LOG_TAG,"Uploading "+legend+" at row "+row+"/"+freeRow+", col "+col+"/"+freeColumn+" sound "+path_sound );
PCBcontext.getVocabulary().saveLocalPicto(
selectedImagePath,
legend,
cat,
category,
row,
col,
freeRow,
freeColumn,
uri_sound,
user_avatar,
path_sound,
new iLocalPicto() {
@Override
public void saved(Picto localPicto) {
refresh();
try {
if (PCBcontext.is_user_online())
......
......@@ -2,11 +2,13 @@ package com.yottacode.pictogram.tabletlibrary.gui.communicator.cropper;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.AudioManager;
import android.media.MediaActionSound;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
......@@ -67,6 +69,7 @@ import static java.lang.Thread.sleep;
public class EditPictoActivity extends Activity {
private static final CharSequence NO_SUP_TEXT = "____________";
private static final String LOG_TAG = EditPictoActivity.class.getName();
Picto p;
private boolean editar;
Random nRandom = new Random();
......@@ -178,7 +181,6 @@ public class EditPictoActivity extends Activity {
tiempoTotal = tiempoGrabado-1;
barraReproducir.setMax(tiempoTotal);
botonGrabar.PhidePressedRing();
Log.i(DEBUG_MESSAGE,"Fin Grabacion.."+tiempoGrabado);
if(tiempoGrabado>1) hayGrabacion = true ;
else hayGrabacion = false;
reiniciarGrabacion();
......@@ -215,7 +217,6 @@ public class EditPictoActivity extends Activity {
protected void onCancelled(){
stopPlaying();
reiniciarReproducción();
Log.i(DEBUG_MESSAGE,"Fin Reproducion..");
}
}
......@@ -293,8 +294,7 @@ public class EditPictoActivity extends Activity {
fileImage.mkdirs();
}
dirAudioPath = Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_MUSIC;
dirAudioPath += "/PictoAudio";
dirAudioPath = Picto.getAudioPictosPath();
File fileAudio = new File(dirAudioPath);
if(!fileAudio.exists()){
fileAudio.mkdirs();
......@@ -445,7 +445,6 @@ public class EditPictoActivity extends Activity {
dirAudioPath + File.separator + legend.getText().toString() + "_new_" + pathNumber + "_audio.mp3");
if (from.exists()){
from.renameTo(to);
Log.i(DEBUG_MESSAGE,to.getPath().toString());
audioPath = to.getPath().toString();
}
}
......@@ -455,7 +454,8 @@ public class EditPictoActivity extends Activity {
Intent intent = getIntent(); //Mandar a pictogram activity el path y el texto de la imagen
intent.putExtra(PictoMenu.IS_EDIT, editar); //Para saber despues si estas editando o añadiendo nuevo y coger los datos de intent o de data en OnActivityResult
intent.putExtra(PictoMenu.PATH, filepath); //Mandar Path imagen
intent.putExtra(Picto.JSON_ATTTRS.URI_SOUND,audioPath); //Mandar el path del audio
intent.putExtra(PictoMenu.PATH_SOUND,audioPath); //Mandar el path del audio
Log.e(LOG_TAG, "PATH SOUND:"+audioPath);
intent.putExtra(Picto.JSON_ATTTRS.EXPRESSION, legend.getText().toString()); //Mandar expresion nueva
intent.putExtra(Picto.JSON_ATTTRS.CATEGORY, getIntent().getIntExtra(Picto.JSON_ATTTRS.CATEGORY, -1));
intent.putExtra(Picto.JSON_ATTTRS.USER_AVATAR, supAsociado.getText().equals(NO_SUP_TEXT)?null:supAsociado.getText());
......@@ -584,7 +584,7 @@ public class EditPictoActivity extends Activity {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_RECORD_AUDIO_PERMISSION:
permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
permissionToRecordAccepted = grantResults.length==0 || grantResults[0] == PackageManager.PERMISSION_GRANTED;
break;
}
if (!permissionToRecordAccepted ) finish();
......@@ -612,6 +612,9 @@ public class EditPictoActivity extends Activity {
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
AudioManager myAudioMgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int nativeSampleRate = Integer.parseInt(myAudioMgr.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE));
mRecorder.setAudioSamplingRate(nativeSampleRate);
mRecorder.setOutputFile(previewAudioPath);
......
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