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

Introduction:

Location awareness is used to get the location details and to set barriers based on the location.

Barrier API

This API is used to set a location barrier based on the target location, radius (not smaller than 200 meters), and stay duration.

For example, if you set a circular region with a 300 meter radius in the target location as a barrier, a notification will be triggered if the user has stayed in the area for the specified period of time.

Capture API

We can use the Capture API to obtain the latitude and longitude of the current location.

For calling Location Awareness capability we have to assign the given permissions in the manifest file.

<!-- Location permission, which is sensitive. After the permission is declared, you need to dynamically apply for it in the code. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- If your app uses the barrier capability and runs on Android 10 or later, you need to add the background location access permission, which is also a sensitive permission. -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

To understand more about Barrier API and Capture API here we can discuss about the following two classes:

LocationBarrier

LocationResponse

Let's go to the details.

LocationBarrier

This class features, barriers to be triggered while enter, exit and stay in a specified location.

Following are the methods in LocationBarrier class.

enter

exit

stay

1) enter

After this barrier is added, when a user enters a specified area, the barrier status is TRUE and a barrier event is reported. After 5 seconds, the barrier status changes to FALSE.

Syntax:

public static AwarenessBarrier enter(double latitude, double longitude, double radius)

Parameters

private void add_enterBarrier(Context context) {
//Home latitude and longitude
double latitude = Utils.getHomeLatitude();
double longitude = Utils.getHomeLongitude();
double radius = 300;

//define the barrier
AwarenessBarrier enterBarrier = LocationBarrier.enter(latitude, longitude, radius);

//define the label for the barrier and add the barrier
String locationBarrierLabel = "enter barrier";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(locationBarrierLabel, enterBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add enter barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add enter barrier failed", Toast.LENGTH_SHORT).show();
}
});
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200716181814.06898309238623696569409711815722:50510724012303:2800:67E9DD8A9D13123B6DA04F7D3F635E8F235D77F048BF27884BBE7F6430B19C2D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200716181814.06898309238623696569409711815722:50510724012303:2800:67E9DD8A9D13123B6DA04F7D3F635E8F235D77F048BF27884BBE7F6430B19C2D.png[/IMG]​

2) exit

After this barrier is added, when a user exits from a specified area, the barrier status is TRUE and a barrier event is reported. After 5 seconds, the barrier status changes to FALSE.

Syntax:

public static AwarenessBarrier exit(double latitude, double longitude, double radius)

private void add_exitBarrier(Context context) {
//Home latitude and longitude
double latitude = Utils.getHomeLatitude();
double longitude = Utils.getHomeLongitude();
double radius = 300;

//define the barrier
AwarenessBarrier exitBarrier = LocationBarrier.exit(latitude, longitude, radius);

//define the label for the barrier and add the barrier
String locationBarrierLabel = "exit barrier";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(locationBarrierLabel, exitBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add exit barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add exit barrier failed", Toast.LENGTH_SHORT).show();
}
});
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200716181907.33813165004898423627390110572874:50510724012303:2800:9E5052541D82058B879D6108848A2789C64BF714399D6548ABE05656C90418E5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200716181907.33813165004898423627390110572874:50510724012303:2800:9E5052541D82058B879D6108848A2789C64BF714399D6548ABE05656C90418E5.png[/IMG]​

3) stay

After this barrier is added, if a user is in the area, the barrier status is TRUE; if a user enters the area and stays in the area for a specified time period, the barrier status is TRUE and a barrier event is reported.

Syntax:

public static AwarenessBarrier stay(double latitude, double longitude, double radius, long timeOfDuration)

private void add_stayBarrier(Context context) {
//Home latitude and longitude
double latitude = Utils.getHomeLatitude();
double longitude = Utils.getHomeLongitude();
double radius = 300;

//define the barrier
AwarenessBarrier stayBarrier = LocationBarrier.stay(latitude, longitude, radius,2000);

//define the label for the barrier and add the barrier
String locationBarrierLabel = "stay barrier";

//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(locationBarrierLabel, stayBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add stay barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add stay barrier failed", Toast.LENGTH_SHORT).show();
}
});
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200716181947.43808036284232712176553418952025:50510724012303:2800:254638951887DA6AD57896F9CA29571E219FC41EBB97FD21AFC831968BD988CB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200716181947.43808036284232712176553418952025:50510724012303:2800:254638951887DA6AD57896F9CA29571E219FC41EBB97FD21AFC831968BD988CB.png[/IMG]​

LocationResponse:

This class provides response to the location request. The app can call the getLocation method provided by CaptureClient to obtain the last geographical location response.

Public Methods:

There is one method in LocationResponse class.

getLocation

1)getLocation

This method is used to gets the location information, including the current longitude and latitude of the device.

Syntax:

public Location getLocation()

private void getLocation() {
Awareness.getCaptureClient(this).getLocation()
.addOnSuccessListener(new OnSuccessListener<LocationResponse>() {
@Override
public void onSuccess(LocationResponse locationResponse) {
Location location = locationResponse.getLocation();
Utils.setHomeLatitude(location.getLatitude());
Utils.setHomeLongitude(location.getLongitude());
location_details_capture.setText("Capture Your Location\nLongitude:" + location.getLongitude()
+ "\nLatitude:" + location.getLatitude());

}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
location_details_capture.setText("Failed to get the location.");
}
});
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200716182022.29258830281905844250270989035754:50510724012303:2800:C4370D4B5CAD87DB1EF70A63FA95299FDE37AA781AC4D81DA7F2C99AA4AE71F9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200716182022.29258830281905844250270989035754:50510724012303:2800:C4370D4B5CAD87DB1EF70A63FA95299FDE37AA781AC4D81DA7F2C99AA4AE71F9.png[/IMG]​

References:

https://developer.huawei.com/consumer/en/doc/awareness-barrier-locationbarrier

https://developer.huawei.com/consumer/en/doc/awareness-capture-locationresponse