Android Open Source - socket-location-provider Socket Location Provider






From Project

Back to project page socket-location-provider.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project socket-location-provider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.languitar.socketlocationprovider;
/*w w w.  j  ava2s.  co m*/
import android.location.Location;
import android.util.Log;

import org.microg.nlp.api.LocationBackendService;
import org.microg.nlp.api.LocationHelper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketLocationProvider extends LocationBackendService {
  private static final String TAG = SocketLocationProvider.class.getName();

  private AcceptorThread acceptor;

  private class AcceptorThread extends Thread {

    private ServerSocket serverSocket;

    @Override
    public void run() {

      try {

        synchronized (this) {
          serverSocket = new ServerSocket(13371);
        }

        // until we are interrupted
        while (!acceptor.isInterrupted()) {

          try {

            // accept a new client
            Log.d(TAG, "Starting to wait for a new client");
            final Socket clientSocket = serverSocket.accept();
            Log.d(TAG, "Accepted a new client at " + clientSocket);
            final BufferedReader in = new BufferedReader(
                new InputStreamReader(
                    clientSocket.getInputStream()));

            // process requests from this client until we are
            // interrupted or the client disconnects
            while (!acceptor.isInterrupted()) {

              final String line = in.readLine();
              Log.d(TAG, "The client sent: '" + line + "'");

              if (line == null) {
                Log.d(TAG, "Client disappeared.");
                break;
              }

              final String[] parts = line.split(",");
              if (parts.length != 2) {
                Log.d(TAG,
                    "Client sent garbage, ignoring message");
                continue;
              }
              double lat = 0;
              double lon = 0;
              try {
                lat = Double.parseDouble(parts[0]);
                lon = Double.parseDouble(parts[1]);
              } catch (final NumberFormatException e) {
                Log.d(TAG,
                    "Client sent garbage, ignoring message",
                    e);
                continue;
              }

              Location location = LocationHelper.create("socket",
                  lat, lon, 10);
              Log.d(TAG, "Reporting location: " + location);
              report(location);

            }

          } catch (final IOException e) {
            Log.e(TAG,
                "Error during client socket communication. Waiting for a new client.",
                e);
          }

        }

        serverSocket.close();

      } catch (final IOException e) {
        Log.e(TAG, "Server socket error. Shutting down.", e);
      }

    }

    public void stopAndWait() {

      Log.d(TAG, "Stopping acceptor thread");

      interrupt();

      // if the server socket is open, try to close it so that a potential
      // call to accept is kicked out of the loop
      synchronized (this) {
        if (serverSocket != null) {
          try {
            serverSocket.close();
          } catch (final Exception e) {
            Log.w(TAG,
                "Exception while trying to close the server socket",
                e);
          }
        }
      }

      try {
        acceptor.join();
      } catch (InterruptedException e) {
        Log.e(TAG, "Problem while joining the acceptor thread.", e);
      }
    }

  }

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

    acceptor = new AcceptorThread();
    acceptor.start();

  }

  @Override
  protected void onClose() {
    if (acceptor != null && acceptor.isAlive()) {
      acceptor.stopAndWait();
    }
  }
}




Java Source Code List

de.languitar.socketlocationprovider.SocketLocationProvider.java