守望者--AIR技术交流

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: ANE FlasCC 炼金术
查看: 698|回复: 0

[框架相关] Flex 4.12 的新API Platform类

[复制链接]
  • TA的每日心情
    擦汗
    2017-11-3 12:47
  • 签到天数: 441 天

    [LV.9]以坛为家II

    1741

    主题

    2093

    帖子

    13万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    517
    贡献
    24
    金币
    51073
    钢镚
    1421

    开源英雄守望者

    发表于 2016-1-22 16:35:01 | 显示全部楼层 |阅读模式
    1. ////////////////////////////////////////////////////////////////////////////////
    2. //
    3. //  Licensed to the Apache Software Foundation (ASF) under one or more
    4. //  contributor license agreements.  See the NOTICE file distributed with
    5. //  this work for additional information regarding copyright ownership.
    6. //  The ASF licenses this file to You under the Apache License, Version 2.0
    7. //  (the "License"); you may not use this file except in compliance with
    8. //  the License.  You may obtain a copy of the License at
    9. //
    10. //      http://www.apache.org/licenses/LICENSE-2.0
    11. //
    12. //  Unless required by applicable law or agreed to in writing, software
    13. //  distributed under the License is distributed on an "AS IS" BASIS,
    14. //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15. //  See the License for the specific language governing permissions and
    16. //  limitations under the License.
    17. //
    18. ////////////////////////////////////////////////////////////////////////////////

    19. package mx.utils
    20. {

    21. import flash.system.Capabilities;
    22. import flash.utils.getDefinitionByName;

    23. /**
    24. *  The Platform utility class contains several static methods to check what
    25. *  desktop or mobile platform the application is running on.
    26. *  
    27. *  @langversion 3.0
    28. *  @playerversion Flash 10
    29. *  @playerversion AIR 2.0
    30. *  @productversion Flex 4.12
    31. */
    32. public class Platform
    33. {
    34.     include "../core/Version.as";

    35.     private static var _instance: Platform;
    36.        
    37.         protected static var _initialized:Boolean;
    38.         protected static var _isAndroid:Boolean;
    39.         protected static var _isIOS:Boolean;
    40.         protected static var _isIPad:Boolean;
    41.         protected static var _isBlackBerry:Boolean;
    42.         protected static var _isMobile:Boolean;
    43.         protected static var _isMac:Boolean;
    44.         protected static var _isWindows:Boolean;
    45.         protected static var _isLinux:Boolean;
    46.         protected static var _isDesktop:Boolean;
    47.         protected static var _isBrowser:Boolean;
    48.         protected static var _isAir:Boolean;
    49.     private static var _osVersion: String = null;

    50.         /**
    51.          * This value is set from AndroidPlatformVersionOverride
    52.          *
    53.          */
    54.         mx_internal static var androidVersionOverride:String;

    55.         /**
    56.          * This value is set from IOSPlatformVersionOverride
    57.          *
    58.          */
    59.         mx_internal static var iosVersionOverride:String;
    60.        
    61.         /**
    62.          *  Returns true if the application is running on IOS.
    63.          *
    64.          *  @langversion 3.0
    65.          *  @playerversion Flash 10
    66.          *  @playerversion AIR 2.0
    67.          *  @productversion Flex 4.12
    68.          */
    69.         public static function get isIOS():Boolean
    70.         {
    71.                 getPlatforms();
    72.                
    73.                 return _isIOS;
    74.         }
    75.        
    76.         /**
    77.          *  Returns true if the application is running on an iPad.
    78.          *  Note this returns false in the AIR mobile device simulator.
    79.          *
    80.          *  @langversion 3.0
    81.          *  @playerversion Flash 10
    82.          *  @playerversion AIR 2.0
    83.          *  @productversion Flex 4.12
    84.          */
    85.         public static function get isIPad():Boolean
    86.         {
    87.                 getPlatforms();
    88.                
    89.                 return _isIPad;
    90.         }
    91.        
    92.         /**
    93.          *  Returns true if the application is running on a BlackBerry.
    94.          *
    95.          *  @langversion 3.0
    96.          *  @playerversion Flash 10
    97.          *  @playerversion AIR 2.0
    98.          *  @productversion Flex 4.12
    99.          */
    100.         public static function get isBlackBerry():Boolean
    101.         {
    102.                 getPlatforms();
    103.                
    104.                 return _isBlackBerry;
    105.         }
    106.        
    107.         /**
    108.          *  Returns true if the application is running on Android.
    109.          *
    110.          *  @langversion 3.0
    111.          *  @playerversion Flash 10
    112.          *  @playerversion AIR 2.0
    113.          *  @productversion Flex 4.12
    114.          */
    115.         public static function get isAndroid():Boolean
    116.         {
    117.                 getPlatforms();
    118.                
    119.                 return _isAndroid;
    120.         }
    121.        
    122.         /**
    123.          *  Returns true if the application is running on Windows.
    124.          *
    125.          *  @langversion 3.0
    126.          *  @playerversion Flash 10
    127.          *  @playerversion AIR 2.0
    128.          *  @productversion Flex 4.12
    129.          */
    130.         public static function get isWindows():Boolean
    131.         {
    132.                 getPlatforms();
    133.                
    134.                 return _isWindows;
    135.         }
    136.        
    137.         /**
    138.          *  Returns true if the application is running on a Mac.
    139.          *
    140.          *  @langversion 3.0
    141.          *  @playerversion Flash 10
    142.          *  @playerversion AIR 2.0
    143.          *  @productversion Flex 4.12
    144.          */
    145.         public static function get isMac():Boolean
    146.         {
    147.                 getPlatforms();
    148.                
    149.                 return _isMac;
    150.         }
    151.        
    152.         /**
    153.          *  Returns true if the application is running on Linux.
    154.          *
    155.          *  @langversion 3.0
    156.          *  @playerversion Flash 10
    157.          *  @playerversion AIR 2.0
    158.          *  @productversion Flex 4.12
    159.          */
    160.         public static function get isLinux():Boolean
    161.         {
    162.                 getPlatforms();
    163.                
    164.                 return _isLinux;
    165.         }
    166.        
    167.         /**
    168.          *  Returns true if the application is running on a Desktop OS.
    169.          *
    170.          *  @langversion 3.0
    171.          *  @playerversion Flash 10
    172.          *  @playerversion AIR 2.0
    173.          *  @productversion Flex 4.12
    174.          */
    175.         public static function get isDesktop():Boolean
    176.         {
    177.                 getPlatforms();
    178.                
    179.                 return _isDesktop;
    180.         }
    181.        
    182.         /**
    183.          *  Returns true if the application is running on a Mobile device.
    184.          *
    185.          *  @langversion 3.0
    186.          *  @playerversion Flash 10
    187.          *  @playerversion AIR 2.0
    188.          *  @productversion Flex 4.12
    189.          */
    190.         public static function get isMobile():Boolean
    191.         {
    192.                 getPlatforms();
    193.                
    194.                 return _isMobile;
    195.         }
    196.        
    197.         /**
    198.          *  Returns true if the application is running on a desktop AIR.
    199.          *
    200.          *  @langversion 3.0
    201.          *  @playerversion Flash 10
    202.          *  @playerversion AIR 2.0
    203.          *  @productversion Flex 4.12
    204.          */
    205.         public static function get isAir():Boolean
    206.         {
    207.                 getPlatforms();
    208.                
    209.                 return _isAir;
    210.         }
    211.        
    212.         /**
    213.          *  Returns true if the application is running in a browser.
    214.          *
    215.          *  @langversion 3.0
    216.          *  @playerversion Flash 10
    217.          *  @playerversion AIR 2.0
    218.          *  @productversion Flex 4.12
    219.          */
    220.         public static function get isBrowser():Boolean
    221.         {
    222.                 getPlatforms();
    223.                
    224.                 return _isBrowser;
    225.         }

    226.     /**
    227.      *  Returns the version of the OS the application  is running on
    228.      *
    229.      *  @langversion 3.0
    230.      *  @playerversion Flash 10
    231.      *  @playerversion AIR 2.0
    232.      *  @productversion Flex 4.13
    233.      */
    234.     public static function get osVersion(): String
    235.     {
    236.         //We needed to compute _osVersion later than getPlatforms, because it relies on resources that  ready later
    237.         if (_osVersion == null){
    238.                         if(mx_internal::androidVersionOverride == null && mx_internal::iosVersionOverride == null)
    239.                         {
    240.                                 _osVersion = computeOSVersionString();       
    241.                         }
    242.                         else if(mx_internal::androidVersionOverride != null)
    243.                         {
    244.                                 _osVersion = mx_internal::androidVersionOverride;
    245.                         }
    246.                         else if(mx_internal::iosVersionOverride != null)
    247.                         {
    248.                                 _osVersion = mx_internal::iosVersionOverride;
    249.                         }
    250.         }
    251.         return _osVersion;
    252.     }

    253.     /* Notes on Capabilities.os for mobile apps:
    254.         - on ADL => returns the OS where the ADL is running ( eg. Windows 7, or Mac OS )
    255.          - on device => returns the OS of the device (eg.  iPhone OS ...  for iOS devices  )
    256.     * */
    257.         protected static function getPlatforms():void {
    258.                 if (!_initialized)
    259.                 {
    260.             var cap: Class = Capabilities;
    261.             var version:  String = Capabilities.version;
    262.             var os: String = Capabilities.os;
    263.             var playerType: String = Capabilities.playerType;

    264.                         _isAndroid = version.indexOf("AND") > -1;
    265.                         _isIOS = version.indexOf('IOS') > -1;
    266.                         _isBlackBerry = version.indexOf('QNX') > -1;
    267.                         _isMobile = _isAndroid || _isIOS || _isBlackBerry;
    268.                        
    269.                         _isMac = os.indexOf("Mac OS") != -1;
    270.                         _isWindows = os.indexOf("Windows") != -1;
    271.                         _isLinux = os.indexOf("Linux") != -1; // note that Android is also Linux
    272.                         _isIPad = os.indexOf('iPad') > -1;
    273.                         _isDesktop = !_isMobile;
    274.                        
    275.                         _isAir = playerType == "Desktop";
    276.                         _isBrowser = (playerType == "PlugIn" || playerType == "ActiveX");
    277.                        
    278.                         _initialized = true;
    279.                 }
    280.         }

    281.     /** @private
    282.      * extract OS version information from Capabilities.os
    283.      * os is typically a non-numeric string (such as Windows,  iPhone OS, Android, etc...)  followed by a number sequence.
    284.      * if no number is found, OS version is set to 0.
    285.      * os on ADL will return the host OS and not the device OS.
    286.      *
    287.      * That's why we need to check for a specific sequence for iOS and Android.
    288.      * On Android, os  is the Linux kernel version (such as Linux 3.4.34-1790463).
    289.      * So the version information must be  retrieved from an internal file.
    290.      * Since reading files API is only available on AIR, it's delegated to PlatformMobileHelper  in mobilecomponents.swc
    291.      * @see   spark.utils.PlatformMobileHelper
    292.      *
    293.      * @return version number string, or empty string if could not retrieve the version.
    294.      * */
    295.     private static function computeOSVersionString(): String
    296.     {
    297.         var os: String = Capabilities.os;
    298.         var osVersionMatch: Array;
    299.         var version: String = "";

    300.         if (isIOS) {
    301.             osVersionMatch = os.match(/iPhone OS\s([\d\.]+)/);
    302.             if (osVersionMatch && osVersionMatch.length == 2)
    303.                 version = osVersionMatch[1];
    304.         }
    305.         else if (isAndroid) {
    306.             try {
    307.                 var mobileHelperClass: Class = Class(getDefinitionByName("spark.utils::PlatformMobileHelper"));
    308.                 if (mobileHelperClass != null) {
    309.                     version = mobileHelperClass["computeOSVersionForAndroid"]();
    310.                 }
    311.             }
    312.             catch (e: Error) {
    313.                 trace("Error: " + e.message);
    314.             }
    315.         }
    316.         else {
    317.             //on  other OS, extract version
    318.             osVersionMatch = os.match(/[A-Za-z\s]+([\d\.]+)/);
    319.             if (osVersionMatch && osVersionMatch.length == 2)
    320.                 version = osVersionMatch[1];
    321.         }
    322.         return version;
    323.     }

    324. }
    325. }
    复制代码


    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    
    关闭

    站长推荐上一条 /4 下一条

    QQ|手机版|Archiver|网站地图|小黑屋|守望者 ( 京ICP备14061876号

    GMT+8, 2017-12-13 23:00 , Processed in 1.296875 second(s), 31 queries .

    守望者AIR

    守望者AIR技术交流社区

    本站成立于 2014年12月31日

    快速回复 返回顶部 返回列表