From 49635c224d1c2e721e0cda6ad85701a84843ee3b Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 5 Apr 2016 21:15:22 +0200 Subject: [PATCH] show download errors in a Toast There are many possible cryptic errors that can cause downloads to fail. So just show the localized message from the Exception in an extended Toast. These might not show up until AppDetails works better with the lifecycle of downloads, but they are being sent :) closes #496 https://gitlab.com/fdroid/fdroidclient/issues/496 closes #594 https://gitlab.com/fdroid/fdroidclient/issues/594 closes #599 https://gitlab.com/fdroid/fdroidclient/issues/599 --- .../java/org/fdroid/fdroid/AppDetails.java | 9 ++++++++- .../java/org/fdroid/fdroid/net/Downloader.java | 1 + .../fdroid/fdroid/net/DownloaderService.java | 14 +++++++++----- .../fdroid/fdroid/views/swap/SwapAppsView.java | 18 +++++++++++++++++- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/AppDetails.java b/app/src/main/java/org/fdroid/fdroid/AppDetails.java index 6434eaf7c..1725011b7 100644 --- a/app/src/main/java/org/fdroid/fdroid/AppDetails.java +++ b/app/src/main/java/org/fdroid/fdroid/AppDetails.java @@ -515,7 +515,14 @@ public class AppDetails extends AppCompatActivity { private final BroadcastReceiver interruptedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - Toast.makeText(context, R.string.details_notinstalled, Toast.LENGTH_LONG).show(); + if (intent.hasExtra(Downloader.EXTRA_ERROR_MESSAGE)) { + String msg = intent.getStringExtra(Downloader.EXTRA_ERROR_MESSAGE) + + " " + intent.getDataString(); + Toast.makeText(context, R.string.download_error, Toast.LENGTH_SHORT).show(); + Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); + } else { // user canceled + Toast.makeText(context, R.string.details_notinstalled, Toast.LENGTH_LONG).show(); + } cleanUpFinishedDownload(); } }; diff --git a/app/src/main/java/org/fdroid/fdroid/net/Downloader.java b/app/src/main/java/org/fdroid/fdroid/net/Downloader.java index ab5474ffb..86e7f2685 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/Downloader.java +++ b/app/src/main/java/org/fdroid/fdroid/net/Downloader.java @@ -25,6 +25,7 @@ public abstract class Downloader { public static final String EXTRA_DOWNLOAD_PATH = "org.fdroid.fdroid.net.Downloader.extra.DOWNLOAD_PATH"; public static final String EXTRA_BYTES_READ = "org.fdroid.fdroid.net.Downloader.extra.BYTES_READ"; public static final String EXTRA_TOTAL_BYTES = "org.fdroid.fdroid.net.Downloader.extra.TOTAL_BYTES"; + public static final String EXTRA_ERROR_MESSAGE = "org.fdroid.fdroid.net.Downloader.extra.ERROR_MESSAGE"; private volatile boolean cancelled = false; private volatile int bytesRead; 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 aaa6ad423..532a0ffbb 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java @@ -180,7 +180,6 @@ public class DownloaderService extends Service { */ protected void handleIntent(Intent intent) { final Uri uri = intent.getData(); - Log.i(TAG, "handleIntent " + uri); File downloadDir = new File(Utils.getApkCacheDir(this), uri.getHost() + "-" + uri.getPort()); downloadDir.mkdirs(); final SanitizedFile localFile = new SanitizedFile(downloadDir, uri.getLastPathSegment()); @@ -190,7 +189,6 @@ public class DownloaderService extends Service { downloader.setListener(new Downloader.DownloaderProgressListener() { @Override public void sendProgress(URL sourceUrl, int bytesRead, int totalBytes) { - //Log.i(TAG, "sendProgress " + sourceUrl + " " + bytesRead + " / " + totalBytes); Intent intent = new Intent(Downloader.ACTION_PROGRESS); intent.setData(uri); intent.putExtra(Downloader.EXTRA_BYTES_READ, bytesRead); @@ -203,22 +201,28 @@ public class DownloaderService extends Service { } catch (InterruptedException e) { sendBroadcast(uri, Downloader.ACTION_INTERRUPTED, localFile); } catch (IOException e) { - sendBroadcast(uri, Downloader.ACTION_INTERRUPTED, localFile); e.printStackTrace(); + sendBroadcast(uri, Downloader.ACTION_INTERRUPTED, localFile, + e.getLocalizedMessage()); } finally { if (downloader != null) { downloader.close(); } } downloader = null; - Log.i(TAG, "handleIntent DONE " + uri); } private void sendBroadcast(Uri uri, String action, File file) { - Log.i(TAG, "sendBroadcast " + uri + " " + action + " " + file); + sendBroadcast(uri, action, file, null); + } + + private void sendBroadcast(Uri uri, String action, File file, String errorMessage) { Intent intent = new Intent(action); intent.setData(uri); intent.putExtra(Downloader.EXTRA_DOWNLOAD_PATH, file.getAbsolutePath()); + if (!TextUtils.isEmpty(errorMessage)) { + intent.putExtra(Downloader.EXTRA_ERROR_MESSAGE, errorMessage); + } localBroadcastManager.sendBroadcast(intent); } diff --git a/app/src/main/java/org/fdroid/fdroid/views/swap/SwapAppsView.java b/app/src/main/java/org/fdroid/fdroid/views/swap/SwapAppsView.java index 190cd747e..ef4080b87 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/swap/SwapAppsView.java +++ b/app/src/main/java/org/fdroid/fdroid/views/swap/SwapAppsView.java @@ -36,6 +36,7 @@ import android.widget.ImageView; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; @@ -268,6 +269,21 @@ public class SwapAppsView extends ListView implements } }; + private final BroadcastReceiver interruptedReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.hasExtra(Downloader.EXTRA_ERROR_MESSAGE)) { + String msg = intent.getStringExtra(Downloader.EXTRA_ERROR_MESSAGE) + + " " + intent.getDataString(); + Toast.makeText(context, R.string.download_error, Toast.LENGTH_SHORT).show(); + Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); + } else { // user canceled + Toast.makeText(context, R.string.details_notinstalled, Toast.LENGTH_LONG).show(); + } + resetView(); + } + }; + private final ContentObserver appObserver = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { @@ -293,7 +309,7 @@ public class SwapAppsView extends ListView implements DownloaderService.getIntentFilter(url, Downloader.ACTION_PROGRESS)); localBroadcastManager.registerReceiver(appListViewResetReceiver, DownloaderService.getIntentFilter(url, Downloader.ACTION_COMPLETE)); - localBroadcastManager.registerReceiver(appListViewResetReceiver, + localBroadcastManager.registerReceiver(interruptedReceiver, DownloaderService.getIntentFilter(url, Downloader.ACTION_INTERRUPTED)); }