com.google.android.gms.location.GeofencingRequest Java Examples

The following examples show how to use com.google.android.gms.location.GeofencingRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeofenceApiHandler.java    From PowerSwitch_Android with GNU General Public License v3.0 6 votes vote down vote up
private void addGeofence(GeofencingRequest geofencingRequest,
                         PendingIntent geofencePendingIntent) {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager
            .PERMISSION_GRANTED) {
        return;
    }

    LocationServices.GeofencingApi.addGeofences(
            googleApiClient,
            geofencingRequest,
            geofencePendingIntent
    ).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            switch (status.getStatusCode()) {
                case CommonStatusCodes.SUCCESS:
                    StatusMessageHandler.showInfoMessage(context, R.string.geofence_enabled, Snackbar.LENGTH_SHORT);
            }

            Log.d(GeofenceApiHandler.class, status.toString());
        }
    });
}
 
Example #2
Source File: GeofenceManager.java    From android-sdk with MIT License 5 votes vote down vote up
private void registerGeofences(final Location location) {
    final List<GeofencingRequest> requests = getGeofencingRequests(location);
    if (requests.isEmpty()) {
        onGeofencesRemoved(location);
        return;
    }
    try {
        for (final GeofencingRequest request : requests) {
            LocationServices.GeofencingApi
                    .addGeofences(
                            play.getClient(),
                            request,
                            GeofenceReceiver.getGeofencePendingIntent(context))
                    .setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(@NonNull Status status) {
                            if (status.isSuccess()) {
                                onGeofencesAdded(location, request.getGeofences(),
                                        request.getInitialTrigger());
                            } else {
                                onGeofencesFailed(null, status.getStatusCode());
                            }
                        }
                    });
        }
    } catch (SecurityException | IllegalStateException ex) {
        onGeofencesFailed(ex, 0);
    }
}
 
Example #3
Source File: RNBoundaryModule.java    From react-native-boundary with Apache License 2.0 5 votes vote down vote up
@ReactMethod
public void add(final ReadableArray readableArray, final Promise promise) {
    final List<Geofence> geofences = createGeofences(readableArray);
    final WritableArray geofenceRequestIds = Arguments.createArray();
    for (Geofence g : geofences) {
        geofenceRequestIds.pushString(g.getRequestId());
    }

    GeofencingRequest geofencingRequest = createGeofenceRequest(createGeofences(readableArray));

    addGeofence(promise, geofencingRequest, geofenceRequestIds);
}
 
Example #4
Source File: MainActivity.java    From location-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Builds and returns a GeofencingRequest. Specifies the list of geofences to be monitored.
 * Also specifies how the geofence notifications are initially triggered.
 */
private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();

    // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
    // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
    // is already inside that geofence.
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);

    // Add the geofences to be monitored by geofencing service.
    builder.addGeofences(mGeofenceList);

    // Return a GeofencingRequest.
    return builder.build();
}
 
Example #5
Source File: RNBoundaryModule.java    From react-native-boundary with Apache License 2.0 5 votes vote down vote up
private void addGeofence(final Promise promise, final GeofencingRequest geofencingRequest, final WritableArray geofenceRequestIds) {
    int permission = ActivityCompat.checkSelfPermission(getReactApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);


    if (permission != PackageManager.PERMISSION_GRANTED) {
        permission = requestPermissions();
    }


    if (permission != PackageManager.PERMISSION_GRANTED) {
        promise.reject("PERM", "Access fine location is not permitted");
    } else {
        mGeofencingClient.addGeofences(
                geofencingRequest,
                getBoundaryPendingIntent()
        )
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        promise.resolve(geofenceRequestIds);
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        promise.reject(e);
                    }
                });
    }

}
 
Example #6
Source File: RNBoundaryModule.java    From react-native-boundary with Apache License 2.0 5 votes vote down vote up
private void addGeofence(final Promise promise, final GeofencingRequest geofencingRequest, final String requestId) {
    int permission = ActivityCompat.checkSelfPermission(getReactApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);


    if (permission != PackageManager.PERMISSION_GRANTED) {
        permission = requestPermissions();
    }

    if (permission != PackageManager.PERMISSION_GRANTED) {
        promise.reject("PERM", "Access fine location is not permitted. Hello");
    } else {
        Log.i(TAG, "Attempting to add geofence.");

        mGeofencingClient.addGeofences(
                geofencingRequest,
                getBoundaryPendingIntent()
        )
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.i(TAG, "Successfully added geofence.");
                        promise.resolve(requestId);
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.i(TAG, "Failed to add geofence.");
                        promise.reject(e);
                    }
                });
    }
}
 
Example #7
Source File: LocationClientImpl.java    From android_external_GmsLib with Apache License 2.0 5 votes vote down vote up
public void addGeofences(GeofencingRequest request, PendingIntent pendingIntent, IGeofencerCallbacks callbacks) throws RemoteException {
    if (nativeLocation != null) {
        nativeLocation.addGeofences(request, pendingIntent, callbacks);
    } else {
        getServiceInterface().addGeofences(request, pendingIntent, callbacks);
    }
}
 
Example #8
Source File: GeofencingAddSingleOnSubscribe.java    From RxGps with Apache License 2.0 4 votes vote down vote up
GeofencingAddSingleOnSubscribe(RxLocation rxLocation, GeofencingRequest geofencingRequest, PendingIntent pendingIntent, Long timeoutTime, TimeUnit timeoutUnit) {
    super(rxLocation, timeoutTime, timeoutUnit);
    this.geofencingRequest = geofencingRequest;
    this.pendingIntent = pendingIntent;
}
 
Example #9
Source File: GoogleLocationManagerServiceImpl.java    From android_packages_apps_GmsCore with Apache License 2.0 4 votes vote down vote up
@Override
public void addGeofences(GeofencingRequest geofencingRequest, PendingIntent pendingIntent,
                         IGeofencerCallbacks callbacks) throws RemoteException {
    Log.d(TAG, "addGeofences: " + geofencingRequest);
}
 
Example #10
Source File: NativeLocationClientImpl.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
public void addGeofences(GeofencingRequest geofencingRequest, PendingIntent pendingIntent, IGeofencerCallbacks callbacks) throws RemoteException {
    Log.d(TAG, "addGeofences(GeofencingRequest)");
    callbacks.onAddGeofenceResult(GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE, new String[0]);
}
 
Example #11
Source File: MainActivity.java    From io2015-codelabs with Apache License 2.0 4 votes vote down vote up
private GeofencingRequest getGeofencingRequest() {
}
 
Example #12
Source File: MainActivity.java    From io2015-codelabs with Apache License 2.0 4 votes vote down vote up
private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
}
 
Example #13
Source File: GeofenceApiHandler.java    From PowerSwitch_Android with GNU General Public License v3.0 4 votes vote down vote up
private static GeofencingRequest getGeofencingRequest(Geofence geofence) {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofence(geofence);
    return builder.build();
}
 
Example #14
Source File: GeofencingImpl.java    From mobile-messaging-sdk-android with Apache License 2.0 4 votes vote down vote up
private GeofencingRequest geofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(geofences);
    return builder.build();
}
 
Example #15
Source File: MainActivity.java    From Android-9-Development-Cookbook with MIT License 4 votes vote down vote up
private GeofencingRequest createGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL);
    builder.addGeofences(createGeofenceList());
    return builder.build();
}
 
Example #16
Source File: Geofencing.java    From RxGps with Apache License 2.0 4 votes vote down vote up
private Single<Status> addInternal(GeofencingRequest geofencingRequest, PendingIntent pendingIntent, Long timeoutTime, TimeUnit timeoutUnit) {
    return Single.create(new GeofencingAddSingleOnSubscribe(rxLocation, geofencingRequest, pendingIntent, timeoutTime, timeoutUnit));
}
 
Example #17
Source File: Geofencing.java    From RxGps with Apache License 2.0 4 votes vote down vote up
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
public Single<Status> add(@NonNull GeofencingRequest geofencingRequest, @NonNull PendingIntent pendingIntent, long timeoutTime, @NonNull TimeUnit timeoutUnit) {
    return addInternal(geofencingRequest, pendingIntent, timeoutTime, timeoutUnit);
}
 
Example #18
Source File: Geofencing.java    From RxGps with Apache License 2.0 4 votes vote down vote up
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
public Single<Status> add(@NonNull GeofencingRequest geofencingRequest, @NonNull PendingIntent pendingIntent) {
    return addInternal(geofencingRequest, pendingIntent, null, null);
}
 
Example #19
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 4 votes vote down vote up
private void listing15_14_15_16_17(Location location, String id) {
  if (
    ActivityCompat
      .checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED ||
    ActivityCompat
      .checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED) {

      Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
      PendingIntent geofenceIntent = PendingIntent.getBroadcast(this, -1, intent, 0);

      // Listing 15-14: Accessing the Geofencing Client
      GeofencingClient geofencingClient =
        LocationServices.getGeofencingClient(this);

      // Listing 15-15: Defining a Geofence
      Geofence newGeofence
        = new Geofence.Builder()
            .setRequestId(id) // unique name of geofence
            .setCircularRegion(location.getLatitude(),
              location.getLongitude(),
              30) // 30 meter radius.
            .setExpirationDuration(Geofence.NEVER_EXPIRE) // Or expiration time in ms
            .setLoiteringDelay(10 * 1000)                   // Dwell after 10 seconds
            .setNotificationResponsiveness(10 * 1000)       // Notify within 10 seconds
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL)
            .build();

      // Listing 15-16: Creating a Geofencing Request
      GeofencingRequest geofencingRequest
        = new GeofencingRequest.Builder()
            .addGeofence(newGeofence)
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL)
            .build();

      // Listing 15-17: Initiating a Geofencing Request
      geofencingClient.addGeofences(geofencingRequest, geofenceIntent)
        .addOnSuccessListener(this, new OnSuccessListener<Void>() {
          @Override
          public void onSuccess(Void aVoid) {
            // TODO Geofence added.
          }
        })
        .addOnFailureListener(this, new OnFailureListener() {
          @Override
          public void onFailure(@NonNull Exception e) {
            Log.d(TAG, "Adding Geofence failed", e);
            // TODO Geofence failed to add.
          }
        });
    }
}
 
Example #20
Source File: RNBoundaryModule.java    From react-native-boundary with Apache License 2.0 4 votes vote down vote up
private GeofencingRequest createGeofenceRequest(Geofence geofence) {
    return new GeofencingRequest.Builder()
            .addGeofence(geofence)
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .build();
}
 
Example #21
Source File: RNBoundaryModule.java    From react-native-boundary with Apache License 2.0 4 votes vote down vote up
private GeofencingRequest createGeofenceRequest(List<Geofence> geofences) {
    return new GeofencingRequest.Builder()
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .addGeofences(geofences)
            .build();
}
 
Example #22
Source File: RNBoundaryModule.java    From react-native-boundary with Apache License 2.0 4 votes vote down vote up
@ReactMethod
public void add(final ReadableMap readableMap, final Promise promise) {
    final GeofencingRequest geofencingRequest = createGeofenceRequest(createGeofence(readableMap));
    addGeofence(promise, geofencingRequest, geofencingRequest.getGeofences().get(0).getRequestId());
}