Remove one layer of indirection in LocalBraodcasts for updating repo.
This creates a hard dependence between `RepoUpdater` and `UpdateService`. However this could be trivially extracted by moving the helper methods from `UpdateService` to `RepoUpdater`, and making the broadcasts more "repo updater" oriented. That would also require changing the broadcasts which `UpdateService` listens for.
This commit is contained in:
parent
df20d2df8d
commit
b7a20bbf01
@ -25,11 +25,9 @@ package org.fdroid.fdroid;
|
|||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@ -88,33 +86,6 @@ import javax.xml.parsers.SAXParserFactory;
|
|||||||
public class RepoUpdater {
|
public class RepoUpdater {
|
||||||
private static final String TAG = "RepoUpdater";
|
private static final String TAG = "RepoUpdater";
|
||||||
|
|
||||||
/**
|
|
||||||
* @see #EXTRA_URL
|
|
||||||
* @see #EXTRA_BYTES_READ
|
|
||||||
* @see #EXTRA_TOTAL_BYTES
|
|
||||||
*/
|
|
||||||
public static final String ACTION_DOWNLOADING = "org.fdroid.fdroid.RepoUpdater.ACTION_DOWNLOADING";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @see #EXTRA_URL
|
|
||||||
* @see #EXTRA_BYTES_READ
|
|
||||||
* @see #EXTRA_TOTAL_BYTES
|
|
||||||
*/
|
|
||||||
public static final String ACTION_INDEX_PROCESSING = "org.fdroid.fdroid.RepoUpdater.ACTION_INDEX_PROCESSING";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @see #EXTRA_URL
|
|
||||||
* @see #EXTRA_APPS_SAVED
|
|
||||||
* @see #EXTRA_TOTAL_APPS
|
|
||||||
*/
|
|
||||||
public static final String ACTION_SAVING_APPS = "org.fdroid.fdroid.RepoUpdater.ACTION_SAVING_APPS";
|
|
||||||
|
|
||||||
public static final String EXTRA_URL = "URL";
|
|
||||||
public static final String EXTRA_BYTES_READ = "BYTES_READ";
|
|
||||||
public static final String EXTRA_TOTAL_BYTES = "TOTAL_BYTES";
|
|
||||||
public static final String EXTRA_APPS_SAVED = "APPS_SAVED";
|
|
||||||
public static final String EXTRA_TOTAL_APPS = "TOTAL_APPS";
|
|
||||||
|
|
||||||
final String indexUrl;
|
final String indexUrl;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -289,31 +260,19 @@ public class RepoUpdater {
|
|||||||
protected final ProgressListener downloadListener = new ProgressListener() {
|
protected final ProgressListener downloadListener = new ProgressListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) {
|
public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) {
|
||||||
Intent intent = new Intent(ACTION_DOWNLOADING);
|
UpdateService.reportDownloadProgress(context, RepoUpdater.this, bytesRead, totalBytes);
|
||||||
intent.putExtra(EXTRA_URL, indexUrl);
|
|
||||||
intent.putExtra(EXTRA_BYTES_READ, bytesRead);
|
|
||||||
intent.putExtra(EXTRA_TOTAL_BYTES, totalBytes);
|
|
||||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
protected final ProgressListener processIndexListener = new ProgressListener() {
|
protected final ProgressListener processIndexListener = new ProgressListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) {
|
public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) {
|
||||||
Intent intent = new Intent(ACTION_INDEX_PROCESSING);
|
UpdateService.reportProcessIndexProgress(context, RepoUpdater.this, bytesRead, totalBytes);
|
||||||
intent.putExtra(EXTRA_URL, indexUrl);
|
|
||||||
intent.putExtra(EXTRA_BYTES_READ, bytesRead);
|
|
||||||
intent.putExtra(EXTRA_TOTAL_BYTES, totalBytes);
|
|
||||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
protected void notifyProcessingApps(int appsSaved, int totalBytes) {
|
protected void notifyProcessingApps(int appsSaved, int totalApps) {
|
||||||
Intent intent = new Intent(ACTION_SAVING_APPS);
|
UpdateService.reportProcessingAppsProgress(context, this, appsSaved, totalApps);
|
||||||
intent.putExtra(EXTRA_URL, indexUrl);
|
|
||||||
intent.putExtra(EXTRA_APPS_SAVED, appsSaved);
|
|
||||||
intent.putExtra(EXTRA_TOTAL_APPS, totalBytes);
|
|
||||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void notifyCommittingToDb() {
|
protected void notifyCommittingToDb() {
|
||||||
|
@ -177,15 +177,15 @@ public class UpdateService extends IntentService {
|
|||||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(updateStatusReceiver);
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(updateStatusReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sendStatus(Context context, int statusCode) {
|
public static void sendStatus(Context context, int statusCode) {
|
||||||
sendStatus(context, statusCode, null, -1);
|
sendStatus(context, statusCode, null, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sendStatus(Context context, int statusCode, String message) {
|
public static void sendStatus(Context context, int statusCode, String message) {
|
||||||
sendStatus(context, statusCode, message, -1);
|
sendStatus(context, statusCode, message, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sendStatus(Context context, int statusCode, String message, int progress) {
|
public static void sendStatus(Context context, int statusCode, String message, int progress) {
|
||||||
Intent intent = new Intent(LOCAL_ACTION_STATUS);
|
Intent intent = new Intent(LOCAL_ACTION_STATUS);
|
||||||
intent.putExtra(EXTRA_STATUS_CODE, statusCode);
|
intent.putExtra(EXTRA_STATUS_CODE, statusCode);
|
||||||
if (!TextUtils.isEmpty(message)) {
|
if (!TextUtils.isEmpty(message)) {
|
||||||
@ -407,14 +407,10 @@ public class UpdateService extends IntentService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
RepoUpdater updater = new IndexV1Updater(this, repo);
|
RepoUpdater updater = new IndexV1Updater(this, repo);
|
||||||
UpdateBroadcastReceivers receivers = new UpdateBroadcastReceivers(updater);
|
|
||||||
if (Preferences.get().isForceOldIndexEnabled() || !updater.update()) {
|
if (Preferences.get().isForceOldIndexEnabled() || !updater.update()) {
|
||||||
receivers.unregisterReceivers();
|
|
||||||
updater = new RepoUpdater(getBaseContext(), repo);
|
updater = new RepoUpdater(getBaseContext(), repo);
|
||||||
receivers = new UpdateBroadcastReceivers(updater);
|
|
||||||
updater.update();
|
updater.update();
|
||||||
}
|
}
|
||||||
receivers.unregisterReceivers();
|
|
||||||
|
|
||||||
if (updater.hasChanged()) {
|
if (updater.hasChanged()) {
|
||||||
updatedRepos++;
|
updatedRepos++;
|
||||||
@ -503,74 +499,7 @@ public class UpdateService extends IntentService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static void reportDownloadProgress(Context context, RepoUpdater updater, int bytesRead, int totalBytes) {
|
||||||
* Creates three broadcast receivers (downloading, processing index, saving apps) and listens
|
|
||||||
* for relevant broadcasts from them. Calling {@link #unregisterReceivers()} will unregister
|
|
||||||
* all three receivers. If you neglect to unregister them, then they will continually receive
|
|
||||||
* events into the future.
|
|
||||||
*
|
|
||||||
* For each broadcast it receives, it checks the URL of the sender, because it may be possible
|
|
||||||
* to have, e.g. a swap and a regular repo update happening at once.
|
|
||||||
*/
|
|
||||||
private class UpdateBroadcastReceivers {
|
|
||||||
|
|
||||||
private final BroadcastReceiver downloading;
|
|
||||||
private final BroadcastReceiver processingIndex;
|
|
||||||
private final BroadcastReceiver savingApps;
|
|
||||||
|
|
||||||
UpdateBroadcastReceivers(final RepoUpdater updater) {
|
|
||||||
downloading = new BroadcastReceiver() {
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
String url = intent.getStringExtra(RepoUpdater.EXTRA_URL);
|
|
||||||
if (TextUtils.equals(url, updater.indexUrl)) {
|
|
||||||
reportDownloadProgress(updater,
|
|
||||||
intent.getIntExtra(RepoUpdater.EXTRA_BYTES_READ, 0),
|
|
||||||
intent.getIntExtra(RepoUpdater.EXTRA_TOTAL_BYTES, 0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
processingIndex = new BroadcastReceiver() {
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
String url = intent.getStringExtra(RepoUpdater.EXTRA_URL);
|
|
||||||
if (TextUtils.equals(url, updater.indexUrl)) {
|
|
||||||
reportProcessIndexProgress(updater,
|
|
||||||
intent.getIntExtra(RepoUpdater.EXTRA_BYTES_READ, 0),
|
|
||||||
intent.getIntExtra(RepoUpdater.EXTRA_TOTAL_BYTES, 0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
savingApps = new BroadcastReceiver() {
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
String url = intent.getStringExtra(RepoUpdater.EXTRA_URL);
|
|
||||||
if (TextUtils.equals(url, updater.indexUrl)) {
|
|
||||||
reportProcessingAppsProgress(updater,
|
|
||||||
intent.getIntExtra(RepoUpdater.EXTRA_APPS_SAVED, 0),
|
|
||||||
intent.getIntExtra(RepoUpdater.EXTRA_TOTAL_APPS, 0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(UpdateService.this);
|
|
||||||
manager.registerReceiver(savingApps, new IntentFilter(RepoUpdater.ACTION_SAVING_APPS));
|
|
||||||
manager.registerReceiver(processingIndex, new IntentFilter(RepoUpdater.ACTION_INDEX_PROCESSING));
|
|
||||||
manager.registerReceiver(downloading, new IntentFilter(RepoUpdater.ACTION_DOWNLOADING));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unregisterReceivers() {
|
|
||||||
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(UpdateService.this);
|
|
||||||
manager.unregisterReceiver(savingApps);
|
|
||||||
manager.unregisterReceiver(processingIndex);
|
|
||||||
manager.unregisterReceiver(downloading);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void reportDownloadProgress(RepoUpdater updater, int bytesRead, int totalBytes) {
|
|
||||||
Utils.debugLog(TAG, "Downloading " + updater.indexUrl + "(" + bytesRead + "/" + totalBytes + ")");
|
Utils.debugLog(TAG, "Downloading " + updater.indexUrl + "(" + bytesRead + "/" + totalBytes + ")");
|
||||||
String downloadedSizeFriendly = Utils.getFriendlySize(bytesRead);
|
String downloadedSizeFriendly = Utils.getFriendlySize(bytesRead);
|
||||||
int percent = -1;
|
int percent = -1;
|
||||||
@ -579,16 +508,16 @@ public class UpdateService extends IntentService {
|
|||||||
}
|
}
|
||||||
String message;
|
String message;
|
||||||
if (totalBytes == -1) {
|
if (totalBytes == -1) {
|
||||||
message = getString(R.string.status_download_unknown_size, updater.indexUrl, downloadedSizeFriendly);
|
message = context.getString(R.string.status_download_unknown_size, updater.indexUrl, downloadedSizeFriendly);
|
||||||
percent = -1;
|
percent = -1;
|
||||||
} else {
|
} else {
|
||||||
String totalSizeFriendly = Utils.getFriendlySize(totalBytes);
|
String totalSizeFriendly = Utils.getFriendlySize(totalBytes);
|
||||||
message = getString(R.string.status_download, updater.indexUrl, downloadedSizeFriendly, totalSizeFriendly, percent);
|
message = context.getString(R.string.status_download, updater.indexUrl, downloadedSizeFriendly, totalSizeFriendly, percent);
|
||||||
}
|
}
|
||||||
sendStatus(getApplicationContext(), STATUS_INFO, message, percent);
|
sendStatus(context, STATUS_INFO, message, percent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reportProcessIndexProgress(RepoUpdater updater, int bytesRead, int totalBytes) {
|
public static void reportProcessIndexProgress(Context context, RepoUpdater updater, int bytesRead, int totalBytes) {
|
||||||
Utils.debugLog(TAG, "Processing " + updater.indexUrl + "(" + bytesRead + "/" + totalBytes + ")");
|
Utils.debugLog(TAG, "Processing " + updater.indexUrl + "(" + bytesRead + "/" + totalBytes + ")");
|
||||||
String downloadedSize = Utils.getFriendlySize(bytesRead);
|
String downloadedSize = Utils.getFriendlySize(bytesRead);
|
||||||
String totalSize = Utils.getFriendlySize(totalBytes);
|
String totalSize = Utils.getFriendlySize(totalBytes);
|
||||||
@ -596,8 +525,8 @@ public class UpdateService extends IntentService {
|
|||||||
if (totalBytes > 0) {
|
if (totalBytes > 0) {
|
||||||
percent = (int) ((double) bytesRead / totalBytes * 100);
|
percent = (int) ((double) bytesRead / totalBytes * 100);
|
||||||
}
|
}
|
||||||
String message = getString(R.string.status_processing_xml_percent, updater.indexUrl, downloadedSize, totalSize, percent);
|
String message = context.getString(R.string.status_processing_xml_percent, updater.indexUrl, downloadedSize, totalSize, percent);
|
||||||
sendStatus(getApplicationContext(), STATUS_INFO, message, percent);
|
sendStatus(context, STATUS_INFO, message, percent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -606,14 +535,14 @@ public class UpdateService extends IntentService {
|
|||||||
* listener with `totalBytes = 0`. Doing so will result in a message of "Saving app details" sent to the user. If
|
* listener with `totalBytes = 0`. Doing so will result in a message of "Saving app details" sent to the user. If
|
||||||
* you know how many apps you have processed, then a message of "Saving app details (x/total)" is displayed.
|
* you know how many apps you have processed, then a message of "Saving app details (x/total)" is displayed.
|
||||||
*/
|
*/
|
||||||
private void reportProcessingAppsProgress(RepoUpdater updater, int appsSaved, int totalApps) {
|
public static void reportProcessingAppsProgress(Context context, RepoUpdater updater, int appsSaved, int totalApps) {
|
||||||
Utils.debugLog(TAG, "Committing " + updater.indexUrl + "(" + appsSaved + "/" + totalApps + ")");
|
Utils.debugLog(TAG, "Committing " + updater.indexUrl + "(" + appsSaved + "/" + totalApps + ")");
|
||||||
String message;
|
String message;
|
||||||
if (totalApps > 0) {
|
if (totalApps > 0) {
|
||||||
message = getString(R.string.status_inserting_x_apps, appsSaved, totalApps, updater.indexUrl);
|
message = context.getString(R.string.status_inserting_x_apps, appsSaved, totalApps, updater.indexUrl);
|
||||||
} else {
|
} else {
|
||||||
message = getString(R.string.status_inserting_apps);
|
message = context.getString(R.string.status_inserting_apps);
|
||||||
}
|
}
|
||||||
sendStatus(getApplicationContext(), STATUS_INFO, message);
|
sendStatus(context, STATUS_INFO, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user