package com.northstar.minimap;

import android.app.Activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Chris on 12/9/13.
 */
public class BluetoothFragment extends Fragment {
    private static final int REQUEST_ENABLE_BT = 1;
    private static final long SCAN_PERIOD = 10000;

    private boolean scanning = false;
    private int scans = 0;

    public static final String TAG = "RNM-BF";

    private Activity activity;
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothAdapter.LeScanCallback leScanCallback;
    private BluetoothManager bluetoothManager;
    private Handler handler;
    private Map<String, Integer> rssiMap = new HashMap<String, Integer>();
    private List<String> rssiList;
    private ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.fragment_bluetooth, container, false);
        listView = (ListView) layout.findViewById(R.id.rssi_list);
        initBluetooth();

        return layout;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(TAG, "Attached");
        this.activity = activity;
        initBluetooth();
    }

    private void initBluetooth() {
        if (activity == null || listView == null) {
            return;
        }

        bluetoothManager =
                (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();

        if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        rssiList = new ArrayList<String>();
        final ArrayAdapter<String> rssiAdapter =
                new ArrayAdapter<String>(activity, R.layout.list_rssi, rssiList);
        listView.setAdapter(rssiAdapter);
        rssiList.add("RSSI LIST");

        leScanCallback =
                new BluetoothAdapter.LeScanCallback() {
                    @Override
                    public void onLeScan(final BluetoothDevice device, final int rssi,
                                         byte[] scanRecord) {
                        Log.d(TAG, "RSSI: " + rssi);
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String address = device.getAddress();
                                if (rssiMap.containsKey(address)) {
                                    rssiMap.remove(address);
                                }
                                rssiMap.put(address, rssi);
                                rssiList.clear();
                                for (String key: rssiMap.keySet()) {
                                    rssiList.add(key + ": " + rssiMap.get(key));
                                }
                                rssiAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                };

        if (!scanning) {
            scanning = true;
            scanLeDevice(true);
        }
    }

    private void scanLeDevice(final boolean enable) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // Stops scanning after a pre-defined scan period.
                handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                    bluetoothAdapter.stopLeScan(leScanCallback);
                    scanLeDevice(true);
                    }
                }, SCAN_PERIOD);

                bluetoothAdapter.startLeScan(leScanCallback);
                Log.d(TAG, "Start scan: " + scans);
                scans++;
            }
        });
    }
}

@@ -3971,36 +3971,32 @@
    scanLeDevice(
-true
 );%0A        %7D%0A   
@@ -4033,142 +4033,12 @@
 ice(
-final boolean enable) %7B%0A        activity.runOnUiThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
+) %7B%0A
     
@@ -4092,24 +4092,16 @@
 period.%0A
-        
         
@@ -4133,24 +4133,16 @@
         
-  
-      
 handler.
@@ -4178,32 +4178,24 @@
             
-        
 @Override%0A  
@@ -4200,32 +4200,24 @@
             
- 
-       
 public void 
@@ -4220,32 +4220,24 @@
 oid run() %7B%0A
-        
             
@@ -4289,32 +4289,24 @@
             
-        
 scanLeDevice
@@ -4310,19 +4310,11 @@
 ice(
-true);%0A    
+);%0A
     
@@ -4317,38 +4317,26 @@
             
-    %7D%0A        
+%7D%0A
         %7D, S
@@ -4353,32 +4353,24 @@
 );%0A%0A        
-    
-    
 bluetoothAda
@@ -4399,32 +4399,24 @@
 nCallback);%0A
-        
         Log.
@@ -4451,32 +4451,24 @@
 s);%0A        
-   
-     
 scans++;%0A   
@@ -4468,34 +4468,8 @@
 ++;%0A
-            %7D%0A        %7D);%0A
     

Remove UI threading.