package com.example.alarm02;



import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;


public class MainActivity extends Activity {

      Button button1,button2,button3,button4,button5;
      
      String imagefile ="/sdcard/IMG_9331.JPG";

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        button1=(Button) findViewById(R.id.button1);

        button2=(Button) findViewById(R.id.button2);

        button3=(Button) findViewById(R.id.button3);

        button4=(Button) findViewById(R.id.button4);

        button5=(Button) findViewById(R.id.button5);
        
        ImageView image = (ImageView)findViewById(R.id.image);
        Bitmap bm = BitmapFactory.decodeFile(imagefile);
        image.setImageBitmap(bm);

    }

    public void onClick(View v)  {

        final int id = v.getId();

        switch (id) {

        case R.id.button1:  


        	TCP_communication instance ;         // définition de la variable référence
        	instance = new TCP_communication(); // création de l’instance
        	instance.send_cmd("bidule");
        	
        	button1.setText("Transfert image");   
	
             
            break;

        case R.id.button2:       

        	String filename = "filename.txt";
        	File file = new File(Environment.getExternalStorageDirectory(), filename);
        	FileOutputStream fos;
        	byte[] data = new String("data to write to file").getBytes();
        	try {
        	    fos = new FileOutputStream(file);
        	    fos.write(data);
        	    fos.flush();
        	    fos.close();
        	} catch (FileNotFoundException e) {
        	    // handle exception
        	} catch (IOException e) {
        	    // handle exception
        	}
        	
        	
        	
        	
        	
            button2.setText("You clicked on Button 2");   

            break;

        case R.id.button3:     

            button3.setText("You clicked on Button 3");

            break;

        case R.id.button4:         

            button4.setText("You clicked on Button 4");

            break;

        case R.id.button5:

            button5.setText("You clicked on Button 5");

            break;

        }

    }

    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}

class TCP_communication 
{
	 	
		void send_cmd(String cmd)
		{
			

			 try
			 {
				 
			  	 String serverHost = "82.234.53.234"; //"localhost";
	             Socket socket = new Socket(serverHost , 8081); //InetAddress.getByName("localhost")
	             byte[] contents = new byte[10000];
	             
	             //Initialize the FileOutputStream to the output file's full path.
//	             FileOutputStream fos = new FileOutputStream("f:\\xxxxx.jpeg");
//	             BufferedOutputStream bos = new BufferedOutputStream(fos);
//	             InputStream is = socket.getInputStream();
	             
	             //No of bytes read in one read() call
//	             int bytesRead = 0; 
	             
//	             while((bytesRead=is.read(contents))!=-1)
//	                 bos.write(contents, 0, bytesRead); 
	             
//	             bos.flush(); 
//	             socket.close(); 
			
				 
	             socket.close();
				
				
			} 
			catch (IOException e) 
			{
				System.out.println(e);
			}
			
		}
		

}
