package br.usp.ime.checkattendance;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Set;

public class BluetoothDeviceListActivity extends AppCompatActivity {

    public static String EXTRA_DEVICE_ADDRESS = "device_address";
    private BluetoothAdapter mBtAdapter;
    private ArrayAdapter<String> mNewDevicesArrayAdapter;

    private void setupActionBar() {
        ActionBar actionBar = this.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("Bluetooth Devices");
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == android.R.id.home)
            finish();

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bluetooth_device_list);
        this.setupActionBar();

        Button scanButton = (Button) findViewById(R.id.button_scan_bluetooth);
        scanButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                doDiscovery();
                v.setVisibility(View.GONE);
            }
        });

        ArrayAdapter<String> pairedDevicesArrayAdapter =
                new ArrayAdapter<String>(this, R.layout.bluetooth_device_name);
        this.mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.bluetooth_device_name);

        final ListView pairedListView = (ListView) findViewById(R.id.lv_paired_devices);
        pairedListView.setAdapter(pairedDevicesArrayAdapter);
        pairedListView.setOnItemClickListener(mDeviceClickListener);

        final ListView newDevicesListView = (ListView) findViewById(R.id.lv_new_devices);
        newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
        newDevicesListView.setOnItemClickListener(mDeviceClickListener);

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, filter);

        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver, filter);

        this.checkBluetoothState();

        if (this.mBtAdapter != null) {
            Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

            if (pairedDevices.size() > 0) {
                findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
                for (BluetoothDevice device : pairedDevices) {
                    pairedDevicesArrayAdapter.insert(device.getName() + "\n" + device.getAddress(), 0);
                }
            } else {
                String noDevices = "None paired devices";
                pairedDevicesArrayAdapter.add(noDevices);
            }
        }
    }

    private void checkBluetoothState() {
        mBtAdapter=BluetoothAdapter.getDefaultAdapter();

        if(mBtAdapter==null) {
            Toast.makeText(getBaseContext(), "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
            finish();
        } else if (! mBtAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, 1);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mBtAdapter != null)
            mBtAdapter.cancelDiscovery();

        this.unregisterReceiver(mReceiver);
    }

    private void doDiscovery() {
        Toast.makeText(BluetoothDeviceListActivity.this, "Scanning", Toast.LENGTH_SHORT).show();

        if (mBtAdapter.isDiscovering())
            mBtAdapter.cancelDiscovery();

        mBtAdapter.startDiscovery();
    }

    private AdapterView.OnItemClickListener mDeviceClickListener
            = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
            mBtAdapter.cancelDiscovery();

            String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);

            Intent intent = new Intent();
            intent.putExtra(EXTRA_DEVICE_ADDRESS, address);

            setResult(Activity.RESULT_OK, intent);
            finish();
        }
    };

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                if (device.getBondState() != BluetoothDevice.BOND_BONDED)
                    mNewDevicesArrayAdapter.insert(device.getName() + "\n" + device.getAddress(), 0);

            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                String message = "Select a device";
                Toast.makeText(BluetoothDeviceListActivity.this, message, Toast.LENGTH_SHORT).show();
                if (mNewDevicesArrayAdapter.getCount() == 0) {
                    String noDevices = "No devices found";
                    mNewDevicesArrayAdapter.insert(noDevices, 0);
                }
            }
        }
    };
}

@@ -989,16 +989,76 @@
 Adapter;
+%0A    private ArrayAdapter%3CString%3E pairedDevicesArrayAdapter;
 %0A%0A    pr
@@ -2039,37 +2039,21 @@
         
-ArrayAdapter%3CString%3E 
+this.
 pairedDe
@@ -2186,24 +2186,40 @@
 rayAdapter =
+%0A               
  new ArrayAd
@@ -3012,24 +3012,68 @@
 othState();%0A
+    %7D%0A%0A    private void setPairedDevices() %7B
 %0A        if 
@@ -3385,32 +3385,37 @@
                 
+this.
 pairedDevicesArr
@@ -3448,32 +3448,60 @@
 tName() + %22%5Cn%22 +
+%0A                           
  device.getAddre
@@ -3737,32 +3737,37 @@
 ate() %7B%0A        
+this.
 mBtAdapter=Bluet
@@ -3808,16 +3808,126 @@
         
+if (this.mBtAdapter != null && this.mBtAdapter.isEnabled())%0A            this.setPairedDevices();%0A        else 
 if(mBtAd
@@ -4280,32 +4280,220 @@
        %7D%0A    %7D%0A%0A
+    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        if (resultCode == Activity.RESULT_OK)%0A            this.setPairedDevices();%0A    %7D%0A%0A
     @Override%0A  

fix paired devices list after enabling bluetooth