Query for fence state

To query for the current state of a fence, call FenceClient.queryFences() and pass the fence key for the fence to query.

The following example calls FenceClient.queryFences() to get a FenceStateMap, and then uses the FenceStateMap to return a FenceState value to show the current state, previous state, and the time when the fence was last updated:

protected void queryFence(final String fenceKey) {
    Awareness.getFenceClient(this)
        .queryFences(FenceQueryRequest.forFences(Arrays.asList(fenceKey)))
        .addOnSuccessListener(new OnSuccessListener<FenceQueryResponse>() {
            @Override
            public void onSuccess(FenceQueryResponse response) {
                FenceStateMap map = response.getFenceStateMap();
                for (String fenceKey : map.getFenceKeys()) {
                    FenceState fenceState = map.getFenceState(fenceKey);
                    Log.i(TAG, "Fence " + fenceKey + ": "
                            + fenceState.getCurrentState()
                            + ", was="
                            + fenceState.getPreviousState()
                            + ", lastUpdateTime="
                            + DATE_FORMAT.format(
                            new Date(fenceState.getLastFenceUpdateTimeMillis())));
                }
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Could not query fence: " + fenceKey);
                return;
            }
        });
}