Search code examples
actionscript-3airmatrixbitmapcache-control

ActionScript / AIR - Determine Device Profile At Runtime?


i'm developing an application for both desktop and mobile devices and would like to use the same code base for each build.

i want to employ cacheAsBitmapMatrix on some of my display objects, but cacheAsBitmapMatrix throws an error if it's included in an AIR application with a device profile other than mobileDevice or extendedMobileDevice.

something like the following would be ideal:

if (cacheAsBitmapMatrix.isSupported)
   myDisplayObject.cacheAsBitmapMatrix = new Matrix();

update using try/catch:

try                {myDisplayObject.cacheAsBitmapMatrix = new Matrix();}
catch(error:Error) {}
finally            {myDisplayObject.cacheAsBitmap = true;}

update:

except for television profiles, this should work as well to distinguish between mobile and desktop:

//Resoslve Profile
if  (Capabilities.os.indexOf("Windows") > -1 || Capabilities.os.indexOf("Mac") > -1 || Capabilities.os.indexOf("Linux") > -1)
    trace("Desktop Profile");
    else
    trace("Mobile Profile");

update 2:

it seems the easiest way, and perhaps the most common way to determine the profile at runtime is to call:

NativeWindow.isSupported;

from the flash.display.NativeWindow documentation:

AIR profile support: This feature is supported on all desktop operating systems, but is not supported on mobile devices or AIR for TV devices. You can test for support at run time on desktop devices using the NativeWindow.isSupported property. See AIR Profile Support for more information regarding API support across multiple profiles.

update 3:

while testing this on the BlackBerry PlayBook simulator, NativeWindow was supported. i haven't tested this on the device to know if it's was just supported on the simulator or not. i've since started using the following to determine the difference between mobile and desktop profiles:

if  (
    (Capabilities.os.toLowerCase().indexOf("mac") == -1) &&
    (Capabilities.os.toLowerCase().indexOf("windows") == -1) &&
    (Capabilities.os.toLowerCase().indexOf("linux") == -1)
    )
    deviceIsMobile = true;

Solution

  • This document specifies device capabilities for different profiles. Since cacheAsBitmapMatrix has no availability getter listed, you'll need to check it yourself once. It must be easy to do with try/catch block.

    Edit: I'll try to illustrate what I meant under "check once":

    public class Capabilities2
    {
        private static var cacheAsBitmapMatrixChecked:Boolean;
        private static var cacheAsBitmapMatrixStatus:Boolean;
    
        public static function get cacheAsBitmapMatrixIsSupported():Boolean
        {
            if (cacheAsBitmapMatrixChecked) return cacheAsBitmapMatrixStatus;
            var test:Sprite = new Sprite();
            try
            {
                text.cacheAsBitmapMatrix = new Matrix();
                cacheAsBitmapMatrixStatus = true;
            }
            catch (error:Error)
            {
                cacheAsBitmapMatrixStatus = false;
            }
            cacheAsBitmapMatrixChecked = true;
            return cacheAsBitmapMatrixStatus;
        }
    }
    

    Get current profile might be cleaner solution, but I don't know how to do it. Another 'idea': using document above, test capabilities and deduce profile from results, like in Einstein riddle :)