• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help enabling downloads through webview (Kotlin)

Good day. I'm new to android development and I'm trying to develop a simple webview application, picked a nice template and went through the steps and made good progress, I managed to load my site fully and enable javascript, that works as intended, however I'm not able to make the app download anything, I host a few pdf files that should open or download through it, but nothing happens.

I looked at a few answers here and it is to my understanding that I need to specifically add a function for that, could you give me a hand? I have tried multiple different code and tweaking them, but I wasn't able to get it to work, here is my base code:
Code:
package com.logista.test.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.app.DownloadManager
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.logista.test.R


class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        homeViewModel =
                ViewModelProvider(this).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_home, container, false)
        val myWebView: WebView = root.findViewById(R.id.webview)
        myWebView.webViewClient = WebViewClient()
        myWebView.settings.javaScriptEnabled = true
        myWebView.loadUrl("https://www.example.org/")
        return root
    }
}
I believe I should be adding
Code:
import android.app.DownloadManager
And tweak this
Code:
        // Set web view download listener

        web_view.setDownloadListener(DownloadListener {
            url,
            userAgent,
            contentDescription,
            mimetype,
            contentLength ->

            // Initialize download request
            val request = DownloadManager.Request(Uri.parse(url))

            // Get the cookie
            val cookies = CookieManager.getInstance().getCookie(url)

            // Add the download request header
            request.addRequestHeader("Cookie",cookies)
            request.addRequestHeader("User-Agent",userAgent)

            // Set download request description
            request.setDescription("Downloading requested file....")

            // Set download request mime tytpe
            request.setMimeType(mimetype)

            // Allow scanning
            request.allowScanningByMediaScanner()

            // Download request notification setting
            request.setNotificationVisibility(
                    DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)

            // Guess the file name
            val fileName = URLUtil.guessFileName(url, contentDescription, mimetype)

            // Set a destination storage for downloaded file
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)

            // Set request title
            request.setTitle(URLUtil.guessFileName(url, contentDescription, mimetype));


            // DownloadManager request more settings
            request.setAllowedOverMetered(true)
            request.setAllowedOverRoaming(false)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                request.setRequiresCharging(false)
                request.setRequiresDeviceIdle(false)
            }
            request.setVisibleInDownloadsUi(true)


            // Get the system download service
            val dManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

            // Finally, request the download to system download service
            dManager.enqueue(request)
        })


        // Load button click listener
        button_load.setOnClickListener{
            // Load url in a web view
            web_view.loadUrl(url)
        }

    }
Taken from here: https://android--code.blogspot.com/2018/03/android-kotlin-webview-file-download.html

I did the basics, renaming the function accordingly and such, but it gives me quite a few errors when building the app, for instance uri isn't defined, cookiemanager isn't defined, environment isn't defined, build isn't defined, and such, could you give me some guidance?

iPhone SE vs. Galaxy A51

I just made the same comparison and went with the A51--not because I am an expert but because the the A51 had twice the RAM, longer battery life, much higher screen & camera resolutions than the iPhone SE.

From what I could tell, the only thing the iPhone SE had going for it is its brand appeal as an Apple device and perhaps more compact size--neither of which impresses me in the least.

It certainly could be that differences in the processor hardware could mean that the iPhone has better performance even with lower numbers; I did not have time to do a deep dive into that or reading a lot of reviews. So that is just my two cents' worth.

Shortcuts

Although I have been supporting various Android devices for clients, I just finally made the move from my ancient Blackberry (don't laugh--my mobile device needs are extremely few but very detailed!) to a Galaxy A51.

Now I am trying to figure out how to make super-accessible shortcuts for a few things. For starters, I need to know how to make a one-tap or two-tap call forward. I do not want my cell phone ringing when I am in my home office; I want all calls to come to my home office so I can keep my home phone headset on and manage one device all day instead of two, with no concern about battery life. So I set up call forwarding that I enable before I head home after every field project. But this requires all of this:

Wake it up (button on right side)
Tap the phone
Tap the menu (three dots)
Tap Settings
Tap Supplementary services
Tap Call Forwarding
Tap Always Forward
Tap Turn On (or Turn Off)

To put this in perspective, to forward on my BB, all I had to do was swipe up, then hold down one pre-assigned shortcut key on the keyboard for three seconds. To unforward, the same, but a different key. It was so simple, I could do it virtually without looking at the BB but perhaps for one second to ensure I had the right key.

How would I configure a single- or two-tap for forward or un-forward?

Please help! Question about deleted messages, backups, etc.

here you go:
https://www.guidingtech.com/restore-android-messages-google-drive/

edit: oops sorry that is for backing up....i guess just do the exact opposite and delete backup

yeah gotcha i just backed up my phone afterwards because i’m pretty sure the store only uses your most current backup when transferring over your phone i believe.

"Password" App using data

Thanks, it was not something he was actually using (at least intentionally). It was not there under Settings -> Apps -> Show all apps -> (tap 3 dot icon) -> Show system apps. We tried deleting everything he had installed and I am letting him install apps a few at a time and monitoring the data usage. It seems to have stopped, but I still don't like the idea of an app that I can't find on the phone sucking up a bunch of data.

I appreciate your help.

Ok, at this point, do the
Settings
Apps
Show all apps
(3 dot icon)
Show system apps

again.

Go all the way to the bottom of the list.

What you are looking for is an app with no name, and no icon, that is still taking up space like any other app.

This is generally how malware/adware hides on a device in my experience.

Because there is no icon or name, it won't show up in the app drawer, or by a search, or other ways either.

Sometimes normal seeming apps can download junk like this, and it can be anything from ads to phishing, or other crap.

Anyway, the good thing is that if you do see such junk, just delete all data from the app and then uninstall the app.

Habitube - The Movie Library

There are many different movie sites, but the quality of the movies is terrible! To watch movies in high quality, I have to buy a monthly subscription. To be honest, I don't like spending money on such nonsense. I will be happy to spend this amount of money on something useful! By the way, I've tried using your app, and it's fantastic, very easy to use, and there's no need to waste time searching for quality movies. Recently, my girlfriend and I have started to watch more WatchSeries, because it is much more interesting. It helps us spend more time together

Calling kotlin from java shows "Cannot resolve symbol" for classes and methods called

There are two modules:
  1. app (main/application module which contains both Java and Kotlin classes)
  2. lib-wrapper (Android-library module which contains Java classes)
lib-wrapper is published on the Amazon S3 bucket and used as a library dependency in app module. Proguard rules are applied for the lib-wrapper. It was all working fine till now.

Then converted all the classes of lib-wrapper from java to kotlin.
  • Now, all the java classes (in app module) which are calling lib-wrapper methods show “Cannot resolve symbol” in red.
  • There is no problem with kotlin classes (in app module) which are calling lib-wrapper methods.
  • Even with “Cannot resolve symbol” in java classes, app compiles and runs without error. But for new users it is very tough to use lib-wrapper as it won’t show library classes and methods as an auto-complete suggestion.
  • Decompiling the apk file, lib-wrapper classes shows all intended classes and methods are public and public members are plain/de-obfuscated. (Which is expected.)
  • If I disable Proguard (“minifyEnabled false” in lib-wrapper’s build.gradle), everything works fine.
So I added below kotlin specific proguard rules in lib-wrapper's proguard-rules.pro file, but didn't help :
-keep class kotlin.*.* { *; }
-keep class kotlin.Metadata { *; }
-dontwarn kotlin.**
-keepclassmembers class **$WhenMappings {
;
}
-keepclassmembers class kotlin.Metadata {
public ;
}
-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
public static void checkParameterIsNotNull(java.lang.Object, java.lang.String);
}

Plugins used in lib-wrapper’s build.gradle file:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'maven-publish'
apply plugin: 'org.jetbrains.dokka-android'

Plugins used in app module’s build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

Any help would be appreciated. Thanks in advance.

Filter

Back
Top Bottom