No prompt is coming out while choosing file

72 views Asked by At
package com.dhakauniversityitsociety.duits

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.webkit.ValueCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import org.mozilla.geckoview.GeckoRuntime
import org.mozilla.geckoview.GeckoRuntimeSettings
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.GeckoView
import java.io.IOException


class WebActivity : AppCompatActivity() {

    private lateinit var geckoView: GeckoView
    private var mFilePathCallback: ValueCallback<Array<Uri>>? = null
    private val FILE_REQUEST_CODE = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        try {
            window.statusBarColor = ContextCompat.getColor(this, R.color.status_bar_color)
        } catch (e: IOException) {
        }
        setContentView(R.layout.activity_web)
        geckoView = findViewById(R.id.geckoView)

        val runtime = GeckoRuntimeSingleton.getInstance(applicationContext)

        val session = GeckoSession()
        session.open(runtime)

        val url = intent.getStringExtra("URL")
        if (url != null) {
            session.loadUri(url)
        }
        geckoView.setSession(session)
        val REQUEST_PERMISSION_CODE = 1
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
            // Permission is granted
        // Add file chooser listener
        geckoView.setOnCreateContextMenuListener { menu, _, _ ->
            val item = menu.add("File Picker")
            item.setOnMenuItemClickListener { menuItem ->
                if (mFilePathCallback != null) {
                    mFilePathCallback!!.onReceiveValue(null)
                }
                mFilePathCallback = ValueCallback { result ->
                    mFilePathCallback?.onReceiveValue(result)
                    mFilePathCallback = null
                }
                val intent = Intent(Intent.ACTION_GET_CONTENT)
                intent.addCategory(Intent.CATEGORY_OPENABLE)
                intent.type = "*/*"
                val i = Intent.createChooser(intent, "File")
                startActivityForResult(i, FILE_REQUEST_CODE)
                true
            }
        }
        } else {
            // Permission is not granted, request it
            ActivityCompat.requestPermissions(this,
                arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),
                REQUEST_PERMISSION_CODE)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == FILE_REQUEST_CODE) {
            if (mFilePathCallback == null) {
                return
            }
            val result = if (data == null || resultCode != RESULT_OK) null else data.data
            if (result != null) {
                mFilePathCallback!!.onReceiveValue(arrayOf(result))
            } else {
                val results = if (data == null || resultCode != RESULT_OK) null else data.clipData
                if (results != null) {
                    val count = results.itemCount
                    val uris = arrayOfNulls<Uri>(count)
                    for (i in 0 until count) {
                        uris[i] = results.getItemAt(i).uri
                    }
                    mFilePathCallback!!.onReceiveValue(uris as Array<Uri>)
                } else {
                    mFilePathCallback!!.onReceiveValue(null)
                }
            }
            mFilePathCallback = null
        }
    }
}

object GeckoRuntimeSingleton {
    private var geckoRuntime: GeckoRuntime? = null

    fun getInstance(context: Context): GeckoRuntime {
        if (geckoRuntime == null) {
            val settings = GeckoRuntimeSettings.Builder()
                .javaScriptEnabled(true) // Enable JavaScript
                .build()
            geckoRuntime = GeckoRuntime.create(context, settings)
        }
        return geckoRuntime!!
    }
}


When I'm clicking on choose file option no file choose prompt is coming out.

here is the logcat result D/GeckoSession: handleMessage GeckoView:Prompt D/Prompts: handleEvent file D/GeckoIdleService: Get idle time: time since reset 4955 msec D/GeckoIdleService: Idle timer callback: current idle time 4955 msec D/GeckoIdleService: next timeout 45 msec from now D/GeckoIdleService: SetTimerExpiryIfBefore: next timeout 45 msec from now D/GeckoIdleService: reset timer expiry to 55 msec from now D/GeckoIdleService: Get idle time: time since reset 5009 msec D/GeckoIdleService: Idle timer callback: current idle time 5009 msec D/GeckoIdleService: next timeout 4294967289990 msec from now D/GeckoIdleService: SetTimerExpiryIfBefore: next timeout 4294967289990 msec from now D/GeckoIdleService: reset timer expiry to 4294967290000 msec from now D/GeckoIdleService: Idle timer callback: tell observer 0x70248a6448 user is idle D/GeckoViewModule: dispatch GeckoView:SetFocused, data={"focused":true} D/GeckoViewContent: onEvent: event=GeckoView:SetFocused, data={"focused":true}

0

There are 0 answers