Developing a Download Manager App with Huawei Network Kit

Abdurrahim Çillioğlu
Huawei Developers
Published in
6 min readJun 22, 2021

Introduction

Hi everyone, In this article, we’ll explore how to develop a download manager app using the Huawei Network Kit. And, we’ll use Kotlin as a programming language in Android Studio.

Huawei Network Kit

Network Kit provides us to upload or download files with additional features such as multithreaded, concurrent, resumable uploads and downloads. Also, it allows us to perform our network operations quickly and safely. It provides a powerful interacting with Rest APIs and sending synchronous and asynchronous network requests with annotated parameters. Finally, we can use it with other Huawei kits such as hQUIC Kit and Wireless Kit to get faster network traffic.

If you want to learn how to use Network Kit with Rest APIs, you can check my Medium article about it.

Download Manager — Sample App

In this project, we’re going to develop a download manager app that helps users download files quickly and reliably to their devices.

Key features:

  • Start, Pause, Resume or Cancel downloads.
  • Enable or Disable Sliced Download.
  • Set’s the speed limit for downloading a file.
  • Calculate downloaded size/total file size.
  • Calculate and display download speed.
  • Check the progress in the download bar.
  • Support HTTP and HTTPS protocols.
  • Copy URL from clipboard easily.

We started a download task. Then, we paused and resumed it. When the download is finished, it showed a snackbar to notify us.

Setup the Project

We’re not going to go into the details of integrating Huawei HMS Core into a project. You can follow the instructions to integrate HMS Core into your project via official docs or codelab. After integrating HMS Core, let’s add the necessary dependencies.

Add the necessary dependencies to build.gradle (app level).

Let’s add the necessary permissions to our manifest.

We added the Internet Permission to access the Internet and the storage permissions to read and write data to the device memory. Also, we will dynamically request the permissions at runtime for storage permissions on devices that runs Android 6.0 (API Level 23) or higher.

Configure the AndroidManifest file to use clear text traffic

If you try to download a file from an HTTP URL on Android 9.0 (API level 28) or higher, you’ll get an error like this:

ErrorCodeFromException errorcode from resclient: 10000802,message:CLEARTEXT communication to ipv4.download.thinkbroadband.com(your url) not permitted by network security policy

Because cleartext support is disabled by default on Android 9.0 or higher. You should add the android:usesClearTextTraffic="true" flag in the AndroidManifest.xml file. If you don’t want to enable it for all URLs, you can create a network security config file. If you are only working with HTTPS files, you don’t need to add this flag.

Layout File

activity_main.xml is the only layout file in our project. There are:

  • A TextInputEditText to enter URL,
  • Four buttons to control the download process,
  • A button to paste URL to the TextInputEditText,
  • A progress bar to show download status,
  • A seekbar to adjust download speed limit,
  • A checkbox to enable or disable the “Slide Download” feature,
  • TextViews to show various information.

activity_main.xml

MainActivity

Let’s interpret some of the functions on this page.

onCreate() - Firstly we used viewBinding instead of findViewById. It generates a binding class for each XML layout file present in that module. With the instance of a binding class, we can access the view hierarchy with type and null safety.
Then, we initialized the ButtonClickListeners and the ViewChangeListeners. And we create a FileRequestCallback object. We’ll go into the details of this object later.
startDownloadButton() - When the user presses the start download button, it requests permissions at runtime. If the user allows accessing device memory, it will start the download process.
startDownload() - First, we check the downloadManager is initialized or not. Then, we check if there is a download task or not. getRequestStatus function provides us the result status as INIT, PROCESS, PAUSE and, INVALID.

If auto-import is active in your Android Studio, It can import the wrong package for the Result Status. Please make sure to import the "com.huawei.hms.network.file.api.Result" package.

The Builder helps us to create a DownloadManager object. We give a name to our task. If you plan to use the multiple download feature, please be careful to give different names to your download managers.
The DownloadManagerBuilder helps us to create a DownloadManager object. We give a tag to our task. In our app, we only allow single downloading to make it simple. If you plan to use the multiple download feature, please be careful to give different tags to your download managers.

When creating a download request, we need a file path to save our file and a URL to download. Also, we can set a speed limit or enable the slice download.

Currently, you can only set the speed limit for downloading a file. The speed limit value ranges from 1 B/s to 1 GB/s. speedLimit() takes a variable of the type INT as a byte value.

You can enable or disable the sliced download.

Sliced Download: It slices the file into multiple small chunks and downloads them in parallel.

Finally, we start an asynchronous request with downloadManager.start() command. It takes the getRequest and the fileRequestCallback.

FileRequestCallback object contains four callback methods: onStart, onProgress, onSuccess and onException.
onStart -> It will be called when the file download starts. We take the startTime to calculate the remaining download time here.
onProgress -> It will be called when the file download progress changes. We can change the progress status here.

These methods run asynchronously. If we want to update the UI, we should change our thread to the UI thread using the runOnUiThread methods.

onSuccess -> It will be called when file download is completed. We show a snackbar to the user after the file download completes here.
onException -> It will be called when an exception occurs.

onException also is triggered when the download is paused or resumed. If the exception message contains the "10042002" number, it is paused, if it contains the "10042003", it is canceled.

MainActivity.kt

Tips & Tricks

  • According to the Wi-Fi status awareness capability of the Huawei Awareness Kit, you can pause or resume your download task. It will reduce the cost to the user and help to manage your download process properly.
  • Before starting the download task, you can check that you’re connected to the internet using the ConnectivityManager.
  • If the download file has the same name as an existing file, it will overwrite the existing file. Therefore, you should give different names for your files.
  • Even if you minimize the application, the download will continue in the background.

Conclusion

In this article, we have learned how to use Network Kit in your download tasks. And, we’ve developed the Download Manager app that provides many features. In addition to these features, you can also use Network Kit in your upload tasks. Please do not hesitate to ask your questions as a comment. You can also ask your questions via the Huawei Developer Forum.

Thank you for your time and dedication. I hope it was helpful. See you in other articles.

References

Huawei Network Kit Official Documentation
Huawei Network Kit Official Codelab
Huawei Network Kit Official Github

--

--