From b7a20bbf01f7cf12e5f0ebb02960354e1bf261a6 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Mon, 3 Jul 2017 08:59:00 +1000 Subject: [PATCH] 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. --- .../java/org/fdroid/fdroid/RepoUpdater.java | 49 +-------- .../java/org/fdroid/fdroid/UpdateService.java | 99 +++---------------- 2 files changed, 18 insertions(+), 130 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/RepoUpdater.java b/app/src/main/java/org/fdroid/fdroid/RepoUpdater.java index b74c530b6..b63424f70 100644 --- a/app/src/main/java/org/fdroid/fdroid/RepoUpdater.java +++ b/app/src/main/java/org/fdroid/fdroid/RepoUpdater.java @@ -25,11 +25,9 @@ package org.fdroid.fdroid; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; -import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.support.annotation.NonNull; -import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; @@ -88,33 +86,6 @@ import javax.xml.parsers.SAXParserFactory; public class 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; @NonNull @@ -289,31 +260,19 @@ public class RepoUpdater { protected final ProgressListener downloadListener = new ProgressListener() { @Override public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) { - Intent intent = new Intent(ACTION_DOWNLOADING); - intent.putExtra(EXTRA_URL, indexUrl); - intent.putExtra(EXTRA_BYTES_READ, bytesRead); - intent.putExtra(EXTRA_TOTAL_BYTES, totalBytes); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); + UpdateService.reportDownloadProgress(context, RepoUpdater.this, bytesRead, totalBytes); } }; protected final ProgressListener processIndexListener = new ProgressListener() { @Override public void onProgress(URL sourceUrl, int bytesRead, int totalBytes) { - Intent intent = new Intent(ACTION_INDEX_PROCESSING); - intent.putExtra(EXTRA_URL, indexUrl); - intent.putExtra(EXTRA_BYTES_READ, bytesRead); - intent.putExtra(EXTRA_TOTAL_BYTES, totalBytes); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); + UpdateService.reportProcessIndexProgress(context, RepoUpdater.this, bytesRead, totalBytes); } }; - protected void notifyProcessingApps(int appsSaved, int totalBytes) { - Intent intent = new Intent(ACTION_SAVING_APPS); - intent.putExtra(EXTRA_URL, indexUrl); - intent.putExtra(EXTRA_APPS_SAVED, appsSaved); - intent.putExtra(EXTRA_TOTAL_APPS, totalBytes); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); + protected void notifyProcessingApps(int appsSaved, int totalApps) { + UpdateService.reportProcessingAppsProgress(context, this, appsSaved, totalApps); } protected void notifyCommittingToDb() { diff --git a/app/src/main/java/org/fdroid/fdroid/UpdateService.java b/app/src/main/java/org/fdroid/fdroid/UpdateService.java index b4a997609..701d5ee43 100644 --- a/app/src/main/java/org/fdroid/fdroid/UpdateService.java +++ b/app/src/main/java/org/fdroid/fdroid/UpdateService.java @@ -177,15 +177,15 @@ public class UpdateService extends IntentService { 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); } - 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); } - 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.putExtra(EXTRA_STATUS_CODE, statusCode); if (!TextUtils.isEmpty(message)) { @@ -407,14 +407,10 @@ public class UpdateService extends IntentService { try { RepoUpdater updater = new IndexV1Updater(this, repo); - UpdateBroadcastReceivers receivers = new UpdateBroadcastReceivers(updater); if (Preferences.get().isForceOldIndexEnabled() || !updater.update()) { - receivers.unregisterReceivers(); updater = new RepoUpdater(getBaseContext(), repo); - receivers = new UpdateBroadcastReceivers(updater); updater.update(); } - receivers.unregisterReceivers(); if (updater.hasChanged()) { updatedRepos++; @@ -503,74 +499,7 @@ public class UpdateService extends IntentService { } } - /** - * 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) { + public static void reportDownloadProgress(Context context, RepoUpdater updater, int bytesRead, int totalBytes) { Utils.debugLog(TAG, "Downloading " + updater.indexUrl + "(" + bytesRead + "/" + totalBytes + ")"); String downloadedSizeFriendly = Utils.getFriendlySize(bytesRead); int percent = -1; @@ -579,16 +508,16 @@ public class UpdateService extends IntentService { } String message; 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; } else { 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 + ")"); String downloadedSize = Utils.getFriendlySize(bytesRead); String totalSize = Utils.getFriendlySize(totalBytes); @@ -596,8 +525,8 @@ public class UpdateService extends IntentService { if (totalBytes > 0) { percent = (int) ((double) bytesRead / totalBytes * 100); } - String message = getString(R.string.status_processing_xml_percent, updater.indexUrl, downloadedSize, totalSize, percent); - sendStatus(getApplicationContext(), STATUS_INFO, message, percent); + String message = context.getString(R.string.status_processing_xml_percent, updater.indexUrl, downloadedSize, totalSize, 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 * 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 + ")"); String message; 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 { - 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); } }