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:
commit
6ad9bbd367
@ -985,14 +985,8 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
|
||||
boolean finished = false;
|
||||
switch (event.type) {
|
||||
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
|
||||
Toast.makeText(this, res, Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(this, R.string.details_notinstalled, Toast.LENGTH_LONG).show();
|
||||
cleanUpFinishedDownload();
|
||||
finished = true;
|
||||
break;
|
||||
|
@ -29,10 +29,6 @@ public interface ProgressListener {
|
||||
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) {
|
||||
this.type = type;
|
||||
this.progress = progress;
|
||||
|
@ -22,14 +22,16 @@ package org.fdroid.fdroid.net;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.fdroid.fdroid.Hasher;
|
||||
import org.fdroid.fdroid.Preferences;
|
||||
import org.fdroid.fdroid.ProgressListener;
|
||||
import org.fdroid.fdroid.R;
|
||||
import org.fdroid.fdroid.Utils;
|
||||
import org.fdroid.fdroid.compat.FileCompat;
|
||||
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 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 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 Apk curApk;
|
||||
@NonNull private final Context context;
|
||||
@ -198,7 +192,8 @@ public class ApkDownloader implements AsyncDownloader.Listener {
|
||||
dlWrapper.download();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
onErrorDownloading(e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
onErrorDownloading();
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -208,12 +203,6 @@ public class ApkDownloader implements AsyncDownloader.Listener {
|
||||
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...
|
||||
private void sendProgressEvent(Event event) {
|
||||
|
||||
@ -224,14 +213,13 @@ public class ApkDownloader implements AsyncDownloader.Listener {
|
||||
}
|
||||
|
||||
Intent intent = new Intent(ACTION_STATUS);
|
||||
intent.setData(Uri.parse(Utils.getApkUrl(repoAddress, curApk)));
|
||||
intent.putExtras(event.getData());
|
||||
intent.putExtra(EXTRA_URL, Utils.getApkUrl(repoAddress, curApk));
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onErrorDownloading(String localisedExceptionDetails) {
|
||||
Log.e(TAG, "Download failed: " + localisedExceptionDetails);
|
||||
public void onErrorDownloading() {
|
||||
delete(localFile);
|
||||
}
|
||||
|
||||
@ -246,7 +234,8 @@ public class ApkDownloader implements AsyncDownloader.Listener {
|
||||
public void onDownloadComplete() {
|
||||
|
||||
if (!verifyOrDelete(localFile)) {
|
||||
sendError(ERROR_HASH_MISMATCH);
|
||||
sendProgressEvent(new Event(EVENT_ERROR));
|
||||
Toast.makeText(context, R.string.corrupt_download, Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
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_ERROR = 4;
|
||||
private static final String MSG_DATA = "data";
|
||||
|
||||
private final Downloader downloader;
|
||||
private DownloadThread downloadThread;
|
||||
@ -61,7 +59,7 @@ class AsyncDownloadWrapper extends Handler implements AsyncDownloader {
|
||||
listener.onDownloadComplete();
|
||||
break;
|
||||
case MSG_ERROR:
|
||||
listener.onErrorDownloading(message.getData().getString(MSG_DATA));
|
||||
listener.onErrorDownloading();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -76,12 +74,7 @@ class AsyncDownloadWrapper extends Handler implements AsyncDownloader {
|
||||
// ignored
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "I/O exception in download thread", e);
|
||||
Bundle data = new Bundle(1);
|
||||
data.putString(MSG_DATA, e.getLocalizedMessage());
|
||||
Message message = new Message();
|
||||
message.arg1 = MSG_ERROR;
|
||||
message.setData(data);
|
||||
AsyncDownloadWrapper.this.sendMessage(message);
|
||||
sendMessage(MSG_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ import org.fdroid.fdroid.ProgressListener;
|
||||
public interface AsyncDownloader {
|
||||
|
||||
interface Listener extends ProgressListener {
|
||||
void onErrorDownloading(String localisedExceptionDetails);
|
||||
void onErrorDownloading();
|
||||
|
||||
void onDownloadComplete();
|
||||
}
|
||||
|
@ -278,7 +278,7 @@ public class SwapAppsView extends ListView implements
|
||||
// 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
|
||||
// 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)) {
|
||||
resetView();
|
||||
}
|
||||
|
@ -702,16 +702,13 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
||||
lrm.copyApksToRepo();
|
||||
broadcast(TYPE_STATUS, getString(R.string.copying_icons));
|
||||
// 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
|
||||
// run on a main thread.
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
|
||||
new Thread() {
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
public void run() {
|
||||
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
|
||||
lrm.copyIconsToRepo();
|
||||
return null;
|
||||
}
|
||||
}.execute();
|
||||
}.start();
|
||||
|
||||
broadcast(TYPE_COMPLETE);
|
||||
} catch (Exception e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user