Friday, February 27, 2015

Changes 2/27/15 - LibriVox Search API

Today, Finn and I worked on getting searching working for our LibriFox app. We are using LibriVox's json api, so our url string looks like:
"https://librivox.org/api/feed/audiobooks/title/^" + input + "?&format=json"
However, we want to make sure that our input string is web safe (so there aren't spaces, slashes, or other characters that don't gel with urls) so we did some googling and found the Javascript built-in function #encodeURIComponent, which takes a string and substitutes all non url-safe characters with their ascii encoded values. For example, the string 'twelve / six' would become 'twelve%20%2F%20six'

This ended up not mattering quite so much after we realized that the LibriVox api gives an error response if any spaces are used in the url query, even if they are properly escaped. For example: space vs. sp ace.

With that done, we continued working on our search functionality, but ran into a bit of a hurdle as LibriVox's response to our queries kept returning null when they ran after text was entered into the search bar, even though they worked fine when doing it from the web console or pasting the url directly into a browser.

What I eventually figured out is that because we were grabbing our response object right after issuing a request, it would always end up as null because the net traffic wouldn't have had time to go through in the split second before we called #response on our object


In order to fix this, I added an onload callback into the #getJSON method, so that we could pass in a function that would execute once the server had responded.


We're now trying to actually get these results to display in our app. However, jQuery Mobile does some weird stuff, and our code to append list items for each book referenced in our url_request.response object doesn't append the list items for some reason.

Wednesday, February 25, 2015

Changes 2/25/15 - JSON from the LibriVox Server

Today I got around the CORS restrictions on Firefox OS and managed to download JSON from an external server.

Here is my code.

I had to use a bunch of different sources, including this "boilerplate" app I found that shows how to use a bunch of different APIs.  I just searched for 'json' in the repo and found this code, which helped a lot.  I also found this stack overflow discussion, where someone mentioned passing in the parameters { mozSystem: true } to the line:
var xhr = new XMLHttpRequest({ mozSystem: true });
This property, along with adding the permission "systemXHR" to my manifest.webapp, allowed me to bypass the CSP.

I haven't done any handling of the JSON, but calling xhr.response returns a json object that contains an array with a bunch of book data in it, so we are now in a position where this stuff can be handled!
Calling xhr.books[0] returns the json object containing all the data we need for this particular entry.  I'm not sure why it is structured as an object within a single element array within another object though.  That seems kind of odd.


One thing I've found to be helpful is to declare a global variable like this one here and then setting the thing you are trying to debug to the global variable so that you can go into console and explore the methods for _xhr.  There's probably a better way to do this, though.  I just don't know what it is.

Changes 2/24/15 - LibriFox app

Finn and I started working on a LibriVox native app (Github repo).  They have a JSON api, but one problem I've run into is that Firefox OS doesn't like that the JSON is originating from a different domain, and throws this error:


This time it at least isn't silently failing, but doesn't really offer much in the way of suggestions.  I guess I can try to figure out if LibriVox actually supports the Cross Origin Request Sharing standard, but I'm assuming it doesn't and don't know how to check.  This article says that there's a permission called network-http I need to add, but I can't find any reference to this on Mozilla's documentation.  I'm assuming this is an older permission that no longer works.

As far as Github goes, I'm not sure if I should be pushing to Finn's repo directly, or if I should have my own repo which I then push upstream to.

Tuesday, February 24, 2015

Changes 2/23/15 - Firefox OS Camera API and Travelling Salesman Problem

I finally got my app to take a picture!  It turns out, I needed to set a permission allowing me to write to the sdcard, and my code to do this was silently failing.  That definitely needs to be fixed!

Some limitations of my current implementation are: the app passes a constant filename 'myImage.jpg' to store the file as, but it seems that if this file already exists, it is not overwritten.  For the actual app functionality, I don't want the image to save to the storage at all, so this isn't really an issue for me.  The other thing is that the camera operation isn't being finished correctly, because it only works once: if you try to press the button again, it says 'HardwareClosed'.  At least in this case it doesn't fail silently!  I'll have to look more closely at the documentation to figure out what steps I'm missing.

Over the weekend, I also worked through some of the exercises in the Algorithm Design Manual book I'm reading. So far I've read chapter 1, and over the weekend I started to work on the programming exercises at the end of the chapter. It asked me to implement two methods of solving the Traveling Salesman Problem, both of which had flaws.

The first implementation was to start at a point, find its nearest neighbor, find the nearest neighbor of that point, and so on until all the points are visited. If the points are in a line in a certain format, the algorithm computes a path way longer than what is optimal.

The second was to find the shortest distance between points in the whole set, connect those points, and continue this process until all points are connected. This still won't come up with the optimal solution in certain scenarios though.

Friday, February 20, 2015

Changes 2/20/15 - Self Hosting Packaged Apps

Finn and I tried to get packaged apps working again today, with no success.  I set up a page that will install the packaged app onto the device, but it keeps saying "download stopped" and gives no other explanation.

I thought it might be a permissions problem, so I deleted the part of my app manifest that gave it api access to the camera, but that didn't work.

Overall, this is very frustrating to try to get working because there are almost no web resources, there is no helpful output, and even if we did get it working, my app wouldn't be able to access the camera (even though that part doesn't work yet anyway).

Thursday, February 19, 2015

Changes 2/19/15 - Header Styling and Self Publishing

I realized the bower stuff I was doing (I blogged about it last friday) doesn't work on firefox OS 2.2 or less because it uses web components, which is a functionality that is still very experimental, and (I think) allows developers to design their own elements.  I gave up on the web components stuff because I couldn't even get it to work on my Firefox phone, which is running OS 3.0.

Previously, I was using this page to implement my header, which uses web components.  I switched over to this page.

Another problem I'm still running into is that the OS is very hard to debug, as it seems like my console.log statements aren't going through, and the errors thrown by their APIs aren't very verbose either.

Finn and I also tried to get self-hosted apps to work, but we got stuck.  We got an app to download off a webpage and run, but it would load the /index of students.gctaa.net rather than the index of the app when starting.

Wednesday, February 18, 2015

Changes 2/18/15 - Github updates, Self Publishing

First of all, I pushed my Firefox app onto the Github repo. I've been trying to get the camera API to work for a while now, and it's still not quite there. The 'Take Picture' button will crash the emulator, and not generate a picture on an actual phone. I'm still not sure why this is, although I assume the emulator crashes because the camera API isn't emulated properly (if at all).

I also have a console on the screen, because my Firefox OS 3.0 device seems to not push console.log output into my laptop's developer console, making it hard to debug why the camera won't work. The console I have on the screen doesn't really work either, because some of my functions are being passed into the camera API, which I'm pretty sure causes my logging code to go out of scope.  Scope is really strange in Javascript, so I could be wrong about this.


Also, we looked at self publishing options for Firefox apps (see Finn's post), and the major caveat is that self-published apps can't use privileged APIs. So for example, assuming I got the camera functionality on my app to work, my app wouldn't run properly as a self-published app because I wouldn't have access to the camera APIs. I think we can get around this by pushing and pulling from the repo, and compiling the apps ourselves. There should be a way to push FXOS apps onto your Nexus from the IDE.