com.google.android.gms.awareness.state.HeadphoneState Java Examples

The following examples show how to use com.google.android.gms.awareness.state.HeadphoneState. 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: SnapshotApiActivity.java    From android-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Check weather the headphones are plugged in or not? This is under snapshot api category.
 */
private void getHeadphoneStatus() {
    Awareness.SnapshotApi.getHeadphoneState(mGoogleApiClient)
            .setResultCallback(new ResultCallback<HeadphoneStateResult>() {
                @Override
                public void onResult(@NonNull HeadphoneStateResult headphoneStateResult) {
                    if (!headphoneStateResult.getStatus().isSuccess()) {
                        Toast.makeText(SnapshotApiActivity.this, "Could not get headphone state.", Toast.LENGTH_LONG).show();
                        return;
                    }
                    HeadphoneState headphoneState = headphoneStateResult.getHeadphoneState();

                    //display the status
                    TextView headphoneStatusTv = (TextView) findViewById(R.id.headphone_status);
                    headphoneStatusTv.setText(headphoneState.getState() == HeadphoneState.PLUGGED_IN ? "Plugged in." : "Unplugged.");
                }
            });
}
 
Example #2
Source File: StorableFenceManagerTest.java    From JCVD with MIT License 5 votes vote down vote up
@Test
public void testAddSucceed() {
    StorableFence fence = StorableHeadphoneFence.during(HeadphoneState.PLUGGED_IN);
    mManager.addFence("fenceId", fence, "");

    // when the gapi is connected, the fence should be added to the gapi
    // nothing in the stores should change
    assertThat(mAddedCalls, is(0));
    assertThat(mRemovedCalls, is(0));
    assertThat(mMockGapiFenceManager.addResultDict.containsKey("fenceId"), is(true));
    assertThat(mManager.mToAddStore.getAllFences().size(), is(1));
    assertThat(mManager.mToAddStore.getAllFences().get(0), is(fence));
    assertThat(mManager.mSyncedStore.getAllFences(), empty());
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(nullValue()));

    // when the fence is really added to the gapi, the fence should be placed in the syncedStore
    // and removed from the toAddStore
    // and the listener should also be called
    ResultCallback<Status> result = mMockGapiFenceManager.addResultDict.get("fenceId");
    result.onResult(new Status(CommonStatusCodes.SUCCESS));
    assertThat(mAddedCalls, is(1));
    assertThat(mRemovedCalls, is(0));
    assertThat(mManager.mToAddStore.getAllFences(), empty());
    assertThat(mManager.mSyncedStore.getAllFences().size(), is(1));
    assertThat(mManager.mSyncedStore.getAllFences().get(0).equals(fence), is(true));
    assertThat(mManager.mToRemoveStore.getAllFences(), empty());
    assertThat(mManager.getFence("fenceId"), is(fence));
}
 
Example #3
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 #4
Source File: StorableHeadphoneFenceTest.java    From JCVD with MIT License 5 votes vote down vote up
@Test
public void testValues() {
    StorableHeadphoneFence fence = StorableHeadphoneFence.during(HeadphoneState.PLUGGED_IN);
    assertThat(fence.getType(), is(StorableFence.Type.HEADPHONE));
    assertThat(fence.getTriggerType(), is(StorableHeadphoneFence.STATE));
    assertThat(fence.getHeadphoneState(), is(HeadphoneState.PLUGGED_IN));

    fence = StorableHeadphoneFence.pluggingIn();
    assertThat(fence.getType(), is(StorableFence.Type.HEADPHONE));
    assertThat(fence.getTriggerType(), is(StorableHeadphoneFence.PLUGGING_IN));

    fence = StorableHeadphoneFence.unplugging();
    assertThat(fence.getType(), is(StorableFence.Type.HEADPHONE));
    assertThat(fence.getTriggerType(), is(StorableHeadphoneFence.UNPLUGGING));
}
 
Example #5
Source File: StorableHeadphoneFenceTest.java    From JCVD with MIT License 5 votes vote down vote up
@Test
public void testEquals() {
    StorableHeadphoneFence fence1 = StorableHeadphoneFence.during(HeadphoneState.PLUGGED_IN);
    StorableHeadphoneFence fence2 = StorableHeadphoneFence.during(HeadphoneState.UNPLUGGED);
    StorableHeadphoneFence fence3 = StorableHeadphoneFence.pluggingIn();
    StorableHeadphoneFence fence4 = StorableHeadphoneFence.unplugging();
    StorableHeadphoneFence fence5 = StorableHeadphoneFence.unplugging();

    assertThat(fence1.equals(fence1), is(true));
    assertThat(fence1.equals(fence2), is(false));
    assertThat(fence2.equals(fence3), is(false));
    assertThat(fence3.equals(fence4), is(false));
    assertThat(fence3.equals(null), is(false));
    assertThat(fence4.equals(fence5), is(true));
}
 
Example #6
Source File: MainActivity.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
private void detectHeadphones() {
    Awareness.SnapshotApi.getHeadphoneState(mGoogleApiClient)
            .setResultCallback(new ResultCallback<HeadphoneStateResult>() {
                @Override
                public void onResult(@NonNull HeadphoneStateResult headphoneStateResult) {
                    HeadphoneState headphoneState = headphoneStateResult.getHeadphoneState();
                    if (headphoneState.getState() == HeadphoneState.PLUGGED_IN) {
                        Log.e("Tuts+", "Headphones are plugged in.");
                    } else {
                        Log.e("Tuts+", "Headphones are NOT plugged in.");
                    }
                }
            });
}
 
Example #7
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);
                }
            });
}