RxJava2 Demo2- Downloading songs/images using Android Download Manager.

Anshul Jain
MindOrks
Published in
4 min readFeb 21, 2017

This is the 2nd part of the series of posts aimed at writing about Android demo projects using RxJava2

The first one can be found here.

About the Project:

This project is for downloading items(songs, images etc) in Android using RxJava2. There are, however 2 conditions which I have set for downloading.

  • Only 2 items can be downloaded at a time. So even if the user clicks multiple items to download, only 2 of them will be actually downloaded at a time and the rest of the downloads will be en queued.
  • The download percent is shown to the user. But only if the difference between the current percentage and the previously shown percentage is greater than 5 percent.

Let’s dive straight into the code now.
Every downloadable item has the following attributes

Creating a stream of Download Requests:

In the above code snippet, a stream is created using Flowable which will emit Downloadable items. While creating an instance of FlowableOnSubscribe, we get an object of FlowableEmitter in the onSubscribe() method, which will be used for emitting items. The Flowable will keep the items in buffer if the number of items emitted by it is greater than the number of items consumed by the subscriber.

Now as soon, as the user clicks on the download button of the item, the item is emitted from the Flowable.

Creating a Subscriber for consuming Download Requests :

In the above code, we create a Subscriber and then subscribe to the Flowable. While creating an instance of Subscriber, we get an object of Subscription class, which is used for requesting items from the Flowable.

We keep a local variable currentDownloadsCount for tracking the number of current downloads. When we get an item in onNext() method of the subscriber, it means that the subscriber has received an item for download. So we increment the value of the variable by 1. And when the download is completed, we decrement the value by 1.

As soon as a download is completed, a new item from the Flowable stream is requested using the Subscription object.
Do not forget to dispose the subscriber when the activity is destroyed.

if (downloadRequestsSubscription != null) {
downloadRequestsSubscription.cancel();
}

Creating the Download Percent Stream:

In the above code, a stream is created using Observable instead of Flowable(because we don’t need Backpressure strategy) which will emit Downloadable items. While creating an instance of ObservableOnSubscribe, we get an object of ObservableEmitter in the onSubscribe() method, which will be used for emitting items.

When a download is given to the DownloadManager, it updates the status (successful, failed, in progress etc) and the total bytes downloaded for a given downloadable item in its db. To know the download status and percent, we poll the db every 200 ms to get the information about the downloaded item.

After getting the information about the item, we check the current download percent and the last emitted download percent. If the difference is greater than 5 percent or if the current download is 100 percent, then we emit this item, else we skip it. This saves us from unnecessarily updating the UI, in case no substantial percent change happens between two polls.

Creating an Observer and subscribing to it:

Finally, we create the observer and subscribe to it. While creating an instance of Subscriber, we get an object of Disposable class, which is used to dispose the connection to the Observable, once we are done receiving events.

Now as soon as we receive the item in the onNext() function of the observer, we update the UI.

The updateImageDetails() functions is responsible for updating each and every item (in Viewholder) based on its download status and download percentage.

Finally, we dispose the connection once the activity is destroyed.

if (downloadPercentDisposable != null) {
downloadPercentDisposable.dispose();
}

The project is hosted at Github. Please do star the project on Github if you find it useful. It will help me on working on more projects.

Thanks for reading the article. There is a lot of documentation and posts on RxJava but very few practical examples which makes it really hard to grasp its concept. I hope that through this article, I was able to make the concept understand a bit.

Also, lets get connected on LinkedIn.

For more about programming, follow Mindorks , so you’ll get notified when we write new posts.

Check out all the top articles at blog.mindorks.com

--

--

Anshul Jain
MindOrks

ex Android Developer | Machine Learning Developer @ Dailyhunt