[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

HMS support different type of awareness like Headset, Location, Time, Behavior etc. In some real-time scenarios we may have to use multiple awareness simultaneously and barrier logic operation is used to integrate these barriers.

For example, we can detect that a person is connecting a headset while running or he is in the beach during sunset.

This article explains the integration of multiple barriers through different logic operations. For that we have to know more about AwarenessBarrier class.

Awareness Barrier

AwarenessBarrier is Barrier basic class, which supports barrier logic operations.

Public Methods:

The following are the public methods in AwarenessBarrier class

public static AwarenessBarrier and(AwarenessBarrier… barriers)

public static AwarenessBarrier and(Collection<AwarenessBarrier> barriers)

public static AwarenessBarrier not(AwarenessBarrier barrier)

public static AwarenessBarrier or(AwarenessBarrier… barriers)

public static AwarenessBarrier or(Collection<AwarenessBarrier> barriers)

Let us go to the details.

public static AwarenessBarrier and(AwarenessBarrier… barriers)

This method contain barrier set as parameter and returns the integrated barriers after the AND logic operation.

Example: Here the integrated barrier will detect whether the user is on the beach during sunrise.

//Logical AND Operation

//define here the Barrier stay in beach
AwarenessBarrier stayBeachBarrier = LocationBarrier.stay(beachLatitude, beachLongitude, radius,2000);

//define here the Barrier in sunrise period
AwarenessBarrier inSunrisePeriodBarrier = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNRISE_CODE,
5* oneHourMilliSecond, 7 * oneHourMilliSecond);

//and operation integrate barriers and detect whether the user is on the beach during sunrise.
AwarenessBarrier beachANDsunrise= AwarenessBarrier.and(stayBeachBarrier,inSunrisePeriodBarrier);

//define the label for the barrier and add the barrier
String barrierLabel = "beach and sunrise";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, beachANDsunrise ,pendingIntent).build();

public static AwarenessBarrier and(Collection<AwarenessBarrier> barriers)

This method contains Collection which consist of barrier set as a parameter and returns the integrated barriers after the AND logic Operation.

Example: Here the integrated barrier will detect whether the user is on the beach during sunrise.

//Logical AND Operation with collection

//define here the Barrier stay in beach
AwarenessBarrier stayBeachBarrier = LocationBarrier.stay(beachLatitude, beachLongitude, radius,2000);

//define here the Barrier in sunrise period
AwarenessBarrier inSunrisePeriodBarrier = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNRISE_CODE,
5* oneHourMilliSecond, 7 * oneHourMilliSecond);

//and operation integrate barriers and detect whether the user is on the beach during sunrise.
Collection<AwarenessBarrier> collection=new ArrayList<AwarenessBarrier>();
collection.add(stayBeachBarrier);
collection.add(inSunrisePeriodBarrier);

// collection and operation
AwarenessBarrier beachANDsunrise=AwarenessBarrier.and(collection);

//define the label for the barrier and add the barrier
String barrierLabel = "beach and sunrise";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, beachANDsunrise ,pendingIntent).build();

public static AwarenessBarrier not(AwarenessBarrier barrier)

This method contains barrier as a parameter and returns the barrier after the NOT logic operation.

Example: Here the logical NOT is using light intensity above 60 lux and will detect the whether the light intensity is below 60 lux or not.

//Logical NOT Operation

//define lux here
final float luxValue = 60.0f;

//define the barrier
AwarenessBarrier intensityAbove = AmbientLightBarrier.above(luxValue);

//NOT operation above barrier.
AwarenessBarrier NOTintensityAbove= AwarenessBarrier.not(intensityAbove);

//define the label for the barrier and add the barrier
String barrierLabel = "NOT Intensity Above";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, NOTintensityAbove ,pendingIntent).build();

public static AwarenessBarrier or(AwarenessBarrier… barriers)

This method contain barrier set as parameter and returns the integrated barrier after the OR logic operation.

Example: Here the integrated barrier will detect whether the user is walking or connected headset.

//Logical OR Operation

//define here the Barrier head is is keeping connected
AwarenessBarrier headsetKeepingConnected = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED);

//define here the Barrier user is walking
AwarenessBarrier keepingWalking = AwarenessBarrier.and(BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_WALKING));

//and operation integrate barriers and detect whether he is walking or connected headset.
AwarenessBarrier headsetConnectedORwalking= AwarenessBarrier.or(headsetKeepingConnected,keepingWalking);

//define the label for the barrier and add the barrier
String barrierLabel = "headset connecting and walking";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, headsetConnectedORwalking ,pendingIntent).build();

public static AwarenessBarrier or(Collection<AwarenessBarrier> barriers)

This method contains Collection which consist of barrier set as a parameter and returns the integrated barrier after the OR logic operation.

Example: Here the integrated barrier will detect whether the user is walking or connected headset.

//Logical OR Operation with Collection

//define here the Barrier head is is keeping connected
AwarenessBarrier headsetKeepingConnected = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED);

//define here the Barrier user is walking
AwarenessBarrier keepingWalking = AwarenessBarrier.and(BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_WALKING));

//and operation integrate barriers and detect whether he is walking or connected headset.
Collection<AwarenessBarrier> collection=new ArrayList<AwarenessBarrier>();
collection.add(headsetKeepingConnected);
collection.add(keepingWalking);

// Collection 'or' operation
AwarenessBarrier headsetConnectedORwalking=AwarenessBarrier.or(collection);

//define the label for the barrier and add the barrier
String barrierLabel = "headset connecting and walking";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, headsetConnectedORwalking ,pendingIntent).build();

Sample:

Let us consider a real-time scenario to detect whether a person is walking on the beach during sunset or sunrise.

public class BarrierIntegrationActivity extends AppCompatActivity {
private static final String TAG ="BarrierIntegration" ;
private TextView barrier_status,sunrise_sunset_txt,beach_txt,walking_txt;
private ImageView sunrise_sunset_img,beach_img,walking_img;
PendingIntent pendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barrier_integration);

barrier_status=findViewById(R.id.barrier_status);
sunrise_sunset_img=findViewById(R.id.sunrise_sunset_img);
beach_img=findViewById(R.id.beach_img);
walking_img=findViewById(R.id.walking_img);
sunrise_sunset_txt=findViewById(R.id.sunrise_sunset_txt);
beach_txt=findViewById(R.id.beach_txt);
walking_txt=findViewById(R.id.walking_txt);

// define PendingIntent that will be triggered upon a barrier status change.
final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "BARRIER_RECEIVER_ACTION";
Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
BehaviorBarrierReceiver barrierReceiver = new BehaviorBarrierReceiver();
registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
addbarrier(this);
}

private void addbarrier(Context context) {

double beachLatitude = Utils.getHomeLatitude();
double beachLongitude = Utils.getHomeLongitude();

double radius = 300;
long oneHourMilliSecond = 60 * 60 * 1000L;

//Logical AND Operation.

//define here the Barrier in sunrise period
AwarenessBarrier inSunrisePeriodBarrier = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNRISE_CODE,
5* oneHourMilliSecond, 7 * oneHourMilliSecond);
//define here the Barrier in sunrise period
AwarenessBarrier inSunsetPeriodBarrier = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNSET_CODE,
17* oneHourMilliSecond, 19* oneHourMilliSecond);

//and operation integrate barriers and detect whether it is in sunrise or sunset.
AwarenessBarrier sunriseORsunset= AwarenessBarrier.or(inSunrisePeriodBarrier,inSunsetPeriodBarrier);

//define here the Barrier user is walking
AwarenessBarrier keepingWalking = AwarenessBarrier.and(BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_WALKING));

//define here the Barrier stay in beach
AwarenessBarrier stayBeachBarrier = LocationBarrier.stay(beachLatitude, beachLongitude, radius,2000);

//and operation integrate barriers and detect whether the user is walking on the beach during sun rise or sunset.
AwarenessBarrier walkingBeachInSunriseorSunset= AwarenessBarrier.and(keepingWalking,stayBeachBarrier,sunriseORsunset);

//define the label for the barrier and add the barrier
String barrierLabel = "walking on the beach during sunrise or sunset";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, walkingBeachInSunriseorSunset ,pendingIntent).build();

Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
Log.e(TAG, "add barrier failed", e);
}
});
}

// define the broadcast receiver to listen for the barrier event.
class BehaviorBarrierReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BarrierStatus barrierStatus = BarrierStatus.extract(intent);
String label = barrierStatus.getBarrierLabel();
switch(barrierStatus.getPresentStatus()) {
case BarrierStatus.TRUE:
Log.i(TAG, label + " status:true");
barrier_status.setText("Status: TRUE" );
sunrise_sunset_img.setImageDrawable(getDrawable(R.drawable.sunrise_sunset));
beach_img.setImageDrawable(getDrawable(R.drawable.beach));
walking_img.setImageDrawable(getDrawable(R.drawable.beach_walk));
sunrise_sunset_txt.setText("Sunset");
beach_txt.setText("Beach");
walking_txt.setText("Walking");
break;
case BarrierStatus.FALSE:
Log.i(TAG, label + " status:false");
// barrier_status.setText("Status: FALSE" );
break;
case BarrierStatus.UNKNOWN:
Log.i(TAG, label + " status:unknown");
break;
}
}
}
}

Output:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200724122447.58391185065282776996340644453102:50510806011956:2800:6079928281AB800F331BB089692595672C6612C6226C9E0ECAE21E1D0FAA9343.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200724122447.58391185065282776996340644453102:50510806011956:2800:6079928281AB800F331BB089692595672C6612C6226C9E0ECAE21E1D0FAA9343.png[/IMG]​

Reference:

https://developer.huawei.com/consumer/en/doc/development/HMS-References/awareness-barrier-awarenessbarrier