package com.example.patrice.mysql01;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class IPScan extends AppCompatActivity {

    private Button btnScan;
    private ListView listViewIp;

    ArrayList<String> ipList;
    ArrayAdapter<String> adapter;

    String host = "http://82.234.53.234";
    int timeout = 5000;

    boolean exists = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ipscan_layout);

        btnScan = (Button)findViewById(R.id.scan);
        listViewIp = (ListView)findViewById(R.id.listviewip);

        ipList = new ArrayList();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
        listViewIp.setAdapter(adapter);



        btnScan.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                try
                {
                    exists = InetAddress.getByName(host).isReachable(timeout);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

                //new ScanIpTask().execute();
            }
        });

    }

    private class ScanIpTask extends AsyncTask<Void, String, Void>
    {

        /*
        Scan IP 192.168.1.100~192.168.1.110
        you should try different timeout for your network/devices
         */
        static final String subnet = "http://82.234.53.234";
        static final int lower = 100;
        static final int upper = 110;
        static final int timeout = 5000;

        @Override
        protected void onPreExecute()
        {
            ipList.clear();
            adapter.notifyDataSetInvalidated();
            Toast.makeText(IPScan.this, "Scan IP...", Toast.LENGTH_LONG).show();
        }

        @Override
        protected Void doInBackground(Void... params)
        {

            for (int i = lower; i <= upper; i++) {
                String host = subnet + i;

                try {
                    InetAddress inetAddress = InetAddress.getByName(host);
                    if (inetAddress.isReachable(timeout))
                    {
                        publishProgress(inetAddress.toString());
                    }

                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(String... values)
        {
            ipList.add(values[0]);
            adapter.notifyDataSetInvalidated();
            Toast.makeText(IPScan.this, values[0], Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onPostExecute(Void aVoid)
        {
            Toast.makeText(IPScan.this, "Done", Toast.LENGTH_LONG).show();
        }
    }

}
