1 /**
  2  * Manager : Eng Ahmed Adel Auhtor : Tarek Salah Company : BadrIT
  3  */
  4 
  5 // add event device ready to begin check the location
  6 // Wait for PhoneGap to load
  7 document.addEventListener("deviceready", onDeviceReady, false);
  8 
  9 /**
 10  * Boolean to check that it is the first run <br>
 11  * if it is the first run load the image <br>
 12  * and if not --> don't load it
 13  */
 14 var first_run;
 15 var first_check = true;
 16 /** Hold the path of the image */
 17 var path;
 18 
 19 /** 
 20  * Used to load image after the page load.<br>
 21  * <u><b> Description </b></u><br>
 22  * It make request to open the file system
 23  * If it is successed in opening the file system call {@link gotFS} <br>
 24  * If not call  {@link fail}
 25  * @function
 26  * @return {void} 
 27  */
 28 function loadImg() {
 29 	first_run = false;
 30 	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
 31 }
 32 
 33 
 34 
 35 
 36 /**
 37  * Called when the device become ready to work with GPS.<br>
 38  * <u><b> Description </b></u><br>
 39  * if it is the first time to run the application so load the last image
 40  * @function 
 41  * @return {void}
 42  */
 43 function onDeviceReady() {
 44 	if (first_check == true) {
 45 		loadImg();
 46 		// disable the boolean so as not to login again
 47 		first_check = false;
 48 	}
 49 }
 50 
 51 /**
 52  * Capture image using native camera application.<br>
 53  * <u><b> Description </b></u><br>
 54  * Use the camera of the device to capture photo<br>
 55  * if it successed in capturing it call {@link captureSuccess}  
 56  * else call {@link captureError}  
 57  * @function 
 58  * @return {void}
 59  */
 60 function captureImage() {
 61 	navigator.device.capture.captureImage(captureSuccess, captureError, {
 62 		limit : 1, // limit to number of captures
 63 		saveToPhotoAlbum : false
 64 	// it shouldn't save to memory but in android
 65 	// this won't work
 66 	});
 67 }
 68 
 69 /**
 70  * Called in success of capturing photo.<br>
 71  * <u><b> Description </b></u><br>
 72  * if there are many captures upload them on device memory using function {@link uploadFile} 
 73  * @function 
 74  * @param {Object} mediaFiles Encapsulates properties of a media capture file<br>
 75  * hold properties like name of the file, fullPath, lastModifiedDate and size	 
 76  * @return {void}
 77  */
 78 function captureSuccess(mediaFiles) {
 79 	// mediafile is the object that contains properties of the
 80 	// pic like name , path ....
 81 	var i, len;
 82 	// for loop in case of many capture
 83 	for (i = 0, len = mediaFiles.length; i < len; i += 1) {
 84 		uploadFile(mediaFiles[i]);
 85 	}
 86 }
 87 
 88 /**	
 89  * @function
 90  * @description called in case of error in capturing photo.<br>
 91  * <u><b> Description </b></u><br>
 92  * printing the error code and the type of it 
 93  * @param {Object} e error code and type
 94  * @return {void}
 95  */
 96 function captureError(error) {
 97 	var msg = 'An error occurred during capture: ' + error.code;
 98 	navigator.notification.alert(msg, null, 'Uh oh!');
 99 }
100 
101 // upload file to server and show the image in html
102 
103 /**	
104  * @function
105  * @description upload file to server and show the image in HTML.<br>
106  * <u><b> Description </b></u><br>
107  * get the path of the photo and view it using div with id=IMG_1
108  * then request write to file system to write the new path of the image 
109  * in the file<br>
110  * if succeeded in open the file system then call {@link gotFS}
111  * if not call  {@link fail}
112  * @param {Object} mediaFiles Encapsulates properties of a media capture file<br>
113  * hold properties like name of the file, fullPath, lastModifiedDate and size	
114  * @return {void}
115  */
116 function uploadFile(mediaFile) {
117 	var ft = new FileTransfer(), name = mediaFile.name;
118 	path = mediaFile.fullPath;
119 	$("#IMG_1").attr("src", path);
120 	first_run = true;
121 	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
122 }
123 
124 /**	
125  * @function
126  * @description request to open the file named save.txt on sd card.<br>
127  * <u><b> Description </b></u><br>
128  * create the file if it is not found and open it if it is found<br>
129  * if succeeded in open the file system then call {@link gotFileEntry}
130  * if not call  {@link fail}
131  * @param {Object} fileSystem reference to the root of the file system
132  * @return {void}
133  */
134 function gotFS(fileSystem) {
135 	fileSystem.root.getFile("save.txt", {
136 		create : true,
137 		exclusive : false
138 	}, gotFileEntry, fail);
139 }
140 
141 /**	
142  * @function
143  * @description Called when succeeded in opening file system.<br>
144  * <u><b> Description </b></u><br>
145  * if it is the first time to run the application it will load the
146  * last image else then get permission to write the new path and call
147  * {@link gotFileWriter}
148  * @param {Object} fileEntry reference to text file that contains the path of the image
149  * @return {void}
150  */
151 function gotFileEntry(fileEntry) {
152 	if (first_run == false) {
153 		// read the file
154 		fileEntry.file(gotFile, fail);
155 		// alert(false);
156 	} else {
157 		// write the file
158 		fileEntry.createWriter(gotFileWriter, fail);
159 		// alert(true);
160 	}
161 }
162 
163 /**	
164  * @function
165  * @description Write the path of the image to the text file named save.txt.<br>
166  * @param {Object} writer reference to the writer that will write to save.txt
167  * @return {void}
168  */
169 function gotFileWriter(writer) {
170 	// alert(path);
171 	writer.write(path);
172 	writer.abort();
173 }
174 
175 /**	
176  * @function
177  * @description read the path of the image and show it in HTML.<br>
178  * <u><b> Description </b></u><br>
179  * used in two situation<br>
180  * 1. when the application load and viewing the last captured image
181  * 2. when successfully capture the image and viewing it  
182  * @param {Object} file reference to text file that contains the path of the image
183  * @return {void}
184  */
185 function gotFile(file) {
186 	var reader = new FileReader();
187 	reader.onloadend = function(evt) {
188 		console.log("Read as data URL");
189 		console.log(evt.target.result);
190 		// alert(evt.target.result);
191 		$("#IMG_1").attr("src", evt.target.result);
192 	};
193 	reader.readAsText(file);
194 }
195 
196 /**	
197  * @function
198  * @description called in case of error.<br>
199  * <u><b> Description </b></u><br>
200  * printing the error code and the type of it 
201  * @param {Object} e error code and type
202  * @return {void}
203  */
204 function fail(error) {
205 	console.log(error.code);
206 }