Android Open Source - bike-friend Internet Station Provider






From Project

Back to project page bike-friend.

License

The source code is released under:

GNU General Public License

If you think the Android project bike-friend 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 com.lemoulinstudio.bikefriend;
/*from w ww  .java 2  s. co m*/
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Vincent Cantin
 */
public abstract class InternetStationProvider<T extends Station> implements StationProvider<T> {
  
  private final URL url;
  private final StationParser<T> stationParser;
  private GoogleMap map;
  private StationInfoWindowAdapter siwa;
  private LatLngBounds bounds;
  private final List<Marker> markers;
  private boolean stationsNeedRefresh;
  private boolean isFetchingStations;
  private boolean isVisible;
  
  protected InternetStationProvider(String urlString, StationParser<T> stationParser) {
    this.url = Utils.toUrl(urlString);
    this.stationParser = stationParser;
    this.stationsNeedRefresh = true;
    this.isFetchingStations = false;
    this.isVisible = true;
    this.markers = new ArrayList<Marker>();
  }
  
  @Override
  public void setMap(GoogleMap map) {
    this.map = map;
  }
  
  @Override
  public void setStationInfoWindowAdapter(StationInfoWindowAdapter siwa) {
    this.siwa = siwa;
  }
  
  @Override
  public void notifyCameraChanged() {
    if (!isVisible || !stationsNeedRefresh || map == null) {
      return;
    }
    
    LatLngBounds cameraBoundingBox = map.getProjection().getVisibleRegion().latLngBounds;
    if (bounds == null || Utils.intersects(cameraBoundingBox, bounds)) {
      fetchStations();
    }
  }
  
  @Override
  public void refreshData() {
    // Mark the current data as obsolete, and let the lazy loading do its work.
    stationsNeedRefresh = true;
    
    // Check if we are viewing the area right now, and refresh if it is the case.
    notifyCameraChanged();
  }
  
  @Override
  public LatLngBounds getLatLngBounds() {
    return bounds;
  }
  
  @Override
  public void notifyVisibilityChanged(boolean isVisible) {
    this.isVisible = isVisible;
    for (Marker marker : markers) {
      marker.setVisible(isVisible);
    }
    
    notifyCameraChanged();
  }
  
  @SuppressWarnings({"unchecked"})
  private synchronized void fetchStations() {
    if (!isFetchingStations) {
      isFetchingStations = true;
      stationsNeedRefresh = false;
      new DownloadStationDataAsyncTask().execute(this);
    }
  }
  
  protected InputStream getDataStream() throws IOException {
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setReadTimeout(10000 /* milliseconds */);
      connection.setConnectTimeout(15000 /* milliseconds */);
      connection.setRequestMethod("GET");
      connection.setDoInput(true);
      connection.connect();
      
      return connection.getInputStream();
  }
  
  private class DownloadStationDataAsyncTask extends AsyncTask<StationProvider<T>, Void, List<T>> {

    private boolean networkProblem;
    private boolean parsingProblem;

    @Override
    protected List<T> doInBackground(StationProvider<T>... stationProviders) {
      try {
        InputStream dataStream = getDataStream();
        if (dataStream == null) {
          throw new IOException();
        }
        
        List<T> stations = stationParser.parse(dataStream);
        bounds = Utils.computeBounds(stations);
        return stations;
      }
      catch (IOException e) {
        Log.d(StationMapActivity.LOG_TAG, "Network problem.", e);
        networkProblem = true;
      }
      catch (ParsingException e) {
        Log.d(StationMapActivity.LOG_TAG, "Parsing problem.", e);
        parsingProblem = true;
      }

      return null;
    }

    @Override
    protected void onPostExecute(List<T> stations) {
      if (stations == null) {
        // TODO: Notification to tell the user that there is a problem getting the data.
        if (networkProblem) {

        }
        else if (parsingProblem) {

        }
      }
      else {
        // Remove the old markers.
        for (Marker marker : markers) {
          marker.remove();
          siwa.unbindMarker(marker);
        }
        
        markers.clear();
        
        for (Station station : stations) {
          MarkerOptions markerOptions = new MarkerOptions()
                  .position(station.getLocation())
                  .icon(BitmapDescriptorFactory.defaultMarker(
                    station.isTestStation() ?
                      BitmapDescriptorFactory.HUE_RED :
                      (station.getNbBikes() == 0 || station.getNbEmptySlots() == 0) ?
                        BitmapDescriptorFactory.HUE_ORANGE :
                        BitmapDescriptorFactory.HUE_GREEN));

          Marker marker = map.addMarker(markerOptions);
          markers.add(marker);
          siwa.bindMarkerToStation(marker, station);
        }
      }
      
      isFetchingStations = false;
    }
  }
}




Java Source Code List

com.lemoulinstudio.bikefriend.InternetStationProvider.java
com.lemoulinstudio.bikefriend.ParsingException.java
com.lemoulinstudio.bikefriend.StationInfoWindowAdapter.java
com.lemoulinstudio.bikefriend.StationMapActivity.java
com.lemoulinstudio.bikefriend.StationParser.java
com.lemoulinstudio.bikefriend.StationProvider.java
com.lemoulinstudio.bikefriend.Station.java
com.lemoulinstudio.bikefriend.Utils.java
com.lemoulinstudio.bikefriend.cbike.CBikeStationXmlParserV1.java
com.lemoulinstudio.bikefriend.cbike.CBikeStation.java
com.lemoulinstudio.bikefriend.cbike.KaohsiungStationProvider.java
com.lemoulinstudio.bikefriend.ubike.ChanghuaStationProvider.java
com.lemoulinstudio.bikefriend.ubike.TaichungStationProvider.java
com.lemoulinstudio.bikefriend.ubike.TaipeiStationProvider.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationCSVParserV1.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationHtmlParserV2.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStationJsonParserV1.java
com.lemoulinstudio.bikefriend.ubike.YouBikeStation.java
com.lemoulinstudio.bikefriend.webapp.StationParser.java
com.lemoulinstudio.bikefriend.webapp.cbike.BIKEStationData.java
com.lemoulinstudio.bikefriend.webapp.cbike.BIKEStation.java
com.lemoulinstudio.bikefriend.webapp.cbike.CBikeStationParser.java
com.lemoulinstudio.bikefriend.webapp.cbike.XmlStation.java
com.lemoulinstudio.bikefriend.webapp.conf.ShutdownHook.java
com.lemoulinstudio.bikefriend.webapp.conf.StartupHook.java
com.lemoulinstudio.bikefriend.webapp.entity.StationLog.java
com.lemoulinstudio.bikefriend.webapp.entity.Station.java
com.lemoulinstudio.bikefriend.webapp.io.InputStreamSequence.java
com.lemoulinstudio.bikefriend.webapp.io.IntArrayInputStream.java
com.lemoulinstudio.bikefriend.webapp.quartz.LoadStationDataJob.java
com.lemoulinstudio.bikefriend.webapp.rest.StationResource.java
com.lemoulinstudio.bikefriend.webapp.ubike.MarkerList.java
com.lemoulinstudio.bikefriend.webapp.ubike.Marker.java
com.lemoulinstudio.bikefriend.webapp.ubike.YouBikeStationParser.java
com.lemoulinstudio.bikefriend.webapp.vo.StationLogVo.java
com.lemoulinstudio.bikefriend.webapp.vo.StationVo.java