From dccbe7ec4a3c3672029364edc506d0ecb4e5d53c Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 15:45:35 +0530 Subject: [PATCH 01/12] error handling added and string formatting replace for timber --- .../feature_downloader/data/Downloader.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java index 214719d2..934ff4e4 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java @@ -92,22 +92,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); } } @@ -136,18 +136,18 @@ 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()); 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, @@ -157,8 +157,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 @@ -198,12 +197,22 @@ private long download(String URL, String name, String description, String subPat }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); @@ -282,13 +291,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()); } } } @@ -357,7 +366,7 @@ public void LogAllLocalData(){ if(cursor!=null) cursor.close(); - Timber.d("Data:"+data); + Timber.d("Data:%s", data); } @Override From 767639e13c2ab7d04d20e0ef6bbe1a4241481a52 Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 15:50:15 +0530 Subject: [PATCH 02/12] string formatting replace for timber --- .../feature_downloader/utils/DownloadObserver.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadObserver.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadObserver.java index c2ea43dd..0e35121f 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadObserver.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadObserver.java @@ -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(){ @@ -37,7 +37,7 @@ 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); @@ -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); } } From b54ee0f315bd0c7282c69b70710d4cef077f5662 Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 16:15:01 +0530 Subject: [PATCH 03/12] log message formatted --- .../audiobook/feature_downloader/utils/downloadUtils.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java index 2120f40a..5590d5b7 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java @@ -6,9 +6,7 @@ import android.database.MatrixCursor; import android.net.Uri; import android.os.Environment; -import android.util.Log; -import com.allsoftdroid.audiobook.feature_downloader.presentation.DownloadManagementActivity; import com.allsoftdroid.audiobook.feature_downloader.data.database.downloadContract; import java.util.ArrayList; @@ -105,7 +103,7 @@ private static String[] DownloadStatus(Cursor cursor){ public static String[] Check_Status(DownloadManager downloadManager, long downloadId) { - String[] statusAndReason = new String[0]; + String[] statusAndReason; DownloadManager.Query downloadQuery = new DownloadManager.Query(); //set the query filter to our previously Enqueued download downloadQuery.setFilterById(downloadId); @@ -131,7 +129,7 @@ public static long DownloadData(DownloadManager downloadManager, long downloadReference; try { - Log.i("DownloadUtils:=>",uri.toString()); + Timber.i(uri.toString()); // Create request for android download manager DownloadManager.Request request = new DownloadManager.Request(uri); @@ -317,6 +315,6 @@ public static void LogAllLocalData(Context context){ if(cursor!=null) cursor.close(); - Timber.d("data:"+data); + Timber.d("data:%s", data); } } \ No newline at end of file From 6948b78fb87faa5628300cd6e1b155d37fe0c0dd Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 16:21:06 +0530 Subject: [PATCH 04/12] method renamed to reflex it's function --- .../audiobook/feature_downloader/data/Downloader.java | 2 +- .../audiobook/feature_downloader/utils/downloadUtils.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java index 934ff4e4..39b8ecc8 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java @@ -179,7 +179,7 @@ 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); + long downloadId= downloadUtils.getDownloadIdIfIsDownloading(mContext,URL); if(downloadId==downloadUtils.DOWNLOADER_NOT_DOWNLOADING){ Uri uri = Uri.parse(URL); Timber.d("DownloaderLOG: =>%s", URL); diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java index 5590d5b7..4a443aa1 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java @@ -140,7 +140,6 @@ public static long DownloadData(DownloadManager downloadManager, request.setDescription(description); //Set the local destination for the downloaded file to a path within the application's external files directory - //request.setDestinationInExternalFilesDir(context,parentDirectoryPath,name); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,subPath+name); //Show notification visibility @@ -155,7 +154,7 @@ public static long DownloadData(DownloadManager downloadManager, return downloadReference; } - public static long isDownloading(Context context, String URL){ + public static long getDownloadIdIfIsDownloading(Context context, String URL){ String[] projection = { downloadContract.downloadEntry.COLUMN_DOWNLOAD_ID }; @@ -188,7 +187,7 @@ public static ArrayList bulkDownload(Context context, DownloadManager down for(int i=0;i0) + if (getDownloadIdIfIsDownloading(context,urls[i])>0) continue; DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urls[i])); From a3ad1a4293425417ec132f40f63d1cf5e210f753 Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 17:16:24 +0530 Subject: [PATCH 05/12] bug fixed: return previous downloadId instead of -99 value. --- .../audiobook/feature_downloader/data/Downloader.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java index 39b8ecc8..4a898bb6 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java @@ -182,7 +182,7 @@ private long download(String URL, String name, String description, String subPat long downloadId= downloadUtils.getDownloadIdIfIsDownloading(mContext,URL); if(downloadId==downloadUtils.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, @@ -193,6 +193,7 @@ private long download(String URL, String name, String description, String subPat if(downloadId !=downloadUtils.DOWNLOADER_PROTOCOL_NOT_SUPPORTED){ + Timber.d("Downloader doesn't support this protocol for file from URL: =>%s", URL); insertDownloadDatabase(downloadId,name,URL); }else { @@ -221,7 +222,9 @@ private long download(String URL, String name, String description, String subPat return downloadUtils.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); + return downloadId; } Timber.d("Downloader2: =>%s",URL); return downloadId; From c1a703b52bd252d5748df1f32b7087bbcf324ef8 Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 17:18:30 +0530 Subject: [PATCH 06/12] observer bug fixed: for already downloaded file skip and call updateDownloaded method. --- .../audiobook/feature_downloader/utils/DownloadObserver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadObserver.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadObserver.java index 0e35121f..d47bddbd 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadObserver.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadObserver.java @@ -42,7 +42,7 @@ public void run() { 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); From 962994993a4a539e9064759c68c1513277d2051d Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 17:20:43 +0530 Subject: [PATCH 07/12] bug fix: url values was wrongly checked with name column, now fixed --- .../audiobook/feature_downloader/utils/downloadUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java index 4a443aa1..2fcaacbb 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java @@ -159,7 +159,7 @@ public static long getDownloadIdIfIsDownloading(Context context, String URL){ downloadContract.downloadEntry.COLUMN_DOWNLOAD_ID }; - String selection= downloadContract.downloadEntry.COLUMN_DOWNLOAD_NAME+" = ?"; + String selection= downloadContract.downloadEntry.COLUMN_DOWNLOAD_URL+" = ?"; String[] selectionArgs={URL}; Cursor cursor = context.getContentResolver().query( downloadContract.downloadEntry.CONTENT_URI, From 248b6d258d1a11950ba191aa81ec55b570c294c1 Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 19:10:40 +0530 Subject: [PATCH 08/12] DownloadStatus.java class added for status code --- .../feature_downloader/utils/DownloadStatus.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadStatus.java diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadStatus.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadStatus.java new file mode 100644 index 00000000..adefc91a --- /dev/null +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/DownloadStatus.java @@ -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; +} From d9f14470a4b017e6597378838d16dc91ac1666b1 Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 19:11:35 +0530 Subject: [PATCH 09/12] status code moved to DownloadStatus.java --- .../feature_downloader/utils/downloadUtils.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java index 2fcaacbb..6f678e02 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/utils/downloadUtils.java @@ -14,11 +14,11 @@ 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; public class downloadUtils { - public static final long DOWNLOADER_PROTOCOL_NOT_SUPPORTED=-444; - public static final long DOWNLOADER_NOT_DOWNLOADING=-145; - public static final long DOWNLOADER_PENDING_ID = 0; private static String[] DownloadStatus(Cursor cursor){ @@ -148,7 +148,7 @@ public static long DownloadData(DownloadManager downloadManager, //Enqueue download and save into referenceId downloadReference = downloadManager.enqueue(request); }catch (Exception e){ - downloadReference = downloadUtils.DOWNLOADER_PROTOCOL_NOT_SUPPORTED; + downloadReference = DOWNLOADER_PROTOCOL_NOT_SUPPORTED; } return downloadReference; @@ -238,7 +238,7 @@ public static MatrixCursor getCustomCursor(Context context){ while (cursor.moveToNext()) { long downloadId = cursor.getLong(cursor.getColumnIndex(downloadContract.downloadEntry.COLUMN_DOWNLOAD_ID)); - if(downloadId!=downloadUtils.DOWNLOADER_PENDING_ID){ + if(downloadId!=DOWNLOADER_PENDING_ID){ query.setFilterById(downloadId); if (downloadManager != null) { Cursor c = downloadManager.query(query); From 5e536613ef9c20c60d33105d71c25f58509d2317 Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 19:12:22 +0530 Subject: [PATCH 10/12] Data structure for LocalFileDetails.java added contain details for locally downloaded file --- .../data/model/LocalFileDetails.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/model/LocalFileDetails.java diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/model/LocalFileDetails.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/model/LocalFileDetails.java new file mode 100644 index 00000000..044d7820 --- /dev/null +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/model/LocalFileDetails.java @@ -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; + } +} From dd21fc413b12769728e62c054fe547b6d13d984c Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 19:13:50 +0530 Subject: [PATCH 11/12] status code imported from DownloaderStatus file and retry logic added if downloaded file is missing --- .../feature_downloader/data/Downloader.java | 77 ++++++++++++++----- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java index 4a898bb6..151d7d3f 100644 --- a/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java +++ b/feature_downloader/src/main/java/com/allsoftdroid/audiobook/feature_downloader/data/Downloader.java @@ -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; @@ -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 { @@ -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"); } @@ -144,8 +149,14 @@ private void downloadOldestRequest() { Download download1 = mDownloadQueue.entrySet().iterator().next().getValue(); 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:%s", file.getAbsolutePath()); @@ -180,7 +191,7 @@ private long download(String URL, String name, String description, String subPat //store downloadId to database for own reference long downloadId= downloadUtils.getDownloadIdIfIsDownloading(mContext,URL); - if(downloadId==downloadUtils.DOWNLOADER_NOT_DOWNLOADING){ + if(downloadId==DOWNLOADER_NOT_DOWNLOADING){ Uri uri = Uri.parse(URL); Timber.d("Downloading file from URL: =>%s", URL); downloadId = downloadUtils.DownloadData( @@ -192,7 +203,7 @@ 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 { @@ -219,13 +230,23 @@ private long download(String URL, String name, String description, String subPat keyStrokeCount.put(URL,count+1); } - return downloadUtils.DOWNLOADER_PROTOCOL_NOT_SUPPORTED; + return DOWNLOADER_PROTOCOL_NOT_SUPPORTED; } }else { 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); - return downloadId; + + 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; } @@ -456,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 From a708f763dc416cf5fdc88960057b1bbcf218b5a9 Mon Sep 17 00:00:00 2001 From: Pravinyo Date: Tue, 21 Apr 2020 19:58:48 +0530 Subject: [PATCH 12/12] progressbar bug fixed --- .../book_details/presentation/AudioBookDetailsFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature_book_details/src/main/java/com/allsoftdroid/feature/book_details/presentation/AudioBookDetailsFragment.kt b/feature_book_details/src/main/java/com/allsoftdroid/feature/book_details/presentation/AudioBookDetailsFragment.kt index 59963399..cc6e6fd7 100644 --- a/feature_book_details/src/main/java/com/allsoftdroid/feature/book_details/presentation/AudioBookDetailsFragment.kt +++ b/feature_book_details/src/main/java/com/allsoftdroid/feature/book_details/presentation/AudioBookDetailsFragment.kt @@ -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) {