private void addGeofence() { final GeofencingRequest geofencingRequest = createGeofencingRequest(); if (geofencingRequest == null) return; final PendingIntent pendingIntent = createNotificationBroadcastPendingIntent(); reactiveLocationProvider .removeGeofences(pendingIntent) .flatMap(new Func1<Status, Observable<Status>>() { @Override public Observable<Status> call(Status pendingIntentRemoveGeofenceResult) { return reactiveLocationProvider.addGeofences(pendingIntent, geofencingRequest); } }) .subscribe(new Action1<Status>() { @Override public void call(Status addGeofenceResult) { toast("Geofence added, success: " + addGeofenceResult.isSuccess()); } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { toast("Error adding geofence."); Log.d(TAG, "Error adding geofence.", throwable); } }); }
private GeofencingRequest createGeofencingRequest() { try { double longitude = Double.parseDouble(longitudeInput.getText().toString()); double latitude = Double.parseDouble(latitudeInput.getText().toString()); float radius = Float.parseFloat(radiusInput.getText().toString()); Geofence geofence = new Geofence.Builder() .setRequestId("GEOFENCE") .setCircularRegion(latitude, longitude, radius) .setExpirationDuration(Geofence.NEVER_EXPIRE) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build(); return new GeofencingRequest.Builder().addGeofence(geofence).build(); } catch (NumberFormatException ex) { toast("Error parsing input."); return null; } }
private GeofencingRequest getGeofencingRequest() { if (this.place == null || this.place.getLocation() == null) { return null; } // called when the transition associated with the Geofence is triggered) List<Geofence> geoFenceList = new ArrayList<Geofence>(); geoFenceList.add(new Geofence.Builder() .setRequestId(GEOFENCE_REQUEST_ID) .setCircularRegion(this.place.getLocation().getLatitude(), this.place.getLocation().getLongitude(), GEOFENCE_RADIUS_IN_METERS ) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_ENTER) .setLoiteringDelay(LOITERING_DELAY_MS) .setNotificationResponsiveness(NOTIFICATION_RESPONSIVENESS_MS) .setExpirationDuration(Geofence.NEVER_EXPIRE) .build()); GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL); builder.addGeofences(geoFenceList); return builder.build(); }
private void startInternal(final Location homeLocation, PendingIntent pendingIntent) { GeofencingRequest geofenceRequest = createGeofenceRequest(homeLocation); if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { ToastLog.logShort(context, TAG, "Starting Play geofencing..."); LocationServices.GeofencingApi.addGeofences( googleApiClient, geofenceRequest, pendingIntent ).setResultCallback(new ResultCallback<Status>() { @Override public void onResult(@NonNull Status status) { if (status.getStatusMessage() != null) { ToastLog.warnShort(context, TAG, status.getStatusMessage()); } } }); } }
private void addGeofence(PendingIntent pendingIntent, GeofencingRequest geofencingRequest) { if (!mGeofencingRequestsMap.containsKey(pendingIntent)) { mGeofencingRequestsMap.put(pendingIntent, geofencingRequest); if (mGoogleApiClient.isConnected()) { if (!isLocationUpdates) { // requestLocationUpdates for getting current position LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this); isLocationUpdates = true; } LocationServices.GeofencingApi.addGeofences( mGoogleApiClient, geofencingRequest, pendingIntent ).setResultCallback(this); } } }
@Override public void onConnected(@Nullable Bundle bundle) { Log.i(TAG, "Connected to GoogleApiClient"); try { // requestLocationUpdates for getting current position LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this); isLocationUpdates = true; for (Map.Entry<PendingIntent, GeofencingRequest> entry : mGeofencingRequestsMap.entrySet()) { addGeofence(entry.getKey(), entry.getValue()); } } catch (SecurityException securityException) { // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission. showSecurityException(securityException); } }
public void add(GMap.Location location, ResultCallback callback){ Log.i(TAG, "add " + location.toString()); String id = location.url.toString()+"\t"+location.name; Geofence fence = new Geofence.Builder() .setRequestId(id) .setCircularRegion( // 500メートル以内 location.latitude, location.longitude, 500) .setExpirationDuration( // 期限を指定できる Geofence.NEVER_EXPIRE) .setTransitionTypes( Geofence.GEOFENCE_TRANSITION_ENTER) .build(); GeofencingRequest request = new GeofencingRequest.Builder() .addGeofence(fence) // Geofenceを1つ登録 .build(); // IntentServiceで受け取る Intent intent = new Intent(context, GeofenceReceiveService.class); PendingIntent pIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); LocationServices.GeofencingApi .addGeofences(client, request, pIntent) .setResultCallback(callback); }
@SuppressWarnings("ResourceType") private void registerGeofence() { orchextraLogger.log("Removing " + geofenceUpdates.getDeleteGeofences().size() + " geofences..."); orchextraLogger.log("Registering " + geofenceUpdates.getNewGeofences().size() + " geofences..."); List<String> deleteCodeList = androidGeofenceConverter.getCodeList(geofenceUpdates.getDeleteGeofences()); if (deleteCodeList.size() > 0) { LocationServices.GeofencingApi.removeGeofences(googleApiClientConnector.getGoogleApiClient(), deleteCodeList); } if (geofenceUpdates.getNewGeofences().size() > 0) { GeofencingRequest geofencingRequest = androidGeofenceConverter.convertGeofencesToGeofencingRequest(geofenceUpdates.getNewGeofences()); if (googleApiClientConnector.isGoogleApiClientAvailable()) { try { LocationServices.GeofencingApi.addGeofences(googleApiClientConnector.getGoogleApiClient(), geofencingRequest, geofencePendingIntentCreator.getGeofencingPendingIntent()).setResultCallback(this); }catch (Exception e){ orchextraLogger.log("Exception trying to add geofences: " + e.getMessage(), OrchextraSDKLogLevel.ERROR); } } } }
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()); } }); }
@Override public void onConnected(Bundle connectionHint) { // We're connected, now we need to create a GeofencingRequest with // the geofences we have stored. mGeofencingRequest = new GeofencingRequest.Builder().addGeofences( mGeofences).build(); //mGeofencingRequest = new getGeofencingRequest().Builder(); mPendingIntent = createRequestPendingIntent(); // This is for debugging only and does not affect // geofencing. LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this); // Submitting the request to monitor geofences. PendingResult<Status> pendingResult = LocationServices.GeofencingApi .addGeofences(mGoogleApiClient, getGeofencingRequest(), mPendingIntent); // Set the result callbacks listener to this class. pendingResult.setResultCallback(this); }
@Override public void onConnected(Bundle bundle) { // Now we can do the actual logic List<Geofence> fences = new ArrayList<Geofence>(); fences.add(fence.toGeofence(venue)); geofencingapi = LocationServices.GeofencingApi; if(locationClient != null && locationClient.isConnected() && fences.size() > 0){ GeofencingRequest request = new GeofencingRequest.Builder() .addGeofences(fences) .build(); PendingResult<Status> status = geofencingapi.addGeofences(locationClient, request, geofenceUtils.getTransitionPendingIntent(this)); status.setResultCallback(this, 10, TimeUnit.SECONDS); // disconnect will be done in resultlistener }else if(locationClient != null && locationClient.isConnected()){ locationClient.disconnect(); } }
/** * Creates a geofencing trigger request. This will make the location service * to launch geofencing events (enter/exit events) with the specified * geofence list. * <br> * See Geofences at <a href="http://developer.android.com/intl/es/training/location/geofencing.html">Google developer</a>. * <br><br> * Note:<br><br> * * Requires the permission {@link android.permission.ACCESS_FINE_LOCATION}. * * @param geofenceList The list of Geofences to watch. */ public static GeofencingRequest location_geofencesCreateTriggerRequest(List<Geofence> geofenceList) { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); //The GEOFENCE_TRANSITION_ENTER/GEOFENCE_TRANSITION_EXIT transition //triggers when a device enters/exits a geofence. // //Specifying INITIAL_TRIGGER_ENTER tells Location services that //GEOFENCE_TRANSITION_ENTER should be triggered if the the device is //already inside the geofence. // //In many cases, it may be preferable to use instead INITIAL_TRIGGER_DWELL, //which triggers events only when the user stops for a defined duration //within a geofence. builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(geofenceList); return builder.build(); }
public GeofencingRequest settingGeofence(AlarmRealmData data) { Geofence geofence = new Geofence.Builder() .setRequestId(data.getGeofenceId()) .setCircularRegion(mTargetLocation.latitude, mTargetLocation.longitude, GEOFENCE_CIRCLE_RADIUS) .setExpirationDuration(Geofence.NEVER_EXPIRE) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER) .build(); GeofencingRequest request = new GeofencingRequest.Builder() .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER ) .addGeofence(geofence) .build(); return request; }
/*** * creates a {@link GeofencingRequest} request object using the mGeofenceList , Arraylist of geofences. * Used by {@link #registerAllGeofences()} * * @return the GeofencingRequest object. */ private GeofencingRequest getGeofenceRequest(){ GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(mGeofenceList); return builder.build(); }
public static GeofencingRequest getGeofencingRequest(Context context) { String geofencingRequestJSON = getSharedPreferences(context).getString(GEOFENCING_REQUEST, null); if (geofencingRequestJSON == null) { return null; } Gson gson = new Gson(); Type type = new TypeToken<GeofencingRequest>() { }.getType(); return gson.fromJson(geofencingRequestJSON, type); }
public static void setGeofencingRequest(Context context, GeofencingRequest request) { SharedPreferences.Editor editor = getEditor(context); Gson gson = new Gson(); String geofencingRequestJSON = gson.toJson(request); editor.putString(GEOFENCING_REQUEST, geofencingRequestJSON); editor.apply(); }
private static GeofencingRequest getGeofencingRequest(List<Place> places) { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL); //builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL); builder.addGeofences(getGeofenceList(places)); return builder.build(); }
/** * 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(); }
private com.google.android.gms.location.Geofence toGoogleGeofence(Geofence geofence) { int radius = preferences.getIntegerFromString(R.string.p_geofence_radius, 250); int responsiveness = (int) TimeUnit.SECONDS.toMillis(preferences.getIntegerFromString(R.string.p_geofence_responsiveness, 60)); return new com.google.android.gms.location.Geofence.Builder() .setCircularRegion(geofence.getLatitude(), geofence.getLongitude(), radius) .setNotificationResponsiveness(responsiveness) .setRequestId(Long.toString(geofence.getMetadataId())) .setTransitionTypes(GeofencingRequest.INITIAL_TRIGGER_ENTER) .setExpirationDuration(NEVER_EXPIRE) .build(); }
/** * Create Geofencing request * * @return GeofencingRequest initialised with a list of Geofences */ private GeofencingRequest getGeofencingRequest() { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); // if just added and if inside the geofence builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(mGeofenceList); return builder.build(); }
@Override public void onDestroy() { try { for (Map.Entry<PendingIntent, GeofencingRequest> entry : mGeofencingRequestsMap.entrySet()) { removeGeofence(entry.getKey()); } } catch (SecurityException securityException) { // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission. showSecurityException(securityException); } mGeofencingRequestsMap.clear(); mGoogleApiClient.disconnect(); super.onDestroy(); }
private GeofencingRequest getGeoFencingRequest() { Log.d(MainActivity.TAG, "getGeoFencingRequest()"); return new GeofencingRequest.Builder() .addGeofences(mGeofences) .setInitialTrigger(mInitialTrigger) .build(); }
private GeofencingRequest getGeoFencingRequest() { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(mGeofenceList); return builder.build(); }
public GeofencingRequest convertGeofencesToGeofencingRequest(List<OrchextraGeofence> orchextraGeofenceList) { List<Geofence> androidGeofenceList = new ArrayList<>(); int i = 0; while (i < orchextraGeofenceList.size() && i < ConstantsAndroidGeofence.MAX_NUM_GEOFENCES) { OrchextraGeofence orchextraGeofence = orchextraGeofenceList.get(i); Geofence geofence = new Geofence.Builder() .setRequestId(orchextraGeofence.getCode()) .setCircularRegion(orchextraGeofence.getPoint().getLat(), orchextraGeofence.getPoint().getLng(), GeofenceUtils.getRadius(orchextraGeofence.getRadius())) .setExpirationDuration(Geofence.NEVER_EXPIRE) .setLoiteringDelay(GeofenceUtils.getStayTimeDelayMs(orchextraGeofence.getStayTime())) // Required when we use the transition type of GEOFENCE_TRANSITION_DWELL .setTransitionTypes( GeofenceUtils.getTransitionTypes(orchextraGeofence.isNotifyOnEntry(), orchextraGeofence.isNotifyOnExit())) .build(); androidGeofenceList.add(geofence); i++; } GeofencingRequest geofencingRequest = new GeofencingRequest.Builder() .addGeofences(androidGeofenceList) .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL) .build(); return geofencingRequest; }
private void addGeofence() { final GeofencingRequest geofencingRequest = createGeofencingRequest(); final PendingIntent pendingIntent = createNotificationBroadcastPendingIntent(); reactiveLocationProvider .removeGeofences(pendingIntent) .flatMap(new Func1<Status, Observable<Status>>() { @Override public Observable<Status> call(Status pendingIntentRemoveGeofenceResult) { if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. // return TODO; } return reactiveLocationProvider.addGeofences(pendingIntent, geofencingRequest); } }) .subscribe(new Action1<Status>() { @Override public void call(Status addGeofenceResult) { Log.d("abc", "Geofence added, success: " + addGeofenceResult.isSuccess()); } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { // toast("Error adding geofence."); Log.d("abc", "Error adding geofence.", throwable); } }); }
private GeofencingRequest createGeofencingRequest() { ArrayList<Geofence> geofences = new ArrayList<>(); for (int i = 0; i < geoFenceData.getGeoFenceLocationInfos().size(); i++) { currentList.put(geoFenceData.getGeoFenceLocationInfos().get(i).getLocation(), null); Geofence geofence = new Geofence.Builder() .setRequestId(geoFenceData.getGeoFenceLocationInfos().get(i).getLocation()) .setCircularRegion(geoFenceData.getGeoFenceLocationInfos().get(i).getLatitude(), geoFenceData.getGeoFenceLocationInfos().get(i).getLongitude(), RADIUS) .setExpirationDuration(Geofence.NEVER_EXPIRE) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build(); geofences.add(geofence); } return new GeofencingRequest.Builder().addGeofences(geofences).setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT).build(); }
/** * Add a geofence to the store * This will also add the geofence to the google api client if connected. If not, it will trigger a connection * This call requires that the permission ACCESS_FINE_LOCATION is granted * @param storableGeofence the geofence to store * @return true if add has been asked, false otherwise. false could be returned if the geofence is expired */ @RequiresPermission("android.permission.ACCESS_FINE_LOCATION") public boolean addGeofence(@NonNull StorableGeofence storableGeofence) { boolean addedOngoing = false; if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { if (!storableGeofence.isExpired()) { mToAddStore.storeGeofence(storableGeofence); if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { GeofenceAddStatus addStatus = new GeofenceAddStatus(storableGeofence); GeofencingRequest.Builder requestBuilder = new GeofencingRequest.Builder(); requestBuilder.addGeofence(storableGeofence.toGeofence()); mGeofencingAPI.addGeofences(mGoogleApiClient, requestBuilder.build(), createRequestPendingIntent(storableGeofence)).setResultCallback(addStatus); Log.i(TAG, "Added " + storableGeofence); } else { googleApiConnect(); } addedOngoing = true; } } else { Log.e(TAG, "Could not add the geofence: permission ACCESS_FINE_LOCATION required"); } return addedOngoing; }
private static GeofencingRequest getGeofencingRequest(List<Geofence> geofenceList) { 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(0); builder.addGeofences(geofenceList); return builder.build(); }
@WorkerThread public void addGeofences(List<Geofence> geofenceList) { Log.d(); if (!isConnected()) return; if (geofenceList.isEmpty()) { Log.d("List of geofences is empty: do nothing"); return; } GeofencingRequest.Builder requestBuilder = new GeofencingRequest.Builder(); requestBuilder.addGeofences(geofenceList); LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, requestBuilder.build(), getPendingIntent()).await(); Log.d(geofenceList.size() + " geofences added"); }
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); } }
public PendingResult<Status> addGeofences(@NonNull Collection<GeoAlarm> alarms) { GeofencingRequest.Builder geofenceRequestBuilder = new GeofencingRequest.Builder(); geofenceRequestBuilder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); for (GeoAlarm alarm : alarms) { geofenceRequestBuilder.addGeofence(getGeofence(alarm)); } return LocationServices.GeofencingApi.addGeofences(apiClient, geofenceRequestBuilder.build(), pendingIntent); }