Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple download bug #77

Merged
merged 12 commits into from
Apr 21, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class AudioBookDetailsFragment : BaseContainerFragment(),KoinComponent {

private fun removeLoading() {
setVisibility(dataBindingReference.networkNoConnection,set = false)
setVisibility(dataBindingReference.pbContentLoading,set = false)
// setVisibility(dataBindingReference.pbContentLoading,set = false)
}

private fun handleDownloaderEvent(event: Event<DownloadEvent>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.allsoftdroid.audiobook.feature_downloader.data.config.ProviderConfig;
import com.allsoftdroid.audiobook.feature_downloader.data.database.downloadContract.downloadEntry;
import com.allsoftdroid.audiobook.feature_downloader.data.model.LocalFileDetails;
import com.allsoftdroid.audiobook.feature_downloader.domain.IDownloader;
import com.allsoftdroid.audiobook.feature_downloader.utils.DownloadObserver;
import com.allsoftdroid.audiobook.feature_downloader.utils.downloadUtils;
Expand All @@ -40,6 +41,10 @@
import timber.log.Timber;

import static android.content.Context.DOWNLOAD_SERVICE;
import static com.allsoftdroid.audiobook.feature_downloader.utils.DownloadStatus.DOWNLOADER_NOT_DOWNLOADING;
import static com.allsoftdroid.audiobook.feature_downloader.utils.DownloadStatus.DOWNLOADER_PENDING_ID;
import static com.allsoftdroid.audiobook.feature_downloader.utils.DownloadStatus.DOWNLOADER_PROTOCOL_NOT_SUPPORTED;
import static com.allsoftdroid.audiobook.feature_downloader.utils.DownloadStatus.DOWNLOADER_RE_DOWNLOAD;
import static com.allsoftdroid.audiobook.feature_downloader.utils.Utility.STATUS_SUCCESS;

public class Downloader implements IDownloader {
Expand Down Expand Up @@ -92,22 +97,22 @@ public void handleDownloadEvent(DownloadEvent downloadEvent) {
mDownloadEventStore.publish(
new Event<>(new Cancelled(cancel.getBookId(), cancel.getChapterIndex(), cancel.getFileUrl()))
);
Timber.d("Cancelled event sent for "+cancel.toString());
Timber.d("Cancelled event sent for %s", cancel.toString());

}else if(downloadEvent instanceof Download){
Download temp = (Download) downloadEvent;
addToDownloadQueueRequest(temp);

Timber.d("Downloading event sent for "+temp.toString());
Timber.d("Downloading event sent for %s", temp.toString());
}
else if(downloadEvent instanceof Downloaded){
Downloaded downloaded = (Downloaded) downloadEvent;
Timber.d("Downloaded event received for "+downloaded.toString());
Timber.d("Downloaded event received for %s", downloaded.toString());
downloadNext(downloaded.getUrl());

}
else{
Timber.d("Download event value: " + downloadEvent);
Timber.d("Download event value: %s", downloadEvent);
}
}

Expand All @@ -118,7 +123,7 @@ private void addToDownloadQueueRequest(Download obj) {
Timber.d("Downloading as it is first request");
downloadOldestRequest();
}else{
insertDownloadDatabase(downloadUtils.DOWNLOADER_PENDING_ID,obj.getName(),obj.getUrl());
insertDownloadDatabase(DOWNLOADER_PENDING_ID,obj.getName(),obj.getUrl());
Timber.d("Added to download queue");
}

Expand All @@ -136,18 +141,24 @@ private void downloadNext(String removeUrl) {
downloadOldestRequest();
}

Timber.d("Staring new Download, Removing URL:"+removeUrl);
Timber.d("Staring new Download, Removing URL:%s", removeUrl);
}

private void downloadOldestRequest() {
if(isDownloading) return;

Download download1 = mDownloadQueue.entrySet().iterator().next().getValue();
Timber.d("New Download event received: "+download1.toString());
Timber.d("New Download event received: %s", download1.toString());

long downloadId = download(download1.getUrl(),download1.getName(),download1.getDescription(),download1.getSubPath());

if(downloadId == DOWNLOADER_RE_DOWNLOAD){
Timber.d("Re-downloading as file appear to be missing from local storage");
downloadId = download(download1.getUrl(),download1.getName(),download1.getDescription(),download1.getSubPath());
}

File file = new File(Environment.DIRECTORY_DOWNLOADS,download1.getSubPath()+download1.getName());
Timber.d("File path:"+file.getAbsolutePath());
Timber.d("File path:%s", file.getAbsolutePath());

mDownloadObserver = new DownloadObserver(
this,
Expand All @@ -157,8 +168,7 @@ private void downloadOldestRequest() {
download1.getUrl(),downloadId);
mDownloadObserver.startWatching();

Timber.d("File tracker attached for New Download: "+download1.toString());
file = null;
Timber.d("File tracker attached for New Download: %s", download1.toString());
}

@Override
Expand All @@ -180,10 +190,10 @@ public void updateProgress(String mUrl,String mBookId,int mChapterIndex,long pro
private long download(String URL, String name, String description, String subPath){

//store downloadId to database for own reference
long downloadId= downloadUtils.isDownloading(mContext,URL);
if(downloadId==downloadUtils.DOWNLOADER_NOT_DOWNLOADING){
long downloadId= downloadUtils.getDownloadIdIfIsDownloading(mContext,URL);
if(downloadId==DOWNLOADER_NOT_DOWNLOADING){
Uri uri = Uri.parse(URL);
Timber.d("DownloaderLOG: =>%s", URL);
Timber.d("Downloading file from URL: =>%s", URL);
downloadId = downloadUtils.DownloadData(
downloadManager,
uri,
Expand All @@ -193,27 +203,50 @@ private long download(String URL, String name, String description, String subPat
);


if(downloadId !=downloadUtils.DOWNLOADER_PROTOCOL_NOT_SUPPORTED){
if(downloadId !=DOWNLOADER_PROTOCOL_NOT_SUPPORTED){
Timber.d("Downloader doesn't support this protocol for file from URL: =>%s", URL);
insertDownloadDatabase(downloadId,name,URL);
}else {

//TODO: way to clear database items
int count = keyStrokeCount.containsKey(URL)?keyStrokeCount.get(URL):0;
int count = 0;
if(keyStrokeCount.containsKey(URL)){
try{
count = keyStrokeCount.get(URL);
}catch (NullPointerException e){
e.printStackTrace();
}
}

if(count >=2 ){
Toast.makeText(mContext,"file link Copied",Toast.LENGTH_SHORT).show();
ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(name, URL);
clipboard.setPrimaryClip(clip);
if(clipboard!=null){
ClipData clip = ClipData.newPlainText(name, URL);
clipboard.setPrimaryClip(clip);
}
}else{
if(count == 0) Toast.makeText(mContext,"Downloading Problem, Double Tap to copy URL",Toast.LENGTH_SHORT).show();
keyStrokeCount.put(URL,count+1);
}

return downloadUtils.DOWNLOADER_PROTOCOL_NOT_SUPPORTED;
return DOWNLOADER_PROTOCOL_NOT_SUPPORTED;
}
}else {
return -99;
Timber.d("It's appears that file is already downloaded from URL: =>%s", URL);
Timber.d("Downloader return id %s for URL =>%s", downloadId ,URL);

if(isFileLocallyExists(downloadId)){
Timber.d("No need to download file already exist");
return downloadId;
}else{
Timber.d("Need to download again as file is missing from local");
cancelDownload(downloadId);
removeFromDownloadDatabase(downloadId);
return DOWNLOADER_RE_DOWNLOAD;
}
}

Timber.d("Downloader2: =>%s",URL);
return downloadId;
}
Expand Down Expand Up @@ -282,13 +315,13 @@ private void insertDownloadDatabase(long downloadId,String name,String url){
String id = cursor.getString(cursor.getColumnIndex(downloadEntry._ID));
Uri currentDownloadUri = ContentUris.withAppendedId(downloadEntry.CONTENT_URI, Long.parseLong(id));
mContext.getContentResolver().update(currentDownloadUri,values,selection,selectionArgs);
Timber.d("Updated at:"+currentDownloadUri.toString());
Timber.d("Updated at:%s", currentDownloadUri.toString());
cursor.close();
}else {
//It is new entry and we will add new in table
Uri newUri = mContext.getContentResolver().insert(downloadEntry.CONTENT_URI,values);
if(newUri!=null){
Timber.d("Inserted at:"+newUri.toString());
Timber.d("Inserted at:%s", newUri.toString());
}
}
}
Expand Down Expand Up @@ -357,7 +390,7 @@ public void LogAllLocalData(){

if(cursor!=null) cursor.close();

Timber.d("Data:"+data);
Timber.d("Data:%s", data);
}

@Override
Expand Down Expand Up @@ -444,33 +477,49 @@ public long getDownloadIdByURL(String url){
@Override
public void openDownloadedFile(Context context, long downloadId) {

DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor cursor = downloadManager.query(query);
LocalFileDetails localFileDetails = getLocalFileUriForDownloadId(downloadId);

if(localFileDetails!=null){
Intent myIntent = new Intent(Intent.ACTION_VIEW);
if(Build.VERSION.SDK_INT>=24){
File newFile = new File(Objects.requireNonNull(localFileDetails.getLocalUri().getPath()));
Uri contentUri = FileProvider.getUriForFile(context, ProviderConfig.PROVIDER_AUTHORITY, newFile);
myIntent.setDataAndType(contentUri, localFileDetails.getMimeType());
myIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {
myIntent.setDataAndType(localFileDetails.getLocalUri(), localFileDetails.getMimeType());
}

Intent open = Intent.createChooser(myIntent, "Choose an application to open with:");
context.startActivity(open);
}
}

private LocalFileDetails getLocalFileUriForDownloadId(long downloadId){
Cursor cursor = query(downloadId);

if (cursor!=null && cursor.moveToFirst()) {
int downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
String downloadLocalUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
String downloadMimeType = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
if ((downloadStatus == DownloadManager.STATUS_SUCCESSFUL) && downloadLocalUri != null) {
openFile(context, Uri.parse(downloadLocalUri), downloadMimeType);
return new LocalFileDetails(downloadLocalUri,downloadMimeType);
}
}

if(cursor!=null) cursor.close();
return null;
}

private void openFile(Context context,Uri item ,String mimeType){
Intent myIntent = new Intent(Intent.ACTION_VIEW);
if(Build.VERSION.SDK_INT>=24){
File newFile = new File(Objects.requireNonNull(item.getPath()));
Uri contentUri = FileProvider.getUriForFile(context, ProviderConfig.PROVIDER_AUTHORITY, newFile);
myIntent.setDataAndType(contentUri, mimeType);
myIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {
myIntent.setDataAndType(item, mimeType);
private boolean isFileLocallyExists(long mDownloadId){
LocalFileDetails localFileDetails = getLocalFileUriForDownloadId(mDownloadId);

if(localFileDetails!=null){
File localFile = new File(Objects.requireNonNull(localFileDetails.getLocalUri().getPath()));
return localFile.isFile();
}

Intent open = Intent.createChooser(myIntent, "Choose an application to open with:");
context.startActivity(open);
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.allsoftdroid.audiobook.feature_downloader.data.model;

import android.net.Uri;

import androidx.annotation.NonNull;

import org.jetbrains.annotations.NotNull;

public class LocalFileDetails {

@NonNull
private Uri localUri;

private String mimeType;

public LocalFileDetails(@NotNull String uri, String mime){
this.localUri = Uri.parse(uri);
this.mimeType = mime;
}

@NonNull
public Uri getLocalUri() {
return localUri;
}

public String getMimeType() {
return mimeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public DownloadObserver(Downloader downloader, String path, String bookId, int c
mUrl = url;
mDownloadId = download_id;
mDownloader = downloader;
Timber.d("File path:"+path);
Timber.d("File path:%s", path);
}

public void startWatching(){
Expand All @@ -37,12 +37,12 @@ public void run() {
String[] response = mDownloader.getStatusByDownloadId(mDownloadId);

if (response==null || response.length==0){
Timber.d("No response for this downloadId:"+mDownloader);
Timber.d("No response for this downloadId:%s", mDownloader);
if(mDownloader.getProgress(mDownloadId)!=null){
downloadRunning();
}else handler.postDelayed(this,checker_delay_time);
}else if(response[0].equals(Utility.STATUS_SUCCESS)){
downloadRunning();
mDownloader.updateDownloaded(mUrl,mBookId,mChapterIndex);
}
else if(response[0].equals(Utility.STATUS_RUNNING)){
long[] progress = mDownloader.getProgress(mDownloadId);
Expand Down Expand Up @@ -94,6 +94,6 @@ public void run() {
public void stopWatching() {
handler.removeCallbacksAndMessages(null);
mDownloader = null;
Timber.d("Tracker removed for fileUrl: "+mUrl);
Timber.d("Tracker removed for fileUrl: %s", mUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.allsoftdroid.audiobook.feature_downloader.utils;

public interface DownloadStatus {
long DOWNLOADER_PROTOCOL_NOT_SUPPORTED=-444;
long DOWNLOADER_NOT_DOWNLOADING=-145;
long DOWNLOADER_PENDING_ID = 0;
long DOWNLOADER_RE_DOWNLOAD = -99;
}
Loading