/*
* File: cell.java
* This file is part of Tico, an application
* to create and perform interactive communication boards to be
* used by people with severe motor disabilities.
*
* Authors: Eduardo Ferrer
*
* Date: January-2012
*
* Company: Universidad de Zaragoza, CPS, DIIS
*
* License:
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package android.TICO;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
//import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import configuration.TSetup;
import android.TICO.Views.RectangleView;
/**
* Defines a cell
and its attributes.
*
* @author Eduardo Ferrer
* @version 1.0 Jan, 2012
*/
public class cell extends RelativeLayout{//LinearLayout {
/**
* The cell
type used as attribute in XML encoding and decoding for Android.
*/
ImageView image;
TextView text;
RelativeLayout marcoInterior;
int frameColor;
int frameColorAlternate;
String imageURL;
String imageAlternateURL;
String soundURL;
String alternativeSoundURL;
String videoURL;
boolean accumulate;
int destiny;
int marcoSize;
int marcoSizeAlternate;
private RelativeLayout.LayoutParams marcoNormalParams;
private RelativeLayout.LayoutParams marcoAlternativoParams;
private RelativeLayout.LayoutParams marcoInteriorNormalParams;
private RelativeLayout.LayoutParams marcoInteriorAlternativoParams;
private boolean isAlternate;
private boolean orderedCell;
private RectangleView rect;
private String textoEnvio=null;
private String areaTextoEnvio=null;
private int sendTextTimer=0;
/**
* Creates a new TCell
.
* @param context The context
that contains the cell data
* @param marginLeft The number marginLeft
of pixels of the left margin
* @param marginTop The number marginTop
of pixels of the top margin
* @param height The height
in pixels of the cell cell
* @param width The width
in pixels of the cell cell
* @param imageURL The URL imageURL
of the image in the cell
* @param texto The text texto
of the cell cell
* @param marcoSize The size
of the frame cell cell
*/
public cell(Context context,int marginLeft, int marginTop, int height,int width,String imageURL, String texto, int marcoSize) {
super(context);
// TODO Auto-generated constructor stub
//initialize the frame sizes
this.marcoSize=marcoSize;
marcoSizeAlternate=marcoSize;
isAlternate=false;
soundURL=null;
alternativeSoundURL=null;
videoURL=null;
accumulate=false;
destiny=-1;
//image creation
if(imageURL!=null && !imageURL.equals("")){
image = new ImageView(context);
//Uri uriImage=Uri.parse(imageURL);
this.imageURL=imageURL;
this.imageAlternateURL=null;
asignImage(width,height,imageURL);
//image.setImageURI(uriImage);
//RelativeLayout.LayoutParams pimage = new RelativeLayout.LayoutParams(width-2*marcoSize,width-2*marcoSize); //imagen cuadrada
//image.setLayoutParams(pimage);
((android.widget.RelativeLayout.LayoutParams) image.getLayoutParams()).addRule(RelativeLayout.CENTER_HORIZONTAL);
}else{
this.imageURL=null;
this.imageAlternateURL=null;
}
//text creation
text=new TextView(context);
text.setText(texto);
text.setGravity(Gravity.CENTER_HORIZONTAL);
marcoNormalParams = new RelativeLayout.LayoutParams(width,height);
marcoNormalParams.setMargins(marginLeft, marginTop,0, 0);
//iniciamos los parametros del layout alternativo
marcoAlternativoParams = new RelativeLayout.LayoutParams(width,height);
marcoAlternativoParams.setMargins(marginLeft, marginTop,0, 0);
//marco interior
marcoInterior=new RelativeLayout(context);
marcoInteriorNormalParams = new RelativeLayout.LayoutParams(width-2*marcoSize,height-2*marcoSize);
marcoInteriorNormalParams.setMargins(marcoSize, marcoSize,0, 0);
//para el alternativo
marcoInteriorAlternativoParams = new RelativeLayout.LayoutParams(width-2*marcoSize,height-2*marcoSize);
marcoInteriorAlternativoParams.setMargins(marcoSize, marcoSize,0, 0);
marcoInterior.setBackgroundColor(TICO.res.getColor(R.color.cell_background_default_color));//por config.xml
rect=new RectangleView(0,0,width,height,context);
rect.setBorderWidth(marcoSize);
this.addView(rect);
frameColor=TICO.res.getColor(R.color.frame_default_color);//
rect.setColor(frameColor);
frameColorAlternate=TICO.res.getColor(R.color.frame_alternate_color);//
marcoInterior.setLayoutParams(marcoInteriorNormalParams);
this.setLayoutParams(marcoNormalParams);
text.setGravity(Gravity.CENTER_HORIZONTAL);
if(TSetup.getCellTextPosition().equals("up")){
if(this.imageURL!=null){
marcoInterior.addView(image);
((android.widget.RelativeLayout.LayoutParams) image.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
if(texto!=null && !texto.equals("")){
marcoInterior.addView(text);
((android.widget.RelativeLayout.LayoutParams) text.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_TOP);
((android.widget.RelativeLayout.LayoutParams) text.getLayoutParams()).addRule(RelativeLayout.CENTER_HORIZONTAL);
}
}else if(TSetup.getCellTextPosition().equals("noText")){
if(this.imageURL!=null){
marcoInterior.addView(image);
((android.widget.RelativeLayout.LayoutParams) image.getLayoutParams()).addRule(RelativeLayout.CENTER_VERTICAL);
}
}else{ //"down" or ""
if(this.imageURL!=null){
marcoInterior.addView(image);
((android.widget.RelativeLayout.LayoutParams) image.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_TOP);
}
if(texto!=null && !texto.equals("")){
marcoInterior.addView(text);
((android.widget.RelativeLayout.LayoutParams) text.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
((android.widget.RelativeLayout.LayoutParams) text.getLayoutParams()).addRule(RelativeLayout.CENTER_HORIZONTAL);
}
}
this.addView(marcoInterior);
//por defecto orderer cell
orderedCell=true;
this.setTextSize(text.getTextSize());
}
/**
* Creates a new TCell
for control options.
* @param context The context
that contains the cell data
* @param marginLeft The number marginLeft
of pixels of the left margin
* @param marginTop The number marginTop
of pixels of the top margin
* @param height The height
in pixels of the cell cell
* @param width The width
in pixels of the cell cell
* @param imageURL The int identificator in R.id of the image in the cell
* @param texto The text texto
of the cell cell
* @param marcoSize The size
of the frame cell cell
*/
public cell(Context context,int marginLeft, int marginTop, int height,int width,int imageURL, String texto, int marcoSize) {
super(context);
// TODO Auto-generated constructor stub
//inicializamos los tamaƱos de marco
this.marcoSize=marcoSize;
marcoSizeAlternate=marcoSize;
isAlternate=false;
soundURL=null;
alternativeSoundURL=null;
videoURL=null;
accumulate=false;
//creamos la imagen
image = new ImageView(context);
image.setImageResource(imageURL);
RelativeLayout.LayoutParams pimage = new RelativeLayout.LayoutParams(width-2*marcoSize,width-2*marcoSize); //imagen cuadrada
image.setLayoutParams(pimage);
//creamos el texto
text=new TextView(context);
text.setText(texto);
text.setGravity(Gravity.CENTER_HORIZONTAL);
marcoNormalParams = new RelativeLayout.LayoutParams(width,height);
marcoNormalParams.setMargins(marginLeft, marginTop,0, 0);
//iniciamos los parametros del layout alternativo
marcoAlternativoParams = new RelativeLayout.LayoutParams(width,height);
marcoAlternativoParams.setMargins(marginLeft, marginTop,0, 0);
//marco interior
marcoInterior=new RelativeLayout(context);
marcoInteriorNormalParams = new RelativeLayout.LayoutParams(width-2*marcoSize,height-2*marcoSize);
marcoInteriorNormalParams.setMargins(marcoSize, marcoSize,0, 0);
//para el alternativo
marcoInteriorAlternativoParams = new RelativeLayout.LayoutParams(width-2*marcoSize,height-2*marcoSize);
marcoInteriorAlternativoParams.setMargins(marcoSize, marcoSize,0, 0);
// marcoInterior.setBackgroundColor(Color.BLACK);
marcoInterior.setBackgroundColor(TICO.res.getColor(R.color.cell_background_default_color));//por config.xml
frameColor=TICO.res.getColor(R.color.frame_default_color);//
rect=new RectangleView(0,0,width,height,context);
rect.setBorderWidth(marcoSize);
this.addView(rect);
rect.setColor(frameColor);
frameColorAlternate=TICO.res.getColor(R.color.frame_alternate_color);//
marcoInterior.setLayoutParams(marcoInteriorNormalParams);
this.setLayoutParams(marcoNormalParams);
if(TSetup.getCellTextPosition().equals("up")){
if(texto!=null && !texto.equals(""))marcoInterior.addView(text);
marcoInterior.addView(image);
}else if(TSetup.getCellTextPosition().equals("noText")){
marcoInterior.addView(image);
}else{ //"down" or ""
marcoInterior.addView(image);
marcoInterior.addView(text);
}
this.addView(marcoInterior);
this.setTextSize(text.getTextSize());
}
/**
* Set the text size of the cell. If the the text doesn't fit in the cell width the text size will be reduced to fit.
* @param size The desired size
of the text in the cell
*/
public void setTextSize(float size){
text.setTextSize(size);
if(size<=0.0f){ //si el tamanyo es 0 o menor no se ve el texto
text.setVisibility(TextView.INVISIBLE);
}else{
text.setVisibility(TextView.VISIBLE);
}
float textWidth=text.getPaint().measureText(text.getText().toString());
float auxSize=size;
if(textWidth>marcoInterior.getLayoutParams().width*0.9f){
auxSize=(float)marcoInterior.getLayoutParams().width*0.9f/textWidth*auxSize;
text.setTextSize(auxSize);
textWidth=text.getPaint().measureText(text.getText().toString());
}
}
/**
* Set the main frame color
* @param color The color
of the main frame color
*/
public void setColorFrame(int color){
frameColor=color;
rect.setColor(frameColor);
}
/**
* Set the alternative frame color
* @param color The color
of the alternative frame color
*/
public void setColorFrameAlternate(int color){
frameColorAlternate=color;
}
/**
* Set the URL of the alternative image
* @param url The url
of the alternative image
*/
public void setImageAlternate(String url){
imageAlternateURL=url;
}
/**
* Set the URL of the sound
* @param url The url
of the sound
*/
public void setSound(String url){
soundURL=url;
}
/**
* Set the URL of the alternative sound
* @param url The url
of the alternative sound
*/
public void setSoundAlternate(String url){
alternativeSoundURL=url;
}
/**
* Set the URL of the video
* @param url The url url
of the video
*/
public void setVideo(String url){
videoURL=url;
}
/**
* Set if the cell accumulates on click
* @param bool boolean
true if accumulates, otherwise false.
*/
public void setAccumulate(boolean bool){
accumulate=bool;
}
/**
* get the URL of the sound
* @return The url
of the sound
*/
public String getSound(){
return soundURL;
}
/**
* Get the URL of the alternative sound
* @return The url
of the alternative sound
*/
public String getSoundAlternate(){
return alternativeSoundURL;
}
/**
* Get the URL of the video
* @return The url
of the video
*/
public String getVideo(){
return videoURL;
}
/**
* Get if the cell accumulates on click
* @return boolean
true if accumulates, otherwise false.
*/
public boolean getAccumulate(){
return accumulate;
}
/**
* Set the destiny board or -1 if there is no destiny.
* @param position integer
of the destiny board position.
*/
public void setDestiny(int position){
destiny=position;
}
/**
* Get the destiny board position.
* @return integer
of the destiny board, -1 if there is no destiny.
*/
public int getDestiny(){
return destiny;
}
/**
* Set the visible cell to the main color frame, main frame size, and main image
*/
public void showMainCell(){
if(isAlternate){
rect.setColor(frameColor);
if(imageAlternateURL!=null){
//Uri uriImage=Uri.parse(imageURL);
//image.setImageURI(uriImage);
asignImage(marcoNormalParams.width,marcoNormalParams.height,imageURL);
}
isAlternate=false;
//cambiamos los layoutparams para cambiar el tamanyo de marco
this.setLayoutParams(marcoNormalParams);
marcoInterior.setLayoutParams(marcoInteriorNormalParams);
rect.setRect(0,0,marcoNormalParams.width,marcoNormalParams.height);
rect.setBorderWidth(marcoSize);
}
}
/**
* Set the visible cell to the alternative color frame, alternative frame size and alternative image
*/
public void showAlternateCell(){
if(!isAlternate){
rect.setColor(frameColorAlternate);
if(imageAlternateURL!=null){
//Uri uriImage=Uri.parse(imageAlternateURL);
//image.setImageURI(uriImage);
asignImage(marcoNormalParams.width,marcoNormalParams.height,imageAlternateURL);
}
isAlternate=true;
//cambiamos los layoutparams
this.setLayoutParams(marcoAlternativoParams);
marcoInterior.setLayoutParams(marcoInteriorAlternativoParams);
rect.setRect(0,0,marcoAlternativoParams.width,marcoAlternativoParams.height);
rect.setBorderWidth(marcoSizeAlternate);
}
}
/**
* Set the color of the text
* @param color The integer that defines the desired color for the text
*/
public void setTextColor(int color){
if(text!=null){
text.setTextColor(color);
}
}
/**
* Set the background frame color
* @param color The color
of the background
*/
public void setInteriorBackgroundColor(int color){
marcoInterior.setBackgroundColor(color);
}
/**
* Set the background frame gradient color
* @param color The gradient color
of the background
*/
public void setBackgroundDrawable(GradientDrawable color){
marcoInterior.setBackgroundDrawable(color);
}
/**
* Set the style font of the text
* @param font The style
of the text
*/
public void setTextFont(int font){
text.setTypeface(Typeface.DEFAULT, font);
}
/**
* Set the alternate frame size
* @param size The size
of the alternate frame
*/
public void setAlternateFrameSize(int size){
marcoSizeAlternate=size;
marcoAlternativoParams = new RelativeLayout.LayoutParams(this.getLayoutParams().width + 2*(size-marcoSize),this.getLayoutParams().height+2*(size-marcoSize));
marcoAlternativoParams.setMargins(((RelativeLayout.LayoutParams)this.getLayoutParams()).leftMargin-(size-marcoSize), ((RelativeLayout.LayoutParams)this.getLayoutParams()).topMargin-(size-marcoSize),0, 0);
marcoInteriorAlternativoParams = new RelativeLayout.LayoutParams(marcoAlternativoParams.width-2*size,marcoAlternativoParams.height-2*size);
marcoInteriorAlternativoParams.setMargins(size, size,0, 0);
}
/**
* Set if the cell is ordered for the automatic scan
* @param ordered boolean
true if is orderer
*/
public void setOrdered(boolean ordered){
this.orderedCell=ordered;
}
/**
* Get if the cell is ordered for the automatic scan
* @return boolean
true if is orderer
*/
public boolean getOrdered(){
return this.orderedCell;
}
private void asignImage(int width, int height, String imageURL){
//si la altura o anchura deseada es menor o igual que 0 se pone a 1, que es el minimo admitido
if(width<=0 || height<=0){
width=Math.max(1,width);
height=Math.max(1,height);
}
/*prueba reducir tamanyo*/
Bitmap bm=null;
bm=BitmapFactory.decodeFile(imageURL);
if(bm!=null){
if(bm.getHeight()<=height && bm.getWidth()<=width){
Uri uriImage=Uri.parse(imageURL);
image.setImageURI(uriImage);
int w,h;
w=width;
h=bm.getHeight()*width/bm.getWidth();
if(h>height){
h=height;
w=bm.getWidth()*height/bm.getHeight();
}
RelativeLayout.LayoutParams pimage = new RelativeLayout.LayoutParams(w-2*marcoSize,h-2*marcoSize);
image.setLayoutParams(pimage);
}else{
// Log.d("control"," bitmap ancho " + bm.getWidth()+" bitmap alto "+bm.getHeight() );
if(bm.getWidth()>width){
bm=Bitmap.createScaledBitmap(bm, width, bm.getHeight()*width/bm.getWidth(), true);
//Log.d("control","reducimos por ancho "+" bitmap ancho " + bm.getWidth()+" bitmap alto "+bm.getHeight());
//image.setImageBitmap(bm);
}
if(bm.getHeight()>height){
bm=Bitmap.createScaledBitmap(bm, bm.getWidth() * height/bm.getHeight(), height, true);
//Log.d("control","reducimos por alto ");
//image.setImageBitmap(bm);
}
image.setImageBitmap(bm);
RelativeLayout.LayoutParams pimage = new RelativeLayout.LayoutParams(bm.getWidth()-2*marcoSize,bm.getHeight()-2*marcoSize);
image.setLayoutParams(pimage);
//Log.d("control","celda ancho "+width +" alto " + height +" bitmap ancho " + bm.getWidth()+" bitmap alto "+bm.getHeight() );
//anyadimos reglas para posicionar la imagen en su lugar correspondiente
((android.widget.RelativeLayout.LayoutParams) image.getLayoutParams()).addRule(RelativeLayout.CENTER_HORIZONTAL);
if(TSetup.getCellTextPosition().equals("up")){
((android.widget.RelativeLayout.LayoutParams) image.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
else if(TSetup.getCellTextPosition().equals("noText")){
((android.widget.RelativeLayout.LayoutParams) image.getLayoutParams()).addRule(RelativeLayout.CENTER_VERTICAL);
}else{ //"down" or ""
((android.widget.RelativeLayout.LayoutParams) image.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_TOP);
}
}
}
}
/**
* Set the text that the cell will send to a text area
*
* @param text The text to send
*/
public void setSendText(String text){
textoEnvio=text;
}
/**
* Set the id
of the textArea
to send text
*
* @param idTextArea the id
of the textArea
*/
public void setSendTextArea(String idTextArea){
areaTextoEnvio=idTextArea;
}
/**
* Set the time the sent text will be visible before get back to the original textArea
text.
* @param time the time in miliseconds
*/
public void setSendTextTimer(int time){
sendTextTimer=time;
}
/**
* Get the text that the cell will send to a text area
* @return The text to send
*/
public String getSendText(){
return textoEnvio;
}
/**
* Get the id
of the textArea
to send text
* @return The id of the textArea
*/
public String getSendTextArea(){
return areaTextoEnvio;
}
/**
* Set the time the sent text will be visible before get back to the original textArea
text.
* @return the time in miliseconds
*/
public int getSendTextTimer(){
return sendTextTimer;
}
}