Hi all, I’m new to Fuse and just to get started with Uno and Foreign Code I thought of making a simple app which turns on and off the flashlight. I’m starting with Android and until now I only can ask for permissions and check if the device has a camera. I’m hoping someone could give me some tips how to do this or tell me what I’m doing wrong.
My uno code:
´´´
public class Flashlight : NativeModule
{
bool isFlashlightOn = false;
bool permissionFlashlight = false;
public Flashlight(){
if defined(Android){
//Ask for permissions
Permissions.Request(Permissions.Android.CAMERA).Then(PermissionAccepted, PermissionRejected);
}
//Members
AddMember(new NativeFunction("getPermissionStatus", (NativeCallback)GetPermissionStatus));
AddMember(new NativeFunction("hasFlashlight", (NativeCallback)HasFlashlight));
AddMember(new NativeFunction("turnFlashlightOn", (NativeCallback)TurnFlashlightOn));
AddMember(new NativeFunction("turnFlashlightOff", (NativeCallback)TurnFlashlightOff));
}
//If permission was accepted
private void PermissionAccepted(PlatformPermission permission){
permissionFlashlight = true;
debug_log "Permited: " + permission;
}
//If permission was denied
private void PermissionRejected(Exception reason){
permissionFlashlight = false;
debug_log "Error: " + reason.Message;
}
//Get the status of the permissions. true if accepted, otherwise false
object GetPermissionStatus(Context c, object[] args){
return permissionFlashlight;
}
//Check if the device supports flashlight
object HasFlashlight(Context c, object[] args){
return HasFlashlight();
}
//Turn the flashlight on
object TurnFlashlightOn(Context c, object[] args){
TurnFlashlightOn();
return null;
}
//Turn the flashlight off
object TurnFlashlightOff(Context c, object[] args){
TurnFlashlightOff();
return null;
}
[Foreign(Language.Java)]
static extern(Android) bool HasFlashlight()
@{
PackageManager pm = com.fuse.Activity.getRootActivity().getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
@}
[Foreign(Language.Java)]
static extern(Android) void TurnFlashlightOn()
@{
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
@}
[Foreign(Language.Java)]
static extern(Android) void TurnFlashlightOff()
@{
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
cam.stopPreview();
@}
static extern(!(iOS||Android)) bool HasFlashlight(){
return false;
}
static extern(!(iOS||Android)) void TurnFlashlightOn(){
return false;
}
static extern(!(iOS||Android)) void TurnFlashlightOff(){
return false;
}
}
´´´
(sorry for the formatting, dunno what happened.)