package com.simperium.android;

import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.http.AsyncHttpGet;
import com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback;
import com.koushikdutta.async.http.WebSocket;

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import java.io.IOException;

class AsyncWebSocketProvider implements WebSocketManager.ConnectionProvider {

    public static final String TAG = "Simperium.AsyncWebSocketProvider";

    protected final String mAppId;
    protected final String mSessionId;
    protected final AsyncHttpClient mAsyncClient;
    protected final Handler mHandler;

    AsyncWebSocketProvider(String appId, String sessionId, AsyncHttpClient asyncClient) {
        mAppId = appId;
        mAsyncClient = asyncClient;
        mSessionId = sessionId;
        mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void connect(final WebSocketManager.ConnectionListener listener) {

        Uri uri = Uri.parse(String.format(AndroidClient.WEBSOCKET_URL, mAppId));

        AsyncHttpRequest request = new AsyncHttpGet(uri);
        request.setHeader(AndroidClient.USER_AGENT_HEADER, mSessionId);

        // Protocol is null
        mAsyncClient.websocket(request, null, new WebSocketConnectCallback() {

            @Override
            public void onCompleted(Exception ex, final WebSocket webSocket) {
                if (ex != null) {
                    listener.onError(ex);
                    return;
                }

                if (webSocket == null) {
                    listener.onError(new IOException("WebSocket could not be opened"));
                    return;
                }

                final WebSocketManager.Connection connection = new WebSocketManager.Connection() {

                    @Override
                    public void close() {
                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {
                                webSocket.close();
                            }

                        });
                    }

                    @Override
                    public void send(final String message) {
                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {
                                webSocket.send(message);
                            }

                        });
                    }

                };

                webSocket.setStringCallback(new WebSocket.StringCallback() {

                   @Override
                   public void onStringAvailable(String s) {
                       listener.onMessage(s);
                   }

                });

                webSocket.setEndCallback(new CompletedCallback() {

                    @Override
                    public void onCompleted(Exception ex) {
                        listener.onDisconnect(ex);
                    }

                });

                webSocket.setClosedCallback(new CompletedCallback() {
                    @Override
                    public void onCompleted(Exception ex) {
                        listener.onDisconnect(ex);
                    }
                });

                listener.onConnect(connection);

            }

        });
    }

}

@@ -25,16 +25,41 @@
 droid;%0A%0A
+import android.net.Uri;%0A%0A
 import c
@@ -204,23 +204,47 @@
 syncHttp
-Request
+Client.WebSocketConnectCallback
 ;%0Aimport
@@ -334,39 +334,15 @@
 Http
-Client.WebSocketConnectCallback
+Request
 ;%0Aim
@@ -390,86 +390,8 @@
 t;%0A%0A
-import android.net.Uri;%0Aimport android.os.Handler;%0Aimport android.os.Looper;%0A%0A
 impo
@@ -568,16 +568,66 @@
 ider%22;%0A%0A
+    protected final AsyncHttpClient mAsyncClient;%0A
     prot
@@ -695,96 +695,8 @@
 nId;
-%0A    protected final AsyncHttpClient mAsyncClient;%0A    protected final Handler mHandler;
 %0A%0A  
@@ -879,64 +879,8 @@
 Id;%0A
-        mHandler = new Handler(Looper.getMainLooper());%0A
     
@@ -1896,500 +1896,141 @@
     
-mHandler.post(new Runnable() %7B%0A%0A                            @Override%0A                            public void run() %7B%0A                                webSocket.close();%0A                            %7D%0A%0A                        %7D);%0A                    %7D%0A%0A                    @Override%0A                    public void send(final String message) %7B%0A                        mHandler.post(new Runnable() %7B%0A%0A                            @Override%0A                            public void run() %7B%0A        
+webSocket.close();%0A                    %7D%0A%0A                    @Override%0A                    public void send(final String message) %7B%0A
     
@@ -2074,24 +2074,16 @@
 ssage);%0A
-        
         
@@ -2100,60 +2100,8 @@
   %7D%0A
-%0A                        %7D);%0A                    %7D%0A%0A
     

Update websocket callbacks to not run on main thread