Wish Integrate functions in the core library

D

Deleted member 103

Guest
Hello,

in B4a there are many functions which can only be used via "JavaObject" or "Reflector", it would be nice if these functions were integrated in the core library.

Thank you
 
D

Deleted member 103

Guest
This is a very general request.
Ok, here are some example: ;)
B4X:
Reflector-Function:

Sub HasFeature(feature As String) As Boolean
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod("getPackageManager")
   Return r.RunMethod2("hasSystemFeature", feature, "java.lang.String")
End Sub

Sub GetDeviceId As String
    Dim R As Reflector
    Dim Api As Int
    Api = R.GetStaticField("android.os.Build$VERSION", "SDK_INT")
    If Api < 9 Then
        'Old device
        If File.Exists(File.DirInternal, "__id") Then
            'Log(File.ReadString(File.DirInternal, "__id"))
            Return File.ReadString(File.DirInternal, "__id")
        Else
            Dim id As Int
            id = Rnd(0x10000000, 0x7FFFFFFF)
            File.WriteString(File.DirInternal, "__id", id)
            Return id
        End If
    Else
        'New device
        Return R.GetStaticField("android.os.Build", "SERIAL")
    End If
End Sub

Sub ExactSize As Double
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    r.Target = r.RunMethod("getDisplayMetrics")
    Dim xdpi As Double = r.GetField("xdpi")
    Dim ydpi As Double = r.GetField("ydpi")
    'Return Sqrt(Power(100%x / xdpi, 2) + Power(100%y / ydpi, 2))

    Dim lv As LayoutValues
    lv = GetDeviceLayoutValues
    Dim size As Double  = Sqrt(Power(lv.Width / xdpi, 2) + Power(lv.Height / ydpi, 2))
    'Log("ExactSize=" & size)
    Return size
End Sub

Sub SetNinePatchDrawable(Control As View, ImageName As String)
    Dim r As Reflector
    Dim package As String
    Dim id As Int
    package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    id = r.GetStaticField(package & ".R$drawable", ImageName)
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    Control.Background = r.RunMethod2("getDrawable", id, "java.lang.int")
End Sub

Sub SetNinePatchButton(Btn As Button, DefaultImage As String, PressedImage As String)
    Dim r As Reflector
    Dim package As String
    Dim idDefault, idPressed As Int
    package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    idDefault = r.GetStaticField(package & ".R$drawable", DefaultImage)
    idPressed = r.GetStaticField(package & ".R$drawable", PressedImage)
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    Dim sd As StateListDrawable
    sd.Initialize
    sd.AddState(sd.State_Pressed, r.RunMethod2("getDrawable", idPressed, "java.lang.int"))
    sd.AddCatchAllState( r.RunMethod2("getDrawable", idDefault, "java.lang.int"))
    Btn.Background = sd
End Sub

Sub SetAirplaneMode(On As Boolean)
    Dim p As Phone
    If On = GetAirplaneMode Then Return 'already in the correct state
    Dim R As Reflector
    Dim contentResolver As Object
    R.Target = R.GetContext
    contentResolver = R.RunMethod("getContentResolver")
    Dim state As Int
    If On Then state = 1 Else state = 0
    R.RunStaticMethod("android.provider.Settings$System", "putInt", _
        Array As Object(contentResolver, "airplane_mode_on", state), _
        Array As String("android.content.ContentResolver", "java.lang.String", "java.lang.int"))
    Dim i As Intent
    i.Initialize("android.intent.action.AIRPLANE_MODE", "")
    i.PutExtra("state", "" & On)
    p.SendBroadcastIntent(i)
End Sub

Private Sub GetDefaultLanguage As String
   Dim r As Reflector
   r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
   Return r.RunMethod("getLanguage")
End Sub

###########################################################################

JavaObject-Function:

Public Sub setSingleLine(TextView As View, SingleLine As Boolean)
    Dim jo = TextView As JavaObject
    jo.RunMethod("setSingleLine", Array As Object(SingleLine))
End Sub

Private Sub GetContext As JavaObject
    Return GetBA.GetField("context")
End Sub

Private Sub GetBA As JavaObject
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetFieldJO("processBA")
End Sub

Sub Rotate(view As View, Degree As Float, Duration As Long)
    Dim jo As JavaObject = view
    jo.RunMethodJO("animate", Null).RunMethodJO("rotationBy", Array(Degree)).RunMethodJO("setDuration", Array(Duration)).RunMethodJO("start", Null)
End Sub

Do you know why B4a software is so much used by Visual Basic programmers?
Because one does not love Java so good. ;)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can find many usages of JavaObject (or Reflector) in the forum. Good examples (like the one you posted) don't require any Java knowledge. You just need to take the sub and put it in your project.

JavaObject is a very powerful tool which allows me as well as many other developers to help others implement what they want. There is no doubt that JavaObject is not a replacement for full libraries. It excels in filling small gaps or in cases where it is technically not possible to create a library (for example when there is a feature only available in newer versions of Android).

If you prefer not to use it then don't use it...
 
Top