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
This commit is contained in:
Hans-Christoph Steiner 2016-04-05 21:15:22 +02:00
parent 3265c8634c
commit 49635c224d
4 changed files with 35 additions and 7 deletions

View File

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

View File

@ -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;

View File

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

View File

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