Skip to content

Commit

Permalink
Some Fetcher cleanups. Added CACHE_1D option which could possibly…
Browse files Browse the repository at this point in the history
… replace some "should I refresh now?" logic still in Giggity/Db right now.
  • Loading branch information
Wilm0r committed Sep 4, 2023
1 parent a378296 commit aa7ff3b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/net/gaast/giggity/Fetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class Fetcher {
public enum Source {
DEFAULT, /* Check online (304 -> cache, and fail if we're offline). */
CACHE_ONLY, /* Get from cache or fail. */
CACHE, /* Get from cache, allow fetch if not available. */
CACHE, /* Get from cache, allow fetch if not available. (Will fetch ~yearly actually) */
CACHE_1D, /* Get from cache but refresh once a day. */
CACHE_IF_OFFLINE, /* Check online if we're not offline, otherwise use cache. */
}

Expand Down Expand Up @@ -106,16 +107,23 @@ public Fetcher(Giggity app_, String url, Source source, String type_) throws IOE
}
switch (source) {
case DEFAULT:
dlc.addRequestProperty("Cache-Control", "max-age=0");
// TODO: This shouldn't really be default, but maybe add a REFRESH option to ignore
// server expiry/max-age and reload now?
// dlc.addRequestProperty("Cache-Control", "max-age=0");
break;
case CACHE_ONLY:
dlc.addRequestProperty("Cache-Control", "only-if-cached");
// 10y (though apparently I could just omit the argument?)
dlc.addRequestProperty("Cache-Control", "max-stale=" + (3650 * 24 * 60 * 60));
break;
case CACHE:
// Add 1y to whatever the server said. Practically rely on cache forever.
dlc.addRequestProperty("Cache-Control", "max-stale=" + (365 * 24 * 60 * 60)); // 1y
break;
case CACHE_1D:
// Rely on cache with daily refreshes, regardless of what the server said.
dlc.addRequestProperty("Cache-Control", "max-age=" + (24 * 60 * 60)); // 1d
break;
}

Log.d("Fetcher", "HTTP status " + dlc.getHeaderField(0));
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/net/gaast/giggity/Giggity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.FileUtils;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -320,4 +323,17 @@ public void onClick(DialogInterface dialogInterface, int i) {
})
.show();
}

public static void copy(InputStream in, OutputStream out) throws IOException {
// Delete this again once support for API <29 is dropped.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q /* 29 */) {
FileUtils.copy(in, out);
} else {
byte[] yo = new byte[4096];
int st;
while ((st = in.read(yo)) != -1) {
out.write(yo, 0, st);
}
}
}
}
17 changes: 6 additions & 11 deletions app/src/main/java/net/gaast/giggity/ScheduleViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ public void onDestroy() {
super.onDestroy();
}

/* Progress dialog for schedule/data load. Could consider putting it into Fetcher. */
/* Progress dialog for data loads. For historical reasons (b5ffc48634c08d1fbe13285bbee8cf236a940757)
* this is separate from LoadProgressView (used for loading the schedule itself). Maybe clean that up some day.. :-/ */
private class LoadProgressDialog extends ProgressDialog implements LoadProgress {
private Handler updater;
private LoadProgressDoneInterface done;
Expand Down Expand Up @@ -369,6 +370,7 @@ public void setDone(LoadProgressDoneInterface done) {
}
}

/* This is the one used for loading the schedule, with that "I'm impatient switch to cache" button. */
private class LoadProgressView extends FrameLayout implements LoadProgress {
ProgressBar prog;
private Handler updater;
Expand Down Expand Up @@ -732,6 +734,7 @@ private boolean viewLink(final Schedule.Link link) {
if (m.find() && !m.group().isEmpty()) {
ext = "." + m.group();
}
// At least some (old) Samsung phones seem to use filename extension over the MIME type we pass.
File fn = new File(dir, FilenameUtils.getBaseName(link.getUrl()) + ext);

final LoadProgressDialog prog = new LoadProgressDialog(this);
Expand Down Expand Up @@ -767,20 +770,12 @@ public void run() {
del.delete();
}
try {
Fetcher f = app.fetch(link.getUrl(), Fetcher.Source.DEFAULT, link.getType());
Fetcher f = app.fetch(link.getUrl(), Fetcher.Source.CACHE_1D, link.getType());
f.setProgressHandler(prog.getUpdater());

try {
OutputStream copy = new FileOutputStream(fn);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q /* 29 */) {
FileUtils.copy(f.getStream(), copy);
} else {
byte[] yo = new byte[4096];
int st;
while ((st = f.getStream().read(yo)) != -1) {
copy.write(yo, 0, st);
}
}
Giggity.copy(f.getStream(), copy);
copy.close();
} catch (IOException e) {
// TODO(http)
Expand Down

0 comments on commit aa7ff3b

Please sign in to comment.