package com.example.alarme04;

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

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 TextView textResponse;
 EditText editTextAddress, editTextPort; 
 Button buttonConnect, buttonClear;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  editTextAddress = (EditText)findViewById(R.id.address);
  editTextPort = (EditText)findViewById(R.id.port);
  buttonConnect = (Button)findViewById(R.id.connect);
  buttonClear = (Button)findViewById(R.id.clear);
  textResponse = (TextView)findViewById(R.id.response);
  
  buttonConnect.setOnClickListener(buttonConnectOnClickListener);
  
  buttonClear.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    textResponse.setText("");
   }});
 }
 
 OnClickListener buttonConnectOnClickListener = 
   new OnClickListener(){

    @Override
    public void onClick(View arg0) {
     /*
      * You have to verify editTextAddress and
      * editTextPort are input as correct format.
      */
     
     MyClientTask myClientTask = new MyClientTask(
       editTextAddress.getText().toString(),
       Integer.parseInt(editTextPort.getText().toString()));
     myClientTask.execute();
    }};

 public class MyClientTask extends AsyncTask<Void, Void, Void> {
  
  String dstAddress;
  int dstPort;
  String response;
  
  MyClientTask(String addr, int port){
   dstAddress = addr;
   dstPort = port;
  }

  @Override
  protected Void doInBackground(Void... arg0) {
   
   try {
	   			Socket socket = new Socket(dstAddress, dstPort);
	   			//InputStream inputStream = socket.getInputStream();
	   			//ByteArrayOutputStream byteArrayOutputStream =  new ByteArrayOutputStream(1024);
	   			//byte[] buffer = new byte[1024];
    
	   			//int bytesRead;
	   			//while ((bytesRead = inputStream.read(buffer)) != -1)
	   			//{
	   			//	byteArrayOutputStream.write(buffer, 0, bytesRead);
	   			//}
             
	   			//socket.close();
	   			//response = byteArrayOutputStream.toString("UTF-8");
             
             
	   			FileOutputStream fos = new FileOutputStream("aqw.jpeg");
	   			BufferedOutputStream bos = new BufferedOutputStream(fos);
	   			InputStream is = socket.getInputStream();
             
	   			byte[] contents = new byte[10000];
	   			int bytesRead = 0; 
	   			
	   			while((bytesRead=is.read(contents))!=-1)
	   				bos.write(contents, 0, bytesRead); 
             
	   			bos.flush(); 

	   			socket.close();
	   			
	   			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
	        	}
	   		
             
             
    
   } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return null;
  }

  @Override
  protected void onPostExecute(Void result) {
   textResponse.setText(response);
   super.onPostExecute(result);
  }
  
 }

}