B4A Class Class to check SDK levels at runtime

Discussion in 'Additional libraries, classes and official updates' started by cimperia, Oct 20, 2015.

  1. cimperia

    cimperia Active Member Licensed User

    Are you tired of checking which Android release is tied to which SDK level?

    I am, so I wrote a little B4A class that links the different Android versions with their SDK. Nothing earth shattering but it has been serving me well and you might find it useful.

    It’ll allow you to check the running Android API version and take decisions based on it, especially in situation where your application can take advantage of Android functionality tied up to a specific version or greater.

    You could rewrite the class as a static module if you preferred, as you only need one instance by application. However creating a class made initialization easier.

    If you are wondering why I hard-coded the values in the class, instead of making native calls to retrieve the values, the reason is that if your application is checking for a sdk higher than the one which it is currently running on, the call will, of course, raise an exception. Dealing with exceptions made the code ugly and therefore I hard-coded the values as they are unlikely to ever change (as sdk codes are monotonically incremented by one with each new sdk release).

    The Class is dependent on the Reflection library.

    24/10/2015
    Added method to get the java version on B4J application.

    AJbuild
    Version:
    1
    Author: Cimperia
    • AJBuild (if B4A)
      Fields:
      • BASE As Int
      • BASE_1_1 As Int
      • CUPCAKE As Int
      • CUR_DEVELOPMENT As Int
      • DONUT As Int
      • ECLAIR As Int
      • ECLAIR_0_1 As Int
      • ECLAIR_MR1 As Int
      • FROYO As Int
      • GINGERBREAD As Int
      • GINGERBREAD_MR1 As Int
      • HONEYCOMB As Int
      • HONEYCOMB_MR1 As Int
      • HONEYCOMB_MR2 As Int
      • ICE_CREAM_SANDWICH As Int
      • ICE_CREAM_SANDWICH_MR1 As Int
      • JELLY_BEAN As Int
      • JELLY_BEAN_MR1 As Int
      • JELLY_BEAN_MR2 As Int
      • KITKAT As Int
      • KITKAT_WATCH As Int
      • LOLLIPOP As Int
      • LOLLIPOP_MR1 As Int
      • M As Int
      • VERSION_CODES As Map
      Methods:

      • Tests whether the object has been initialized.
      • IsInitialized As Boolean
        The current development codename, or the string "REL" if this is a release build.
        java static field: CODENAME
      • CODENAME As String
        Synomym for SDK_INT
      • DEVICE_API_LEVEL As Int
        Display the Release Name
      • DEVICE_API_NAME As String
        Initalize AJBuild
        AJBuild retrieves the device running SDK level.
        More details about Platform Releases and SDK Levels here
      • INITIALIZE As String
        The user-visible version string. E.g., "1.0" or "3.4b5".
        In other words, the platform version.
        java static field: RELEASE
      • RELEASE As String
        The user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES.
        java static field: SDK_INT or SDK if API Level < 4
      • SDK_INT As Int
    • AJBuild (if B4J)
      Methods:

      • Tests whether the object has been initialized.
      • IsInitialized As Boolean
        Returns the running Java specific version as double
      • JavaVersion As Double
    How to use
    Code:
    '
       Dim OS As AJBuild
       OS.Initialize

       
    Log(OS.CODENAME)
       
    Log(OS.RELEASE)
       
    Log(OS.SDK_INT)

       
    'DEVICE_API_LEVEL is a synonym for SDK_INT
       Log(OS.DEVICE_API_LEVEL)
       
    Log(OS.DEVICE_API_NAME)

       
    If OS.SDK_INT < OS.JELLY_BEAN Then
         
    Msgbox($"Functionnality is not available on this platform: ${OS.VERSION_CODES.Get(OS.SDK_INT)} "$,"INFO")
       
    End If
    Class Code
    Code:
    'Class AJBuild
    #IF B4A
    Private Sub Class_Globals
       
    Private this As AJBuild = Me
       
    Private r As Reflector
       
    Public VERSION_CODES As Map
       
    '
       Public Const BASE = 1 As Int
       
    Public Const BASE_1_1 = 2 As Int
       
    Public Const CUPCAKE = 3 As Int
       
    Public Const CUR_DEVELOPMENT = 10000 As Int
       
    Public Const DONUT = 4 As Int
       
    Public Const ECLAIR = 5 As Int
       
    Public Const ECLAIR_0_1 = 6 As Int
       
    Public Const ECLAIR_MR1 = 7 As Int
       
    Public Const FROYO = 8 As Int
       
    Public Const GINGERBREAD = 9 As Int
       
    Public Const GINGERBREAD_MR1 = 10 As Int
       
    Public Const HONEYCOMB = 11 As Int
       
    Public Const HONEYCOMB_MR1 = 12 As Int
       
    Public Const HONEYCOMB_MR2 = 13 As Int
       
    Public Const ICE_CREAM_SANDWICH = 14 As Int
       
    Public Const ICE_CREAM_SANDWICH_MR1 = 15 As Int
       
    Public Const JELLY_BEAN = 16 As Int
       
    Public Const JELLY_BEAN_MR1 = 17 As Int
       
    Public Const JELLY_BEAN_MR2 = 18 As Int
       
    Public Const KITKAT = 19 As Int
       
    Public Const KITKAT_WATCH = 20 As Int
       
    Public Const LOLLIPOP = 21 As Int
       
    Public Const LOLLIPOP_MR1 = 22 As Int
       
    Public Const M = 23 As Int
       
    '
       Private device_api_level_ As Int
    End Sub

    'Initalize AJBuild
    'AJBuild retrieves the device running SDK level.
    'More details about Platform Releases and SDK Levels <a href="http://developer.android.com/reference/android/os/Build.VERSION_CODES.html">here</a> and <a href ="http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels">here</a>
    Public Sub Initialize
       VERSION_CODES.Initialize
       VERSION_CODES.Put(BASE, 
    "BASE")
       VERSION_CODES.Put(BASE_1_1, 
    "BASE_1_1")
       VERSION_CODES.Put(CUPCAKE, 
    "CUPCAKE")
       VERSION_CODES.Put(CUR_DEVELOPMENT, 
    "CUR_DEVELOPMENT")
       VERSION_CODES.Put(DONUT, 
    "DONUT")
       VERSION_CODES.Put(ECLAIR, 
    "ECLAIR")
       VERSION_CODES.Put(ECLAIR_0_1, 
    "ECLAIR_0_1")
       VERSION_CODES.Put(ECLAIR_MR1, 
    "ECLAIR_MR1")
       VERSION_CODES.Put(FROYO, 
    "FROYO")
       VERSION_CODES.Put(GINGERBREAD, 
    "GINGERBREAD")
       VERSION_CODES.Put(GINGERBREAD_MR1, 
    "GINGERBREAD_MR1")
       VERSION_CODES.Put(HONEYCOMB, 
    "HONEYCOMB")
       VERSION_CODES.Put(HONEYCOMB_MR1, 
    "HONEYCOMB_MR1")
       VERSION_CODES.Put(HONEYCOMB_MR2, 
    "HONEYCOMB_MR2")
       VERSION_CODES.Put(ICE_CREAM_SANDWICH, 
    "ICE_CREAM_SANDWICH")
       VERSION_CODES.Put(ICE_CREAM_SANDWICH_MR1, 
    "ICE_CREAM_SANDWICH_MR1")
       VERSION_CODES.Put(JELLY_BEAN, 
    "JELLY_BEAN")
       VERSION_CODES.Put(JELLY_BEAN_MR1, 
    "JELLY_BEAN_MR1")
       VERSION_CODES.Put(JELLY_BEAN_MR2, 
    "JELLY_BEAN_MR2")
       VERSION_CODES.Put(KITKAT, 
    "KITKAT")
       VERSION_CODES.Put(KITKAT_WATCH, 
    "KITKAT_WATCH")
       VERSION_CODES.Put(LOLLIPOP, 
    "LOLLIPOP")
       VERSION_CODES.Put(LOLLIPOP_MR1, 
    "LOLLIPOP_MR1")
       VERSION_CODES.Put(M, 
    "M")
       
    '
       this.device_api_level_ = SDK_INT
    End Sub

    #Region NATIVE METHOD CALLS
    'The current development codename, or the string "REL" if this is a release build.
    'java static field: CODENAME
    Public Sub CODENAME As String
       
    Return r.GetStaticField("android.os.Build$VERSION""CODENAME")
    End Sub

    'The user-visible version string. E.g., "1.0" or "3.4b5".
    'In other words, the platform version.
    'java static field: RELEASE
    Public Sub RELEASE As String
       
    Return r.GetStaticField("android.os.Build$VERSION""RELEASE")
    End Sub

    'The user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES.
    'java static field: SDK_INT or SDK if API Level < 4
    Public Sub SDK_INT As Int
       
    Try
         
    Return r.GetStaticField("android.os.Build$VERSION""SDK_INT")
       
    Catch
         
    Return r.GetStaticField("android.os.Build$VERSION""SDK")
       
    End Try
    End Sub
    #End Region NATIVE METHOD CALLS

    #Region SUGAR
    Public Sub getDEVICE_API_LEVEL As Int
       
    Return this.device_api_level_
    End Sub

    Public Sub getDEVICE_API_NAME As String
       
    Return VERSION_CODES.Get(this.device_api_level_)
    End Sub
    #End Region SUGAR
    #Else If B4J
    Private Sub Class_Globals
      
    Private r As Reflector
    End Sub

    Public Sub Initialize
    End Sub

    'Return the running Java specific version as double
    Public Sub JavaVersion As Double
       
    Return r.RunStaticMethod("java.lang.System""getProperty", _
                                 
    Array As Object("java.specification.version"), _
                                 
    Array As String("java.lang.String"))
    End Sub
    #End If
     
    Last edited: Oct 24, 2015
    Ed Brown, johndb, moster67 and 3 others like this.
  2. cimperia

    cimperia Active Member Licensed User

    Added method to get the java specific version on B4J applications.

    The class can be used unchanged on B4A or B4J applications as the class will only expose methods relevant to the platform (B4A or B4J) it's on.
     
    DonManfred likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice