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()`.
This commit is contained in:
Peter Serwylo 2016-04-21 14:15:50 +10:00
parent e56d604d6f
commit 18f3d86b68
3 changed files with 51 additions and 11 deletions

View File

@ -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();
}
/**

View File

@ -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());
}
/**

View File

@ -367,6 +367,7 @@
<string name="uninstall_confirm">Do you want to uninstall this app?</string>
<string name="tap_to_install">Download completed, tap to install</string>
<string name="download_error">Download unsuccessful</string>
<string name="download_pending">Waiting to start download…</string>
<string name="perms_new_perm_prefix">New: </string>
<string name="perms_description_app">Provided by %1$s.</string>