air-android-utils icon indicating copy to clipboard operation
air-android-utils copied to clipboard

Utility extension for Adobe AIR

AIRAndroidUtils | Utility extension for Adobe AIR

Simple native extension offering several utility methods for Android platform:

  • Getting full screen width & height of the device
  • Getting DisplayMetrics
  • Getting system version
  • Setting UI visibility flags
  • Setting brightness
  • Setting status bar color

Getting started

Just copy the extension from the bin directory over to your project and modify your app descriptor XML with the extension's ID:

<extensions>
    <extensionID>com.marpies.ane.androidutils</extensionID>
</extensions>

Code snippets

Logs

// If desired, enable logging before calling any API
AIRAndroidUtils.logEnabled = true;

Full screen width & height

trace( AIRAndroidUtils.screenWidth );
trace( AIRAndroidUtils.screenHeight );

DisplayMetrics

const dm:AndroidDisplayMetrics = AIRAndroidUtils.displayMetrics;
trace( dm.density );
trace( dm.densityDpi );
trace( dm.widthPixels );
trace( dm.heightPixels );
trace( dm.xdpi );
trace( dm.ydpi );

System version

if( AIRAndroidUtils.systemVersion >= AndroidVersionCodes.KITKAT ) {
    // Use KITKAT's API
}

UI visibility flags

// Immersive full screen
if( AIRAndroidUtils.isImmersiveFullScreenSupported ) {
    AIRAndroidUtils.setUIVisibility(
        AndroidUIFlags.SYSTEM_UI_FLAG_HIDE_NAVIGATION | // Hides software buttons
        AndroidUIFlags.SYSTEM_UI_FLAG_FULLSCREEN | // Hides status bar
        AndroidUIFlags.SYSTEM_UI_FLAG_IMMERSIVE_STICKY // Enters immersive full screen
    );
}

// Temporarily hide the software buttons (appear on touch)
AIRAndroidUtils.setUIVisibility( AndroidUIFlags.SYSTEM_UI_FLAG_HIDE_NAVIGATION );

See the class AndroidUIFlags.as for available UI flags.

Brightness

// Use values from 0.0 to 1.0
AIRAndroidUtils.setBrightness( Math.random() );

// Use user's preferences
AIRAndroidUtils.setBrightness( -1 );

Status bar color

if( AIRAndroidUtils.isStatusBarColorSupported ) {
    // Color in the format #RRGGBB or #AARRGGBB, or one of the following values: red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray, darkgray.
    AIRAndroidUtils.setStatusBarColor( "#FF0000" );
}

UI visibility listener

You can track changes to the system UI by enabling the UI visibility listener. This is helpful when you initially set your custom UI flags and then for example present a native UI dialog. After closing the dialog, the UI flags would reset to the AIR default settings and your custom UI flags would no longer be working.

AIRAndroidUtils.addEventListener(AndroidUIVisibilityEvent.CHANGE, onUIVisibilityChanged);
AIRAndroidUtils.enableUIVisibilityListener(true);

private function onUIVisibilityChanged(event:AndroidUIVisibilityEvent):void
{
    // The UI flags have been reset to the AIR default value, set custom after a small delay
    if( event.uiVisibility == AndroidUIFlags.SYSTEM_UI_FLAG_VISIBLE ) {
        setTimeout(setCustomUIFlags, 2000);
    }
}

Display cutouts

To render your app's content within the device's cutout area, use the setCutoutMode method. Note you will need to set <fullScreen>true</fullScreen> in your app descriptor XML for this to work.

import com.marpies.ane.androidutils.data.CutoutMode;

...

AIRAndroidUtils.setCutoutMode(CutoutMode.SHORT_EDGES);

To restore cutout mode, use:

AIRAndroidUtils.setCutoutMode(CutoutMode.DEFAULT);

To get information about the device's cutout areas, use the displayCutoutRects getter. Note it will return null if the cutout mode is NOT CutoutMode.SHORT_EDGES.

var areas:Vector.<Rectangle> = AIRAndroidUtils.displayCutoutRects;
if (areas != null) {
    trace(areas);
}

Build ANE

ANT build scripts are available in the build directory. Edit build.properties to correspond with your local setup.

Author

The ANE has been written by Marcel Piestansky and is distributed under Apache License, version 2.0.

Changelog

November 27, 2020 (v1.2.1)

  • Added CutoutMode.NEVER
  • Renamed CutoutMode.NONE to CutoutMode.DEFAULT

November 21, 2020 (v1.2.0)

  • Added setCutoutMode method
  • Added displayCutoutRects getter

August 25, 2019 (v1.1.0)

  • Added support for Android 64 bit
  • Changed names of the following classes:
    • AIRAndroidUtilsDisplayMetricsAndroidDisplayMetrics
    • AIRAndroidUtilsUIFlagsAndroidUIFlags
    • AIRAndroidUtilsVersionCodesAndroidVersionCodes
  • Added setTranslucentNavigation method
  • Added enableUIVisibilityListener method

November 2, 2015 (v1.0.0)

  • Public release