Android Open Source - WhatsUp Data Provider






From Project

Back to project page WhatsUp.

License

The source code is released under:

GNU General Public License

If you think the Android project WhatsUp 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 nu.placebo.whatsup.datahandling;
//from   w  w w. j  a va  2  s  .  c om
import java.util.ArrayList;
import java.util.List;

import nu.placebo.whatsup.ctrl.SessionHandler;
import nu.placebo.whatsup.model.Annotation;
import nu.placebo.whatsup.model.Comment;
import nu.placebo.whatsup.model.GeoLocation;
import nu.placebo.whatsup.model.ReferencePoint;
import nu.placebo.whatsup.model.SessionInfo;
import nu.placebo.whatsup.network.AnnotationCreate;
import nu.placebo.whatsup.network.AnnotationRetrieve;
import nu.placebo.whatsup.network.CommentCreate;
import nu.placebo.whatsup.network.GeoLocationsRetrieve;
import nu.placebo.whatsup.network.NetworkOperationListener;
import nu.placebo.whatsup.network.NetworkTask;
import nu.placebo.whatsup.network.OperationResult;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class DataProvider implements NetworkOperationListener<Annotation>, LocationListener {
  
  private Context c;
  /**
   * Class is Singelton, disallow outside creation
   */
  private DataProvider(Context c) {
    this.c = c;
    
    DatabaseConnectionLayer.setDatabaseHelper(new DatabaseHelper(c));
    Location lastKnownLocation = ((LocationManager) c.getSystemService(Context.LOCATION_SERVICE)).
                          getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(lastKnownLocation == null) {
      lastKnownLocation = ((LocationManager) c.getSystemService(Context.LOCATION_SERVICE)).
                        getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }
    if(lastKnownLocation == null) {
      addReferencePoint(new GeoPoint(57706900, 11982051), "physical_position");
      for(ReferencePoint rp : getAllReferencePoints()) {
        Log.i("Name is: ", rp.getName());
      }
      setCurrentReferencePoint(-1);
    } else {
      addReferencePoint(new GeoPoint((int)(lastKnownLocation.getLatitude() * 1000000 + 0.5),
          (int)(lastKnownLocation.getLongitude() * 1000000 + 0.5)),
          "physical_position");
      setCurrentReferencePoint(-1);
    }
  }
  
  private static volatile DataProvider instance;

  /**
   * Gets the single instance of this class. The context parameter is only necessary the
   * first call. Once instantiated, it can be null. For example, if a class does not have
   * easy access to a contex and is sure that the DataProvider has been used earlier in
   * the application lifetime, may call this with null rather than going to lengths to get
   * a context.
   * 
   * @param c the context with which to create the database.
   * @return
   */
  public static DataProvider getDataProvider(Context c) {
    if(instance == null) {
      instance = new DataProvider(c);
    }
    return instance;
  }
  
  /**
   * This method supplies the caller with a DataReturn<Annotation> object that will
   * contain the annotation with the given id, if it has previously been saved to
   * the database. If it was not found, the local data will be null, and the requester
   * will have to wait until new server data is available.
   * 
   * @param nid
   * @return
   */
  public DataReturn<Annotation> getAnnotation(int nid) {
    
    DataReturn<Annotation> result;
    
    result = new DataReturn<Annotation>(DatabaseConnectionLayer.getAnnotation(nid),
            activeObjects.size());  
    activeObjects.add(result);
    AnnotationRetrieve ar = new AnnotationRetrieve(nid);
    ar.addOperationListener(result);
    new NetworkTask<Annotation>().execute(ar);
    return result;
  }
  
  /**
   * Convenience method for getAnnotationMarkers(int, int, int, int) when you have two
   * locations.
   * 
   * @param a one of the points
   * @param b the other point
   * @return 
   */
  public DataReturn<List<GeoLocation>> getAnnotationMarkers(GeoLocation a, GeoLocation b) {
    return getAnnotationMarkers(a.getLocation().getLatitudeE6(), 
                  a.getLocation().getLongitudeE6(),
                  b.getLocation().getLatitudeE6(),
                  b.getLocation().getLongitudeE6());
  }
  
  /**
   * Calling this method is a request to get GeoLocations within
   * the rectangular area given by applying the parameters 
   * constraints.
   * 
   * @param latitudeA the latitude of the first point, in microlatitude
   * @param longitudeA the longitude of the first point, in microlongitude
   * @param latitudeB the latitude of the second point, in microlatitude
   * @param longitudeB the longitude of the second point, in microlngitude
   * @return a DataReturn object containing any local data found and which can
   * be listened to by
   */
  
  public DataReturn<List<GeoLocation>> getAnnotationMarkers(int latitudeA,
      int longitudeA, int latitudeB, int longitudeB) {
    int maxLat = Math.max(latitudeA, latitudeB);
    int maxLong = Math.max(longitudeA, longitudeB);
    int minLat = Math.min(latitudeA, latitudeB);
    int minLong = Math.min(longitudeA, longitudeB);

    DataReturn<List<GeoLocation>> result;
    GeoLocationsRetrieve glr = new GeoLocationsRetrieve(
        (latitudeA - 0.5) / 1000000, (longitudeA - 0.5) / 1000000,
        (latitudeB - 0.5) / 1000000, (longitudeB - 0.5) / 1000000);
    
    synchronized(this) {
      result = new DataReturn<List<GeoLocation>>(DatabaseConnectionLayer.getAnnotationMarkers(
                      maxLat, maxLong, minLat, minLong), activeObjects.size());
      activeObjects.add(result);
    }
    glr.addOperationListener(result);
    new NetworkTask<List<GeoLocation>>().execute(glr);
    return result;
  }
  
  /**
   * Gets the ReferencePoint that is currently used as reference point.
   * In the case that the user has not chosen a reference point, the physical
   * position of the phone is returned, as it is the standard refernce point.
   * 
   * @return the current reference point, as a ReferencePoint object.
   */
  public ReferencePoint getCurrentReferencePoint() {
    return currentReferencePoint;
  }
  
  /**
   * Returns all reference points saved in the database, and the physical
   * position of the phone, which is always a reference point.
   * 
   * @return all reference point, including the physical location of the phone.
   */
  public List<ReferencePoint> getAllReferencePoints() {
    return DatabaseConnectionLayer.getAllReferencePoints();
  }
  
  /**
   * Sets the ReferencePoint to use as the current reference point, both short-time
   * and in the database. Sending a negative id will set the physical location as
   * current location (calling with the id of the physical location also works).
   * 
   * @param id the id of the already existing reference point. If the id does not
   * match any existing reference point, nothing changes.
   */
  public void setCurrentReferencePoint(int id) {
    currentReferencePoint = DatabaseConnectionLayer.setCurrentReferencePoint(id);
  }
  
  /**
   * Adds a reference point to the database.
   * 
   * @param gp the geographical location of the reference point.
   * @param name the name of the reference point.
   */
  public void addReferencePoint(GeoPoint gp, String name) {
    DatabaseConnectionLayer.addReferencePoint(gp, name);
  }
    
  /**
   * Removes a reference point from the database.
   * 
   * @param gl the point to remove
   */
  public void removeReferencePoint(int id) {
    DatabaseConnectionLayer.removeReferencePoint(id);
  }
  
  /**
   * Creates an Annotation with the specified values. A valid SessionInfo is required, and the object
   * that wants the annotation should be sent as listener. The listener can be null if no object wants the
   * returned Annotation immediately.
   * 
   * When the object is returned, the DataProvider will receive it as well, and it will be put into the
   * database. The time until the annotation is stably in the database is determined by the workload of the
   * phone, threading priority and the time a SQLite insert takes. As this is mostly random, trying to get
   * this annotation from the database before it's fully updated will result
   * 
   * @param title
   * @param desc
   * @param author
   * @param gp
   * @param sInfo
   * @param listener
   */
  public void createAnnotation(String title, String desc, String author, 
      GeoPoint gp, SessionInfo sInfo, 
      NetworkOperationListener<Annotation> listener) {
    AnnotationCreate ac = new AnnotationCreate(title, desc, author, gp, sInfo);
    if(listener != null) {
      ac.addOperationListener(listener);
    }
    new NetworkTask<Annotation>().execute(ac);
  }
  
  public void createComment(int nid, String author, String commentText, String title,
                NetworkOperationListener<Comment> listener) {
    SessionHandler sh = SessionHandler.getInstance(this.c);
    CommentCreate cc = new CommentCreate(title, commentText, nid,sh.getUserName(), sh.getSession());

    if(listener != null) {
      cc.addOperationListener(listener);
    }
    new NetworkTask<Comment>().execute(cc);
    DatabaseConnectionLayer.storeComment(nid, author, commentText, title);
  }
  
  /**
   * Called by DataReturn object to notify the DataProvider about new data
   * to insert into the local database.
   * 
   * @param newData whether the data differs from that in the local database
   * @param id the id of the DataReturn that has finished
   */
  @SuppressWarnings("unchecked")
  void newDataRecieved(boolean newData, int id) {
    if(newData) {
      OperationResult<?> result = activeObjects.get(id).getNewData();
      
      if(result == null || result.getResult() == null) {
        return;
      }
      if(!result.hasErrors()) {
        Object data = result.getResult();
        //Test what type the new data has    
        if(data.getClass() == Annotation.class) {
          if(!insertData((Annotation) data)) {
            //TODO Error handling
          }
        } else if(data.getClass() == List.class) {
          if(!insertData((List<GeoLocation>) data)) {
            //TODO Error handling
          }
        }
      }
    }
    activeObjects.remove(id);
  }
  
  //Inserts the given Annotation into its table, and it's auxiliary
  //information into their tables. If error occurs, false is returned.
  private boolean insertData(Annotation a) {
    return DatabaseConnectionLayer.storeAnnotation(a);
  }

  //Inserts the given list with GeoLocations into its table.
  //If error occurs, false is returned.
  private boolean insertData(List<GeoLocation> glList) {
    return DatabaseConnectionLayer.storeGeoLocations(glList);
  }

  private List<DataReturn<?>> activeObjects = new ArrayList<DataReturn<?>>();
  private ReferencePoint currentReferencePoint;

  public void operationExcecuted(OperationResult<Annotation> result) {
    if(!result.hasErrors()) {
      insertData(result.getResult());
    }
  }

  public void onLocationChanged(Location location) {
    DatabaseConnectionLayer.updateCurrentLocation(
        new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()));
  }
  public void onProviderDisabled(String provider) {}
  public void onProviderEnabled(String provider) {}
  public void onStatusChanged(String provider, int status, Bundle extras) {}
}




Java Source Code List

nu.placebo.whatsup.activity.AnnotationActivity.java
nu.placebo.whatsup.activity.CreateAnnotationActivity.java
nu.placebo.whatsup.activity.CreateCommentActivity.java
nu.placebo.whatsup.activity.ListViewActivity.java
nu.placebo.whatsup.activity.LogInActivity.java
nu.placebo.whatsup.activity.LoginRegTabActivity.java
nu.placebo.whatsup.activity.MapViewActivity.java
nu.placebo.whatsup.activity.Marker.java
nu.placebo.whatsup.activity.MenuHelper.java
nu.placebo.whatsup.activity.PositionPickerActivity.java
nu.placebo.whatsup.activity.RefPointActivity.java
nu.placebo.whatsup.activity.RegisterActivity.java
nu.placebo.whatsup.android.os.AsyncTask.java
nu.placebo.whatsup.balloon.BalloonItemizedOverlay.java
nu.placebo.whatsup.balloon.BalloonOverlayView.java
nu.placebo.whatsup.constants.Constants.java
nu.placebo.whatsup.ctrl.SessionHandler.java
nu.placebo.whatsup.datahandling.DataProvider.java
nu.placebo.whatsup.datahandling.DataReturnListener.java
nu.placebo.whatsup.datahandling.DataReturn.java
nu.placebo.whatsup.datahandling.DatabaseConnectionLayer.java
nu.placebo.whatsup.datahandling.DatabaseHelper.java
nu.placebo.whatsup.model.Annotation.java
nu.placebo.whatsup.model.Comment.java
nu.placebo.whatsup.model.ExtendedOverlayItem.java
nu.placebo.whatsup.model.GeoLocation.java
nu.placebo.whatsup.model.ListMarker.java
nu.placebo.whatsup.model.ReferencePoint.java
nu.placebo.whatsup.model.SessionInfo.java
nu.placebo.whatsup.network.AbstractNetworkOperation.java
nu.placebo.whatsup.network.Action.java
nu.placebo.whatsup.network.AnnotationCreate.java
nu.placebo.whatsup.network.AnnotationRetrieve.java
nu.placebo.whatsup.network.CommentCreate.java
nu.placebo.whatsup.network.GeoLocationsRetrieve.java
nu.placebo.whatsup.network.Login.java
nu.placebo.whatsup.network.NetworkCalls.java
nu.placebo.whatsup.network.NetworkOperationListener.java
nu.placebo.whatsup.network.NetworkOperation.java
nu.placebo.whatsup.network.NetworkTask.java
nu.placebo.whatsup.network.OperationResult.java
nu.placebo.whatsup.network.RegisterOperation.java
nu.placebo.whatsup.network.SessionTest.java
nu.placebo.whatsup.util.GeoPointUtil.java
nu.placebo.whatsup.util.Geodetics.java
nu.placebo.whatsup.util.ValidationUtil.java
nu.placebo.whatsuptest.TestStarter.java
nu.placebo.whatsuptest.activitytest.AnnotationActivityTest.java
nu.placebo.whatsuptest.activitytest.ListViewActivityTest.java
nu.placebo.whatsuptest.activitytest.MapViewActivityTest.java
nu.placebo.whatsuptest.networktest.NetworkTest.java
nu.placebo.whatsuptest.utiltest.GeoPointUtilTest.java
nu.placebo.whatsuptest.utiltest.GeodeticsTest.java
nu.placebo.whatsuptest.utiltest.ValidationTest.java