Skip to content

Commit

Permalink
Cleanup openLink/viewLink a little bit. Added "Open in browser" l…
Browse files Browse the repository at this point in the history
…ink in case a PDF/whatever viewer isn't available. (Though I doubt that it'd help on a minimal phone like that?)
  • Loading branch information
Wilm0r committed Sep 5, 2023
1 parent 457a82a commit dc46d0e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 34 deletions.
23 changes: 3 additions & 20 deletions app/src/main/java/net/gaast/giggity/Fetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public Fetcher(Giggity app_, String url, Source source) throws IOException {
break;
}

Log.d("Fetcher", "HTTP status " + dlc.getHeaderField(0));
String status = dlc.getResponseCode() + " " + dlc.getResponseMessage();
Log.d("Fetcher", "HTTP status " + status);
String loc = dlc.getHeaderField("Location");
if (loc != null) {
Log.d("http-location", loc);
Expand Down Expand Up @@ -156,26 +157,8 @@ public Fetcher(Giggity app_, String url, Source source) throws IOException {
inStream = new GZIPInputStream(inStream);
}
} else {
throw new IOException("Download error: HTTP " + dlc.getHeaderField(0));
throw new IOException("Download error: HTTP " + status);
}

// if (inStream == null && fn.canRead()) {
// /* We have no download stream and should use the cached copy (i.e. we're offline). */
// flen = fn.length();
// inStream = new ProgressStream(new FileInputStream(fn));
// dlc = null;
// if (source != Source.ONLINE)
// source = Source.CACHE;
// } else if (inStream == null) {
// throw new IOException(app.getString(R.string.no_network_or_cached));
// }
}

// Generate an (exportable) content:// URL to cached copy of this URL, if possible. Otherwise, null.
public Uri cacheUri() {
// if (source == Source.CACHE)
// return FileProvider.getUriForFile(app, "net.gaast.giggity.paths", fn);
return null;
}

public void setProgressHandler(Handler handler) {
Expand Down
27 changes: 14 additions & 13 deletions app/src/main/java/net/gaast/giggity/ScheduleViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,11 @@ public void finishNavDrawer() {
item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openLink(link);
if (link.getType() != null) {
viewLink(link);
} else {
openLink(link);
}
drawerLayout.closeDrawers();
}
});
Expand Down Expand Up @@ -701,24 +705,16 @@ private void navDrawerItemState(TextView v, boolean enabled) {
}
}

/** Open a link object, either just through the browser or by downloading locally and using a
* dedicated viewer.
* @param link Link object - if type is set we'll try to download and use a viewer, unless:
*/
private void openLink(final Schedule.Link link) {
if (link.getType() != null && viewLink(link)) {
return;
}

/* If type was not set, or if neither of the two inner ifs were true (do we have the file,
or, are we allowed to download it?), fall back to browser. */
/** Open a link object through a browser intent. Meant mostly for website links. */
private void openLink(final Schedule.Link link) {
Uri uri = Uri.parse(link.getUrl());
Intent browser = new Intent(Intent.ACTION_VIEW, uri);
browser.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(browser);
}

/** Download and view linked content instead of opening in a browser. */
/** Download (and cache) and view linked content locally instead of opening in a browser. */
private boolean viewLink(final Schedule.Link link) {
File dir = new File(app.getCacheDir(), "view"); // R.paths.view
Matcher m = Pattern.compile("[a-z]+$").matcher(link.getType());
Expand Down Expand Up @@ -747,6 +743,12 @@ public void done() {
.setTitle(R.string.loading_error)
.setMessage(getString(R.string.no_viewer_error) + " " +
link.getType() + ": " + e.getMessage())
.setPositiveButton(R.string.open_link_with_browser_instead, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
openLink(link);
}
})
.show();
}
}
Expand All @@ -770,7 +772,6 @@ public void run() {
Giggity.copy(f.getStream(), copy);
copy.close();
} catch (IOException e) {
// TODO(http)
throw new RuntimeException(e);
}
f.keep();
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
<string name="use_cached_version">Uit cache laden</string>
<string name="loading_image">Bestand wordt geladen...</string>
<string name="loading_error">Laadfout</string>
<string name="no_viewer_error">Geen lezer geïnstalleerd voor bestandtype</string>
<string name="no_viewer_error">Geen lezer geïnstalleerd voor bestandstype</string>
<string name="open_link_with_browser_instead">In browser openen</string>
<string name="export_selections">Selecties exporteren</string>
<string name="import_selections">Selecties importeren</string>
<string name="no_selections">Geen exporteerbare data</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<string name="loading_image">Loading image data...</string>
<string name="loading_error">Load error</string>
<string name="no_viewer_error">No viewer installed for filetype</string>
<string name="open_link_with_browser_instead">Open in browser</string>
<string name="export_selections">Export selections</string>
<string name="home_shortcut">Home shortcut</string>
<string name="import_selections">Import selections</string>
Expand Down

0 comments on commit dc46d0e

Please sign in to comment.