package com.example.download;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class DownloadFileActivity1 extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.downloadfile1);
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new MyButtonListener());
  }

  class MyButtonListener implements OnClickListener {
    public void onClick(View v) {
      EditText editText = (EditText) findViewById(R.id.url);
      String url = editText.getText().toString();
      Toast.makeText(getBaseContext(), "Beginning download...",
          Toast.LENGTH_SHORT).show();
      new MyAsyncTask().execute(url);
    }
  }

  class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
    private ProgressBar bar;
    private ImageView imageView;

    @Override
    protected void onPreExecute() {
      imageView = (ImageView) findViewById(R.id.imageView);
      bar = (ProgressBar) findViewById(R.id.progress);

      imageView.setImageBitmap(null);
      bar.setProgress(0);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
      String url = params[0];

      try {
        int bufferSize = 1024;

        URLConnection conn = new URL(url).openConnection();
        InputStream is = conn.getInputStream();
        int imageSize = conn.getContentLength();

        byte[] buffer = new byte[bufferSize];
        byte[] imageArray = new byte[imageSize];
        int numberOfBuffers = imageSize / bufferSize;

        int bytesRead = 0;
        int position = 0;
        int progress = 0;
        while ((bytesRead = is.read(buffer)) != -1) {
          System.arraycopy(buffer, 0, imageArray, position, bytesRead);
          position += bytesRead;

          publishProgress(numberOfBuffers, ++progress);
        }

        return BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length);
      }
      catch (IOException e) {
        System.err.println(e);
        return null;
      }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
      bar.setMax(values[0]);
      bar.setProgress(values[1]);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
      Toast toast = null;
      if (result != null) {
        imageView.setImageBitmap(result);
        toast = Toast.makeText(DownloadFileActivity1.this,
            R.string.downloadSucess, Toast.LENGTH_SHORT);
      }
      else {
        LayoutInflater inflator = getLayoutInflater();
        View layout = inflator.inflate(R.layout.custom_toast, null);

        toast = new Toast(DownloadFileActivity1.this);
        toast.setView(layout);
        toast.setDuration(Toast.LENGTH_LONG);
      }
      toast.setGravity(Gravity.CENTER, 0, 0);
      toast.show();
    }
  }
}