"Fossies" - the Fresh Open Source Software Archive

Member "MediaInfo_CLI_GNU_FromSource/MediaInfoLib/Project/Android/MediaInfoJs/app/src/main/java/com/example/mediainfojs/MainActivity.kt" (10 Sep 2019, 4606 Bytes) of package /linux/misc/MediaInfo_CLI_19.09_GNU_FromSource.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Kotlin source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package com.example.mediainfojs
    2 
    3 import android.app.Activity
    4 import android.net.Uri
    5 import androidx.appcompat.app.AppCompatActivity
    6 import android.os.Bundle
    7 import android.webkit.*
    8 import android.content.Intent
    9 import kotlinx.android.synthetic.main.activity_main.*
   10 
   11 class MainActivity : AppCompatActivity() {
   12     var callback: ValueCallback<Array<Uri>>? = null
   13 
   14 
   15     /*
   16     * Override WebViewClient::shouldInterceptRequest to respond with MediaInfoJS files from assets when they are requested
   17     */
   18     inner class WebViewClientImpl(private val activity: Activity) : WebViewClient() {
   19         override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
   20             var response: WebResourceResponse? = null
   21             when {
   22                 request?.url.toString() == "https://mi/" -> // load sample html from assets
   23                     response = WebResourceResponse("text/html", "utf-8", activity.assets.open("MediaInfo_DLL_JavaScript/HowToUse_Dll.html"))
   24                 request?.url.toString() == "https://mi/MediaInfoWasm.js" -> // load mediainfo wasm javascript module from assets
   25                     response = WebResourceResponse("application/javascript", "utf-8", activity.assets.open("MediaInfo_DLL_JavaScript/MediaInfoWasm.js"))
   26                 request?.url.toString() == "https://mi/MediaInfoWasm.wasm" -> // load mediainfo wasm binary from assets
   27                     response = WebResourceResponse("application/octet-stream", "utf-8", activity.assets.open("MediaInfo_DLL_JavaScript/MediaInfoWasm.wasm"))
   28                 request?.url.toString() == "https://mi/MediaInfo.js" -> // load mediainfo asmjs javascript module from assets
   29                     response = WebResourceResponse("application/javascript", "utf-8", activity.assets.open("MediaInfo_DLL_JavaScript/MediaInfo.js"))
   30                 request?.url.toString() == "https://mi/MediaInfo.js.mem" -> // load mediainfo asmjs memory file from assets
   31                     response = WebResourceResponse("application/octet-stream", "utf-8", activity.assets.open("MediaInfo_DLL_JavaScript/MediaInfo.js.mem"))
   32 
   33             }
   34             return response
   35         }
   36     }
   37 
   38 
   39     /*
   40      * Handle file selection request
   41     */
   42     inner class WebChromeClientImpl(private val activity: Activity) : WebChromeClient() {
   43         override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
   44             callback?.onReceiveValue(null) // Terminate previous request if exist
   45             callback = filePathCallback
   46 
   47             val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
   48 
   49             intent.addCategory(Intent.CATEGORY_OPENABLE)
   50             intent.type = "*/*"
   51             intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
   52             activity.startActivityForResult(intent, MainActivity.OPEN_FILE_REQUEST)
   53 
   54             return true
   55         }
   56     }
   57 
   58     override fun onCreate(savedInstanceState: Bundle?) {
   59         super.onCreate(savedInstanceState)
   60         setContentView(R.layout.activity_main)
   61 
   62         WebView.setWebContentsDebuggingEnabled(true)
   63 
   64         web_view.webViewClient = WebViewClientImpl(this)
   65         web_view.webChromeClient = WebChromeClientImpl(this)
   66         web_view.settings.javaScriptEnabled = true
   67         web_view.settings.allowFileAccess = true
   68 
   69         // WebView automatically append a slash to this url, all url referenced in this page will be relative to https://mi/
   70         web_view.loadUrl("https://mi") // Chrome allow JavaScript loading only from http/https protocols
   71     }
   72 
   73     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
   74         if (resultCode == Activity.RESULT_OK) {
   75             when (requestCode) {
   76                 OPEN_FILE_REQUEST -> {
   77                     if (data == null)
   78                         return
   79 
   80                     var uris: Array<Uri>? = null
   81                     val clipData = data.clipData
   82                     if (clipData != null) {
   83                         uris = Array(clipData.itemCount) {
   84                             clipData.getItemAt(it).uri
   85                         }
   86                     } else if (data.data != null) {
   87                         uris = Array(1) {
   88                             data.data
   89                         }
   90                     }
   91                     if (uris != null) {
   92                         callback?.onReceiveValue(uris)
   93                     }
   94 
   95                     callback = null
   96                 }
   97             }
   98         }
   99     }
  100     companion object {
  101         const val OPEN_FILE_REQUEST = 1
  102     }
  103 }