Permalink
Browse files

Added the demo for the File API

  • Loading branch information...
1 parent d3e326a commit 4e2e6ae1c79c1c30979530a24b043b32dc521186 Adrian Kosmaczewski committed Apr 3, 2012
Showing with 141 additions and 11 deletions.
  1. +1 −0 README.md
  2. +16 −10 index.html
  3. +123 −1 js/app.js
  4. +1 −0 js/seedrandom-min.js
View
1 README.md
@@ -16,6 +16,7 @@ included in the PhoneGap bridge:
- Connection
- Contacts
- Device
+- File
- Geolocation
- Media
- Notification
View
26 index.html
@@ -1,20 +1,22 @@
<!DOCTYPE html>
<html>
<head>
- <title>Compass Example</title>
-
- <script type="text/javascript" charset="utf-8" src="libs/jquery-1.7.1.min.js"></script>
- <script type="text/javascript" charset="utf-8" src="libs/jquery.mobile-1.0.1.min.js"></script>
- <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>
- <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
- <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
+ <title>PhoneGap Kitchen Sink</title>
+
+ <meta charset="utf-8">
+ <script src="libs/jquery-1.7.1.min.js"></script>
+ <script src="libs/jquery.mobile-1.0.1.min.js"></script>
+ <script src="cordova-1.5.0.js"></script>
+ <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
+ <script src="js/seedrandom-min.js"></script>
+ <script src="js/app.js"></script>
<link href="libs/jquery.mobile-1.0.1.min.css" rel="stylesheet" type="text/css">
<link href="css/app.css" rel="stylesheet" type="text/css">
</head>
<body>
<div data-role="page" id="mainPage">
<div data-role="header">
- <h1>PhoneGap demo</h1>
+ <h1>PG Kitchen Sink</h1>
</div>
<div data-role="content">
<ul data-role="listview">
@@ -26,7 +28,7 @@
<li><a href="#contacts">Contacts</a></li>
<li><a href="#device">Device</a></li>
<!--<li><a href="#events">Events</a></li>-->
- <!--<li><a href="#file">File</a></li>-->
+ <li><a href="#file">File</a></li>
<li><a href="#geolocation">Geolocation</a></li>
<li><a href="#media">Media</a></li>
<li><a href="#notification">Notification</a></li>
@@ -191,7 +193,11 @@
<h1>File</h1>
</div>
<div data-role="content">
-
+ <a data-role="button" id="createFileButton">Create file</a>
+ <a data-role="button" id="readFileButton">Read file</a>
+ <a data-role="button" id="fileInfoButton">Show file info</a>
+ <a data-role="button" id="deleteFileButton">Delete file</a>
+ <div id="fileOutput"></div>
</div>
<div data-role="footer" data-position="fixed">
<h4>&copy; http://mobile-training.ch</h4>
View
124 js/app.js
@@ -426,8 +426,9 @@ function onDeviceReady() {
}, null, options);
});
+ var soundFile = null;
+
$("#media").live('pageinit', function (event) {
- var soundFile = null;
var recording = false;
function failure(error) {
console.log('error: ' + error.code + ', message: ' + error.message);
@@ -492,6 +493,127 @@ function onDeviceReady() {
console.log('file playing');
}
});
+ }).live('pagehide', function () {
+ if (soundFile) {
+ soundFile.release();
+ soundFile = null;
+ }
+ });
+
+ $('#file').live('pageinit', function () {
+
+ function failure(error) {
+ console.log('error: ' + error.code + ', message: ' + error.message);
+ }
+
+ $('#createFileButton').hide();
+ $('#readFileButton').hide();
+ $('#fileInfoButton').hide();
+ $('#deleteFileButton').hide();
+
+ var fileSystem = null;
+ var path = 'file.txt';
+ var texts = [
+ "abcdefghijklmnopqrstuvwxyz\nABCDEFGHIJKLMNOPQRSTUVWXYZ\n1234567890\n!@#$%^&*()_-+={}[];'\\:\"|<>?,./",
+ "The quick brown fox jumps over a lazy dog.",
+ "Zwei Boxkämpfer jagen Eva quer durch Sylt.",
+ "Pchnąć w tę łódź jeża lub osiem skrzyń fig. Żywioł, jaźń, Świerk.",
+ "Flygande bäckasiner söka strax hwila på mjuka tuvor.",
+ "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
+ "PhoneGap rocks!",
+ "Mobile web apps rock!",
+ "This code generates random files",
+ "This application is brought to you by http://mobile-training.ch/"
+ ];
+
+ // This better number generator comes from
+ // http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html
+ Math.seedrandom();
+
+ window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
+ console.log('file system ready');
+ fileSystem = fs;
+ $('#createFileButton').show();
+ }, failure);
+
+ $('#createFileButton').on('tap', function () {
+ if (fileSystem) {
+ fileSystem.root.getFile(path, {create: true}, function (fileEntry) {
+ console.log('file got');
+ fileEntry.createWriter(function (writer) {
+
+ var index = Math.floor(Math.random() * 10);
+ var selectedText = texts[index];
+
+ writer.onwriteend = function (evt) {
+ console.log('file written!');
+ $('#fileOutput').empty();
+ $('#fileOutput').append('File ready to be read!');
+ $('#createFileButton').hide();
+ $('#readFileButton').show();
+ $('#fileInfoButton').show();
+ $('#deleteFileButton').show();
+ };
+ writer.write(selectedText);
+
+ }, failure);
+ }, failure);
+ }
+ });
+
+ $('#readFileButton').on('tap', function () {
+ if (fileSystem) {
+ fileSystem.root.getFile(path, {create: true}, function (fileEntry) {
+ console.log('file got, ready to be read');
+ fileEntry.file(function (file) {
+ var reader = new FileReader();
+ reader.onloadend = function(evt) {
+ console.log("Read as text");
+ var text = reader.result;
+ $('#fileOutput').empty();
+ $('#fileOutput').append(text);
+ };
+ reader.readAsText(file);
+ }, failure);
+ }, failure);
+ }
+ });
+
+ $('#fileInfoButton').on('tap', function () {
+ if (fileSystem) {
+ fileSystem.root.getFile(path, {create: true}, function (fileEntry) {
+ console.log('file got, ready to get info');
+ fileEntry.file(function (file) {
+ var data = [
+ 'Full path: ' + file.fullPath,
+ 'MIME type: ' + file.type,
+ 'Modified on: ' + file.lastModifiedDate,
+ 'Size: ' + file.size
+ ];
+ var info = data.join('<br>');
+ $('#fileOutput').empty();
+ $('#fileOutput').append(info);
+ }, failure);
+ }, failure);
+ }
+ });
+
+ $('#deleteFileButton').on('tap', function () {
+ if (fileSystem) {
+ fileSystem.root.getFile(path, { create: true }, function (fileEntry) {
+ console.log('file got, ready to be removed');
+ fileEntry.remove(function() {
+ console.log('file removed!');
+ $('#fileOutput').empty();
+ $('#fileOutput').append('File deleted');
+ $('#createFileButton').show();
+ $('#readFileButton').hide();
+ $('#fileInfoButton').hide();
+ $('#deleteFileButton').hide();
+ }, failure);
+ }, failure);
+ }
+ });
});
}
View
1 js/seedrandom-min.js
@@ -0,0 +1 @@
+(function(j,i,g,m,k,n,o){function q(b){var e,f,a=this,c=b.length,d=0,h=a.i=a.j=a.m=0;a.S=[];a.c=[];for(c||(b=[c++]);d<g;)a.S[d]=d++;for(d=0;d<g;d++)e=a.S[d],h=h+e+b[d%c]&g-1,f=a.S[h],a.S[d]=f,a.S[h]=e;a.g=function(b){var c=a.S,d=a.i+1&g-1,e=c[d],f=a.j+e&g-1,h=c[f];c[d]=h;c[f]=e;for(var i=c[e+h&g-1];--b;)d=d+1&g-1,e=c[d],f=f+e&g-1,h=c[f],c[d]=h,c[f]=e,i=i*g+c[e+h&g-1];a.i=d;a.j=f;return i};a.g(g)}function p(b,e,f,a,c){f=[];c=typeof b;if(e&&c=="object")for(a in b)if(a.indexOf("S")<5)try{f.push(p(b[a],e-1))}catch(d){}return f.length?f:b+(c!="string"?"\0":"")}function l(b,e,f,a){b+="";for(a=f=0;a<b.length;a++){var c=e,d=a&g-1,h=(f^=e[a&g-1]*19)+b.charCodeAt(a);c[d]=h&g-1}b="";for(a in e)b+=String.fromCharCode(e[a]);return b}i.seedrandom=function(b,e){var f=[],a;b=l(p(e?[b,j]:arguments.length?b:[(new Date).getTime(),j,window],3),f);a=new q(f);l(a.S,j);i.random=function(){for(var c=a.g(m),d=o,b=0;c<k;)c=(c+b)*g,d*=g,b=a.g(1);for(;c>=n;)c/=2,d/=2,b>>>=1;return(c+b)/d};return b};o=i.pow(g,m);k=i.pow(2,k);n=k*2;l(i.random(),j)})([],Math,256,6,52);

0 comments on commit 4e2e6ae

Please sign in to comment.