package gr.mybook.mywebasync;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	Button btn1, btn2;
	ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
	}

	public void getImage(View v) {
		String url1 = "http://icons.iconarchive.com/icons/icons-land/multiple-smiley/48/Auto-Angry-icon.png";
		new HttpAsyncTask1().execute(url1);
	}
	
	public void getText(View v) {
		String url2 = "http://www.enet.gr/rss?i=news.el.categories&c=episthmh--texnologia";
		new HttpAsyncTask2().execute(url2);
	}

	private Bitmap downloadImage(String urli) {
 // 	  progressDialog = ProgressDialog.show(this, "", "Fetching Image...");
        Bitmap bitmap = null;
        InputStream in = null;        
        try {
            in = openHttpConnection(urli);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;                
 	}
	
	private String downloadText(String urlt) {
//        progressDialog = ProgressDialog.show(this, "", "Fetching Text...");
		String text=null;
        int BUFFER_SIZE = 2000;
        InputStream in = null;
        try {
           in = openHttpConnection(urlt);
           InputStreamReader isr = new InputStreamReader(in);
           int charRead;
           text = "";
           char[] inputBuffer = new char[BUFFER_SIZE];
           while ((charRead = isr.read(inputBuffer))>0) {                    
              //---convert the chars to a String---
              String readString = String.copyValueOf(inputBuffer, 0, charRead);                    
              text += readString;
              inputBuffer = new char[BUFFER_SIZE];
           }
           in.close();
        }catch (IOException e) {
           e.printStackTrace();
         }
        return text;
	} 
	
    private InputStream openHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;
            URL url = new URL(urlString);
            URLConnection conx = url.openConnection();
             
            if (!(conx instanceof HttpURLConnection)) {
                throw new IOException ("URL is not an Http URL");
            }
            try {  
            HttpURLConnection httpConnection = (HttpURLConnection)conx;
            httpConnection.setAllowUserInteraction(false);
            httpConnection.setInstanceFollowRedirects(true);
            httpConnection.setRequestMethod("GET");
            httpConnection.connect(); 
 
            response = httpConnection.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConnection.getInputStream();                                 
            }         
        } catch (IOException e) {
            e.printStackTrace();
        }
        return in;
    }
	
    private class HttpAsyncTask1 extends AsyncTask<String, Void, Bitmap> {
        @Override
        protected Bitmap doInBackground(String... url) {
         Bitmap bitmap = downloadImage(url[0]);
         return bitmap;
        }

        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(Bitmap bitmap) {
          ImageView img = (ImageView) findViewById( R.id.imageView1);
          img.setImageBitmap(bitmap);
        }
      }

    private class HttpAsyncTask2 extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... url) {
          String text = downloadText(url[0]);
          return text;
        }

        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String text) {
          TextView txt = (TextView) findViewById( R.id.textView1);
          txt.setText(text);
        }
      } 
	
	@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;
	}

}
