package com.Plantilla3;
// © Juan Antonio Villalpando
// kio4.com
// Creacion de extensiones. Enero 2018.
// Plantilla3 para comenzar a escribir el codigo.
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.*;
///////////// IMPORTACIONES PARA EL TONO ///////////////////////////
import android.media.AudioTrack;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.ToneGenerator;
/////////////////////////////////////////////////////////////////////////////////////////////
@DesignerComponent(version = Plantilla3.VERSION,
description = "Este texto aparecera cuando pulses la interrogacion en las zona de Importar esta extension. "
+ "Juan Antonio Villalpando - KIO4.COM ",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "http://juanant91.byethost3.com/notificacion2.gif")
@SimpleObject(external = true)
public class Plantilla3 extends AndroidNonvisibleComponent implements Component {
public static final int VERSION = 1;
public static final String DEFAULT_TEXTO = "Hola";
public static final int DEFAULT_NUMERO = 123;
private ComponentContainer container;
private String texto = "";
private int numero= 0;
public Plantilla3(ComponentContainer container) {
super(container.$form());
this.container = container;
Texto(DEFAULT_TEXTO);
Numero(DEFAULT_NUMERO);
}
// Creación de las Propiedades.
@SimpleProperty(
category = PropertyCategory.BEHAVIOR)
public String Texto() {
return texto;
}
@SimpleProperty(
category = PropertyCategory.BEHAVIOR)
public double Numero() {
return numero;
}
// Establecimiento de las Propiedades.
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING, defaultValue = Plantilla3.DEFAULT_TEXTO + "")
@SimpleProperty(description = "Ayuda al situar el raton sobre este bloque. ")
public void Texto(String nuevoTexto) {
this.texto = nuevoTexto;
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_INTEGER, defaultValue = Plantilla3.DEFAULT_NUMERO + "")
@SimpleProperty(description = "Ayuda al situar el raton sobre este bloque. ")
public void Numero(int nuevoNumero) {
this.numero = nuevoNumero;
}
////////////////////// FUNCIONES //////////////////////////////////////////////////////////////////////
// Funcion Entran dos valores (entero y entero) y retorna un valor (float)
@SimpleFunction(description = "Area del triangulo.")
public float Triangulo(int base, int altura) {
float area = 0;
area = (base * altura) / 2;
return area;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Funcion NO ENTRAN VALORES y retorna un entero
@SimpleFunction(description = "Numero aleatorio.")
public int Dado() {
int dado = 0;
dado = (int) (6.0 * Math.random()) + 1;
return dado;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// A esta funcion le suministramos VALORES DE FRECUENCIA Y TIEMPO.
// Pero NO RETORNA resultado.
// Es una funcion con VOID
// Mediante el bloque de EVENTO puede devolver informacion.
@SimpleFunction(description = "Daria un sonido elegido.")
public void PonerTono(double frecuenciaHz, int duracionMs) {
AudioTrack tone = generateTone(frecuenciaHz, duracionMs); // Va a la Funcion Generador del Tono.
tone.play();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Función NO RETORNA VALORES NI DA RESULTADO.
// Es una funcion con VOID
@SimpleFunction(description = "Daria un sonido establecido.")
public void Tono() {
AudioTrack tone = generateTone(1000, 2000); // Va a la Funcion Generador del Tono.
tone.play();
}
/////////////////////////// FUNCION GENERADORA DEL TONO /////////////////////////////////////
// Función Generadora del Tono.
// Para reducir el código se podría haber introducido el contenido de esta función en la @SimpleFunction
public AudioTrack generateTone(double freqHz, int durationMs)
{
int count = (int)(44100.0 * 2.0 * (durationMs / 1000.0)) & ~1;
short[] samples = new short[count];
for(int i = 0; i < count; i += 2){
short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
samples[i + 0] = sample;
samples[i + 1] = sample;
}
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
count * (Short.SIZE / 8), AudioTrack.MODE_STATIC);
track.write(samples, 0, count);
return track;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Función NO RETORNA VALORES NI DA RESULTADO.
// Es una funcion con VOID
// Funcion Entra un valor (flotante) y NO RETORNA resultado.
// El resultado lo obtenemos mediante un EVENTO.
@SimpleFunction(description = "Area del triangulo.")
public void Circulo(float radio) {
float area = 0;
area = 3.1416f * radio * radio;
AreaCirculo(area);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Bloques de EVENTOS.
@SimpleEvent(description = "Area del circulo.")
public void AreaCirculo(float area_circulo){
EventDispatcher.dispatchEvent(this, "AreaCirculo", area_circulo);
}
} // ==> FIN
|