DownloaderService: convert String to Uri to avoid repeated parsing

This parses the String into a Uri once per Intent, rather than once per
broadcast.  The Uri instance is also nicer to work with, since it is the
native URL format for Intents.

It should make the progress updates a bit more efficient also.
fdroid/fdroidclient#1742
This commit is contained in:
Hans-Christoph Steiner 2019-03-27 12:04:25 +01:00
parent 288577407d
commit 7f22c3c221
2 changed files with 13 additions and 7 deletions

View File

@ -113,10 +113,16 @@ public class ApkCache {
}
/**
* Get the full path for where an APK URL will be downloaded into.
* Get the full path for where an package URL will be downloaded into.
*/
public static SanitizedFile getApkDownloadPath(Context context, String urlString) {
Uri uri = Uri.parse(urlString);
return getApkDownloadPath(context, Uri.parse(urlString));
}
/**
* Get the full path for where an package URL will be downloaded into.
*/
public static SanitizedFile getApkDownloadPath(Context context, Uri uri) {
File dir = new File(getApkCacheDir(context), uri.getHost() + "-" + uri.getPort());
if (!dir.exists()) {
dir.mkdirs();

View File

@ -200,8 +200,8 @@ public class DownloaderService extends Service {
*/
private void handleIntent(Intent intent) {
final Uri uri = intent.getData();
long repoId = intent.getLongExtra(Downloader.EXTRA_REPO_ID, 0);
String canonicalUrl = intent.getStringExtra(Downloader.EXTRA_CANONICAL_URL);
final long repoId = intent.getLongExtra(Downloader.EXTRA_REPO_ID, 0);
final Uri canonicalUrl = Uri.parse(intent.getStringExtra(Downloader.EXTRA_CANONICAL_URL));
final SanitizedFile localFile = ApkCache.getApkDownloadPath(this, canonicalUrl);
sendBroadcast(uri, Downloader.ACTION_STARTED, localFile, repoId, canonicalUrl);
@ -249,7 +249,7 @@ public class DownloaderService extends Service {
sendBroadcast(uri, action, null, null);
}
private void sendBroadcast(Uri uri, String action, File file, long repoId, String canonicalUrl) {
private void sendBroadcast(Uri uri, String action, File file, long repoId, Uri canonicalUrl) {
sendBroadcast(uri, action, file, null, repoId, canonicalUrl);
}
@ -258,10 +258,10 @@ public class DownloaderService extends Service {
}
private void sendBroadcast(Uri uri, String action, File file, String errorMessage, long repoId,
String canonicalUrl) {
Uri canonicalUrl) {
Intent intent = new Intent(action);
if (canonicalUrl != null) {
intent.setData(Uri.parse(canonicalUrl));
intent.setData(canonicalUrl);
}
if (file != null) {
intent.putExtra(Downloader.EXTRA_DOWNLOAD_PATH, file.getAbsolutePath());