/** send user location to server */
 public void sendLocation(double lat, double lon) {
   if (connection != null && connection.isConnected()) {
     JSONObject json = new JSONObject();
     try {
       json.put("lon", lon);
       json.put("lat", lat);
     } catch (JSONException e) {
       SLog.e(TAG, e.getMessage());
     }
     String message = json.toString();
     SLog.d(TAG, "trying to send: " + message);
     connection.sendTextMessage(message);
   }
 }
 @Override
 public void onTextMessage(String response) {
   WakeLock wakelock =
       ((PowerManager) getSystemService(POWER_SERVICE))
           .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SocketServiceLock");
   wakelock.acquire();
   SLog.d(TAG, "recieved: " + response);
   handleMessage(response, wakelock);
 }
 @Override
 public void onOpen() {
   isConnecting = false;
   SLog.d(TAG, "Connected to websocket");
   if (connectionWakeLock != null && connectionWakeLock.isHeld()) {
     connectionWakeLock.release();
   }
   initLocationClient();
 }
  /** start listen for location updates */
  private void startUpdateLocation() {
    LocationRequest request =
        LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_LOW_POWER)
            .setInterval(Consts.LOCATION_UPDATE_TIME)
            .setFastestInterval(Consts.LOCATION_UPDATE_TIME);

    locationClient.requestLocationUpdates(request, locationListener);
    SLog.d(TAG, "Location update started");
  }
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
   // allow to use service when device is inactive
   WakeLock wakelock =
       ((PowerManager) getSystemService(POWER_SERVICE))
           .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SocketServiceLock");
   wakelock.acquire();
   SLog.d(TAG, "onStartCommand");
   if (intent != null) {
     SLog.d(TAG, intent.toUri(0));
   }
   shutDown = false;
   // initialize connection
   if (connection == null || (!connection.isConnected() && !isConnecting)) {
     connectionWakeLock =
         ((PowerManager) getSystemService(POWER_SERVICE))
             .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SocketService_clientLock");
     connection = new WebSocketConnection();
     try {
       isConnecting = true;
       connection.connect(
           String.format(
               Consts.SERVER_URL, Settings.getUserLogin(this), Settings.getUserPassword(this)),
           new WebSocketHandlerImpl());
       // wake lock will be released in connection listener;
     } catch (WebSocketException e) {
       SLog.e(TAG, e.getMessage());
       if (connectionWakeLock != null && connectionWakeLock.isHeld()) {
         connectionWakeLock.release();
       }
     }
     // start shutting down
   } else if (intent != null) {
     if (ACTION_SHUT_DOWN.equals(intent.getAction())) {
       shutDown = true;
       if (connection.isConnected()) connection.disconnect();
     }
   }
   wakelock.release();
   return START_STICKY;
 }
 @Override
 public void onClose(int code, String reason) {
   isConnecting = false;
   SLog.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason));
   if (!shutDown) {
     startService(startIntent(SocketService.this));
   } else {
     stopSelf();
   }
   if (connectionWakeLock != null && connectionWakeLock.isHeld()) {
     connectionWakeLock.release();
   }
 }
 @Override
 public void onDestroy() {
   super.onDestroy();
   SLog.d(TAG, "Destroying Service " + this.toString());
   // disconnect web socket
   if (connection != null && connection.isConnected()) connection.disconnect();
   // detach active parsing task to prevent NPE
   if (parseTask != null) {
     parseTask.detachCallback();
   }
   // remove location update
   if (locationClient != null && locationClient.isConnected() && locationListener != null) {
     locationClient.removeLocationUpdates(locationListener);
   }
 }
 @Override
 public void onCreate() {
   super.onCreate();
   SLog.d(TAG, "Creating Service " + this.toString());
 }
 @Override
 public void onLocationChanged(Location location) {
   SLog.d(TAG, "received new location");
   sendLocation(location.getLatitude(), location.getLongitude());
 }