Android Example Check if the application is running on a Chromebook

If you want to check if the application is running on a Chromebook (for example to do some specific GUI reconfiguration to run in a resizable Window), you can use the following routine.
Hope it helps. Returns true if is a Chromebook, False otherwise.
B4X:
Sub isChromebook As Boolean
#If JAVA
import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
public static final String ARC_FEATURE = "org.chromium.arc";
public static final String ARC_DEVICE_MANAGEMENT_FEATURE = "org.chromium.arc.device_management";

     /** Returns true if the device has a given system feature */
    public boolean hasSystemFeature(String feature) {
        return getPackageManager().hasSystemFeature(feature);
    }
    
    /** Returns {@code true} if device is an ARC++ device. */
    public boolean isArc() {
        return hasSystemFeature(ARC_FEATURE) || hasSystemFeature(ARC_DEVICE_MANAGEMENT_FEATURE);
    }

#End If
    Dim ctx As JavaObject
    ctx.InitializeContext
    Return ctx.RunMethod("isArc",Null)
End Sub
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Do we have to define the static strings
B4X:
public static final String ARC_FEATURE = "org.chromium.arc";
public static final String ARC_DEVICE_MANAGEMENT_FEATURE = "org.chromium.arc.device_management";

Aren't these define somewhere? The way Google changes things this code might fail next week

But thanks for the code.
 

yo3ggx

Active Member
Licensed User
Longtime User
Everything is possible with Google.
I am not aware of any other way to get the two strings.
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Just tried your code and I get this error while compiling
B4X:
  return getPackageManager().hasSystemFeature(feature);
               ^
  symbol:   method getPackageManager()

So I did it this way and commented out all the #if JAVA code
B4X:
            Try 
                Dim jo As JavaObject
            
                jo.InitializeContext

                Dim ChromeBook As Boolean = False
                
                
                ChromeBook = jo.RunMethodJO("getPackageManager", Null).RunMethod("hasSystemFeature", Array("org.chromium.arc"))
            
                If  ChromeBook == False Then
                    ChromeBook = jo.RunMethodJO("getPackageManager", Null).RunMethod("hasSystemFeature", Array("org.chromium.arc.device_management"))
                End If
                
                Log($"IsChromeBook: ${ChromeBook}"$)        
                
                Return ChromeBook        
            Catch
                Log("Check for IsChromeBook failed")
                
                Return False
            End Try

Code now compiles clean, but I don't yet have a Chromebook so it always returns false but hope it will work later
 

yo3ggx

Active Member
Licensed User
Longtime User
I don't get any error with my code and it was tested on a Chromebook too.
Did you copy my code as it is, or make some changes?
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Final version
B4X:
#Region IsChromeBook
Public  Sub IsChromeBook As Boolean
            LogColor("IsChromeBook", Colors.Red)
            
            Dim ARC_FEATURE                     As String = "org.chromium.arc"
            Dim ARC_DEVICE_MANAGEMENT_FEATURE    As String = "org.chromium.arc.device_management"

            Try 
                Dim jo As JavaObject
            
                jo.InitializeContext

                Dim ChromeBook As Boolean = False                
                
                ChromeBook = jo.RunMethodJO("getPackageManager", Null).RunMethod("hasSystemFeature", Array(ARC_FEATURE))
            
                If  ChromeBook == False Then
                    ChromeBook = jo.RunMethodJO("getPackageManager", Null).RunMethod("hasSystemFeature", Array(ARC_DEVICE_MANAGEMENT_FEATURE))
                End If
                
                LogColor($"IsChromeBook: ${ChromeBook}"$, Colors.Red)        
                
                Return ChromeBook        
            Catch
                LogColor($"IsChromeBook Check failed - ${LastException.Message}"$, Colors.Red)
                
                Return False
            End Try
End Sub    
#end region
 

yo3ggx

Active Member
Licensed User
Longtime User
You are doing something wrong.
I tested again in a new app with just a label.
B4X:
#Region  Project Attributes
    #ApplicationLabel: ChromebookTest
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
End Sub

Sub Globals
    Dim lblChromebook As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
End Sub

Sub Activity_Resume
    lblChromebook.Initialize("")
    Activity.AddView(lblChromebook,0,0,100%x,100%y)
    lblChromebook.Gravity = Bit.Or(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL)
    lblChromebook.TextSize = 100
    If isChromebook Then
        lblChromebook.text = "Is a Chromebook"
    Else
        lblChromebook.text = "Is not a Chromebook"
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub isChromebook As Boolean
#If JAVA
import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
public static final String ARC_FEATURE = "org.chromium.arc";
public static final String ARC_DEVICE_MANAGEMENT_FEATURE = "org.chromium.arc.device_management";

     /** Returns true if the device has a given system feature */
    public boolean hasSystemFeature(String feature) {
        return getPackageManager().hasSystemFeature(feature);
    }
    
    /** Returns {@code true} if device is an ARC++ device. */
    public boolean isArc() {
        return hasSystemFeature(ARC_FEATURE) || hasSystemFeature(ARC_DEVICE_MANAGEMENT_FEATURE);
    }

#End If
    Dim ctx As JavaObject
    ctx.InitializeContext
    Return ctx.RunMethod("isArc",Null)
End Sub
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Well, I have to assume it is my machine.

I made a new B4XPages program and put the code in there and it does not compile

Attached is project
 

Attachments

  • Chromebook.zip
    14.1 KB · Views: 160

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Don't understand your question, but if I put it in the Main program it compiles just fine

But if I move it to the B4XMainPage or any other page it doesn't
 

yo3ggx

Active Member
Licensed User
Longtime User
Is not a question, is a sentence. This is a "Default", not "B4X Pages" project.
Create a new "Default" project, not "B4X Pages" project, remove the default layout and replace everything with my code.
 
Top