Java Code Examples for com.google.android.gms.location.GeofencingEvent#getGeofenceTransition()

The following examples show how to use com.google.android.gms.location.GeofencingEvent#getGeofenceTransition() . 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: UtilityService.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Called when a geofence is triggered
 */
private void geofenceTriggered(Intent intent) {
    Log.v(TAG, ACTION_GEOFENCE_TRIGGERED);

    // Check if geofences are enabled
    boolean geofenceEnabled = Utils.getGeofenceEnabled(this);

    // Extract the geofences from the intent
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    List<Geofence> geofences = event.getTriggeringGeofences();

    if (geofenceEnabled && geofences != null && geofences.size() > 0) {
        if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) {
            // Trigger the notification based on the first geofence
            showNotification(geofences.get(0).getRequestId(), Constants.USE_MICRO_APP);
        } else if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) {
            // Clear notifications
            clearNotificationInternal();
            clearRemoteNotifications();
        }
    }
    UtilityReceiver.completeWakefulIntent(intent);
}
 
Example 2
Source File: GeofenceBroadcastReceiver.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

  if (geofencingEvent.hasError()) {
    int errorCode = geofencingEvent.getErrorCode();
    String errorMessage = GeofenceStatusCodes.getStatusCodeString(errorCode);
    Log.e(TAG, errorMessage);
  } else {
    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // A single event can trigger multiple geofences.
    // Get the geofences that were triggered.
    List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

    // TODO React to the Geofence(s) transition(s).
  }
}
 
Example 3
Source File: ReceiveTransitionsIntentService.java    From Leanplum-Android-SDK with Apache License 2.0 6 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
  try {
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    if (event.hasError()) {
      int errorCode = event.getErrorCode();
      Log.d("Location Client Error with code: " + errorCode);
    } else {
      int transitionType = event.getGeofenceTransition();
      List<Geofence> triggeredGeofences = event.getTriggeringGeofences();
      if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ||
          transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
        LocationManagerImplementation locationManager = (LocationManagerImplementation)
            ActionManager.getLocationManager();
        if (locationManager != null) {
          locationManager.updateStatusForGeofences(triggeredGeofences, transitionType);
        }
      }
    }
  } catch (Throwable t) {
    Util.handleException(t);
  }
}
 
Example 4
Source File: UtilityService.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
/**
 * Called when a geofence is triggered
 */
private void geofenceTriggered(Intent intent) {
    Log.v(TAG, ACTION_GEOFENCE_TRIGGERED);

    // Check if geofences are enabled
    boolean geofenceEnabled = Utils.getGeofenceEnabled(this);

    // Extract the geofences from the intent
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    List<Geofence> geofences = event.getTriggeringGeofences();

    if (geofenceEnabled && geofences != null && geofences.size() > 0) {
        if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) {
            // Trigger the notification based on the first geofence
            showNotification(geofences.get(0).getRequestId(), Constants.USE_MICRO_APP);
        } else if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) {
            // Clear notifications
            clearNotificationInternal();
            clearRemoteNotifications();
        }
    }
    UtilityReceiver.completeWakefulIntent(intent);
}
 
Example 5
Source File: UtilityService.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
/**
 * Called when a geofence is triggered
 */
private void geofenceTriggered(Intent intent) {
    Log.v(TAG, ACTION_GEOFENCE_TRIGGERED);

    // Check if geofences are enabled
    boolean geofenceEnabled = Utils.getGeofenceEnabled(this);

    // Extract the geofences from the intent
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    List<Geofence> geofences = event.getTriggeringGeofences();

    if (geofenceEnabled && geofences != null && geofences.size() > 0) {
        if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) {
            // Trigger the notification based on the first geofence
            showNotification(geofences.get(0).getRequestId(), Constants.USE_MICRO_APP);
        } else if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) {
            // Clear notifications
            clearNotificationInternal();
            clearRemoteNotifications();
        }
    }
    UtilityReceiver.completeWakefulIntent(intent);
}
 
Example 6
Source File: GeofenceHandler.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    String className = intent.getStringExtra("geofenceClass");
    String id = intent.getStringExtra("geofenceID");
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

    for (com.google.android.gms.location.Geofence gf : geofencingEvent.getTriggeringGeofences()) {
        try {
            id = gf.getRequestId();
            GeofenceListener l = (GeofenceListener) Class.forName(className).newInstance();
            if (geofencingEvent.getGeofenceTransition() == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_ENTER) {
                l.onEntered(id);
            } else if (geofencingEvent.getGeofenceTransition() == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_EXIT) {
                l.onExit(id);
            }
        } catch (Exception e) {
            Log.e("Codename One", "geofence error", e);
        }
    }

}
 
Example 7
Source File: GeofenceData.java    From android-sdk with MIT License 6 votes vote down vote up
private static String check(GeofencingEvent event) {
    if (event == null) {
        return "GeofencingEvent is null";
    }
    if (event.hasError()) {
        return "GeofencingEvent has error: "+event.getErrorCode();
    }
    if (event.getTriggeringGeofences() == null) {
        return "GeofencingEvent has no triggering geofences";
    }
    if (event.getTriggeringGeofences().isEmpty()) {
        return "GeofencingEvent has empty list of triggering geofences";
    }
    int transition = event.getGeofenceTransition();
    //Only GEOFENCE_TRANSITION_ENTER and GEOFENCE_TRANSITION_EXIT are supported for now.
    if (transition != Geofence.GEOFENCE_TRANSITION_ENTER && transition != Geofence.GEOFENCE_TRANSITION_EXIT) {
        return "GeofencingEvent has invalid transition: "+transition;
    }
    return null;
}
 
Example 8
Source File: GeofenceIntentService.java    From Android-9-Development-Cookbook with MIT License 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        Toast.makeText(getApplicationContext(), "Geofence error code= "
                        + geofencingEvent.getErrorCode(), Toast.LENGTH_SHORT).show();
        return;
    }
    int geofenceTransition = geofencingEvent.getGeofenceTransition();
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL) {
        Toast.makeText(getApplicationContext(), "GEOFENCE_TRANSITION_DWELL",
                Toast.LENGTH_SHORT).show();
    }
}
 
Example 9
Source File: GeoTransitionHelper.java    From mobile-messaging-sdk-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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()));
}
 
Example 10
Source File: GeofenceIntentService.java    From PowerSwitch_Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Handles incoming intents.
 *
 * @param intent sent by Location Services. This Intent is provided to Location
 *               Services (inside a PendingIntent) when addGeofences() is called.
 */
@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        Log.e(this, "GeofencingError");
        return;
    }

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        // Get the geofences that were triggered. A single event can trigger
        // multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the Ids of each geofence that was triggered.
        ArrayList<String> triggeringGeofencesIdsList = new ArrayList<>();
        for (Geofence geofence : triggeringGeofences) {
            triggeringGeofencesIdsList.add(geofence.getRequestId());
        }
        Log.d(this, getTransitionString(geofenceTransition) + ": " + TextUtils.join(", ", triggeringGeofencesIdsList));

        executeGeofences(triggeringGeofences, geofenceTransition);
    } else {
        // Log the error.
        Log.e(this, "Unknown Geofence transition: " + geofenceTransition);
    }
}
 
Example 11
Source File: GeofenceTransitionsIntentService.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this,
                geofencingEvent.getErrorCode());
        Log.e(TAG, errorMessage);
        return;
    }

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        // Get the geofences that were triggered. A single event can trigger multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(
                this,
                geofenceTransition,
                triggeringGeofences
        );

        // Send notification and log the transition details.
        sendNotification(geofenceTransitionDetails);
        Log.i(TAG, geofenceTransitionDetails);
    } else {
        // Log the error.
    }
}
 
Example 12
Source File: GeofenceTransitionsJobIntentService.java    From location-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Handles incoming intents.
 * @param intent sent by Location Services. This Intent is provided to Location
 *               Services (inside a PendingIntent) when addGeofences() is called.
 */
@Override
protected void onHandleWork(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this,
                geofencingEvent.getErrorCode());
        Log.e(TAG, errorMessage);
        return;
    }

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        // Get the geofences that were triggered. A single event can trigger multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(geofenceTransition,
                triggeringGeofences);

        // Send notification and log the transition details.
        sendNotification(geofenceTransitionDetails);
        Log.i(TAG, geofenceTransitionDetails);
    } else {
        // Log the error.
        Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition));
    }
}
 
Example 13
Source File: GeofenceTransitionsIntentService.java    From android-Geofencing with Apache License 2.0 4 votes vote down vote up
/**
 * Handles incoming intents.
 * @param intent The Intent sent by Location Services. This Intent is provided to Location
 * Services (inside a PendingIntent) when addGeofences() is called.
 */
@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geoFenceEvent = GeofencingEvent.fromIntent(intent);
    if (geoFenceEvent.hasError()) {
        int errorCode = geoFenceEvent.getErrorCode();
        Log.e(TAG, "Location Services error: " + errorCode);
    } else {

        int transitionType = geoFenceEvent.getGeofenceTransition();
        if (Geofence.GEOFENCE_TRANSITION_ENTER == transitionType) {
            // Connect to the Google Api service in preparation for sending a DataItem.
            mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
            // Get the geofence id triggered. Note that only one geofence can be triggered at a
            // time in this example, but in some cases you might want to consider the full list
            // of geofences triggered.
            String triggeredGeoFenceId = geoFenceEvent.getTriggeringGeofences().get(0)
                    .getRequestId();
            // Create a DataItem with this geofence's id. The wearable can use this to create
            // a notification.
            final PutDataMapRequest putDataMapRequest =
                    PutDataMapRequest.create(GEOFENCE_DATA_ITEM_PATH);
            putDataMapRequest.getDataMap().putString(KEY_GEOFENCE_ID, triggeredGeoFenceId);
            putDataMapRequest.setUrgent();
            if (mGoogleApiClient.isConnected()) {
                Wearable.DataApi.putDataItem(
                        mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();
            } else {
                Log.e(TAG, "Failed to send data item: " + putDataMapRequest
                        + " - Client disconnected from Google Play Services");
            }
            Toast.makeText(this, getString(R.string.entering_geofence),
                    Toast.LENGTH_SHORT).show();
            mGoogleApiClient.disconnect();
        } else if (Geofence.GEOFENCE_TRANSITION_EXIT == transitionType) {
            // Delete the data item when leaving a geofence region.
            mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
            Wearable.DataApi.deleteDataItems(mGoogleApiClient, GEOFENCE_DATA_ITEM_URI).await();
            showToast(this, R.string.exiting_geofence);
            mGoogleApiClient.disconnect();
        }
    }
}