Don't show progress in AppDetails when using a cached download.

The ApkDownloader now returns true or false depending on whether it
is using a cached version on disk, or hitting the network.
I was also a little more defensive about checking for if downloadHandler
is null before deciding to show a progress dialog.
This commit is contained in:
Peter Serwylo 2014-05-22 21:43:00 +09:30
parent 2f7d6f6452
commit 009c8a6be5
2 changed files with 39 additions and 23 deletions

View File

@ -425,7 +425,6 @@ public class AppDetails extends ListActivity implements ProgressListener {
@Override @Override
protected void onResume() { protected void onResume() {
Log.d(TAG, "onresume");
super.onResume(); super.onResume();
// register observer to know when install status changes // register observer to know when install status changes
@ -1021,8 +1020,9 @@ public class AppDetails extends ListActivity implements ProgressListener {
private void startDownload(Apk apk, String repoAddress) { private void startDownload(Apk apk, String repoAddress) {
downloadHandler = new ApkDownloader(apk, repoAddress, Utils.getApkCacheDir(getBaseContext())); downloadHandler = new ApkDownloader(apk, repoAddress, Utils.getApkCacheDir(getBaseContext()));
downloadHandler.setProgressListener(this); downloadHandler.setProgressListener(this);
downloadHandler.download(); if (downloadHandler.download()) {
updateProgressDialog(); updateProgressDialog();
}
} }
private void installApk(File file, String packageName) { private void installApk(File file, String packageName) {
setProgressBarIndeterminateVisibility(true); setProgressBarIndeterminateVisibility(true);
@ -1052,10 +1052,6 @@ public class AppDetails extends ListActivity implements ProgressListener {
@Override @Override
public void run() { public void run() {
if (operation == Installer.InstallerCallback.OPERATION_INSTALL) { if (operation == Installer.InstallerCallback.OPERATION_INSTALL) {
if (downloadHandler != null) {
downloadHandler = null;
}
PackageManagerCompat.setInstaller(mPm, app.id); PackageManagerCompat.setInstaller(mPm, app.id);
} }
@ -1121,8 +1117,13 @@ public class AppDetails extends ListActivity implements ProgressListener {
pd.setOnCancelListener(new DialogInterface.OnCancelListener() { pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override @Override
public void onCancel(DialogInterface dialog) { public void onCancel(DialogInterface dialog) {
downloadHandler.cancel(); Log.d(TAG, "User clicked 'cancel' on download, attempting to interrupt download thread.");
downloadHandler = null; if (downloadHandler != null) {
downloadHandler.cancel();
downloadHandler = null;
} else {
Log.e(TAG, "Tried to cancel, but the downloadHandler doesn't exist.");
}
progressDialog = null; progressDialog = null;
Toast.makeText(AppDetails.this, getString(R.string.download_cancelled), Toast.LENGTH_LONG).show(); Toast.makeText(AppDetails.this, getString(R.string.download_cancelled), Toast.LENGTH_LONG).show();
} }
@ -1148,21 +1149,28 @@ public class AppDetails extends ListActivity implements ProgressListener {
* {@link org.fdroid.fdroid.ProgressListener.Event}. * {@link org.fdroid.fdroid.ProgressListener.Event}.
*/ */
private void updateProgressDialog() { private void updateProgressDialog() {
updateProgressDialog(downloadHandler.getProgress(), downloadHandler.getTotalSize()); if (downloadHandler != null) {
updateProgressDialog(downloadHandler.getProgress(), downloadHandler.getTotalSize());
}
} }
private void updateProgressDialog(int progress, int total) { private void updateProgressDialog(int progress, int total) {
ProgressDialog pd = getProgressDialog(downloadHandler.getRemoteAddress()); if (downloadHandler != null) {
if (total > 0) { ProgressDialog pd = getProgressDialog(downloadHandler.getRemoteAddress());
pd.setIndeterminate(false); if (total > 0) {
pd.setProgress(progress); pd.setIndeterminate(false);
pd.setMax(total); pd.setProgress(progress);
} else { pd.setMax(total);
pd.setIndeterminate(true); } else {
pd.setProgress(progress); pd.setIndeterminate(true);
pd.setMax(0); pd.setProgress(progress);
pd.setMax(0);
}
if (!pd.isShowing()) {
Log.d(TAG, "Showing progress dialog for download.");
pd.show();
}
} }
pd.show();
} }
@Override @Override
@ -1200,7 +1208,7 @@ public class AppDetails extends ListActivity implements ProgressListener {
switch (requestCode) { switch (requestCode) {
case REQUEST_ENABLE_BLUETOOTH: case REQUEST_ENABLE_BLUETOOTH:
fdroidApp.sendViaBluetooth(this, resultCode, app.id); fdroidApp.sendViaBluetooth(this, resultCode, app.id);
break; break;
} }
} }
} }

View File

@ -131,12 +131,17 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
} }
} }
public void download() { /**
* If the download successfully spins up a new thread to start downloading, then we return
* true, otherwise false. This is useful, e.g. when we use a cached version, and so don't
* want to bother with progress dialogs et al.
*/
public boolean download() {
// Can we use the cached version? // Can we use the cached version?
if (verifyOrDeleteCachedVersion()) { if (verifyOrDeleteCachedVersion()) {
sendMessage(EVENT_APK_DOWNLOAD_COMPLETE); sendMessage(EVENT_APK_DOWNLOAD_COMPLETE);
return; return false;
} }
String remoteAddress = getRemoteAddress(); String remoteAddress = getRemoteAddress();
@ -147,12 +152,15 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
Downloader downloader = new HttpDownloader(remoteAddress, localFile); Downloader downloader = new HttpDownloader(remoteAddress, localFile);
dlWrapper = new AsyncDownloadWrapper(downloader, this); dlWrapper = new AsyncDownloadWrapper(downloader, this);
dlWrapper.download(); dlWrapper.download();
return true;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
onErrorDownloading(e.getLocalizedMessage()); onErrorDownloading(e.getLocalizedMessage());
} catch (IOException e) { } catch (IOException e) {
onErrorDownloading(e.getLocalizedMessage()); onErrorDownloading(e.getLocalizedMessage());
} }
return false;
} }
private void sendMessage(String type) { private void sendMessage(String type) {