package com.example.proj06;

import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
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.ProgressBar;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

	ImageView downloadedImg;
	ProgressBar progressBar;
	
	private String downloadUrl = "http://www.analog-design.net/ALARME/Videos/met.jpg";
	
	
	private String[] imageURLArray = new String[]{
			"http://www.analog-design.net/ALARME/Videos/0001.jpg",
			"http://www.analog-design.net/ALARME/Videos/0020.jpg",
			"http://www.analog-design.net/ALARME/Videos/0046.jpg",
			"http://www.analog-design.net/ALARME/Videos/0084.jpg",
			"http://www.analog-design.net/ALARME/Videos/0005.jpg",
			"http://www.analog-design.net/ALARME/Videos/0006.jpg",
			"http://www.analog-design.net/ALARME/Videos/0007.jpg",
			"http://www.analog-design.net/ALARME/Videos/0008.jpg",
			"http://www.analog-design.net/ALARME/Videos/0009.jpg",
			"http://www.analog-design.net/ALARME/Videos/0010.jpg",
			"http://www.analog-design.net/ALARME/Videos/0011.jpg",
			"http://www.analog-design.net/ALARME/Videos/0012.jpg"	};

	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		Button imageDownloaderBtn = (Button) findViewById(R.id.downloadButton);
		downloadedImg = (ImageView) findViewById(R.id.imageView);
		progressBar=(ProgressBar) findViewById(R.id.progressBar);
		progressBar.setVisibility(View.GONE);

		imageDownloaderBtn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) 
			{
				progressBar.setVisibility(View.VISIBLE);
				//new ImageDownloader().execute(downloadUrl);
				
				
				
				for (int i = 0; i < 3; i++) //imageURLArray.length
				{
					
					
			    	String str = "+++++++" + imageURLArray[i] + "+++++++++";
			    	System.out.println(str);
			    	
					new ImageDownloader().execute(imageURLArray[i]);
					
					try
	                {
	                    Thread.sleep(3000);
	                }
	                catch(Exception e)
	                {
	                    e.printStackTrace();
	                }
					
				}
			}

		});
	}

	 class ImageDownloader extends AsyncTask<String, Void, Bitmap>
	 {

		@Override
		protected Bitmap doInBackground(String... param) {
			// TODO Auto-generated method stub
			return downloadBitmap(param[0]);
		}

		@Override
		protected void onPreExecute() {
			Log.i("Async-Example", "onPreExecute Called");
			

		}

		@Override
		protected void onPostExecute(Bitmap result) {
			Log.i("Async-Example", "onPostExecute Called");
			
			//downloadedImg.setImageResource(R.drawable);
			//downloadedImg.setImageResource(null);
			downloadedImg.setImageDrawable(null);
			//downloadedImg = (ImageView) findViewById(R.id.imageView);
			
			downloadedImg.setImageBitmap(result);
			progressBar.setVisibility(View.GONE);

		}

		private Bitmap downloadBitmap(String url) {
			// initilize the default HTTP client object
			final DefaultHttpClient client = new DefaultHttpClient();

			//forming a HttoGet request 
			final HttpGet getRequest = new HttpGet(url);
			try {

				HttpResponse response = client.execute(getRequest);

				//check 200 OK for success
				final int statusCode = response.getStatusLine().getStatusCode();

				if (statusCode != HttpStatus.SC_OK) {
					Log.w("ImageDownloader", "Error " + statusCode + 
							" while retrieving bitmap from " + url);
					return null;

				}

				final HttpEntity entity = response.getEntity();
				if (entity != null) {
					InputStream inputStream = null;
					try {
						// getting contents from the stream 
						inputStream = entity.getContent();

						// decoding stream data back into image Bitmap that android understands
						final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

						return bitmap;
					} finally {
						if (inputStream != null) {
							inputStream.close();
						}
						entity.consumeContent();
					}
				}
			} catch (Exception e) {
				// You Could provide a more explicit error message for IOException
				getRequest.abort();
				Log.e("ImageDownloader", "Something went wrong while" +
						" retrieving bitmap from " + url + e.toString());
			} 

			return null;
		}
	}
}

