Created
November 5, 2013 20:58
-
-
Save rf/7326196 to your computer and use it in GitHub Desktop.
PDF links on Android in Titanium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var wv = Ti.UI.createWebView(); | |
var pdf = require('pdf'); | |
wv.addEventListener('pdf', function (event) { | |
pdf(event.url, event.cookies, function (err) { | |
if (err) alert(err); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Checks to see if a file exists | |
function exists (file) { | |
try { | |
if (file.exists()) { | |
return true; | |
} | |
} catch (e) { | |
console.log(e); | |
} | |
return false; | |
} | |
// Checks if a file is a pdf by reading it and checking magic word | |
function isPdf (file) { | |
try { | |
// read first 4 bytes and check if its equal to %PDF | |
var blob = file.read(); | |
if (!blob) return false; | |
// sometimes its a blob, sometimes its text already. | |
if (!blob.slice) blob = blob.text; | |
if (!blob) return false; | |
console.log("first few characters of pdf file: " + blob.slice(0, 5)); | |
if (blob.indexOf("%PDF") === 0) return true; | |
} catch (e) { | |
console.log(e); | |
} | |
return false; | |
} | |
// Downloads a pdf | |
function download (url, cookies, done) { | |
var base = Ti.Utils.md5HexDigest(url) + '.pdf'; | |
var file = Ti.Filesystem.getFile( | |
Ti.Filesystem.applicationDataDirectory, | |
base | |
); | |
if (exists(file) && isPdf(file)) { | |
console.log("File already exists and is pdf, returning"); | |
return done(null, file, base, url); | |
} | |
// Download pdf file | |
var client = Ti.Network.createHTTPClient(); | |
client.onload = function (e) { | |
try { | |
// reopen the file, because otherwise the app will segfault, no really. | |
var base = Ti.Utils.md5HexDigest(url) + '.pdf'; | |
var file = Ti.Filesystem.getFile( | |
Ti.Filesystem.applicationDataDirectory, | |
base | |
); | |
if (e.source.status != 200) | |
throw new Error("http status " + e.source.status); | |
file.write(e.source.responseData); | |
return done(null, file, base, url); | |
} catch (e) { | |
return done(e); | |
} | |
}; | |
client.onerror = function (e) { | |
console.log("http error " + e.source.status); | |
return done(e); | |
}; | |
client.setRequestHeader("Cookie", cookies); | |
client.open("GET", url); | |
client.send(); | |
return client; | |
} | |
// copies srcFile to a temp dir / filename.pdf | |
function copyToTemp (srcFile, base, url) { | |
// create temp directory (with md5 hash as dirname) and put file in there. | |
// This is so that the name of the file (on the server) can be the same | |
// as the name of the file on the device tha we're about to write so the | |
// filename shows up properly in whatever reader the user is using. | |
// Otherwise it'd be some hex string (ie, the md5 hash as filename | |
var tempdir = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, base); | |
tempdir.createDirectory(); | |
var filename = url.split('/'); | |
filename = filename[filename.length - 1]; | |
var tempFile = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, base, filename); | |
tempFile.write(srcFile.read()); | |
return tempFile; | |
} | |
// launch intent to read pdf | |
function launch (file) { | |
console.log("launching pdf path: " + file.getNativePath()); | |
var intent = Ti.Android.createIntent({ | |
action: Ti.Android.ACTION_VIEW, | |
data: file.getNativePath(), | |
type: "application/pdf" | |
}); | |
Ti.Android.currentActivity.startActivity(intent); | |
} | |
// do whole thing -- download url w/ cookies and launch pdf | |
function pdf (url, cookies, done) { | |
if (!Ti.Filesystem.isExternalStoragePresent()) { | |
// TODO: display error | |
return done(new Error("external")); | |
} | |
download(url, cookies, function (err, file, base, url) { | |
if (err) return done(err); | |
var tempFile = copyToTemp(file, base, url); | |
launch(tempFile); | |
done(); | |
}); | |
} | |
module.exports = pdf; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebViewClient.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebViewClient.java | |
index 99f8031..adb6bc9 100644 | |
--- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebViewClient.java | |
+++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiWebViewClient.java | |
@@ -22,6 +22,7 @@ import android.webkit.SslErrorHandler; | |
import android.webkit.URLUtil; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
+import android.webkit.CookieManager; | |
public class TiWebViewClient extends WebViewClient | |
{ | |
@@ -117,7 +118,18 @@ public class TiWebViewClient extends WebViewClient | |
Intent geoviewer = Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), "Choose Viewer"); | |
webView.getProxy().getActivity().startActivity(geoviewer); | |
return true; | |
- } else { | |
+ } | |
+ | |
+ else if (url.endsWith(".pdf")) { | |
+ KrollDict data = new KrollDict(); | |
+ data.put("url", url); | |
+ data.put("cookies", CookieManager.getInstance().getCookie(url)); | |
+ | |
+ webView.getProxy().fireEvent("pdf", data); | |
+ return true; | |
+ } | |
+ | |
+ else { | |
String extension = MimeTypeMap.getFileExtensionFromUrl(url); | |
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); | |
if (mimeType != null) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment