com.google.android.gms.awareness.fence.HeadphoneFence Java Examples

The following examples show how to use com.google.android.gms.awareness.fence.HeadphoneFence. 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: StorableHeadphoneFence.java    From JCVD with MIT License 5 votes vote down vote up
@Override
AwarenessFence getAwarenessFence(Context ctx) {
    switch (mTriggerType) {
        case STATE:
            return HeadphoneFence.during(mHeadphoneState);
        case PLUGGING_IN:
            return HeadphoneFence.pluggingIn();
        case UNPLUGGING:
            return HeadphoneFence.unplugging();
    }
    return null;
}
 
Example #2
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 4 votes vote down vote up
private void listing15_29_30_31_32() {
  if (ActivityCompat.checkSelfPermission(this,
    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    return;
  }

  int flags = PendingIntent.FLAG_UPDATE_CURRENT;
  Intent intent = new Intent(this, WalkFenceReceiver.class);
  PendingIntent awarenessIntent = PendingIntent.getBroadcast(this, -1,
    intent, flags);

  // Listing 15-29: Creating Awareness Fences
  // Near one of my custom beacons.
  BeaconState.TypeFilter typeFilter
    = BeaconState.TypeFilter.with("com.professionalandroid.apps.beacon", "my_type");

  AwarenessFence beaconFence = BeaconFence.near(typeFilter);

  // While walking.
  AwarenessFence activityFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);

  // Having just plugged in my headphones.
  AwarenessFence headphoneFence = HeadphoneFence.pluggingIn();

  // Within 1km of Google for longer than a minute.
  double lat = 37.4220233;
  double lng = -122.084252;
  double radius = 1000; // meters
  long dwell = 60000; // milliseconds.
  AwarenessFence locationFence = LocationFence.in(lat, lng, radius, dwell);

  // In the morning
  AwarenessFence timeFence = TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_MORNING);

  // During holidays
  AwarenessFence holidayFence = TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_HOLIDAY);

  // Listing 15-30: Combining Awareness Fences
  // Trigger when headphones are plugged in and walking in the morning
  // either within a kilometer of Google or near one of my beacons --
  // but not on a holiday.
  AwarenessFence morningWalk = AwarenessFence
                                 .and(activityFence,
                                   headphoneFence,
                                   timeFence,
                                   AwarenessFence.or(locationFence,
                                     beaconFence),
                                   AwarenessFence.not(holidayFence));

  // Listing 15-31: Creating an Awareness Fence Update Request
  FenceUpdateRequest fenceUpdateRequest
    = new FenceUpdateRequest.Builder()
        .addFence(WalkFenceReceiver.WALK_FENCE_KEY, morningWalk, awarenessIntent)
        .build();

  // Listing 15-32: Adding a new Awareness Fence
  Awareness.FenceApi.updateFences(
    mGoogleApiClient, fenceUpdateRequest).setResultCallback(new ResultCallback<Status>() {
    @Override
    public void onResult(@NonNull Status status) {
      if (!status.isSuccess()) {
        Log.d(TAG, "Fence could not be registered: " + status);
      }
    }
  });
}
 
Example #3
Source File: MainActivity.java    From android-play-awareness with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up {@link AwarenessFence}'s for the sample app, and registers callbacks for them
 * with a custom {@link BroadcastReceiver}
 */
private void setupFences() {
    // DetectedActivityFence will fire when it detects the user performing the specified
    // activity.  In this case it's walking.
    AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);

    // There are lots of cases where it's handy for the device to know if headphones have been
    // plugged in or unplugged.  For instance, if a music app detected your headphones fell out
    // when you were in a library, it'd be pretty considerate of the app to pause itself before
    // the user got in trouble.
    AwarenessFence headphoneFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN);

    // Combines multiple fences into a compound fence.  While the first two fences trigger
    // individually, this fence will only trigger its callback when all of its member fences
    // hit a true state.
    AwarenessFence walkingWithHeadphones = AwarenessFence.and(walkingFence, headphoneFence);

    // We can even nest compound fences.  Using both "and" and "or" compound fences, this
    // compound fence will determine when the user has headphones in and is engaging in at least
    // one form of exercise.
    // The below breaks down to "(headphones plugged in) AND (walking OR running OR bicycling)"
    AwarenessFence exercisingWithHeadphonesFence = AwarenessFence.and(
            headphoneFence,
            AwarenessFence.or(
                    walkingFence,
                    DetectedActivityFence.during(DetectedActivityFence.RUNNING),
                    DetectedActivityFence.during(DetectedActivityFence.ON_BICYCLE)));


    // Now that we have an interesting, complex condition, register the fence to receive
    // callbacks.

    // Register the fence to receive callbacks.
    Awareness.getFenceClient(this).updateFences(new FenceUpdateRequest.Builder()
            .addFence(FENCE_KEY, exercisingWithHeadphonesFence, mPendingIntent)
            .build())
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.i(TAG, "Fence was successfully registered.");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "Fence could not be registered: " + e);
                }
            });
}