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

Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202331460267570004&fid=0101188387844930001

Introduction

HMS Ambient Light Awareness is used to obtain the illuminance and to set barriers based on the illuminance. We can use this features in photography applications, auto flash light applications etc.

This article provides information about main classes and methods used for HMS Ambient Light Awareness.

AmbientLightBarrier

This class features, barriers to be triggered while light intensity is below, above or in the specified range.

Following are the methods in AmbientLightBarrier class.

range

below

above

All these methods will return Awareness barrier object.

range

When the illuminance is within the range specified by [minLightIntensity, maxLightIntensity), the barrier status is TRUE and a barrier event is reported. When the illuminance is not within the range, the barrier status is FALSE and a barrier event is reported.

Syntax:

public static AwarenessBarrier range(float minLightIntensity, float maxLightIntensity)

//define lux range here
final float minLightIntensity = 40.0f;
final float maxLightIntensity =60.0f;

//define the barrier
AwarenessBarrier lightRangeBarrier = AmbientLightBarrier.range(minLightIntensity,maxLightIntensity);

//define the label for the barrier and add the barrier
String lightBarrierLabel = "light range barrier";

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

It will Throws IllegalArgumentException when the value of minLightIntensity is less than 0, or the value of MinLightIntensity is greater than that of maxLightIntensity, or the value of maxLightIntensity is greater than that of Float.MAX_VALUE.

below

When the illuminance is within the range specified by [0, maxLightIntensity), the barrier status is TRUE and a barrier event is reported. When the illuminance is not within the range, the barrier status is FALSE and a barrier event is reported.

Syntax:

public static AwarenessBarrier below(float maxLightIntensity)

//define lux here
final float maxLightIntensity = 50.0f;

//define the barrier
AwarenessBarrier lightBelowBarrier = AmbientLightBarrier.below(maxLightIntensity);

//define the label for the barrier and add the barrier
String lightBarrierLabel = "light below barrier";

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

It will throw an IllegalArgumentException when the value of maxLightIntensityis less than 0 or the value of maxLightIntensityis greater than that of Float.MAX_VALUE.

above

When the illuminance is within the range specified by [minLightIntensity, Float.MAX_VALUE), the barrier status is TRUE and a barrier event is reported. When the illuminance is not within the range, the barrier status is FALSE and a barrier event is reported.

Syntax:

public static AwarenessBarrier above(float minLightIntensity)

//define lux here
final float minLightIntensity = 40.0f;

//define the barrier
AwarenessBarrier lightAboveBarrier = AmbientLightBarrier.above(minLightIntensity);

//define the label for the barrier and add the barrier
String lightBarrierLabel = "light above barrier";

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

It will throws IllegalArgumentException when the value of minLightIntensity is less than 0, or the value of minLightIntensity is greater than that of Float.MAX_VALUE.

AmbientLightResponse

This class provide the response to the request for obtaining the the illuminance status. We can use the getAmbientLightStatus method provided by CaptureClient to obtain illuminance status.

getAmbientLightStatus

This method is used to obtains the illuminance status. The value of AmbientLightStauts contains the illuminance in lux.

Syntax:

public AmbientLightStatus getAmbientLightStatus()

private void getLightIntensity() {
Awareness.getCaptureClient(this).getLightIntensity()
.addOnSuccessListener(new OnSuccessListener<AmbientLightResponse>() {
@Override
public void onSuccess(AmbientLightResponse ambientLightResponse) {
AmbientLightStatus ambientLightStatus = ambientLightResponse.getAmbientLightStatus();
ambientLight_capture.setTextColor(getColor(R.color.green));
ambientLight_capture.setText("Light intensity is " + ambientLightStatus.getLightIntensity() + " lux");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
ambientLight_capture.setTextColor(getColor(R.color.red));
ambientLight_capture.setText("Failed to get the light intensity.");
}
});
}

Output

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200818153022.68609079062307862785763141804664:50510901064137:2800:7F5478C44D211C0EFE30D5962E16A1E1F03D6741549C68162C74348B73A490B5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/745/226/2640091000001745226.20200818153022.68609079062307862785763141804664:50510901064137:2800:7F5478C44D211C0EFE30D5962E16A1E1F03D6741549C68162C74348B73A490B5.png[/IMG]

Reference

https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/awareness-ambientlight-dev