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

The following examples show how to use com.google.android.gms.awareness.fence.DetectedActivityFence. 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: StorableActivityFenceTest.java    From JCVD with MIT License 6 votes vote down vote up
@Test
public void testValues() {
    StorableActivityFence fence = StorableActivityFence.starting(
            DetectedActivityFence.IN_VEHICLE, DetectedActivityFence.RUNNING);
    int[] startActivities = {DetectedActivityFence.IN_VEHICLE, DetectedActivityFence.RUNNING};
    assertThat(fence.getType(), Matchers.is(StorableFence.Type.ACTIVITY));
    assertThat(fence.getActivityTypes(), is(startActivities));
    assertThat(fence.getTransitionType(), is(StorableActivityFence.START_TYPE));

    fence = StorableActivityFence.stopping(
            DetectedActivityFence.ON_BICYCLE, DetectedActivityFence.WALKING);
    int[] stopActivities = {DetectedActivityFence.ON_BICYCLE, DetectedActivityFence.WALKING};
    assertThat(fence.getType(), Matchers.is(StorableFence.Type.ACTIVITY));
    assertThat(fence.getActivityTypes(), is(stopActivities));
    assertThat(fence.getTransitionType(), is(StorableActivityFence.STOP_TYPE));

    fence = StorableActivityFence.during(
            DetectedActivityFence.ON_FOOT, DetectedActivityFence.STILL, DetectedActivityFence.UNKNOWN);
    int[] duringActivities = {DetectedActivityFence.ON_FOOT, DetectedActivityFence.STILL, DetectedActivityFence.UNKNOWN};
    assertThat(fence.getType(), Matchers.is(StorableFence.Type.ACTIVITY));
    assertThat(fence.getActivityTypes(), is(duringActivities));
    assertThat(fence.getTransitionType(), is(StorableActivityFence.DURING_TYPE));
}
 
Example #2
Source File: AwarenessMotionUpdatesProvider.java    From PrivacyStreams with Apache License 2.0 5 votes vote down vote up
@Override
    protected void provide() {
        Thread thread = Thread.currentThread();
        Thread.UncaughtExceptionHandler wrapped = thread.getUncaughtExceptionHandler();
        if (!(wrapped instanceof GoogleApiFixUncaughtExceptionHandler)) {
            GoogleApiFixUncaughtExceptionHandler handler = new GoogleApiFixUncaughtExceptionHandler(wrapped);
            thread.setUncaughtExceptionHandler(handler);
        }
//        Thread thread = Thread.currentThread();
//        thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
//            @Override
//            public void uncaughtException(Thread thread, Throwable throwable) {
//                System.out.println(thread.getName() + " throws exception: " + throwable);
//            }
//        });

            client = new GoogleApiClient.Builder(getContext())                              //Establish Connection
                    .addApi(Awareness.API)
                    .build();
            client.connect();
            walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);     //Create Fence
            onFootFence = DetectedActivityFence.during(DetectedActivityFence.ON_FOOT);
            runningFence = DetectedActivityFence.during(DetectedActivityFence.RUNNING);

            intent = new Intent(FENCE_RECEIVER_ACTION);                                     //Set up the intent and intent filter
            myFillter = new IntentFilter(FENCE_RECEIVER_ACTION);
            myPendingIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);           //Set up the pendingIntent
            myFenceReceiver = new FenceReceiver();                                              //Set up the receiver
            getContext().registerReceiver(myFenceReceiver, myFillter);
            registerFence(WALKINGFENCE, walkingFence);                                       //register the fences
            registerFence(TILTINGFENCE, tiltingFence);
            registerFence(ONFOOTFENCE, onFootFence);
            registerFence(RUNNINGFENCE, runningFence);
    }
 
Example #3
Source File: FenceChooserActivity.java    From JCVD with MIT License 5 votes vote down vote up
public void onWalkingClicked(View view) {
    if (mActivityType.contains(DetectedActivityFence.WALKING)) {
        mActivityType.remove(Integer.valueOf(DetectedActivityFence.WALKING));
        view.setBackgroundColor(Color.LTGRAY);
    } else {
        mActivityType.add(DetectedActivityFence.WALKING);
        view.setBackgroundColor(Color.WHITE);
    }
}
 
Example #4
Source File: FenceChooserActivity.java    From JCVD with MIT License 5 votes vote down vote up
public void onRunningClicked(View view) {
    if (mActivityType.contains(DetectedActivityFence.RUNNING)) {
        mActivityType.remove(Integer.valueOf(DetectedActivityFence.RUNNING));
        view.setBackgroundColor(Color.LTGRAY);
    } else {
        mActivityType.add(DetectedActivityFence.RUNNING);
        view.setBackgroundColor(Color.WHITE);
    }
}
 
Example #5
Source File: FenceChooserActivity.java    From JCVD with MIT License 5 votes vote down vote up
public void onDrivingClicked(View view) {
    if (mActivityType.contains(DetectedActivityFence.IN_VEHICLE)) {
        mActivityType.remove(Integer.valueOf(DetectedActivityFence.IN_VEHICLE));
        view.setBackgroundColor(Color.LTGRAY);
    } else {
        mActivityType.add(DetectedActivityFence.IN_VEHICLE);
        view.setBackgroundColor(Color.WHITE);
    }
}
 
Example #6
Source File: FenceChooserActivity.java    From JCVD with MIT License 5 votes vote down vote up
public void onBicyclingClicked(View view) {
    if (mActivityType.contains(DetectedActivityFence.ON_BICYCLE)) {
        mActivityType.remove(Integer.valueOf(DetectedActivityFence.ON_BICYCLE));
        view.setBackgroundColor(Color.LTGRAY);
    } else {
        mActivityType.add(DetectedActivityFence.ON_BICYCLE);
        view.setBackgroundColor(Color.WHITE);
    }
}
 
Example #7
Source File: FenceChooserActivity.java    From JCVD with MIT License 5 votes vote down vote up
public void onStillClicked(View view) {
    if (mActivityType.contains(DetectedActivityFence.STILL)) {
        mActivityType.remove(Integer.valueOf(DetectedActivityFence.STILL));
        view.setBackgroundColor(Color.LTGRAY);
    } else {
        mActivityType.add(DetectedActivityFence.STILL);
        view.setBackgroundColor(Color.WHITE);
    }
}
 
Example #8
Source File: FenceChooserActivity.java    From JCVD with MIT License 5 votes vote down vote up
public void onFootClicked(View view) {
    if (mActivityType.contains(DetectedActivityFence.ON_FOOT)) {
        mActivityType.remove(Integer.valueOf(DetectedActivityFence.ON_FOOT));
        view.setBackgroundColor(Color.LTGRAY);
    } else {
        mActivityType.add(DetectedActivityFence.ON_FOOT);
        view.setBackgroundColor(Color.WHITE);
    }
}
 
Example #9
Source File: StorableActivityFence.java    From JCVD with MIT License 5 votes vote down vote up
@RequiresPermission("com.google.android.gms.permission.ACTIVITY_RECOGNITION")
@Override
AwarenessFence getAwarenessFence(Context ctx) {
    switch (mTransitionType) {
        case DURING_TYPE:
            return DetectedActivityFence.during(mActivityTypes);
        case START_TYPE:
            return DetectedActivityFence.starting(mActivityTypes);
        case STOP_TYPE:
            return DetectedActivityFence.stopping(mActivityTypes);
    }
    return null;
}
 
Example #10
Source File: FenceStoreTest.java    From JCVD with MIT License 5 votes vote down vote up
@Before
public void setUp() {
    mStore = new FenceStore(mContext, "test");
    mAct1 = StorableActivityFence.starting(DetectedActivityFence.IN_VEHICLE);
    mAct2 = StorableActivityFence.during(DetectedActivityFence.RUNNING);
    mLoc1 = StorableLocationFence.entering(2, 3, 30);
    mLoc2 = StorableLocationFence.exiting(3, 4, 40);
    mAnd = StorableFence.and(mAct1, mAct2);
    mNot = StorableFence.or(mLoc1);
    mOr = StorableFence.or(mNot, mAnd, mLoc2);
}
 
Example #11
Source File: StorableFenceTest.java    From JCVD with MIT License 5 votes vote down vote up
@Before
public void setUp() {
    mAct1 = StorableActivityFence.starting(DetectedActivityFence.IN_VEHICLE);
    mAct1.setPendingIntentClass("className");
    mAct2 = StorableActivityFence.during(DetectedActivityFence.RUNNING);
    mLoc1 = StorableLocationFence.entering(2, 3, 30);
    mLoc1.setId("1");
    mLoc2 = StorableLocationFence.exiting(3, 4, 40);
    mHead1 = StorableHeadphoneFence.during(HeadphoneState.PLUGGED_IN);
    mTime1 = StorableTimeFence.inFridayInterval(TimeZone.getDefault(), 20, 20000);

}
 
Example #12
Source File: StorableActivityFenceTest.java    From JCVD with MIT License 5 votes vote down vote up
@Test
public void testEquals() {
    StorableActivityFence fence1 = StorableActivityFence.starting(
            DetectedActivityFence.IN_VEHICLE, DetectedActivityFence.RUNNING);
    StorableActivityFence fence2 = StorableActivityFence.stopping(
            DetectedActivityFence.ON_BICYCLE, DetectedActivityFence.WALKING);
    StorableActivityFence fence3 = StorableActivityFence.stopping(
            DetectedActivityFence.ON_BICYCLE, DetectedActivityFence.WALKING);

    assertThat(fence1.equals(fence1), is(true));
    assertThat(fence2.equals(fence3), is(true));
    assertThat(fence3.equals(null), is(false));
    assertThat(fence3.equals(fence1), is(false));
}
 
Example #13
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 #14
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);
                }
            });
}
 
Example #15
Source File: FenceRecyclerAdapter.java    From JCVD with MIT License 4 votes vote down vote up
public String getFenceStr(StorableFence fence) {
    String str = "";
    switch (fence.getType()) {
        case ACTIVITY:
            for (@StorableActivityFence.ActivityType int act : ((StorableActivityFence) fence).getActivityTypes()) {
                if (!str.isEmpty()) {
                    str += ", ";
                }
                switch (act) {
                    case DetectedActivityFence.IN_VEHICLE:
                        str += "IN_VEHICLE";
                        break;
                    case DetectedActivityFence.ON_BICYCLE:
                        str += "ON_BICYCLE";
                        break;
                    case DetectedActivityFence.ON_FOOT:
                        str += "ON_FOOT";
                        break;
                    case DetectedActivityFence.RUNNING:
                        str += "RUNNING";
                        break;
                    case DetectedActivityFence.STILL:
                        str += "STILL";
                        break;
                    case DetectedActivityFence.WALKING:
                        str += "WALKING";
                        break;
                    case DetectedActivityFence.UNKNOWN:
                    default:
                        str += "UNKNOWN";
                        break;
                }
            }
            break;
        case LOCATION:
            StorableLocationFence locFence = (StorableLocationFence) fence;
            str += "(" + locFence.getLatitude() + ", " + locFence.getLongitude() + ") ";
            break;
        default:
            break;
    }
    return str;
}
 
Example #16
Source File: MainActivity.java    From AndroidDemoProjects with Apache License 2.0 4 votes vote down vote up
private void createFence() {
    checkLocationPermission();

    AwarenessFence activityFence = DetectedActivityFence.during(DetectedActivityFence.STILL);
    AwarenessFence homeFence = LocationFence.in(39.92, -105.7, 100000, 1000 );

    AwarenessFence sittingAtHomeFence = AwarenessFence.and(homeFence, activityFence);

    Intent intent = new Intent(ACTION_FENCE);
    PendingIntent fencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    mFenceBroadcastReceiver = new FenceBroadcastReceiver();
    registerReceiver(mFenceBroadcastReceiver, new IntentFilter(ACTION_FENCE));

    FenceUpdateRequest.Builder builder = new FenceUpdateRequest.Builder();
    builder.addFence(KEY_SITTING_AT_HOME, sittingAtHomeFence, fencePendingIntent);

    Awareness.FenceApi.updateFences( mGoogleApiClient, builder.build() );
}