package com.freak.fidji.locationoverbt;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Parcel;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

public class SendingThread extends Thread {

    private static final  String TAG = "SENDING_THREAD";
    private static final boolean DEBUG = true;
    private static final UUID MY_UUID = UUID.fromString("4364cf1a-7621-11e4-b116-123b93f75cba");
    private static final int MAX_CONNECTION_ATTEMPT = 6;
    private final BluetoothDevice mDevice;
    private final Context mContext;
    private boolean mRunning;
    private SendingThreadListener mListener;

    public SendingThread(Context context, BluetoothDevice device) {
        if (DEBUG)
            Log.d(TAG, "Create thread for device " + device.getAddress());
        mContext = context;
        mDevice = device;
        mRunning = false;
        mListener = null;
    }

    @Override
    public synchronized void start() {
        Log.i(TAG, "Start thread for device " + mDevice.getAddress());
        mRunning = true;
        super.start();
    }

    @Override
    public void run() {
        boolean connected;
        int attempt = 0;
        BluetoothSocket tmp, socket;
        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        while (mRunning) {
            if (DEBUG)
                Log.d(TAG, "Retrieve location " + mDevice.getAddress());
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (DEBUG)
                Log.d(TAG, "Create socket for device " + mDevice.getAddress());
            tmp = null;
            try {
                tmp = mDevice.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                e.printStackTrace();
            }
            socket = tmp;

            if (socket != null) {
                if (DEBUG)
                    Log.d(TAG, "Connect socket " + mDevice.getAddress());
                try {
                    socket.connect();
                    connected = true;
                } catch (IOException e) {
                    connected = false;
                    Log.e(TAG, "Error while connecting for device " + mDevice.getAddress());
                    e.printStackTrace();

                    attempt++;
                    if (attempt == MAX_CONNECTION_ATTEMPT) {
                        mRunning = false;
                        Log.e(TAG, "Too much attempts, stop thread");
                    }
                }

                if (connected) {
                    if (DEBUG)
                        Log.d(TAG, "Connected" + mDevice.getAddress());
                    attempt = 0;

                    if (mListener != null) {
                        if (DEBUG)
                            Log.d(TAG, "Inform service that connection is successful for device " + mDevice.getAddress());
                        mListener.onConnected(mDevice);
                    }

                    if (location != null) {
                        if (DEBUG)
                            Log.d(TAG, "Convert location to byte array " + mDevice.getAddress());
                        Parcel parcel = Parcel.obtain();
                        location.writeToParcel(parcel, 0);
                        byte[] bytes = parcel.marshall();

                        if (DEBUG)
                            Log.d(TAG, "Send location " + mDevice.getAddress());
                        try {
                            DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
                            dOut.write(bytes, 0, bytes.length);
                            dOut.flush();
                            dOut.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Error while sending datas for device " + mDevice.getAddress());
                            e.printStackTrace();
                        }
                    }
                    else {
                        if (DEBUG)
                            Log.d(TAG, "No location to send to " + mDevice.getAddress());
                    }
                }
                
                try {
                    if (DEBUG)
                        Log.d(TAG, "Close socket for device " + mDevice.getAddress());
                    socket.close();
                } catch (IOException e) {
                    Log.e(TAG, "Error while closing socket " + mDevice.getAddress());
                    e.printStackTrace();
                }
            }

            try {
                if (DEBUG)
                    Log.d(TAG, "Sleep 10s for device " + mDevice.getAddress());
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Log.i(TAG, "Thread stopped for device " + mDevice.getAddress());
        if (mListener != null) {
            mListener.onThreadFinished(mDevice);
        }
    }

    public void disconnect() {
        if (DEBUG)
            Log.d(TAG, "Stop thread for device " + mDevice.getAddress());
        mRunning = false;
    }

    public void setListener(SendingThreadListener listener) {
        mListener = listener;
    }
}

@@ -665,16 +665,77 @@
 PT = 6;%0A
+    private static final long TIME_BETWEEN_TO_SENDS = 15000;%0A
     priv
@@ -3273,32 +3273,66 @@
              %7D%0A%0A
+                    byte%5B%5D bytes;%0A
                 
@@ -3627,31 +3627,24 @@
             
- byte%5B%5D
  bytes = par
@@ -3656,24 +3656,73 @@
 arshall();%0A%0A
+                    %7D%0A                    else %7B%0A
             
@@ -3784,20 +3784,18 @@
 d(TAG, %22
-Send
+No
  locatio
@@ -3788,32 +3788,43 @@
 G, %22No location 
+to send to 
 %22 + mDevice.getA
@@ -3854,38 +3854,258 @@
             
+S
 tr
-y %7B%0A        
+ing string  = %22No datas%22;%0A                        bytes = string.getBytes();%0A                    %7D%0A%0A                    if (DEBUG)%0A                        Log.d(TAG, %22Send datas %22 + mDevice.getAddress());%0A                    try %7B%0A
             
@@ -4112,16 +4112,20 @@
         
+    
 DataOutp
@@ -4208,28 +4208,24 @@
             
-    
 dOut.write(b
@@ -4268,28 +4268,24 @@
             
- 
-   
 dOut.flush()
@@ -4282,28 +4282,24 @@
 ut.flush();%0A
-    
             
@@ -4336,36 +4336,32 @@
                 
- 
-   
 %7D catch (IOExcep
@@ -4362,36 +4362,32 @@
 OException e) %7B%0A
-    
                 
@@ -4486,36 +4486,32 @@
                 
-    
 e.printStackTrac
@@ -4519,208 +4519,8 @@
 ();%0A
-                        %7D%0A                    %7D%0A                    else %7B%0A                        if (DEBUG)%0A                            Log.d(TAG, %22No location to send to %22 + mDevice.getAddress());%0A
     
@@ -5033,18 +5033,55 @@
  %22Sleep 
-10
+%22 + (TIME_BETWEEN_TO_SENDS / 1000) + %22 
 s for de
@@ -5145,13 +5145,29 @@
 eep(
-10000
+TIME_BETWEEN_TO_SENDS
 );%0A 

Send text message if there's no location available