From d1fc05d7b0f234546cb593edfafbf6041aad093b Mon Sep 17 00:00:00 2001 From: Michael Basil Date: Sun, 26 Apr 2015 10:14:08 +0100 Subject: [PATCH] Download [pdffile]_updates.plist when appropriate. #299 --- .../librelio/fragments/PlistGridFragment.java | 2 +- .../librelio/model/dictitem/MagazineItem.java | 6 +++- .../librelio/model/dictitem/PlistItem.java | 10 +++--- .../model/dictitem/UpdatesPlistItem.java | 13 ++++++++ .../service/MagazineDownloadService.java | 31 +++++++++++-------- .../com/librelio/utils/PlistDownloader.java | 12 +++++-- 6 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 main/src/main/java/com/librelio/model/dictitem/UpdatesPlistItem.java diff --git a/main/src/main/java/com/librelio/fragments/PlistGridFragment.java b/main/src/main/java/com/librelio/fragments/PlistGridFragment.java index 5956ba3..18152ec 100644 --- a/main/src/main/java/com/librelio/fragments/PlistGridFragment.java +++ b/main/src/main/java/com/librelio/fragments/PlistGridFragment.java @@ -175,7 +175,7 @@ public void onPause() { private void reloadPlist() { // force a redownload of the plist - PlistDownloader.updateFromServer(getActivity(), plistName, true); + PlistDownloader.updateFromServer(getActivity(), plistName, true, false); // Also try downloading any failed assets AssetDownloadService.startAssetDownloadService(getActivity()); } diff --git a/main/src/main/java/com/librelio/model/dictitem/MagazineItem.java b/main/src/main/java/com/librelio/model/dictitem/MagazineItem.java index b8d54fa..3fdc14e 100644 --- a/main/src/main/java/com/librelio/model/dictitem/MagazineItem.java +++ b/main/src/main/java/com/librelio/model/dictitem/MagazineItem.java @@ -89,7 +89,7 @@ public String getSamplePdfPath() { filePath.length()).replace("_.", "."); } - public String getSamplePdfUrl() { + public String getSampleItemUrl() { return getItemUrl().replace("_.", "."); } @@ -97,6 +97,10 @@ public String getItemFilePath() { return getItemStorageDir(context) + FilenameUtils.getName(filePath); } + public String getSampleFilePath() { + return filePath.replace("_.", "."); + } + @Override public String getItemUrl() { return LibrelioApplication.getAmazonServerUrl() + filePath; diff --git a/main/src/main/java/com/librelio/model/dictitem/PlistItem.java b/main/src/main/java/com/librelio/model/dictitem/PlistItem.java index 29f4fcc..48adf8d 100644 --- a/main/src/main/java/com/librelio/model/dictitem/PlistItem.java +++ b/main/src/main/java/com/librelio/model/dictitem/PlistItem.java @@ -22,22 +22,22 @@ public PlistItem(Context context, String title, String fullFilePath) { this.context = context; this.filePath = fullFilePath; - valuesInit(fullFilePath); + valuesInit(); } - private void valuesInit(String fullFileName) { + void valuesInit() { String actualFileName; Pattern actualFileNamePattern = Pattern.compile("(?=.*\\?)[^\\?]+"); - Matcher actualFileNameMatcher = actualFileNamePattern.matcher(fullFileName); + Matcher actualFileNameMatcher = actualFileNamePattern.matcher(filePath); if (actualFileNameMatcher.find()) { actualFileName = actualFileNameMatcher.group(); } else { - actualFileName = fullFileName; + actualFileName = filePath; } Pattern updateFrequencyPattern = Pattern.compile("waupdate=([0-9]+)"); - Matcher updateFrequencyMatcher = updateFrequencyPattern.matcher(fullFileName); + Matcher updateFrequencyMatcher = updateFrequencyPattern.matcher(filePath); if (updateFrequencyMatcher.find()) { updateFrequency = Integer.parseInt(updateFrequencyMatcher.group(1)); } diff --git a/main/src/main/java/com/librelio/model/dictitem/UpdatesPlistItem.java b/main/src/main/java/com/librelio/model/dictitem/UpdatesPlistItem.java new file mode 100644 index 0000000..04bb0e6 --- /dev/null +++ b/main/src/main/java/com/librelio/model/dictitem/UpdatesPlistItem.java @@ -0,0 +1,13 @@ +package com.librelio.model.dictitem; + +import android.content.Context; + +public class UpdatesPlistItem extends PlistItem { + public UpdatesPlistItem(Context context, String title, String fullFilePath) { + super(context, title, fullFilePath); + + // FIXME valuesInit being run twice + fullFilePath = fullFilePath.replace(".pdf", "_updates.plist"); + valuesInit(); + } +} diff --git a/main/src/main/java/com/librelio/service/MagazineDownloadService.java b/main/src/main/java/com/librelio/service/MagazineDownloadService.java index ef403af..a18642d 100644 --- a/main/src/main/java/com/librelio/service/MagazineDownloadService.java +++ b/main/src/main/java/com/librelio/service/MagazineDownloadService.java @@ -30,6 +30,7 @@ import com.librelio.model.dictitem.MagazineItem; import com.librelio.storage.DataBaseHelper; import com.librelio.storage.DownloadsManager; +import com.librelio.utils.PlistDownloader; import com.niveales.wind.BuildConfig; import com.niveales.wind.R; import com.squareup.okhttp.Request; @@ -106,25 +107,29 @@ public void run() { return; } - String fileUrl = magazine.getItemUrl(); - String filePath = magazine.getItemFilePath(); + String itemUrl = magazine.getItemUrl(); + String itemFilePath = magazine.getItemFilePath(); boolean isDownloadingSample = intent.getBooleanExtra(EXTRA_IS_SAMPLE, false); if (isDownloadingSample) { // If sample - fileUrl = magazine.getSamplePdfUrl(); - filePath = magazine.getSamplePdfPath(); + itemUrl = magazine.getSampleItemUrl(); + itemFilePath = magazine.getSamplePdfPath(); } else if (intent.getBooleanExtra(EXTRA_IS_TEMP_URL, false)) { // If temp url - fileUrl = intent.getStringExtra(EXTRA_TEMP_URL_KEY); + itemUrl = intent.getStringExtra(EXTRA_TEMP_URL_KEY); } - Log.d(TAG, "isDownloadingSample: " + isDownloadingSample + "\nfileUrl: " + fileUrl - + "\nfilePath: " + filePath); + Log.d(TAG, "isDownloadingSample: " + isDownloadingSample + "\nitemUrl: " + itemUrl + + "\nitemFilePath: " + itemFilePath); + + // Start downloading plist update file in background + PlistDownloader.updateFromServer(this, isDownloadingSample ? magazine.getSampleFilePath() : + magazine.getFilePath(), true, true); - String tempFilePath = filePath + TEMP_FILE_SUFFIX; + String tempItemFilePath = itemFilePath + TEMP_FILE_SUFFIX; - Request.Builder requestBuilder = new Request.Builder().url(fileUrl); + Request.Builder requestBuilder = new Request.Builder().url(itemUrl); - File currentFile = new File(tempFilePath); + File currentFile = new File(tempItemFilePath); // FIXME Download never resumes because the magazine directory is deleted just before starting download if (currentFile.exists()) { @@ -147,7 +152,7 @@ public void run() { throw new IOException(String.valueOf(response.code())); } - RandomAccessFile out = new RandomAccessFile(tempFilePath, "rw"); + RandomAccessFile out = new RandomAccessFile(tempItemFilePath, "rw"); byte[] buffer = new byte[BUFFER_SIZE]; @@ -203,8 +208,8 @@ public void run() { .setLabel(magazine.getFilePath()) .setValue(1).build()); - File tempFile = new File(filePath + TEMP_FILE_SUFFIX); - tempFile.renameTo(new File(filePath)); + File tempFile = new File(itemFilePath + TEMP_FILE_SUFFIX); + tempFile.renameTo(new File(itemFilePath)); DownloadsManager.removeDownload(this, magazine); manager.addDownload(magazine, DataBaseHelper.TABLE_DOWNLOADED_ITEMS, true); diff --git a/main/src/main/java/com/librelio/utils/PlistDownloader.java b/main/src/main/java/com/librelio/utils/PlistDownloader.java index c60eb1a..ae6e6e7 100644 --- a/main/src/main/java/com/librelio/utils/PlistDownloader.java +++ b/main/src/main/java/com/librelio/utils/PlistDownloader.java @@ -8,6 +8,7 @@ import com.librelio.event.ReloadPlistEvent; import com.librelio.event.ShowProgressBarEvent; import com.librelio.model.dictitem.PlistItem; +import com.librelio.model.dictitem.UpdatesPlistItem; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.niveales.wind.R; @@ -32,12 +33,17 @@ public class PlistDownloader { private static SimpleDateFormat updateDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); public static void updateFromServer(final Context context, final String plistName, - boolean force) { + boolean force, boolean downloadUpdateFile) { - final PlistItem plistItem = new PlistItem(context, "", plistName); + final PlistItem plistItem; + if (downloadUpdateFile) { + plistItem = new UpdatesPlistItem(context, "", plistName); + } else { + plistItem = new PlistItem(context, "", plistName); + } // Don't update if updates not required - i.e. waupdate=0 - if (plistItem.getUpdateFrequency() == -1) { + if (!force && plistItem.getUpdateFrequency() == -1) { EventBus.getDefault().post(new ReloadPlistEvent(plistName)); return; }