Java Code Examples for com.google.android.gms.location.Geofence#GEOFENCE_TRANSITION_EXIT

The following examples show how to use com.google.android.gms.location.Geofence#GEOFENCE_TRANSITION_EXIT . 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: 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 3
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 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: MainActivity.java    From android-Geofencing with Apache License 2.0 6 votes vote down vote up
/**
 * In this sample, the geofences are predetermined and are hard-coded here. A real app might
 * dynamically create geofences based on the user's location.
 */
public void createGeofences() {
    // Create internal "flattened" objects containing the geofence data.
    mAndroidBuildingGeofence = new SimpleGeofence(
            ANDROID_BUILDING_ID,                // geofenceId.
            ANDROID_BUILDING_LATITUDE,
            ANDROID_BUILDING_LONGITUDE,
            ANDROID_BUILDING_RADIUS_METERS,
            GEOFENCE_EXPIRATION_TIME,
            Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT
    );
    mYerbaBuenaGeofence = new SimpleGeofence(
            YERBA_BUENA_ID,                // geofenceId.
            YERBA_BUENA_LATITUDE,
            YERBA_BUENA_LONGITUDE,
            YERBA_BUENA_RADIUS_METERS,
            GEOFENCE_EXPIRATION_TIME,
            Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT
    );

    // Store these flat versions in SharedPreferences and add them to the geofence list.
    mGeofenceStorage.setGeofence(ANDROID_BUILDING_ID, mAndroidBuildingGeofence);
    mGeofenceStorage.setGeofence(YERBA_BUENA_ID, mYerbaBuenaGeofence);
    mGeofenceList.add(mAndroidBuildingGeofence.toGeofence());
    mGeofenceList.add(mYerbaBuenaGeofence.toGeofence());
}
 
Example 6
Source File: MainActivity.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
/**
 * In this sample, the geofences are predetermined and are hard-coded here. A real app might
 * dynamically create geofences based on the user's location.
 */
public void createGeofences() {
    // Create internal "flattened" objects containing the geofence data.
    mAndroidBuildingGeofence = new SimpleGeofence(
            ANDROID_BUILDING_ID,                // geofenceId.
            ANDROID_BUILDING_LATITUDE,
            ANDROID_BUILDING_LONGITUDE,
            ANDROID_BUILDING_RADIUS_METERS,
            GEOFENCE_EXPIRATION_TIME,
            Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT
    );
    mYerbaBuenaGeofence = new SimpleGeofence(
            YERBA_BUENA_ID,                // geofenceId.
            YERBA_BUENA_LATITUDE,
            YERBA_BUENA_LONGITUDE,
            YERBA_BUENA_RADIUS_METERS,
            GEOFENCE_EXPIRATION_TIME,
            Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT
    );

    // Store these flat versions in SharedPreferences and add them to the geofence list.
    mGeofenceStorage.setGeofence(ANDROID_BUILDING_ID, mAndroidBuildingGeofence);
    mGeofenceStorage.setGeofence(YERBA_BUENA_ID, mYerbaBuenaGeofence);
    mGeofenceList.add(mAndroidBuildingGeofence.toGeofence());
    mGeofenceList.add(mYerbaBuenaGeofence.toGeofence());
}
 
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: GeofencingService.java    From AndroidDemoProjects with Apache License 2.0 6 votes vote down vote up
@Override
protected void onHandleIntent( Intent intent ) {
	NotificationCompat.Builder builder = new NotificationCompat.Builder( this );
	builder.setSmallIcon( R.drawable.ic_launcher );
	builder.setDefaults( Notification.DEFAULT_ALL );
	builder.setOngoing( true );

	int transitionType = LocationClient.getGeofenceTransition( intent );
	if( transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ) {
		builder.setContentTitle( "Geofence Transition" );
		builder.setContentText( "Entering Geofence" );
		mNotificationManager.notify( 1, builder.build() );
	}
	else if( transitionType == Geofence.GEOFENCE_TRANSITION_EXIT ) {
		builder.setContentTitle( "Geofence Transition" );
		builder.setContentText( "Exiting Geofence" );
		mNotificationManager.notify( 1, builder.build() );
	}
}
 
Example 9
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 10
Source File: GeofenceIntentService.java    From PowerSwitch_Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Maps geofence transition types to their human-readable equivalents.
 *
 * @param transitionType A transition type constant defined in Geofence
 * @return A String indicating the type of transition
 */
private String getTransitionString(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_DWELL:
            return "dwell";
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return getString(R.string.enter);
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return getString(R.string.exit);
        default:
            return "Unknown Transition";
    }
}
 
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: GeofenceTransitionsIntentService.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
private String getTransitionString(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return "Entered";
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return "Exited";
        default:
            return "Unknown Transition";
    }
}
 
Example 13
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 14
Source File: GeofenceTransitionsJobIntentService.java    From location-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Maps geofence transition types to their human-readable equivalents.
 *
 * @param transitionType    A transition type constant defined in Geofence
 * @return                  A String indicating the type of transition
 */
private String getTransitionString(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return getString(R.string.geofence_transition_entered);
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return getString(R.string.geofence_transition_exited);
        default:
            return getString(R.string.unknown_geofence_transition);
    }
}
 
Example 15
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();
        }
    }
}
 
Example 16
Source File: GeofenceTransitionsIntentService.java    From AndroidWearable-Samples 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) {
    // First check for errors.
    if (LocationClient.hasError(intent)) {
        int errorCode = LocationClient.getErrorCode(intent);
        Log.e(TAG, "Location Services error: " + errorCode);
    } else {
        // Get the type of geofence transition (i.e. enter or exit in this sample).
        int transitionType = LocationClient.getGeofenceTransition(intent);
        // Create a DataItem when a user enters one of the geofences. The wearable app will
        // receive this and create a notification to prompt him/her to check in.
        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 = LocationClient.getTriggeringGeofences(intent).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);
            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");
            }
            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();
            mGoogleApiClient.disconnect();
        }
    }
}