Android Open Source - trip-chain-android Roads






From Project

Back to project page trip-chain-android.

License

The source code is released under:

MIT License

If you think the Android project trip-chain-android 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 fi.aalto.tripchain.route;
//w w w.jav a2 s .com
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.location.Location;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.widget.Toast;
import fi.aalto.tripchain.here.Address;
import fi.aalto.tripchain.here.ReverseGeocoder;

/**
 * Models a trip as RoadSegments.
 *
 */
public class Roads {
  private static final String TAG = Roads.class.getSimpleName();
  
  private List<RoadSegment> roadSegments = new ArrayList<RoadSegment>();
  private RoadSegment lastRoadSegment = null;
  
  /**
   * HandlerThread for network activity. Prevents blocking on main thread.
   */
  private HandlerThread thread = new HandlerThread("reverseGeocodingHandlerThread");
  
  private Handler handler;
  
  void start() {
    thread.start();
    handler = new Handler(thread.getLooper());
  }
  
  void stop() {
    thread.quit();
  }
  
  /**
   * Does reverse geocoding on coordinates.
   * @param location
   */
  private void addressQuery(final Location location) {
    handler.post(new Runnable() {
      @Override
      public void run() {
        List<Address> addresses = ReverseGeocoder.query(location);
        if (addresses.size() > 0) {
          onAddress(location, addresses);
        }
      }
    });   
  }
  
  void onLocation(Location location) {
    addressQuery(location);
  }
  
  /**
   * Handles roadSegments list.
   * @param location
   * @param addresses
   */
  private void onAddress(Location location, List<Address> addresses) {
    if (roadSegments.size() == 0) {
      lastRoadSegment = new RoadSegment(location, addresses);
      roadSegments.add(lastRoadSegment);
    } else if (lastRoadSegment.stillOnTheSameStreet(addresses)) {
      lastRoadSegment.addLocation(location, addresses);
    } else {
      lastRoadSegment = new RoadSegment(location, addresses);
      roadSegments.add(lastRoadSegment);
    }
  }
  
  public JSONArray toJson() throws JSONException {
    JSONArray roads = new JSONArray();
    
    for (RoadSegment rs : this.roadSegments) {
      JSONObject j = new JSONObject();
      j.put("locations", Trip.toLocations(rs.locations));
      j.put("street", rs.latestAddress.street);
      j.put("city", rs.latestAddress.city);
      j.put("country", rs.latestAddress.country);
      j.put("addresses", rs.toAddresses());
      
      roads.put(j);
    }
    
    return roads;
  }
}




Java Source Code List

fi.aalto.tripchain.BackgroundService.java
fi.aalto.tripchain.Configuration.java
fi.aalto.tripchain.LoginActivity.java
fi.aalto.tripchain.MainActivity.java
fi.aalto.tripchain.StartFragment.java
fi.aalto.tripchain.TripFragment.java
fi.aalto.tripchain.receivers.ActivityReceiver.java
fi.aalto.tripchain.receivers.EventDispatcher.java
fi.aalto.tripchain.receivers.EventListener.java
fi.aalto.tripchain.receivers.LocationReceiver.java
fi.aalto.tripchain.receivers.Receiver.java
fi.aalto.tripchain.route.ActivityListener.java
fi.aalto.tripchain.route.Activity.java
fi.aalto.tripchain.route.LocationListener.java
fi.aalto.tripchain.route.RoadSegment.java
fi.aalto.tripchain.route.Roads.java
fi.aalto.tripchain.route.RouteSegment.java
fi.aalto.tripchain.route.Route.java
fi.aalto.tripchain.route.TripRecorder.java
fi.aalto.tripchain.route.Trip.java