Merge branch 'more-downloaderservice-progress' into 'master'

More DownloaderService progress

As part of the incremental approach of moving downloading to an IntentService, here are a few more commits refactoring things into events instead of listerners/callbacks/etc.

ping @pserwylo 

See merge request !240
This commit is contained in:
Hans-Christoph Steiner 2016-03-31 08:15:35 +00:00
commit 6ad9bbd367
7 changed files with 18 additions and 49 deletions

View File

@ -985,14 +985,8 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
boolean finished = false; boolean finished = false;
switch (event.type) { switch (event.type) {
case ApkDownloader.EVENT_ERROR: case ApkDownloader.EVENT_ERROR:
final int res;
if (event.getData().getInt(ApkDownloader.EVENT_DATA_ERROR_TYPE) == ApkDownloader.ERROR_HASH_MISMATCH) {
res = R.string.corrupt_download;
} else {
res = R.string.details_notinstalled;
}
// this must be on the main UI thread // this must be on the main UI thread
Toast.makeText(this, res, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.details_notinstalled, Toast.LENGTH_LONG).show();
cleanUpFinishedDownload(); cleanUpFinishedDownload();
finished = true; finished = true;
break; break;

View File

@ -29,10 +29,6 @@ public interface ProgressListener {
this(type, NO_VALUE, NO_VALUE, null); this(type, NO_VALUE, NO_VALUE, null);
} }
public Event(String type, Bundle data) {
this(type, NO_VALUE, NO_VALUE, data);
}
public Event(String type, int progress, int total, Bundle data) { public Event(String type, int progress, int total, Bundle data) {
this.type = type; this.type = type;
this.progress = progress; this.progress = progress;

View File

@ -22,14 +22,16 @@ package org.fdroid.fdroid.net;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import org.fdroid.fdroid.Hasher; import org.fdroid.fdroid.Hasher;
import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.ProgressListener; import org.fdroid.fdroid.ProgressListener;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.compat.FileCompat; import org.fdroid.fdroid.compat.FileCompat;
import org.fdroid.fdroid.data.Apk; import org.fdroid.fdroid.data.Apk;
@ -54,18 +56,10 @@ public class ApkDownloader implements AsyncDownloader.Listener {
public static final String EVENT_ERROR = "apkDownloadError"; public static final String EVENT_ERROR = "apkDownloadError";
public static final String ACTION_STATUS = "apkDownloadStatus"; public static final String ACTION_STATUS = "apkDownloadStatus";
public static final String EXTRA_URL = "apkDownloadUrl";
public static final int ERROR_HASH_MISMATCH = 101;
private static final String EVENT_SOURCE_ID = "sourceId"; private static final String EVENT_SOURCE_ID = "sourceId";
private static long downloadIdCounter; private static long downloadIdCounter;
/**
* Used as a key to pass data through with an error event, explaining the type of event.
*/
public static final String EVENT_DATA_ERROR_TYPE = "apkDownloadErrorType";
@NonNull private final App app; @NonNull private final App app;
@NonNull private final Apk curApk; @NonNull private final Apk curApk;
@NonNull private final Context context; @NonNull private final Context context;
@ -198,7 +192,8 @@ public class ApkDownloader implements AsyncDownloader.Listener {
dlWrapper.download(); dlWrapper.download();
return true; return true;
} catch (IOException e) { } catch (IOException e) {
onErrorDownloading(e.getLocalizedMessage()); e.printStackTrace();
onErrorDownloading();
} }
return false; return false;
@ -208,12 +203,6 @@ public class ApkDownloader implements AsyncDownloader.Listener {
sendProgressEvent(new ProgressListener.Event(type)); sendProgressEvent(new ProgressListener.Event(type));
} }
private void sendError(int errorType) {
Bundle data = new Bundle(1);
data.putInt(EVENT_DATA_ERROR_TYPE, errorType);
sendProgressEvent(new Event(EVENT_ERROR, data));
}
// TODO: Completely remove progress listener, only use broadcasts... // TODO: Completely remove progress listener, only use broadcasts...
private void sendProgressEvent(Event event) { private void sendProgressEvent(Event event) {
@ -224,14 +213,13 @@ public class ApkDownloader implements AsyncDownloader.Listener {
} }
Intent intent = new Intent(ACTION_STATUS); Intent intent = new Intent(ACTION_STATUS);
intent.setData(Uri.parse(Utils.getApkUrl(repoAddress, curApk)));
intent.putExtras(event.getData()); intent.putExtras(event.getData());
intent.putExtra(EXTRA_URL, Utils.getApkUrl(repoAddress, curApk));
LocalBroadcastManager.getInstance(context).sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
} }
@Override @Override
public void onErrorDownloading(String localisedExceptionDetails) { public void onErrorDownloading() {
Log.e(TAG, "Download failed: " + localisedExceptionDetails);
delete(localFile); delete(localFile);
} }
@ -246,7 +234,8 @@ public class ApkDownloader implements AsyncDownloader.Listener {
public void onDownloadComplete() { public void onDownloadComplete() {
if (!verifyOrDelete(localFile)) { if (!verifyOrDelete(localFile)) {
sendError(ERROR_HASH_MISMATCH); sendProgressEvent(new Event(EVENT_ERROR));
Toast.makeText(context, R.string.corrupt_download, Toast.LENGTH_LONG).show();
return; return;
} }

View File

@ -1,6 +1,5 @@
package org.fdroid.fdroid.net; package org.fdroid.fdroid.net;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.util.Log; import android.util.Log;
@ -13,7 +12,6 @@ class AsyncDownloadWrapper extends Handler implements AsyncDownloader {
private static final int MSG_DOWNLOAD_COMPLETE = 2; private static final int MSG_DOWNLOAD_COMPLETE = 2;
private static final int MSG_ERROR = 4; private static final int MSG_ERROR = 4;
private static final String MSG_DATA = "data";
private final Downloader downloader; private final Downloader downloader;
private DownloadThread downloadThread; private DownloadThread downloadThread;
@ -61,7 +59,7 @@ class AsyncDownloadWrapper extends Handler implements AsyncDownloader {
listener.onDownloadComplete(); listener.onDownloadComplete();
break; break;
case MSG_ERROR: case MSG_ERROR:
listener.onErrorDownloading(message.getData().getString(MSG_DATA)); listener.onErrorDownloading();
break; break;
} }
} }
@ -76,12 +74,7 @@ class AsyncDownloadWrapper extends Handler implements AsyncDownloader {
// ignored // ignored
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "I/O exception in download thread", e); Log.e(TAG, "I/O exception in download thread", e);
Bundle data = new Bundle(1); sendMessage(MSG_ERROR);
data.putString(MSG_DATA, e.getLocalizedMessage());
Message message = new Message();
message.arg1 = MSG_ERROR;
message.setData(data);
AsyncDownloadWrapper.this.sendMessage(message);
} }
} }

View File

@ -5,7 +5,7 @@ import org.fdroid.fdroid.ProgressListener;
public interface AsyncDownloader { public interface AsyncDownloader {
interface Listener extends ProgressListener { interface Listener extends ProgressListener {
void onErrorDownloading(String localisedExceptionDetails); void onErrorDownloading();
void onDownloadComplete(); void onDownloadComplete();
} }

View File

@ -278,7 +278,7 @@ public class SwapAppsView extends ListView implements
// once for each ViewHolder in order to get the repository address for the // once for each ViewHolder in order to get the repository address for the
// apkToInstall. This way, we can wait until we receive an incoming intent (if // apkToInstall. This way, we can wait until we receive an incoming intent (if
// at all) and then lazily load the apk to install. // at all) and then lazily load the apk to install.
String broadcastUrl = intent.getStringExtra(ApkDownloader.EXTRA_URL); String broadcastUrl = intent.getDataString();
if (TextUtils.equals(Utils.getApkUrl(apk.repoAddress, apk), broadcastUrl)) { if (TextUtils.equals(Utils.getApkUrl(apk.repoAddress, apk), broadcastUrl)) {
resetView(); resetView();
} }

View File

@ -702,16 +702,13 @@ public class SwapWorkflowActivity extends AppCompatActivity {
lrm.copyApksToRepo(); lrm.copyApksToRepo();
broadcast(TYPE_STATUS, getString(R.string.copying_icons)); broadcast(TYPE_STATUS, getString(R.string.copying_icons));
// run the icon copy without progress, its not a blocker // run the icon copy without progress, its not a blocker
// TODO: Fix lint error about this being run from a worker thread, says it should be new Thread() {
// run on a main thread.
new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
lrm.copyIconsToRepo(); lrm.copyIconsToRepo();
return null;
} }
}.execute(); }.start();
broadcast(TYPE_COMPLETE); broadcast(TYPE_COMPLETE);
} catch (Exception e) { } catch (Exception e) {