Question

I'm trying to make a native extension for Android Vibration. Every open source extension I've found only utilizes the vibrate(duration) but not the vibrate(pattern, repeat) and vibrate.cancel()

I need to be able to pass a patten, and cancel functions.

I'm able to get vibration(duration) to work fine with no issues. There is a simple args[1].getAsInt(); method for me to use. But, there is not .getAsArray or .getAsLong.

I found something called FREArray, but i'm really not understanding how to use it.

So, my question is, with an AS3 function that I can pass an array (with the pattern) and an int (for the repeat count) How can I receive the array on the Android side? I'm not having any trouble receiving the int for the repeat.

Here's what i've I was working with so far, but due to the errors it looks like im not using it correctly. Any suggestions?

  public FREObject call(FREContext context, FREObject[] args) {


                VibrateExtensionContext vibrate(pattern, repeat)ExtensionContext) context;

                long[] pattern;
                int repeat = 0;

                try {
                    pattern = FREArray.newArray(args[0]); //this is where im stuck

                    //it would be ideal if there was something like this..

                    //pattern = args[0].getAsLong(); but this doesnt exist

                    repeat = args[1].getAsInt(); //this works fine
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FRETypeMismatchException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FREInvalidObjectException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FREWrongThreadException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                vibExtContext.androidVibrator.vibrate(pattern, repeat);

                return null;
            }
Was it helpful?

Solution

You only have access to either double or int, which are equivalent to the Number and int objects on the ActionScript side. To read in an array passed from the AS you'll need to do something like the following:

try
{
    FREArray array = (FREArray)args[0];

    long[] pattern = new long[(int)array.getLength()];
    for (int i = 0; i < array.getLength(); i++)
    {
        FREObject object = array.getObjectAt(i);
        double number = object.getAsDouble(); 

        // use number - assuming you are okay truncating the decimal points
        pattern[i] = (long)number;
    }       
}
catch (Exception e)
{
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top