@Override protected void onHandleIntent(Intent intent) { GeofencingEvent fenceEvent = GeofencingEvent.fromIntent(intent); if (fenceEvent.hasError()) { String errorMessage = GeofenceStatusCodes.getStatusCodeString(fenceEvent.getErrorCode()); Timber.i(errorMessage); return; } Timber.i("We got a geofence intent"); if (fenceEvent.getGeofenceTransition() != Geofence.GEOFENCE_TRANSITION_DWELL || fenceEvent.getTriggeringGeofences().isEmpty()) { return; } String placeId = fenceEvent.getTriggeringGeofences().get(0).getRequestId(); Realm realm = Realm.getDefaultInstance(); Place place = Place.findFirst(realm, placeId); String title = place != null ? getString(R.string.geofence_notification_place_title, place.getName()) : getString(R.string.geofence_notification_title); String content = getString(R.string.geofence_notification_content); String imageUrl = place != null ? place.getFirstImage(GEOFENCE_LARGE_ICON_SIZE) : null; realm.close(); sendNotification(title, content, placeId, imageUrl); }
@Override public void onReceive(Context context, Intent intent) { final ActiveAlarmManager activeAlarmManager = new ActiveAlarmManager(context); GeofencingEvent event = GeofencingEvent.fromIntent(intent); if (event.hasError()) { final String errorMessage = GeofenceStatusCodes.getStatusCodeString(event.getErrorCode()); Log.e(TAG, errorMessage); return; } final int transition = event.getGeofenceTransition(); final List<Geofence> affectedGeofences = event.getTriggeringGeofences(); if (null != affectedGeofences && !affectedGeofences.isEmpty()) { final Collection<GeoAlarm> affectedAlarms = filter( Lists.transform(affectedGeofences, getGeoAlarmForGeofenceFn(context)), a -> a != null); ImmutableSet<UUID> affectedAlarmIds = ImmutableSet.copyOf(transform(affectedAlarms, alarm -> alarm.id)); if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) { activeAlarmManager.addActiveAlarms(affectedAlarmIds); } else { activeAlarmManager.removeActiveAlarms(affectedAlarmIds); } } }
/** * Returns the error string for a geofencing error code. */ public static String getGeofenceErrorString(Context context, int errorCode) { Resources mResources = context.getResources(); switch (errorCode) { case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE: return mResources.getString(R.string.geofence_not_available); case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES: return mResources.getString(R.string.geofence_too_many_geofences); case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS: return mResources.getString(R.string.geofence_too_many_pending_intents); default: return mResources.getString(R.string.unknown_geofence_error); } }
/** * Returns the error string for a geofencing error code. */ public static String getErrorString(Context context, int errorCode) { Resources mResources = context.getResources(); switch (errorCode) { case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE: return mResources.getString(R.string.geofence_not_available); case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES: return mResources.getString(R.string.geofence_too_many_geofences); case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS: return mResources.getString(R.string.geofence_too_many_pending_intents); default: return mResources.getString(R.string.unknown_geofence_error); } }
/** * Returns the error string for a geofencing error code. */ public static String getErrorString(Context context, int errorCode) { switch (errorCode) { case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE: return "Geofence service is not available now"; case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES: return "Your app has registered too many geofences"; case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS: return "You have provided too many PendingIntents to the addGeofences() call"; default: return "Unknown error: the Geofence service is not available now"; } }
/** * Returns the error string for a geofencing error code. */ public static String getErrorString(Context context, int errorCode) { switch (errorCode) { case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE: return context.getString(R.string.geofence_error_unavailable); case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES: return context.getString(R.string.geofence_error_many_geofences); case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS: return context.getString(R.string.geofence_error_many_intents); default: return context.getString(R.string.geofence_error_unknown); } }
/** * Resolves transition information from geofencing intent * * @param intent geofencing intent * @return transition information * @throws RuntimeException if information cannot be resolved */ static GeoTransition resolveTransitionFromIntent(Intent intent) throws RuntimeException { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent == null) { throw new RuntimeException("Geofencing event is null, cannot process"); } if (geofencingEvent.hasError()) { if (geofencingEvent.getErrorCode() == GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE) { throw new GeofenceNotAvailableException(); } throw new RuntimeException("ERROR: " + GeofenceStatusCodes.getStatusCodeString(geofencingEvent.getErrorCode())); } GeoEventType event = supportedTransitionEvents.get(geofencingEvent.getGeofenceTransition()); if (event == null) { throw new RuntimeException("Transition is not supported: " + geofencingEvent.getGeofenceTransition()); } Set<String> triggeringRequestIds = new ArraySet<>(); for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) { triggeringRequestIds.add(geofence.getRequestId()); } Location location = geofencingEvent.getTriggeringLocation(); return new GeoTransition(event, triggeringRequestIds, new GeoLatLng(location.getLatitude(), location.getLongitude())); }
public static String getErrorString(Context context, int errorCode) { Resources mResources = context.getResources(); switch (errorCode) { case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE: return "Geofence service is not available now."; case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES: return "Your app has registered too many geofences."; case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS: return "You have provided too many PendingIntents!"; default: return "Unknown error."; } }
/** * Returns the error string for a geofencing error code. */ public static String getErrorString(@NonNull Context context, int errorCode) { Resources mResources = context.getResources(); switch (errorCode) { case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE: return mResources.getString(R.string.geofence_not_available); case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES: return mResources.getString(R.string.geofence_too_many_geofences); case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS: return mResources.getString(R.string.geofence_too_many_pending_intents); default: return mResources.getString(R.string.unknown_geofence_error); } }
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]); }
public void removeGeofences(List<String> requestIds, IGeofencerCallbacks callbacks) throws RemoteException { Log.d(TAG, "removeGeofences(List<RequestId>)"); callbacks.onRemoveGeofencesByRequestIdsResult(GeofenceStatusCodes.ERROR, requestIds.toArray(new String[requestIds.size()])); }