com.backendless.Geo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android Show documentation
Show all versions of android Show documentation
Android SDK used by developers to provide Backendless API in apps.
/*
* ********************************************************************************************************************
*
* BACKENDLESS.COM CONFIDENTIAL
*
* ********************************************************************************************************************
*
* Copyright 2012 BACKENDLESS.COM. All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property of Backendless.com and its suppliers,
* if any. The intellectual and technical concepts contained herein are proprietary to Backendless.com and its
* suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret
* or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden
* unless prior written permission is obtained from Backendless.com.
*
* ********************************************************************************************************************
*/
package com.backendless;
import android.content.res.Resources;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.core.responder.AdaptingResponder;
import com.backendless.core.responder.policy.CollectionAdaptingPolicy;
import com.backendless.exceptions.BackendlessException;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.exceptions.ExceptionMessage;
import com.backendless.geo.*;
import com.backendless.geo.geofence.*;
import weborb.types.Types;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
public final class Geo
{
public final static String GEO_MANAGER_SERVER_ALIAS = "com.backendless.services.geo.GeoService";
private final static String DEFAULT_CATEGORY_NAME = "Default";
private static final Geo instance = new Geo();
static Geo getInstance()
{
return instance;
}
private Geo()
{
Types.addClientClassMapping( "com.backendless.geo.model.GeoPoint", GeoPoint.class );
Types.addClientClassMapping( "com.backendless.geo.model.GeoCluster", GeoCluster.class );
Types.addClientClassMapping( "com.backendless.geo.model.SearchMatchesResult", SearchMatchesResult.class );
Types.addClientClassMapping( "com.backendless.geo.BackendlessGeoQuery", BackendlessGeoQuery.class );
Types.addClientClassMapping( "com.backendless.geo.model.GeoCategory", GeoCategory.class );
Types.addClientClassMapping( "com.backendless.geo.Units", Units.class );
Types.addClientClassMapping( "com.backendless.geofence.model.GeoFenceAMF", GeoFence.class );
Types.addClientClassMapping( "com.backendless.geofence.model.FenceType", FenceType.class );
}
public GeoCategory addCategory( String categoryName ) throws BackendlessException
{
checkCategoryName( categoryName );
return (GeoCategory) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "addCategory", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), categoryName } );
}
public void addCategory( String categoryName, AsyncCallback responder )
{
try
{
checkCategoryName( categoryName );
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "addCategory", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), categoryName }, responder );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public boolean deleteCategory( String categoryName ) throws BackendlessException
{
checkCategoryName( categoryName );
return (Boolean) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "deleteCategory", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), categoryName } );
}
public void deleteCategory( String categoryName, AsyncCallback responder )
{
try
{
checkCategoryName( categoryName );
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "deleteCategory", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), categoryName }, responder );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public GeoPoint savePoint( final double latitude, final double longitude,
final Map metadata ) throws BackendlessException
{
return savePoint( latitude, longitude, null, metadata );
}
public GeoPoint savePoint( double latitude, double longitude, List categories,
final Map metadata ) throws BackendlessException
{
return savePoint( new GeoPoint( latitude, longitude, categories, metadata ) );
}
public GeoPoint savePoint( GeoPoint geoPoint ) throws BackendlessException
{
if( geoPoint == null )
throw new IllegalArgumentException( ExceptionMessage.NULL_GEOPOINT );
checkCoordinates( geoPoint.getLatitude(), geoPoint.getLongitude() );
String remoteMethod = geoPoint.getObjectId() == null ? "addPoint" : "updatePoint";
return (GeoPoint) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, remoteMethod, new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint } );
}
public void savePoint( final double latitude, final double longitude, final Map metadata,
AsyncCallback responder )
{
savePoint( latitude, longitude, null, metadata, responder );
}
public void savePoint( double latitude, double longitude, List categories, Map metadata,
AsyncCallback responder )
{
savePoint( new GeoPoint( latitude, longitude, categories, metadata ), responder );
}
public void savePoint( GeoPoint geoPoint, AsyncCallback responder )
{
try
{
if( geoPoint == null )
throw new IllegalArgumentException( ExceptionMessage.NULL_GEOPOINT );
checkCoordinates( geoPoint.getLatitude(), geoPoint.getLongitude() );
String remoteMethod = geoPoint.getObjectId() == null ? "addPoint" : "updatePoint";
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, remoteMethod, new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint }, responder );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public void removePoint( GeoPoint geoPoint )
{
Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "removePoint", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId() } );
}
public void removePoint( GeoPoint geoPoint, AsyncCallback responder )
{
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "removePoint", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId() }, responder );
}
public BackendlessCollection getPoints( BackendlessGeoQuery geoQuery ) throws BackendlessException
{
checkGeoQuery( geoQuery );
CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy();
BackendlessCollection result = (BackendlessCollection) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "getPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoQuery }, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) );
complementResponse( result, geoQuery );
if( geoQuery.getDpp() != null && geoQuery.getDpp() > 0 )
{
setReferenceToCluster( result );
}
return result;
}
public void getPoints( final BackendlessGeoQuery geoQuery,
final AsyncCallback> responder )
{
try
{
checkGeoQuery( geoQuery );
CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy();
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "getPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoQuery }, new AsyncCallback>()
{
@Override
public void handleResponse( BackendlessCollection response )
{
complementResponse( response, geoQuery );
if( geoQuery.getDpp() != null && geoQuery.getDpp() > 0 )
{
setReferenceToCluster( response );
}
if( responder != null )
responder.handleResponse( response );
}
@Override
public void handleFault( BackendlessFault fault )
{
responder.handleFault( fault );
}
}, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public BackendlessCollection getPoints( final GeoCluster geoCluster )
{
CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy();
BackendlessCollection result = (BackendlessCollection) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "loadGeoPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoCluster.getObjectId(), geoCluster.getGeoQuery() }, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) );
result.setQuery( geoCluster.getGeoQuery() );
result.setType( GeoPoint.class );
return result;
}
public void getPoints( final GeoCluster geoCluster, final AsyncCallback> responder )
{
try
{
CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy();
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "loadGeoPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoCluster.getObjectId(), geoCluster.getGeoQuery() }, new AsyncCallback>()
{
@Override
public void handleResponse( BackendlessCollection response )
{
response.setQuery( geoCluster.getGeoQuery() );
response.setType( GeoPoint.class );
if( responder != null )
responder.handleResponse( response );
}
@Override
public void handleFault( BackendlessFault fault )
{
if( responder != null )
responder.handleFault( fault );
}
}, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public BackendlessCollection relativeFind(
BackendlessGeoQuery geoQuery ) throws BackendlessException
{
if( geoQuery == null )
throw new IllegalArgumentException( ExceptionMessage.NULL_GEO_QUERY );
if( geoQuery.getRelativeFindMetadata().isEmpty() || geoQuery.getRelativeFindPercentThreshold() == 0 )
throw new IllegalArgumentException( ExceptionMessage.INCONSISTENT_GEO_RELATIVE );
CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy();
BackendlessCollection result = (BackendlessCollection) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "relativeFind", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoQuery }, new AdaptingResponder( SearchMatchesResult.class, adaptingPolicy ) );
result.setQuery( geoQuery );
result.setType( SearchMatchesResult.class );
return result;
}
public BackendlessCollection getPoints( String geofenceName,
BackendlessGeoQuery query ) throws BackendlessException
{
checkGeoQuery( query );
CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy();
BackendlessCollection result = (BackendlessCollection) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "getPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geofenceName, query }, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) );
complementResponse( result, query );
return result;
}
public void getPoints( final String geofenceName, final BackendlessGeoQuery query,
final AsyncCallback> responder )
{
try
{
checkGeoQuery( query );
CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy();
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "getPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geofenceName, query }, new AsyncCallback>()
{
@Override
public void handleResponse( BackendlessCollection response )
{
complementResponse( response, query );
if( responder != null )
responder.handleResponse( response );
}
@Override
public void handleFault( BackendlessFault fault )
{
responder.handleFault( fault );
}
}, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public BackendlessCollection getPoints( String geofenceName ) throws BackendlessException
{
return getPoints( geofenceName, new BackendlessGeoQuery() );
}
public void getPoints( final String geofenceName, final AsyncCallback> responder )
{
getPoints( geofenceName, new BackendlessGeoQuery(), responder );
}
public void relativeFind( final BackendlessGeoQuery geoQuery,
final AsyncCallback> responder )
{
try
{
if( geoQuery == null )
throw new IllegalArgumentException( ExceptionMessage.NULL_GEO_QUERY );
if( geoQuery.getRelativeFindMetadata().isEmpty() || geoQuery.getRelativeFindPercentThreshold() == 0 )
throw new IllegalArgumentException( ExceptionMessage.INCONSISTENT_GEO_RELATIVE );
CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy();
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "relativeFind", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoQuery }, new AsyncCallback>()
{
@Override
public void handleResponse( BackendlessCollection response )
{
response.setQuery( geoQuery );
response.setType( SearchMatchesResult.class );
if( responder != null )
responder.handleResponse( response );
}
@Override
public void handleFault( BackendlessFault fault )
{
if( responder != null )
responder.handleFault( fault );
}
}, new AdaptingResponder( SearchMatchesResult.class, adaptingPolicy ) );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}
public List getCategories() throws BackendlessException
{
GeoCategory[] response = Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "getCategories", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() } );
return Arrays.asList( response );
}
public void getCategories( final AsyncCallback> responder )
{
Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "getCategories", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() }, new AsyncCallback()
{
@Override
public void handleResponse( GeoCategory[] response )
{
if( responder != null )
responder.handleResponse( Arrays.asList( response ) );
}
@Override
public void handleFault( BackendlessFault fault )
{
if( responder != null )
responder.handleFault( fault );
}
} );
}
public GeoPoint loadMetadata( final GeoPoint geoPoint )
{
Map metadata;
if( geoPoint instanceof GeoCluster )
{
metadata = (Map) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "loadMetadata", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId(), ((GeoCluster) geoPoint).getGeoQuery() } );
}
else
{
metadata = (Map) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "loadMetadata", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId(), null } );
}
geoPoint.setMetadata( metadata );
return geoPoint;
}
public void loadMetadata( final GeoPoint geoPoint, final AsyncCallback responder )
{
AsyncCallback