package com.example.patrice.http01;

/**
 * Created by Patrice on 21/12/2017.
 */

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
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;

// http://www.softwarepassion.com/android-series-download-files-with-progress-dialog/

// http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/

// https://junjunguo.com/blog/android-resumable-download/

// http://www.androhub.com/android-download-files-save/

// http://www.androhub.com/android-json-parser/

//https:android--examples.blogspot.fr/2017/02/android-asynctask-download-multiple.html   ++++

//        http://www.viralandroid.com/2016/12/downloading-file-or-image-and-displaying-in-android.html

// https://androidjson.com/capture-image-camera-upload-server/


public class android_asynchro extends Activity {

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    private Button startBtn;
    private ProgressDialog mProgressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.asynchrone_layout);

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

        startBtn.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                startDownload();
            }
        });
    }

    private void startDownload()
    {
        String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
        url = "http://analog-design.net/WORKAREA/WEBSITE/Codebar/logo_appli.jpg";
        new DownloadFileAsync().execute(url);
    }
    @Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id)
        {
            case DIALOG_DOWNLOAD_PROGRESS:
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setMessage("Downloading file..");
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mProgressDialog.setCancelable(false);
                mProgressDialog.show();
                return mProgressDialog;
            default:
                return null;
        }
    }
    class DownloadFileAsync extends AsyncTask<String, String, String>
    {

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
        protected String doInBackground(String... aurl)
        {
            int count;

            try {
                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lenghtOfFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1)
                {
                    total += count;
                    publishProgress(""+(int)((total*100)/lenghtOfFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            }

            catch (Exception e)
            {}
            return null;

        }
        protected void onProgressUpdate(String... progress)
        {
            Log.d("ANDRO_ASYNC",progress[0]);
            mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }

        @Override
        protected void onPostExecute(String unused)
        {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
    }
}