package org.hirodana.libtool.activity;
import java.io.*;
import java.net.*;
import org.hirodana.libtool.R;
import org.hirodana.libtool.notif.NotifPanier;
import org.hirodana.libtool.objects.CD;
import org.hirodana.libtool.objects.Dvd;
import org.hirodana.libtool.objects.Livre;
import org.hirodana.libtool.objects.Revue;
import org.hirodana.libtool.sqLite.*;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.*;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Classe OeuvreActivity
* Affiche une oeuvre et permet de l'enregistrer dans le panier
*
*/
public class OeuvreActivity extends Activity{
public String titre, description, type,editeur, auteur, collection, isbn, ean, issn, period, chanteur, amazon_min, realisateur;
public int nbExemplaire;
String id;
/**
* Mthode qui tlcharge l'image distante grce l'URL.
* @param URL l'url amazon de l'oeuvre rcupre.
* @return l'image de l'oeuvre dans ImageView.
*/
private ImageView afficheImage(String URL) {
final ImageView image = (ImageView)findViewById(R.id.ivImage);
final String URLe = URL;
new Thread(new Runnable() {
public void run() {
try {
URL urlImage = new URL(URLe);
HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
InputStream inputStream = connection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
image.post(new Runnable() {
public void run() {
image.setImageBitmap(bitmap);
}
});
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}).start();
return image;
}// fin de afficheImage
// On dfinit une variable globale qui sera l'id unique correspondant notre notification
public static final int ID_NOTIFICATION = 69001;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oeuvre);
//On rcupre l'objet Bundle envoy par l'autre Activity
Bundle objetbundle = this.getIntent().getExtras();
//On rcupre les donnes du Bundle
if (objetbundle != null && objetbundle.containsKey("titre") && objetbundle.containsKey("description") && objetbundle.containsKey("type") && objetbundle.containsKey("editeur")) {
type = this.getIntent().getStringExtra("type");
titre = this.getIntent().getStringExtra("titre");
description = this.getIntent().getStringExtra("description");
id = this.getIntent().getStringExtra("id");
editeur = this.getIntent().getStringExtra("editeur");
auteur = this.getIntent().getStringExtra("auteur");
collection = this.getIntent().getStringExtra("collection");
isbn = this.getIntent().getStringExtra("isbn");
nbExemplaire = this.getIntent().getIntExtra("nbExemplaire", nbExemplaire);
ean = this.getIntent().getStringExtra("ean");
chanteur = this.getIntent().getStringExtra("auteur");
issn = this.getIntent().getStringExtra("issn");
realisateur = this.getIntent().getStringExtra("real");
period = this.getIntent().getStringExtra("period");
}
else {
//Erreur (c'est quoi cette gestion d'erreur la Hippolyte?)
titre = "Error";
description = "Error";
type = "Error";
}
TextView monTexte=(TextView)findViewById(R.id.Type);
monTexte.setText("Vous avez slectionn un " + type);
TextView monTexte2=(TextView)findViewById(R.id.Titre);
monTexte2.setText("Titre : " + titre);
// si on possde une image (Livre,CD,Dvd)
if (objetbundle.containsKey("amazon_min")) {
amazon_min = this.getIntent().getStringExtra("amazon_min");
// on affiche l'image
afficheImage(amazon_min);
}
else {
amazon_min= "Pas d'image";
}
TextView monTexte3=(TextView)findViewById(R.id.Description);
monTexte3.setText("Description : "+description);
description = description.toLowerCase();
Log.i("titre", String.valueOf(titre));
// On rcupre notre bouton cr en XML grce son id
final Button boutonReserver = (Button) findViewById(R.id.Reserver);
PanierSQLite bdd = new PanierSQLite(OeuvreActivity.this);
bdd.open();
// Si l'oeuvre est dans le panier, le bouton doit permettre de la supprimer du panier
if (bdd.getLivreWithId(Integer.valueOf(id)) != null ||
bdd.getCDWithId(Integer.valueOf(id)) != null ||
bdd.getDvdWithId(Integer.valueOf(id)) != null ||
bdd.getRevueWithId(Integer.valueOf(id)) != null )
{
boutonReserver.setText("Supprimer du panier");
}
bdd.close();
//On applique un couteur d'vnement notre bouton "Crer une notification"
boutonReserver.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
PanierSQLite bdd = new PanierSQLite(OeuvreActivity.this);
bdd.open();
if(type.compareTo("livre") == 0) {
Livre livre = new Livre(Integer.valueOf(id), titre, description, editeur,nbExemplaire, auteur, collection, isbn);
//Si le livre n'est pas dans le panier on l'ajoute
if (bdd.getLivreWithId(Integer.valueOf(id)) == null){
creerNotifReservation();
//insertion du livre dans la base
long ins = bdd.insertLivre(livre);
if (ins != -1){
Toast.makeText(OeuvreActivity.this,"Le livre a bien t ajout votre panier",Toast.LENGTH_SHORT).show();
boutonReserver.setText("Supprimer du panier");
}
else {
Toast.makeText(OeuvreActivity.this,"Erreur lors de l'insertion du livre dans le panier",Toast.LENGTH_SHORT).show();
}
}
//sinon, on le supprime
else {
long suppr = bdd.removeLivreWithId(Integer.valueOf(id));
if (suppr != -1){
Toast.makeText(OeuvreActivity.this,"Le livre a bien t supprim de votre panier",Toast.LENGTH_SHORT).show();
boutonReserver.setText("Ajouter au panier");
}
else {
Toast.makeText(OeuvreActivity.this,"Erreur lors de la suppression du livre dans le panier",Toast.LENGTH_SHORT).show();
}
}
}
else if (type.compareTo("CD") == 0){
CD cd = new CD(Integer.valueOf(id), titre, description, editeur,nbExemplaire, chanteur, ean);
//Si le CD n'est pas dans le panier on l'ajoute
if (bdd.getCDWithId(Integer.valueOf(id)) == null){
creerNotifReservation();
//insertion du livre dans la base
bdd.insertCD(cd);
Toast.makeText(OeuvreActivity.this,"Le CD a bien t ajout votre panier",Toast.LENGTH_SHORT).show();
boutonReserver.setText("Supprimer du panier");
}
//sinon, on le supprime
else {
bdd.removeCDWithID(Integer.valueOf(id));
Toast.makeText(OeuvreActivity.this,"Le CD a bien t supprim de votre panier",Toast.LENGTH_SHORT).show();
boutonReserver.setText("Ajouter au panier");
}
}
else if (type.compareTo("Dvd") == 0){
Dvd dvd = new Dvd(Integer.valueOf(id), titre, description, editeur,nbExemplaire, realisateur, ean);
//Si le DVD n'est pas dans le panier on l'ajoute
if (bdd.getDvdWithId(Integer.valueOf(id)) == null){
creerNotifReservation();
//insertion du dvd dans la base
long ins = bdd.insertDvd(dvd);
if (ins != -1){
Toast.makeText(OeuvreActivity.this,"Le DVD a bien t ajout votre panier",Toast.LENGTH_SHORT).show();
boutonReserver.setText("Supprimer du panier");
}
else {
Toast.makeText(OeuvreActivity.this,"Erreur lors de l'insertion du DVD dans le panier",Toast.LENGTH_SHORT).show();
}
}
//sinon, on le supprime
else {
long suppr = bdd.removeDvdWithId(Integer.valueOf(id));
if (suppr != -1){
Toast.makeText(OeuvreActivity.this,"Le DVD a bien t supprim de votre panier",Toast.LENGTH_SHORT).show();
boutonReserver.setText("Ajouter au panier");
}
else {
Toast.makeText(OeuvreActivity.this,"Erreur lors de la suppression du DVD dans le panier",Toast.LENGTH_SHORT).show();
}
}
}
else if (type.compareTo("Revue") == 0){
Revue revue = new Revue(Integer.valueOf(id), titre, description, editeur,nbExemplaire, issn, period);
//Si la revue n'est pas dans le panier on l'ajoute
if (bdd.getRevueWithId(Integer.valueOf(id)) == null){
creerNotifReservation();
//insertion de la revue dans la base
long ins = bdd.insertRevue(revue);
if (ins != -1){
Toast.makeText(OeuvreActivity.this,"La revue a bien t ajoute votre panier",Toast.LENGTH_SHORT).show();
boutonReserver.setText("Supprimer du panier");
}
else {
Toast.makeText(OeuvreActivity.this,"Erreur lors de l'insertion de la revue dans le panier",Toast.LENGTH_SHORT).show();
}
}
//sinon, on le supprime
else {
long suppr = bdd.removeRevueWithID(Integer.valueOf(id));
if (suppr != -1){
Toast.makeText(OeuvreActivity.this,"La revue a bien t supprim de votre panier",Toast.LENGTH_SHORT).show();
boutonReserver.setText("Ajouter au panier");
}
else {
Toast.makeText(OeuvreActivity.this,"Erreur lors de la suppression de la revue dans le panier",Toast.LENGTH_SHORT).show();
}
}
}
bdd.close();
}
});
}
/**
* Mthode qui va crer une notification pour une oeuvre enregistre dans le panier
*/
private void creerNotifReservation(){
//On cre un "gestionnaire de notification"
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//On cre la notification avec son icne et son texte dfilant (optionel, null sinon)
Notification notification = new Notification(R.drawable.icone, "Vous avez enregistr une nouvelle oeuvre dans votre panier", System.currentTimeMillis());
//Le PendingIntent c'est ce qui va nous permettre d'atteindre notre Activit NotifReservation, charge de l'affichage de la Notification
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotifPanier.class), 0);
String titreNotification = "Oeuvre ajoute"; // Titre de la notification
String texteNotification = "Vous venez d'enregistrer une nouvelle oeuvre dans votre panier"; // Texte de la notification
//On configure notre notification avec tous les paramtres que l'on vient de crer
notification.setLatestEventInfo(this, titreNotification, texteNotification, pendingIntent);
notification.vibrate = new long[] {0,200,100,200,100,200}; //Vibration lors de la notification
//Enfin on ajoute notre notification et son ID notre gestionnaire de notification
notificationManager.notify(ID_NOTIFICATION, notification);
}
}
|