Android Question WebViewExtras v.2 sample with all events

peacemaker

Expert
Licensed User
Longtime User
HI. All

Please, help to find a working under Android 4.4+ sample with working Javascript and all events of DefaultWebChromeClient, including error catch....
How to know the events support of any lib verion ?
So many versions :(((((
 

peacemaker

Expert
Licensed User
Longtime User
FInally, i have prepared the working sample myself:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim sURL As String '= "file:///" & Starter.InitFilesFolder & "/login.html"
    Dim LastURL As String = sURL
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim CookieManager1 As CookieManager
    Dim wv As WebView, wvs As WebViewSettings
    Dim WebViewExtras1 As WebViewExtras
    Dim WebChromeClient1 As DefaultWebChromeClient
    Dim JavascriptInterface1 As DefaultJavascriptInterface
    Dim WebViewClient1 As DefaultWebViewClient
  
    Private IME1 As IME
    Private ActivityParent As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")

    WebViewExtras1.Initialize(wv)
    WebViewExtras1.JavaScriptEnabled = True
  
    JavascriptInterface1.Initialize
    WebViewExtras1.AddJavascriptInterface(JavascriptInterface1, "B4A")

    WebChromeClient1.Initialize("WebChromeClient1")
    WebViewExtras1.SetWebChromeClient(WebChromeClient1)
  
    WebViewClient1.Initialize("wv")
    WebViewExtras1.SetWebViewClient(WebViewClient1)

    wvs.setUseWideViewPort(wv, True)
    wvs.setDisplayZoomControls(wv, False)
    wvs.setLoadsImagesAutomatically(wv, True)

IME1.Initialize("IME1")
IME1.AddHeightChangedEvent

Dim jo As JavaObject = Activity
jo.RunMethodJO("getContext", Null).RunMethodJO("getWindow", Null).RunMethod("setSoftInputMode", _
Array As Object(0x20))
ActivityParent = jo.RunMethodJO("getParent", Null)
After_Change
End Sub

Sub Activity_Resume
'call SetAcceptCookie after WebView has been initialized
CookieManager1.SetAcceptCookies(True)
After_Change

If LastURL <> "" Then
    Start
End If
End Sub

Sub After_Change
   Dim ajo As Panel = Activity
   Dim width As Int = ActivityParent.RunMethod("getMeasuredWidth", Null)
   Dim height As Int = ActivityParent.RunMethod("getMeasuredHeight", Null)
   If width = 0 Or height = 0 Then Return
   ajo.width = width 'update the "activity" width and height
   ajo.height = height
   wv.width = width
   wv.height = height
End Sub

Sub Start
ProgressDialogShow("")
wv.LoadUrl(LastURL)
End Sub

Sub If_Exit As Boolean
Dim a As Int = Msgbox2("Exit now ?", "Warning:", "Exit", "Cancel", "Log out", Null)
If a = DialogResponse.POSITIVE Then
    Activity.Finish
    Return True
Else if a = DialogResponse.NEGATIVE Then
    CookieManager1.RemoveAllCookies
    Activity.Finish
    Return True
End If
Return False
End Sub

Sub wv_PageFinished(URL As String)
ProgressDialogHide
End Sub

'exacute Javascript like Do_JS("document.getElementById('password').value")
Sub Do_JS (JS As String)
    Dim Javascript As String
    Javascript="B4A.CallSub('Process_HTML', true, " & JS & ")"
    WebViewExtras1.executeJavascript( Javascript)
End Sub

Sub Process_HTML(Html As String)
    '   This is the Sub that we'll get the web page to send it's HTML content to
    '   Log may truncate a large page so you'll not see all of the HTML in the log but the 'html' String should still contain all of the web page HTML
    CallSubDelayed2(Me, "Show_result", Html)
End Sub

'decide yourself what to do with JS result
Sub Show_result (res As String)
Log(res)
End Sub

Sub wv_ReceivedError(ErrorCode As Int, Description As String, FailingUrl As String)
    wv.LoadUrl("file:///" & Starter.InitFilesFolder & "/errors.html")
End Sub

Sub wv_OverrideUrl (URL As String) As Boolean
If URL.ToLowerCase.StartsWith("tel:") Then
    Dim in As Intent
    in.Initialize(in.ACTION_VIEW, URL)    '"tel:" & tel
    StartActivity(in)
    Return True
else If URL.ToLowerCase.StartsWith("adr:") Then    'special href-tag for address opening on a map: adr:London, Big Ben
    Dim s() As String = Regex.Split("adr:", URL)
    if s(1) <> "" then
        Dim p As PhoneIntents
        StartActivity (p.OpenBrowser("http://maps.google.com/maps?daddr=" & s(1)))    'open goole map with the address
    end if
    Return True
End If
#If debug
    Log("override=" & URL)
#End If
End Sub

Sub IME1_HeightChanged (NewHeight As Int, OldHeight As Int)
   CallSubDelayed(Me, "After_Change")
   Start
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
    Try
        If WebViewExtras1.CanGoback Then  
            ProgressDialogShow("")
            wv.Back
            Return True
        Else
            If If_Exit Then
                ExitApplication
                Return False
            Else
                Return True    'not exit
            End If
        End If
    Catch
        If_Exit
    End Try
End If
End Sub
 
Last edited:
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
here's the code that strangely didnt appear yesterday when i post it but it looks like ur working sample is far more cooler than mine :D

B4X:
#Region  Project Attributes
    #ApplicationLabel: sample
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
  
#End Region

Sub Process_Globals
  
    Dim Timer1 As Timer
    Dim Timer2 As Timer
    Dim Exit As Boolean = False
      
End Sub

Sub Globals

    Dim webview As WebView
    Private panel1 As Panel
    Dim webviewextras1 As WebViewExtras
  
  
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Activity.LoadLayout("main")
  
  
  
        panel1.Visible=True
        panel1.BringToFront
  
  
        Timer1.Initialize("Timer1", 3000)
        Timer1.Enabled=True
  
    End If
End Sub

Sub Timer1_tick

    panel1.Visible=False
    Timer1.Enabled = False

  

    Timer2.Initialize("timer2", 2000)
    Timer2.Enabled=True
  
    ProgressDialogShow("please wait")
  
  
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Activity.Title = ""
        webview.Initialize("webview")
        Activity.AddView(webview, 0, 0, 100%x, 100%y)
      
        webview.ZoomEnabled = False
      
        webviewextras1.Initialize(webview)
      
  
          
        Dim WebChromeClient1 As DefaultWebChromeClient
        WebChromeClient1.Initialize("")
        webviewextras1.SetWebChromeClient(WebChromeClient1)
  
        Dim WebViewClient1 As DefaultWebViewClient
        WebViewClient1.Initialize("WebViewClient1")

        webviewextras1.SetWebViewClient(WebViewClient1)
      
      
        webviewextras1.LoadUrl("URL")
      
      
    Else
              
        If Msgbox2("Please Check your Internet Connection", "Connection Error","ok","","", Null)  = DialogResponse.POSITIVE Then
            ProgressDialogHide
            Activity.Finish
            Exit = True
        Else
            Activity.Finish
            Exit = True
        End If
          
    End If
    ProgressDialogHide
End Sub

Sub WebViewClient1_ReceivedError(ErrorCode As Int, Description As String, FailingUrl As String)
    Log("WebViewClient1_ReceivedError "&ErrorCode)
  
    webview.LoadHtml("<html><body><head><script>function newDoc() {window.location.assign('URL')}</script></head><img src='file:///android_asset/errorimage.png'> <p>Error Connection, Your Account will automatically Logout.</p> <input type='button' value='Refresh' onclick='newDoc()'> </body></html>")
  
End Sub


Sub Timer2_tick
    Timer2.Enabled = False
    Dim check As HttpJob
    check.Initialize("checkconnection",Me)
    check.Download("https:/google.com")
  
  
  
End Sub


Sub Activity_Resume
    If Exit = True Then
        Activity.Finish
        ExitApplication
    Else
              
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        If Msgbox2("Do you want to close the app?", "Close App","Yes","","No", Null)  = DialogResponse.POSITIVE Then
      
      
            Exit = True
            Activity.Finish
          
  
      
      
      
        Else
  
        End If

        Return True
    End If

End Sub
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks for the sample, can you provide the HTML page(s) that have the Java code within.
I can't figure out how to get the Do_JS to fire.
Thanks
Rusty
 
Upvote 0
Top