Skip to content

Commit

Permalink
Download [pdffile]_updates.plist when appropriate. #299
Browse files Browse the repository at this point in the history
  • Loading branch information
intrications committed Apr 26, 2015
1 parent 514099f commit d1fc05d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,18 @@ public String getSamplePdfPath() {
filePath.length()).replace("_.", ".");
}

public String getSamplePdfUrl() {
public String getSampleItemUrl() {
return getItemUrl().replace("_.", ".");
}

public String getItemFilePath() {
return getItemStorageDir(context) + FilenameUtils.getName(filePath);
}

public String getSampleFilePath() {
return filePath.replace("_.", ".");
}

@Override
public String getItemUrl() {
return LibrelioApplication.getAmazonServerUrl() + filePath;
Expand Down
10 changes: 5 additions & 5 deletions main/src/main/java/com/librelio/model/dictitem/PlistItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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];

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions main/src/main/java/com/librelio/utils/PlistDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit d1fc05d

Please sign in to comment.