From 18f3d86b68e32a79a2e8cddddcf580ba5f0443b4 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Thu, 21 Apr 2016 14:15:50 +1000 Subject: [PATCH] Show indeterminite progess for apks queued to download. Previously, navigating back to an app which is in the queue qould indeed grey out the "Install" button and show the text "Downloading..." in that disabled button. However, it woulnd't show any sort of progress. This change shows an indeterminite progress bar with the text "Waiting to start download..." underneath. Happy to receive input on the best terminology if that is not desirable. In order to do this, I had to be more specific about when the header fragment is updated. Previously, `headerFragment.updateViews()` would get called by the `onResumeFragments()` activity method. This was redundant because the `onResume()` method of the fragment also invokes `updateViews()`. Thus, that call was removed (though we still need to obtain a reference to the fragment in `onResumeFragments()`. --- .../java/org/fdroid/fdroid/AppDetails.java | 35 ++++++++++++++++--- .../fdroid/fdroid/net/DownloaderService.java | 26 ++++++++++---- app/src/main/res/values/strings.xml | 1 + 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/AppDetails.java b/app/src/main/java/org/fdroid/fdroid/AppDetails.java index b63f1e8b5..a9288bed2 100644 --- a/app/src/main/java/org/fdroid/fdroid/AppDetails.java +++ b/app/src/main/java/org/fdroid/fdroid/AppDetails.java @@ -426,8 +426,8 @@ public class AppDetails extends AppCompatActivity { @Override protected void onResumeFragments() { super.onResumeFragments(); + headerFragment = (AppDetailsHeaderFragment)getSupportFragmentManager().findFragmentById(R.id.header); refreshApkList(); - refreshHeader(); supportInvalidateOptionsMenu(); if (DownloaderService.isQueuedOrActive(activeDownloadUrlString)) { registerDownloaderReceivers(); @@ -615,8 +615,6 @@ public class AppDetails extends AppCompatActivity { } private void refreshHeader() { - headerFragment = (AppDetailsHeaderFragment) - getSupportFragmentManager().findFragmentById(R.id.header); if (headerFragment != null) { headerFragment.updateViews(); } @@ -1416,17 +1414,44 @@ public class AppDetails extends AppCompatActivity { public void onResume() { super.onResume(); updateViews(); + restoreProgressBarOnResume(); + } + + /** + * After resuming the fragment, decide whether or not we need to show the progress bar. + * Also, put an appropriate message depending on whether or not the download is active or + * just queued. + * + * NOTE: this can't be done in the `updateViews` method as it currently stands. The reason + * is because that method gets called all the time, for all sorts of reasons. The progress + * bar is updated with actual progress values in response to async broadcasts. If we always + * tried to force the progress bar in `updateViews`, it would override the values that were + * set by the async progress broadcasts. + */ + private void restoreProgressBarOnResume() { + if (appDetails.activeDownloadUrlString != null) { + // We don't actually know what the current progress is, so this will show an indeterminate + // progress bar until the first progress/complete event we receive. + final String message = DownloaderService.isQueued(appDetails.activeDownloadUrlString) + ? getString(R.string.download_pending) + : ""; + showIndeterminateProgress(message); + } } /** * Displays empty, indeterminate progress bar and related views. */ public void startProgress() { + showIndeterminateProgress(getString(R.string.download_pending)); + updateViews(); + } + + private void showIndeterminateProgress(String message) { setProgressVisible(true); progressBar.setIndeterminate(true); - progressSize.setText(""); + progressSize.setText(message); progressPercent.setText(""); - updateViews(); } /** diff --git a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java index df1875e86..152f881e4 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java @@ -328,18 +328,32 @@ public class DownloaderService extends Service { } /** - * Check if a URL is waiting in the queue for downloading or if actively - * being downloaded. This is useful for checking whether to re-register - * {@link android.content.BroadcastReceiver}s in - * {@link android.app.Activity#onResume()} + * Check if a URL is waiting in the queue for downloading or if actively being downloaded. + * This is useful for checking whether to re-register {@link android.content.BroadcastReceiver}s + * in {@link android.app.Activity#onResume()}. + * @see DownloaderService#isQueued(String) + * @see DownloaderService#isActive(String) */ public static boolean isQueuedOrActive(String urlString) { + return isQueued(urlString) || isActive(urlString); + } + + /** + * Check if a URL is waiting in the queue for downloading. + */ + public static boolean isQueued(String urlString) { if (TextUtils.isEmpty(urlString)) { return false; } Integer what = QUEUE_WHATS.get(urlString); - return (what != null && serviceHandler.hasMessages(what)) - || (downloader != null && TextUtils.equals(urlString, downloader.sourceUrl.toString())); + return (what != null && serviceHandler.hasMessages(what)); + } + + /** + * Check if a URL is actively being downloaded. + */ + public static boolean isActive(String urlString) { + return downloader != null && TextUtils.equals(urlString, downloader.sourceUrl.toString()); } /** diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e3969b4c6..29a42b952 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -367,6 +367,7 @@ Do you want to uninstall this app? Download completed, tap to install Download unsuccessful + Waiting to start download… New: Provided by %1$s.